var windowWidth  		= '';
var windowHeight 		= '';
var windowRatio 		= '';
var backgroundLowWidth 	= '';
var backgroundLowHeight = '';
var backgroundRatio     = '';
var timer 				= '';
var resizing 			= false;
var iFile				= '';
var isHomePage			= false;
var lastHash			= '';
var lastScrollY			= 0;
var isZoomed			= false;

function resize(doRatio) {
    $('#backgroundImage').css('height', '0px');

    windowWidth  = $(window).width();
    windowHeight = $(window).height();
    windowRatio 				= windowWidth / windowHeight;

    backgroundLowWidth  = $('#backgroundImageLow').width();
    backgroundLowHeight = $('#backgroundImageLow').height();
    if(doRatio) backgroundRatio = backgroundLowWidth / backgroundLowHeight;

    $('#backgroundImage').css('height', windowHeight);

    $('#backgroundImage').css('width', windowWidth);

    if (windowRatio > backgroundRatio) {
    	$('#backgroundImageLow').css('width',    windowWidth);
    	$('#backgroundImageLow').css('height',   (windowWidth/backgroundRatio));
    	$('#backgroundImageHigh').css('width',   windowWidth);
    	$('#backgroundImageHigh').css('height',  (windowWidth/backgroundRatio));
    } else {
    	$('#backgroundImageLow').css('width',   (windowHeight*backgroundRatio));
    	$('#backgroundImageLow').css('height',  windowHeight);
    	$('#backgroundImageHigh').css('width',  (windowHeight*backgroundRatio));
    	$('#backgroundImageHigh').css('height', windowHeight);
    }
    
    if (resizing) $('#backgroundImageHigh').hide();
    
    if(doRatio) updatePicture(windowWidth, windowHeight);
    			    
    resizing = true;
    
    clearTimeout(timer)
    timer = setTimeout(updatePicture,500);
    
    $('.zoom').live('click', function(event) {
    	event.preventDefault();

    	if (isZoomed) {
    		isZoomed = false;
	    	$('#zoom').hide();
	    	$('#imageContent').prepend($('.viewer'));
    		resizeImage();   
    		return; 		
    	}
 
    	isZoomed = true;
    	$('#zoom').show();
    	$('#zoom').append($('.viewer'));
    	resizeImageZoom();
    });
    
    $('#close').click(function() {
   		isZoomed = false;
	    $('#zoom').hide();
	    $('#imageContent').prepend($('.viewer'));
    	resizeImage();    		
    })
}

function updatePicture() {
    $('#backgroundImageLow').show();
    $('#backgroundImageHigh').show();
    if (windowRatio > backgroundRatio) {
    	$('#backgroundImageHigh').attr('src', "/img.php?file="+dFile+"&w="+windowWidth);
    } else {
    	$('#backgroundImageHigh').attr('src', "/img.php?file="+dFile+"&h="+windowHeight);
    }
    resizing = false;
}

function wHsize() {
	var myWidth = 0, myHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return myHeight;
}

function resizeImageZoom(doRatio) {

	if(isHomePage) return;
	
    windowHeight = wHsize();
	if (windowHeight < 200) windowHeight = 200;

    backgroundLowWidth  = $('#imageLow').width();
    backgroundLowHeight = $('#imageLow').height();
	
    $('#imageLow').css('height', '0px');

    if(doRatio) backgroundRatio = backgroundLowWidth / backgroundLowHeight;

    $('#imageLow').css('height', windowHeight);

	if (Math.round(windowHeight*backgroundRatio)> $(window).width()) {
	 	$('#imageLow').css('width',   $(window).width());
    	$('#imageLow').css('height',  $(window).width()/backgroundRatio);
   		$('#imageHigh').css('width',  $(window).width());
    	$('#imageHigh').css('height', $(window).width()/backgroundRatio);	
	} else {
	 	$('#imageLow').css('width',   (windowHeight*backgroundRatio));
    	$('#imageLow').css('height',  windowHeight);
   		$('#imageHigh').css('width',  (windowHeight*backgroundRatio));
    	$('#imageHigh').css('height', windowHeight);
    }
    
    if (resizing) $('#imageHigh').hide();
    
    if(doRatio) updatePictureImage(windowWidth, windowHeight);
    			    
    resizing = true;
    
    clearTimeout(timer)
    timer = setTimeout(updatePictureImage, 500);
}

function resizeImage(doRatio) {
	if (isZoomed) {
		resizeImageZoom(doRatio);
		return;
	}
	
	if(isHomePage) return;
	
	var w = 240;
	
	if (location.toString().search(/chronique/) != -1) w = 340;

    windowHeight = wHsize()-w;
	if (windowHeight < 200) windowHeight = 200;

    backgroundLowWidth  = $('#imageLow').width();
    backgroundLowHeight = $('#imageLow').height();
	
    $('#imageLow').css('height', '0px');

    if(doRatio) backgroundRatio = backgroundLowWidth / backgroundLowHeight;

    $('#imageLow').css('height', windowHeight);

 	$('#imageLow').css('width',   (windowHeight*backgroundRatio));
    $('#imageLow').css('height',  windowHeight);
    $('#imageHigh').css('width',  (windowHeight*backgroundRatio));
    $('#imageHigh').css('height', windowHeight);
    
    if (resizing) $('#imageHigh').hide();
    
    if(doRatio) updatePictureImage(windowWidth, windowHeight);
    			    
    resizing = true;
    
    clearTimeout(timer)
    timer = setTimeout(updatePictureImage, 500);
}

