/*
	This is a modified version of Lightbox. It was inspired by the modifications
	done by www.macrabbit.com (Jan Van Boghout).
		
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://huddletogether.com/projects/lightbox/

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
*/

var currentMonth = null;
var currentYear = null;
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);

var macImageZoomAnimationTimer = null;
var macImageZoomWaitTimer = null;
var macImageZoomAnimationFrame = 0;
var isFirstMacImageZoom = true;
var progressWheelFrameHeight = 40;

$(document).ready(function() {
	$('.ecogal_text').click(showLayoutEvent);
	$('.ecogal_thumb').click(showLayoutEvent);
	initLayoutEvent(); //this generate some html
});


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageOffsetY(){
	if (self.pageYOffset)
		return self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		return document.documentElement.scrollTop;
	else if (document.body)
		return document.body.scrollTop;
		
	return 0;
}

function pause(numberMillis){
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


function eventKeyDown(e){
	var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
	var c = (window.event) ? event.keyCode : e.keyCode;
	if (c == Esc) {hideLayoutGal();}
}

/* display a transparent gallery, display a loading wheel, and send ajax request to get the player*/
function showLayoutEvent(){
	
	//stop the gallery crop fade
	if(isCrossFade){
		for(var i = 0 ;i < galNum; i++){
			eval('clearTimeout(transInterval'+i+');');
		}
	}
	
	// prep objects
	var zoomedImage = document.getElementById('macImageZoom-image');
	
	var arrayPageSize = getPageSize();
	var scrollY = getPageOffsetY();

	// set height of Overlay to take up whole page and show
	$('#overlayGal').css('height',(arrayPageSize[1]+ 10) + 'px');
	$('#overlayGal').css('display','block');
	
	var spaceTop = scrollY + ((arrayPageSize[3] - 35 - progressWheelFrameHeight) / 2);
	var spaceLeft = ((arrayPageSize[0] - 20 - progressWheelFrameHeight) / 2);
	$('#ply1Container').css('top',(spaceTop - 200) + 'px');
	//add a little correction 
	switch(slideWidth){
		case 400 : 
			var correction = 180;
			break;
		case 500 : 
			var correction = 240;
			break;
		case 600 : 
			var correction = 280;
			break;
		default :
			var correction = 200;
	}
	$('#ply1Container').css('left',(spaceLeft-correction) + 'px');
	$('#progressWheel').css('top',spaceTop + 'px');
	$('#progressWheel').css('left',spaceLeft + 'px');
	
	// Listen to escape
	$(document).keypress(eventKeyDown);
	
	macImageZoomWaitTimer = setInterval(delayAnimateLoad, 500); //make the wheel turns
		
	if($.browser.msie){
		//prevent a display bug in IE
		$('select').css('visibility','hidden');
	}
	
	//get and parse the url 
	var p = new Poly9.URLParser(window.location.href);
	var queryString = '';
	if(p.getQuerystring() != ''){
		queryString = '?' + p.getQuerystring(); 
	}
	var selection = $('input',this.parentNode).val();
	$.post(getPly1URL,{'tt_content': tt_content,'selection': selection,'order': order,'pathName': p.getPathname() + queryString,'eco_gal':eco_gal},setPly1);
}

function hideLayoutGal()
{
	clearInterval(macImageZoomWaitTimer);
	clearInterval(macImageZoomAnimationTimer);
	$('#overlayGal').css('display','none');
  	$('#closeBtDiv').css('display','none');
	$('#progressWheelContainer').css('display','inline');
	if($.browser.msie){
		$('select').css('visibility','visible');
	}
	//reinit the layout
	$('#tableDetailDiv').empty();
  	$('#closeDiv').css('visibility','hidden');
	document.onkeypress = '';
	
	//restart the gallery
	if(isCrossFade){
		for(var i = 0 ;i < galNum; i++){
			var timeInt = Math.random()  * interval + 1000;
			eval("transInterval"+i+" = setTimeout('show"+i+".startShow("+i+")',"+timeInt+");");
		}
	}
	return false;
}

function setPly1(data){
	//reset some global variables
	imageGalleryCaptions = new Array();	
	imgLastNo = -1;
	imgNo = 1;
	doTranslate = false;
	
	//IE and Safari do not interprate javascript on the fly... help them a bit
	var js = data.split('// ]]')[0].split('// <![CDATA[')[1].split(';'); //extract the javascript from the data
	eval(js[0]) //var originalImgs = new Array()
	eval(js[1]) //var galleryImgs = new Array()
	
	$('#tableDetailDiv').html(data);
  	$('#closeBtDiv').css('display','block');
  	$('#closeDiv').css('visibility','visible');
	
	// rebuild the new gallery player
	$('#theImages a').click(showPreview);
	initSlideShow();
	$('#progressWheelContainer').css('display','none');
}

function delayAnimateLoad()
{
	clearInterval(macImageZoomWaitTimer);
	macImageZoomAnimationTimer = setInterval(animateLoad, 66);
}

function animateLoad()
{
	var loadIndicator = document.getElementById('progressWheel');
	loadIndicator.style.backgroundPosition = '0 -'+(macImageZoomAnimationFrame * progressWheelFrameHeight)+'px';
	macImageZoomAnimationFrame = (macImageZoomAnimationFrame + 1) % 12;
}

function stopProp(event){
	if(window.event){
		window.event.cancelBubble = true;
	}
	if(event){
		event.stopPropagation(); 
	}
}

function initLayoutEvent(){
	var strCloseBt = 'Fermer';
	var html = '<div style="display: none;" id="overlayGal">' +
					'<div id="bgTransparent"></div>' +
				    '<div id="ply1Container" style="width:' + slideWidth + 'px">' +
				      '<div id="tableDetailDiv"></div>' +
				      '<div id="closeDiv" class="hidden"></div>' +
				      '<div id="closeBtDiv" style="display:none">' +
				        '<a href=""  style="text-decoration: underline" id="closeBt">'+strCloseBt+'</a>' +
				        '<div style="margin: 5px;">' +
				          '<a href="http://www.mozilla-europe.org/" target="_blank">' +
				            '<img src="'+galSharedImg+'bd2_mozilla.gif"/>' +
				          '</a>' +
				        '</div>' +
				      '</div>' +
				    '</div>' +
				    '<a href="#" id="progressWheelContainer">' +
				      '<span id="progressWheel" style="position: absolute; z-index: 150;"/>' +
				    '</a>' +
				'</div>';
	$('body').append(html);
	$('#ply1Container').click(stopProp);
	$('#overlayGal').click(hideLayoutGal);
	$('#closeDiv').click(hideLayoutGal);
	$('#closeDiv').css('visibility','hidden');
	$('#closeBtDiv a:eq(0)').click(hideLayoutGal);
}

