var supersleight = '';

var menuColor = '';

function unGrey() {
	$('line').style.backgroundColor = "#000";
	$('menu1').style.color = "#000";
	$('menu1').style.borderColor = "#000";
	$('menu2').style.color = "#000";
	$('menu2').style.borderColor = "#000";

	var elments = $('menu1Content').childNodes;
	for (var i = 0; i < elments.length; i++) {
		elm = elments[i].childNodes[0];
		elm.className = 'linkBlack';
	}

	var elments = $('menu2Content').childNodes;
	for (var i = 0; i < elments.length; i++) {
		elm = elments[i].childNodes[0];
		elm.className = 'linkBlack';
	}

	menuColor = '';
	$('menu1Image').src = "/images/menu_close.gif";
	$('menu2Image').src = "/images/menu_close.gif";
	$('headeline').src  = "/images/headline.png";
}

function doGrey() {
	$('line').style.backgroundColor = "#818181";
	$('menu1').style.color = "#818181";
	$('menu1').style.borderColor = "#818181";
	$('menu2').style.color = "#818181";
	$('menu2').style.borderColor = "#818181";

	var elments = $('menu1Content').childNodes;
	for (var i = 0; i < elments.length; i++) {
		elm = elments[i].childNodes[0];
		elm.className = 'linkGrey';
	}

	var elments = $('menu2Content').childNodes;
	for (var i = 0; i < elments.length; i++) {
		elm = elments[i].childNodes[0];
		elm.className = 'linkGrey';
	}

	menuColor = '-grey';
	$('menu1Image').src = "/images/menu_close-grey.gif";
	$('menu2Image').src = "/images/menu_close-grey.gif";
	$('headeline').src  = "/images/headline-grey.png";
}

function logout() {
	var url = '/ajax/logout.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: '',
		onSuccess: function(t) {
			showPage(3, 'Login');
		}
	});
}

function toggleLine(elm) {
	if ($(elm).visible()) {
		Effect.BlindUp(elm, { duration: 0.3 });
	} else {
		Effect.BlindDown(elm, { duration: 0.3 });
	}
}

function clientLogin() {
	var url = '/ajax/clientLogin.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: 'username='+$('username').value+'&password='+$('password').value,
		onSuccess: function(t) {
			if(t.responseText == 'w-password') $('wrongInfos').update("Wrong password");
			if(t.responseText == 'w-username') $('wrongInfos').update("Wrong username");
			
			if(t.responseText == 'ok') {
				showPage(3, 'Login');
			}
		}
	});
}

function showPage(pageId, title) {
	$('image').hide();
	$('index').hide();
	$('page').show();

	unGrey();
	
	$('menu1Text').update("Content");

	var url = '/ajax/getPage.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: 'lng='+lng+'&id='+pageId,
		onSuccess: function(t) {
			$('page').update(t.responseText);
		}
	});
	
	$('menu2Text').update(title);
	hideMenu(2);
}

function hidePage() {
	$('index').show();
	$('page').hide();
}

function showSerie() {
	isPictureVisible = false;
	$('imageLow').hide();
	$('imageHigh').hide();
	$('legend').hide();
	$('imageLoader').hide();
	$('bottomMenu').hide();
	$('serie').show();
	$('menu4').show();
	$('menu3').style.left = "333px";
}

function hideSerie() {
	isPictureVisible = true;
	resize();
	$('imageLow').show();
	$('imageHigh').show();
	$('imageLoader').show();
	$('bottomMenu').show();
	$('serie').hide();
	$('menu4').hide();
	var nWidth  = $('imageLow').getDimensions().width;
	$('menu3').style.left		= (nWidth-92)+"px";
}

function showCaption() {
	$('legend').toggle();
}

function hideCaption() {
	$('legend').hide()
}

function changePicture(event) {
	if (!event) var event = window.event;
	
	var xPos = (!event.x) ? event.layerX : event.x;

	if (event.x) {
		if (event.layerX < event.x) xPos = event.layerX;
	}

	if(xPos < $('imageLow').getDimensions().width/2) {
		showPicture(image.lastId, serie.id);	
	} else {
		showPicture(image.nextId, serie.id);
	}
}