function updatePictureImage() {
    $('#imageLow').show();
    $('#imageHigh').show();
   	$('#imageHigh').attr('src', "/img.php?file="+iFile+"&h="+windowHeight);
    resizing = false;
}

function navigateTo(url) {
	if (url.charAt(0) == '/') url = url.substring(1, url.length);
	$.get('/ajax/getPage.php?req='+url, function(data) {
	 	$('#imageContent').replaceWith(data);
	 	resizeImage(true);
	 	//lastScrollY = $(document).scrollTop();
	 	window.scrollTo(0,0);
	 	
	 	var u = url.split('/');
	 	location.hash = u[u.length-2];
	 	lastHash      = location.hash;
	 	
	 	/*whiteMask*/
	 	$('.thumb .whiteMask').hide();
	 	$('#thumb_'+u[u.length-2]+' .whiteMask').show();
	 	
	});
}

function analyzeHash() {
	if (lastHash != location.hash) {
		var url = location.pathname;
		if (url.charAt(0) == '/') url = url.substring(1, url.length);
		
		var u = url.split('/');
		
		if (location.hash != '') url = u[0]+'/'+u[1]+'/'+location.hash.replace(/#/, '')+'/';
		navigateTo(url);
	}
}

$(document).ready(function () {
    resize(true);
    resizeImage(true);
    
    $('#home').css('top', windowHeight/2-45).show();
    $('#homeBackground').css('top', windowHeight/2-45).show();
    
    $('#home li, #menu li').mouseover(function() {
    	$(this).children('ul').show().css('left', $(this).children('a').offset().left);
    }).mouseleave(function() {
    	$(this).children('ul').hide();
    });
    
	$('.thumbMonth').each(function() {
    	$(this).css('width', $(this).children('div').width());
    	$(this).css('top', ($(this).parent('div').height()/2)-($(this).height()/2));
	});
    
	$('.thumb').mouseover(function() {
    	$(this).children('.thumbMonth').css('width', $(this).width());
    	$(this).children('.thumbMonth').css('top', ($(this).height()/2)-($(this).children('.thumbMonth').height()/2));
		$(this).children('.thumbMonth').hide();
    	$(this).children('.thumbLegend').css('width', $(this).width());
    	$(this).children('.thumbLegend').css('top', ($(this).height()/2)-($(this).children('.thumbLegend').height()/2));
		$(this).children('.thumbLegend').show();
	}).mouseout(function() {
		$(this).children('.thumbMonth').show();
		//if ($(this).children('.thumbLegend').attr('rel') == 'no') {
			//$(this).children('.thumbLegend').attr('rel', '');
		//} else {
			$(this).children('.thumbLegend').hide();
		//}
	}).live('click', function() {
		if ($(this).hasClass('ajax') && !($.browser.msie && $.browser.version < "8.0")) {
			lastScrollY = this;
			navigateTo($(this).children('div').children('div').children('a').attr('href'));
		} else {
			window.location.href = $(this).children('div').children('div').children('a').attr('href');
		}
	});
	
	$('a.ajax').live('click', function() {
		if (!($.browser.msie && $.browser.version < "8.0")) {
			lastScrollY = $(this).parents('.thumb').get(0);
			navigateTo($(this).attr('href'));
			return false;
		}
	});
	 
	$('#mail').html("<a href='mailto:olivier-saudan@bluewin.ch'>olivier-saudan@bluewin.ch</a>");
	
	$('.eCol').mouseover(function() {
		$('.ePriv').css("background", "#FFFFFF");
		$('.isPriv').children('span').removeClass("eSel");
		$(this).css("background", "#DCDCDC");
		$('.isCol').children('span').addClass("eSel");
	});

	$('.ePriv').mouseover(function() {
		$('.eCol').css("background", "#FFFFFF");
		$('.isCol').children('span').removeClass("eSel");
		$(this).css("background", "#DCDCDC");
		$('.isPriv').children('span').addClass("eSel");
	});
	
	$('.down').live('click', function() {
		var p = $(lastScrollY).position().top;
	 	window.scrollTo(0,p-180);
		$(lastScrollY).children('.thumbLegend').show();
	});
	
	setInterval(analyzeHash, 250);
});

$(window).resize(function() {
    resize(false);
    resizeImage(false);
    
    $('#home').css('top', windowHeight/2-45);
    $('#homeBackground').css('top', windowHeight/2-45);
});
