var bufferMenu = new Array();
var currentContent 		= -1;
var previousContent 	= -1;
var posAnim				= 0;
var offsetAnim			= 0;
var side				= 1;
var previousHash		= false;
var contentWidth		= 900;
var contentSpacing		= 100;
var previousBgColor		= 0;
var previousBdColor		= 0;
var changeHash			= false;
var oscillColor			= [false, false];
var oscillPos			= 0;
var ci					= 0;


function iframeDocument(id){
	var iframe = document.getElementById(id);
	
	if(iframe.contentDocument) // Firefox, Opera  
	doc = iframe.contentDocument;  
	else if(iframe.contentWindow)  // Internet Explorer  
	doc = iframe.contentWindow.document;  
	
	return doc;
}


function makeRequest(url, functionName, id, post) {

    var httpRequest = false;
	
	
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
        }
    }

    if (!httpRequest) {
        return false;
    }
	
	httpRequest.onreadystatechange = function() { callFunction(httpRequest, functionName, id); };
	   
	if (post){
		httpRequest.open('POST', url, true);
		
		httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		httpRequest.send(post);
	} else {
	    httpRequest.open('GET', url, true);
	    httpRequest.send(null);
	}

}


function explode(table, needle){
	if (!needle)
		needle = "\r\n";
		
	if (needle == "\r\n" && !document.all)
		needle = "\n";
		
	lines = table.split(needle);
	
	return lines;
}

function callFunction(httpRequest, functionName, id) {
	
	if (functionName)
	    if (httpRequest.readyState == 4) {
	        if (httpRequest.status == 200)
				error = false;else
				error = true;
			
			text = httpRequest.responseText;
			
			if (functionName){
				if (id)
					eval(functionName+'(text,id,error);');else
					eval(functionName+'(text,0,error);');
			}
	    }
}