function manageArrows(event) {
	if (!event) var e = window.event;
	
	var xPos = (!event.x) ? event.layerX : event.x;
	
	if (event.x) {
		if (event.layerX < event.x) xPos = event.layerX;
	}
	
	if(xPos < $('imageLow').getDimensions().width/2) {
		$('imageLow').style.cursor = "w-resize";
		$('imageHigh').style.cursor = "w-resize";
		$('navPrevText').style.textDecoration = 'underline';
		$('navNextText').style.textDecoration = '';
	} else {
		$('imageLow').style.cursor = "e-resize";
		$('imageHigh').style.cursor = "e-resize";
		$('navPrevText').style.textDecoration = '';
		$('navNextText').style.textDecoration = 'underline';
	}
}

function hideArrows() {
	$('navPrevText').style.textDecoration = '';
	$('navNextText').style.textDecoration = '';
}

function bigX(item, char) {
	if (char == undefined) char = "X"
	$(item).update(char);
}

function littleX(item, char) {
	if (char == undefined) char = "x"
	$(item).update(char);
}

var isPictureVisible = false;
var lastScroll		 = new Array();
var imageRatio;
var image;
var serie;
function showPicture(imageId, serieId) {
	if (imageId =='') return;
	
	if(!isPictureVisible) {
		lastScroll = getScroll();
		window.scrollTo(0,0);
		$('index').hide();
		$('image').show();
		isPictureVisible = true;
	}

	doGrey();
	hideArrows();
	//$('legend').hide();
	$('imageLow').show();
	$('imageHigh').show();
	$('imageLoader').show();
	$('bottomMenu').show();
	$('serie').hide();
	$('menu4').hide();

	
	$('imageLow').src = "/images/empty.png";
	$('imageHigh').src = "/images/empty.png";

	var url = '/ajax/getImage.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: 'lng='+lng+'&id='+imageId+"&serie="+serieId,
		onSuccess: function(t) {
			//Parsing du XML
	    	var xmlDoc		= t.responseXML.documentElement;
	    	var xmlSerie	= xmlDoc.getElementsByTagName('serie');
	    	var xmlImage	= xmlDoc.getElementsByTagName('image');
				image 		= {id: xmlImage[0].getAttribute('id'), lastId:xmlImage[0].getAttribute('lastId'), nextId:xmlImage[0].getAttribute('nextId'), number:xmlImage[0].getAttribute('number') , width: xmlImage[0].getAttribute('width'), height: xmlImage[0].getAttribute('height'), src: xmlImage[0].getAttribute('src'), title: xmlImage[0].getAttribute('title'), text: xmlImage[0].getAttribute('text')};			
				serie		= {id: xmlSerie[0].getAttribute('id'), owner: xmlSerie[0].getAttribute('owner'), imageCount: xmlSerie[0].getAttribute('imageCount'), date: xmlSerie[0].getAttribute('date'), title:xmlSerie[0].getAttribute('title'), place:xmlSerie[0].getAttribute('place'), text:xmlSerie[0].getAttribute('text')};
			
			if (serie.owner == 4) {
				$('imageLow').className = 'border';
				$('imageHigh').className = 'border';
			} else {
				$('imageLow').className = '';
				$('imageHigh').className = '';			
			}

			if (serie.id == 1 || serie.owner == 4) {
				$('toggleSerieText').hide();
			} else { 
				$('toggleSerieText').show();
			}
					
    	    imageRatio = image.width / image.height;

			/*var width  = $('image').getDimensions().width-38;
			var height = $('image').getDimensions().height-22;
			*/
			var mySize = getPageSize();

			var width  = mySize.windowWidth - 79;
			var height = mySize.windowHeight - 91;
			dRatio = mySize.width / mySize.height;
			//Hauteur
			if (dRatio > imageRatio) {

				if ((height*imageRatio) < 425) height = 425/imageRatio;
				
				$('imageLow').style.width	= (height*imageRatio)+"px";
		    	$('imageLow').style.height	= height+"px";
				$('imageHigh').style.width	= (height*imageRatio)+"px";
		    	$('imageHigh').style.height	= height+"px";
				$('imageLow').src			= "/img.php?file="+image.src+"&h=1024";
				$('imageHigh').src			= "/img.php?file="+image.src+"&h="+height;
			} else {

				if (width < 425)  width = 425;

				$('imageLow').style.width	= width+"px";
		    	$('imageLow').style.height	= (width/imageRatio)+"px";
				$('imageHigh').style.width	= width+"px";
		    	$('imageHigh').style.height	= (width/imageRatio)+"px";
				$('imageLow').src			= "/img.php?file="+image.src+"&w=1024";
				$('imageHigh').src			= "/img.php?file="+image.src+"&w="+width;
			}
						
			var nWidth  = $('imageLow').getDimensions().width;
			var nHeight = $('imageLow').getDimensions().height;
			$('bottomMenu').style.top	= nHeight+"px";		
			$('bottomMenu').style.width = nWidth+"px";
			$('menu3').style.left		= (nWidth-92)+"px";
			$('imageLoader').style.left = (nWidth/2-64)+"px";
			$('imageLoader').style.top = (nHeight/2-16)+"px";
			
			//Texte
			$('legendText').update(image.text);
			$('legendTitle').update(image.title);
			if (image.title == '') {
				$('legendNumbers').style.marginLeft = "5px";
			} else {
				$('legendNumbers').style.marginLeft = "0px";
			}
			$('legendNumbers').update(image.number+"/"+serie.imageCount);
			
			$('serieTitle').update(serie.title);
			$('serieText').update(serie.text);
			$('serieInfos').update("("+serie.place+" / "+serie.date+")");
			var lHeight				= $('legend').getDimensions().height;
			$('legend').style.top	= (nHeight-lHeight)+"px";
		}
	});
}