function rand( min, max ) {
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function isIE(){
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}


function hex(d) {
	h = d.toString(16);
	if (h.length < 2)
		h = "0" + h;
	
	return h;
}

function dec(h) {
	return parseInt(h,16);
} 
 
function getPercentage(min, max, value){
	return (100 / (max - min)) * (value - min);
}

function blendValue(val1, val2, percent){
	return Math.round(val1 + ((val2 - val1)/100)*percent)
}

function getWidth(){
	return (document.all)?document.body.offsetWidth:window.innerWidth;
}

 function getHeight(){
 	return (document.all)?document.body.offsetHeight:window.innerHeight;
 }

function fadeObject(id, amount){
	
	if (!id.style)
		obj = document.getElementById(id);else
		obj = id;
	
	if (obj == null)
		return false;
		
	// IE
	obj.style.filter = "alpha(opacity:"+amount+")";
  
	// Safari<1.2, Konqueror
	/*obj.style.KHTMLOpacity = amount/100;*/
  
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = amount/100;
}



function findContentID (name){
	var i;
	
	for (i = 0; i < contentData.length; i++)
		if (contentData[i][0] == name)
			return i;
	
	return -1;
}


function getContent(contentID){
	return document.getElementById('content'+contentID);
}

function colorRGB(color){
	if (color){
		var r = dec(color.substring(1, 3));
		var g = dec(color.substring(3, 5));
		var b = dec(color.substring(5, 7));
	}
	return [r, g, b];
}

function colorHex(color){
	return "#" + hex(color[0]) + hex(color[1]) + hex(color[2]);
}

function fadeColor(colorhex1, colorhex2, percent){
	var i;
	
	if (percent < 0)
		return colorhex1;else
	
	if (percent > 100)
		return colorhex2;
	
	var color = [0, 0, 0];
	color1 = colorRGB(colorhex1);
	color2 = colorRGB(colorhex2);
	
	
	for (i = 0; i < 3; i++)
		color[i] = Math.round(color1[i] + ((color2[i] - color1[i])/100)*percent);
	return colorHex(color);
}

function findPosX(percent, contentID){
	var centerX = (getWidth() / 2);
	var posX = centerX - (contentWidth / 2);	
	var offset = contentWidth + contentSpacing;
	
	if (contentID == currentContent)
		posX -= side * offset;
	
	posX += Math.ceil((offset / 100) * percent * side);
	
	return posX;
}

function animContents(){
	cContent 	= getContent(currentContent);
	pContent 	= getContent(previousContent);
	
	
	
	if (Math.round(posAnim) >= 100){
		
		posAnim = 0;
		
		if (pContent)
		pContent.style.display = 'none';
		
		previousContent = -1;
		previousBgColor = contentData[currentContent][2];
		previousBdColor	= contentData[currentContent][3];
		
		cContent.style.left = findPosX(100, currentContent) + "px";
		cContent.style.backgroundColor = contentData[currentContent][2];
		document.getElementById('scrollarea').style.backgroundColor = contentData[currentContent][2];
		/*document.getElementById('scrollarea').style.borderTop =  '1px solid '+contentData[currentContent][3];
		document.getElementById('scrollarea').style.borderBottom =  '1px solid '+contentData[currentContent][3];*/
		
	} else {
		bgColor 	= fadeColor(previousBgColor, contentData[currentContent][2], posAnim);
		bdColor 	= fadeColor(previousBdColor, contentData[currentContent][3], posAnim);
		
		document.getElementById('scrollarea').style.backgroundColor = bgColor;
		//document.getElementById('infoGalerie').style.backgroundColor = bgColor;
		/*document.getElementById('scrollarea').style.borderTop =  '1px solid '+bdColor;
		document.getElementById('scrollarea').style.borderBottom =  '1px solid '+bdColor;
		*/
		
		if (pContent){
			pContent.style.left = findPosX(posAnim, previousContent) + "px";
			pContent.style.backgroundColor = bgColor;
			fadeObject(pContent, 100 - Math.floor(posAnim));
		}
			
		cContent.style.left = findPosX(Math.round(posAnim)+1.05, currentContent) + "px";
		cContent.style.backgroundColor = bgColor;
		fadeObject(cContent, Math.ceil(posAnim));
		
		
		if (posAnim < 30)
			posAnim += Math.ceil(posAnim / 2) + 1;else
			posAnim += ((100 - Math.floor(posAnim)) / 4);
		
		
		cContent.style.display = 'block';
		setTimeout("animContents()", 20);
	}
	
}

function initContents(){
	cContent 			= getContent(currentContent);
	posAnim 			= 0;
	//fadeObject(nContent, 0);
	
	if (currentContent < previousContent)
		side = 1;else
		side = -1;
	
	animContents();
}

function overmenu(menuID, state){
	
	if (menuID != currentContent){
		tImagemenu = document.getElementById('menu'+menuID);
		contentData[menuID][4] = state;
	}
	
}

function menuselect(menuID){
	
	if (menuID == currentContent || posAnim){
		return false;
	}
	
	overmenu(menuID, false);
	
	if (currentContent != -1){
		if (document.getElementById('menufond'+currentContent))
		document.getElementById('menufond'+currentContent).style.backgroundImage = "url(images/" + contentData[currentContent][1] + "0.png)";
		contentData[currentContent][5] = 100;
		fadeObject('menu'+currentContent, 100);
	}
	
	if (contentData[menuID][1])
	document.getElementById('menufond'+menuID).style.backgroundImage = "url(images/" + contentData[menuID][1] +"2.png)";
	
	
	previousContent = currentContent;
	currentContent 	= menuID;
	
	if (isIE())
		document.getElementById('dummylocation').src = "location.php?hash=" + contentData[menuID][0];else
		document.location.href = "#" + contentData[menuID][0];
	
	initContents();
	
	return false;
}

function adjustPosition(){
	if (!posAnim){
		var cContent = getContent(currentContent);
		if (cContent)
			cContent.style.left = findPosX(0, -1) + "px";
	}
}

function animMenu(){
	var i;
	
	for (i = 0; i < totalmenus; i++){
		var offset = 0;
		if (contentData[i][4] && contentData[i][5] < 100)
			offset = 50;else
		if (!contentData[i][4] && contentData[i][5] > 0){
			if (i == currentContent)
				offset = -10;else
				offset = -5;
		}
		
		if (offset){
			contentData[i][5] += offset;
			if (contentData[i][5] > 100)
				contentData[i][5] = 100;
			
			if (contentData[i][5] < 0)
				contentData[i][5] = 0;
			fadeObject('menu'+i, contentData[i][5]);
		}
	}
}

function animOscillateur(){
	
	var i;
	return false;
	
	if (oscillPos == 1){
		oscillPos = 0;
		return;
	} else	
		oscillPos++;
	
	
	if (ci > 100 || ci == 0){
		ci = 0;
		
		
		if (!oscillColor[0]){
			/*for (i = 1; i <= 5; i++){
				fadeObject('b' + i, 50 + (i*5));
				fadeObject('b' + (11 - i), 50 + (i*5));
			}*/
			
			oscillColor[0] = oscillData[rand(0, 7)];
		} else
			oscillColor[0] = oscillColor[1];
		
		do{
			oscillColor[1] = oscillData[rand(0, 7)];
		} while (oscillColor[1] == oscillColor[0])
	}
	
	for (i = 1; i <= 10; i++){
	
		extra = (10 - Math.abs(i - (ci-50)));
		if (extra < 0)
			extra = 0;
			
		dif = (extra/5);
		if (dif < 1)
			dif = 1;
			
		hauteur = 1 + Math.round((rand(5, 25)/dif) + (extra*5));
		
		b = document.getElementById('b' + i);
		b.style.backgroundColor = fadeColor(oscillColor[0], oscillColor[1], ci - (i-1)*4);
		
		b.style.top = (82 - hauteur) + 'px';
		b.style.height = hauteur + 'px';
	}
	ci++;
}

function animBlackZone(pos, direction){
	var top 	= document.getElementById('bzTop');
	var bottom	= document.getElementById('bzBottom');
	
	if (pos == 0){
		document.getElementById('blackzone').style.display = 'block';
		pos ++;
	}
	
	if (direction)
		offset = pos;else
		offset = 174 - pos;
	
	if (pos < 100)
		pos += Math.ceil(pos/5);else
		pos += Math.ceil((175 - pos)/10);
	
	top.style.height = (257 - offset) + 'px';
	bottom.style.top = (257 + offset) + 'px';
	bottom.style.height = (257 - offset) + 'px';
	
	if (pos > 175){
		if (direction)
			document.getElementById('blackzone').style.display = 'none';
	}
	
	return pos;
}


function animGallery(state, pos){
	var i;
	var thumb;
	
	if (state == 1){
		if (pos == 0){
			document.getElementById('musicplayer').style.visibility= 'hidden';
		}
		
		pos = animBlackZone(pos, false);
		
		if (pos >= 175){
			document.getElementById('menu').style.display='none';
			document.getElementById('content' + currentContent).style.display='none';
			document.getElementById('loading').style.display='block';
			state = 2;
		} else{
			setTimeout('animGallery('+state+', '+pos+');', 10);
			return;
		}
		
	} else
	
	if (state == 2){
		
		var total = 0;
		
		for (i = 0; i < galleryImages.length; i++){
			thumb = document.getElementById('imagethumb'+i);
			if (thumb.complete)
				total ++;
		}
		
		if (total < galleryImages.length){
			document.getElementById('loading').innerHTML = 'Chargement: ' + Math.ceil(getPercentage(0, galleryImages.length, total))+'%';
			
		} else {
			//document.getElementById('infoGalerie').style.display = 'block';
			document.getElementById('scrollimages').style.display = 'block';
			document.getElementById('loading').style.display = 'none';
			pMaxWidth = 0;
			showGallery = true;
			placeThumbnails();
			state = 3;
			pos = 0;
		}
		
	} else
	
	if (state == 3) {
		pos = animBlackZone(pos, true);
		
		if (pos > 100){
			document.getElementById('infoGalerie').innerHTML = '<div><h1>'+galleryTitle+' <small>- '+galleryImages.length+' image(s)</small></h1><a href="javascript:closeGallery()">Cliquez ici pour revenir à la page précédente</a></div>';
			document.getElementById('infoGalerie').style.display = 'block';
			animThumbnails();
			pos = 0;
			return;
		} else{
			setTimeout('animGallery('+state+', '+pos+');', 10);
			return;
		}
	} else
	
	if (state == 4){
		showGallery = false;
		if (pos == 0){
			pos = 100;
		}
		pos = animBlackZone(pos, false);
		
		if (pos >= 175){
			document.getElementById('menu').style.display='block';
			document.getElementById('content' + currentContent).style.display='block';
			document.getElementById('scrollimages').style.display = 'none';
			document.getElementById('infoGalerie').style.display = 'none';
			pos = 0;
			state = 5;
		} else{
			setTimeout('animGallery('+state+', '+pos+');', 10);
			return;
		}
	} else
	
	if (state == 5){
		pos = animBlackZone(pos, true);
		
		if (pos >= 175){
			document.getElementById('blackzone').style.display = 'none';
			document.getElementById('musicplayer').style.visibility= '';
			return;
		} else{
			setTimeout('animGallery('+state+', '+pos+');', 10);
			return;
		}
	}
	
	if (state != 0)
		setTimeout('animGallery('+state+', '+pos+');', 10);
}

function openGallery(folder, images, title){
	initGallery('scrollimages', folder, images, title);
	animGallery(1, 0);
}

function closeGallery(){
	animGallery(4, 0);
}


function intro(state, pos){
	
	var top 	= document.getElementById('bzTop');
	var bottom	= document.getElementById('bzBottom');
	
	if (!state){
		state = 1;
		pos  = 0;
		document.getElementById('scrollarea').style.background = '#000000';
		document.getElementById('musicplayer').style.visibility= 'hidden';
		document.getElementById('musicplayer').style.width = '20px';
		fadeObject('top', 0);
		top.style.height = '257px';
		bottom.style.top = '259px';
	} else
	
	if (state == 1){
		document.getElementById('scrollarea').style.background = fadeColor('#000000', previousBgColor, pos);
		pos+=5;
		if (pos > 100){
			pos = 0.05;
			state = 2;
		}
	} else
	
	if (state == 2){
		if (pos < 100)
			pos += Math.ceil(pos/5);else
			pos += Math.ceil((175 - pos)/10);
			
		top.style.height = (257 - pos) + 'px';
		bottom.style.top = (257 + pos) + 'px';
		bottom.style.height = (257 - pos) + 'px';
		
		if (pos > 175){
			pos = 0;
			state = 3;
			document.getElementById('blackzone').style.display = 'none';
			setTimeout('intro('+state+', '+pos+');', 50);
			return;
		}
	} else
	
	if (state == 3){
		pos += Math.ceil((100 - pos) /8)
		fadeObject('top', pos);
		
		if (pos >= 100){
			document.getElementById('fpObject').style.visibility = '';
			document.getElementById('fpObject').width = 1;
			state = 4;
			pos = 0;
		}
	} else
	
	if (state == 4){
		document.getElementById('musicplayer').style.visibility = '';
		document.getElementById('fpObject').width = pos + 1;
		document.getElementById('musicplayer').style.width = (pos+14) + 'px';
		document.getElementById('musicplayer').style.left = Math.round(158 - (pos /2)) + 'px'; 
		
		pos += Math.ceil((265 - pos) / 5)
		
		if (pos >= 265){
			menuselect(0);
			process();
			return;
		}
	}
	
	setTimeout('intro('+state+', '+pos+');', 10);
}


function process(){
	var actualHash;
	
	if (!showGallery){
		if (isIE() && changeHash){
			actualHash 		= "#" + changeHash;
			previousHash 	= -1;
			changeHash = false;
		} else {
			actualHash = document.location.hash;
		}
		
		if (actualHash != previousHash){
			previousHash = actualHash;
			
			contentID = findContentID(actualHash.substring(1));
			
			if (contentID != currentContent && contentID != -1)
				menuselect(contentID);
		}
		
		adjustPosition();
		//animOscillateur();
		animMenu();
		
		setTimeout("process()", 10);
	} else
	
		setTimeout("process()", 150);
}


function init(){
	
	for (i = 0; i < totalmenus; i++){
		fadeObject('menu'+i, 0);
		contentData[i][4] = 0;
		contentData[i][5] = 0;
		bufferMenu[i] = new Image(200, 200);
		bufferMenu[i].src = 'images/'+contentData[1]+'2.png';
	}

	//fadeObject('oscillateur', 25);
}