function hidePicture() {
	$('legend').hide();
	$('index').show();
	$('image').hide();
	isPictureVisible = false;
	unGrey();
	window.scrollTo(0, lastScroll);
}

function loadIndexContent(title, id) {

	var url = '/ajax/getIndex.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: 'lng='+lng+'&id='+id,
		onSuccess: function(t) {
			$('index').show();
			$('image').hide();
			$('page').hide();
			isPictureVisible = false;
			unGrey();
			$('index').update(t.responseText);
		}
	});
	
	$('menu1Text').update(title);
	$('menu2Text').update("Informations");
	hideMenu(1);
}

function showMenu(id) {
	$('menu'+id+'Content').show();
	$('menu'+id+'Image').src = "/images/menu_open"+menuColor+".gif";
}

function hideMenu(id) {
	$('menu'+id+'Content').hide();
	$('menu'+id+'Image').src = "/images/menu_close"+menuColor+".gif";
}

function showIndexTitle(id) {
	$('thumbnail_'+id).show();
}

function hideIndexTitle(id) {
	$('thumbnail_'+id).hide();
}

var indexHidden = true;
function showIndex(fromNothing) {	
	if (indexHidden) {
		$('siteInto').absolutize();
		Effect.Appear('header', { duration: 0.5, afterFinish:function() { $('backgroundImage').hide(); }});
		Effect.Appear('white', { duration: 0.5, afterFinish:function() { $('backgroundImage').hide(); }});
		Effect.Fade('siteInto', { duration: 0.5 });
		
	}
	if (indexHidden || !fromNothing) {
		Effect.Appear('index', { duration: 0.5 });
		indexHidden = false;
	}
}

function getScroll() {
	return (!window.pageYOffset) ? document.body.scrollTop : window.pageYOffset;
}

function getPageSize() {

	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 if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in 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;
	} 

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){ 
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return {width:pageWidth, height:pageHeight, windowWidth:windowWidth,windowHeight:windowHeight}; //arrayPageSize;
}

var iRatio;	
var dRatio;
var rRatio;
var dSize;
var resizing = false;
var timer;
var timer2;
var isWidth;
var dim;
function updatePicture() {
	if (indexHidden) {
	 	$('backgroundImageLow').show();
		$('backgroundImageHigh').show();
		if (dRatio > iRatio) {
			$('backgroundImageHigh').src = "/img.php?file="+dFile+"&w="+dSize.width;
		} else {
			$('backgroundImageHigh').src = "/img.php?file="+dFile+"&h="+dSize.height;
		}
		resizing = false;
	}
}

function updatePicture2() {
 	$('imageLow').show();
	$('imageHigh').show();

	if (isWidth) {
		$('imageHigh').src			= "/img.php?file="+image.src+"&w="+dim;
	} else {
		$('imageHigh').src			= "/img.php?file="+image.src+"&h="+dim;
	}
	resizing = false;
}

function resize(doRatio) {
	$('backgroundImage').style.height = "0px";
	
	    dSize = getPageSize(); //$('body').getDimensions(); //document.viewport.getDimensions();
    var iSize = $('backgroundImageLow').getDimensions();
        dRatio = dSize.width / dSize.height;

    $('backgroundImage').style.height = dSize.height+"px";	    
    if(doRatio) iRatio = iSize.width / iSize.height;
	
	var limit = (dSize.height > dSize.windowHeight) ? 520 : 506;
    if (dSize.width < limit) {
    	$('backgroundImage').style.width	= limit+"px";
    	$('index').style.width 				= "444px";
    	$('image').style.width 				= "425px";
    	$('header').style.width 			= "425px";
    } else {
    	$('backgroundImage').style.width	= "100%";    
    	$('index').style.width 				= "";
    	$('header').style.width 			= "";
    	$('image').style.width 				= "";
    }
      		
    if (dRatio > iRatio) {
    	$('backgroundImageLow').style.width   = dSize.width+"px";
    	$('backgroundImageLow').style.height  = (dSize.width/iRatio)+"px";
    	$('backgroundImageHigh').style.width  = dSize.width+"px";
    	$('backgroundImageHigh').style.height = (dSize.width/iRatio)+"px";
	} else {
    	$('backgroundImageLow').style.width   = (dSize.height*iRatio)+"px";
    	$('backgroundImageLow').style.height  = dSize.height+"px";				
    	$('backgroundImageHigh').style.width  = (dSize.height*iRatio)+"px";
    	$('backgroundImageHigh').style.height = dSize.height+"px";				
    }
    
    
    $('backgroundLoader').style.left = (dSize.windowWidth/2-64)+"px";
    $('backgroundLoader').style.top = (dSize.windowHeight/2-16)+"px";

	if (isPictureVisible) {
		$('imageHigh').hide();
		var width  = dSize.windowWidth-79; //$('image').getDimensions().width-38;
		var height = dSize.windowHeight-91;//$('image').getDimensions().height-22;
		
		idRatio = width / height;	
		
		//Hauteur
		if (idRatio > imageRatio) {
			if ((height*imageRatio) < 425) height = 425/imageRatio;

			$('imageLow').style.width	= (height*imageRatio)+"px";
	    	$('imageLow').style.height	= height+"px";
			$('imageHigh').style.width	= (height*imageRatio)+"px";
	  		$('imageHigh').style.height	= height+"px";
			isWidth = false;
			dim = height;
		} else {

			if (width < 425) width = 425;

			$('imageLow').style.width	= width+"px";
			$('imageLow').style.height	= (width/imageRatio)+"px";
			$('imageHigh').style.width	= width+"px";
			$('imageHigh').style.height	= (width/imageRatio)+"px";
			isWidth = true;
			dim = width;
		}
		
		var nWidth  = $('imageLow').getDimensions().width;
		var nHeight = $('imageLow').getDimensions().height;
		$('bottomMenu').style.top	= nHeight+"px";		
		$('bottomMenu').style.width = nWidth+"px";
		$('menu3').style.left		= (nWidth-92)+"px";
		$('imageLoader').style.left = (nWidth/2-64)+"px";
		$('imageLoader').style.top = (nHeight/2-16)+"px";
	
		var lHeight				= $('legend').getDimensions().height;
		$('legend').style.top	= (nHeight-lHeight)+"px";
	    
	    clearTimeout(timer2)
	    timer2 = setTimeout(updatePicture2,500);
	}

    if (resizing) $('backgroundImageHigh').hide();
    
    if(doRatio) updatePicture(dSize);
    
    //resizePage(doRatio);
    
    resizing = true;
    
    clearTimeout(timer)
    timer = setTimeout(updatePicture,500);
}
