// perso.js 
// Copyright (c) 2008 Bulteau Francois 

var imgs;
var fondEnCours;
var ouvertLesGens = "false";
var ouvertLesTravaux = "false";
var ouvertGoogleMap = "false";

var albumEnCours = "";
var domaineSite ="/";
var pathImageBlanche = "/images/blanc.png";

var geoXml="";
var map="";
var divVideo;


function initFunc(){
	
	//Evenements Menu
	Event.observe($('slideStudio').previous(), 'mousedown', slideMenu);
	Event.observe($('slideTravaux').previous(), 'mousedown',slideMenu);
	Event.observe($('slideVideos').previous(), 'mousedown',slideMenu);
	Event.observe($('slideGens').previous(), 'click',slideMenu);
	Event.observe($('slideBook').previous(), 'mousedown',slideMenu);
	Event.observe($('slideContact').previous(), 'mousedown',GM);
}


function slideMenu(){
	scrollTo(0,0);
	switch(this.next().id){
		case 'slideStudio':
			cacherFonds();
			Effect.toggle(this.next().id,'slide');
			break;
		case 'slideTravaux':
			afficherLesTravaux();
			Effect.toggle(this.next().id,'slide');
			break;
		case 'slideVideos':
			Effect.toggle(this.next().id,'slide');
			break;
		case 'slideGens':
			Event.stopObserving($('slideGens').previous(), 'click',slideMenu);
			afficherLesGens();
			Effect.toggle(this.next().id,'slide');
			setTimeout("initFunc()",1000);
			break;
		case 'slideBook':
			cacherFonds();
			Effect.toggle(this.next().id,'slide');
			break;
	}
}


function cacherFonds(){
	$("fond").style.visibility = "hidden";
	$("lesGens").style.visibility = "hidden";
	$("imgs").style.visibility = "hidden";
	if(albumEnCours!="") {$(albumEnCours).style.visibility = "hidden";}
	$("imgs").replace("<div id='imgs'></div>");
	$("flashcontent").style.visibility = "hidden";
	deleteVideo();
}

function afficherLesGens(){
	if(fondEnCours == "lesGens" || ouvertLesGens == "true") {
		fondEnCours = "";
		$("lesGens").style.visibility = "hidden";
	} else {
		cacherFonds();
		fondEnCours = "lesGens";
		$("lesGens").style.visibility = "visible";
	}
	
	if(ouvertLesGens == "false"){ouvertLesGens = "true";} else {ouvertLesGens = "false";}
}




function GM(){
	setTimeout('afficherGoogleMap()', 500); 
	$("fond").style.visibility = "visible";
}


function afficherGoogleMap(){
		cacherFonds();
		if (GBrowserIsCompatible()) {
			 
			 
			//affichage de la carte google maps personnalisé en utilisant le fichier kml
	        var geoXml = new GGeoXml("http://www.juliendoucet.com/data/map/map.kml");
	        var map = new GMap2(document.getElementById('fond')); 
	        map.setCenter(new GLatLng(48.86204,2.351031), 12); 
	        map.addOverlay(geoXml);
	        
	        GEvent.addListener(map, "click", evtMap);
	        
	        //suppresion des conditions d'utilisation de google
	        $('fond').childElements()[2].setStyle("visibility:hidden;");
			
			fondEnCours = "googleMaps";
			$("fond").style.visibility = "visible";
	        //window.map = map;	
			
		}
}

function chgtVideo(){

	
if($('iw_kml')!=null && $('iw_kml').firstDescendant().firstDescendant()!=null){
		var liUrl = $('iw_kml').firstDescendant().firstDescendant().firstDescendant();
		//Cas d'une demande de vidéo : Vérification qu'il s'agit d'une liste
		if(liUrl != null && liUrl.nodeName == "LI"){
			var urlVideo = liUrl.childNodes[0].nodeValue;
			//Vérification que l'adresse url de la video est sur le site courant et que le fichier est un swf
			if(urlVideo.search(/juliendoucet.com/) != -1 && urlVideo.search(/.swf$/)){

				//alignement de la vidéo
				$('iw_kml').style.textAlign = "center";
				
				var heightVideo = $('iw_kml').firstDescendant().childElements()[1].readAttribute("height");
				var widthVideo = $('iw_kml').firstDescendant().childElements()[1].readAttribute("width");
				
				//suppression de la liste et de l'image de fond de chargement
				$('iw_kml').removeChild($('iw_kml').firstDescendant());			
		 		
		 		divVideo = document.createElement('div');
				 divVideo.setAttribute('id',"video");
				 //divVideo.appendChild(objectNode);
				 $('iw_kml').appendChild(divVideo);
				 divVideo.innerHTML = "<object height=\'"+heightVideo+"\' width=\'"+widthVideo+"\' type='application/x-shockwave-flash' data=\'"+urlVideo+"\'><param name='movie' value=\'"+urlVideo+"\'/><param name='quality'/><param name='wmode' value='transparent'/><param name='loop' value='true'/></object>";
				 
				  
			} 

		}
	}

}

function evtMap(){
	//reproduit le schéma liste plus image de fond, puis supprime la video; dû à ie qui ne supprime pas le fichier flash automatiquement
	if(divVideo!=null){		
		if(divVideo.firstChild!=null && divVideo.firstChild.nodeName=="OBJECT"){
			//ajout d'une liste
			//alert(divVideo.firstChild.getAttribute("height"));
			var ultmp = document.createElement('ul');
			var litmp = document.createElement('li');
			litmp.setAttribute('height',divVideo.firstChild.getAttribute("height"));
			litmp.setAttribute('width',divVideo.firstChild.getAttribute("width"));
			litmp.innerHTML=divVideo.firstChild.getAttribute("data");
			
			//ajoutde l'image de fond de chargement
			var imgtmp = document.createElement('img');

			imgtmp.setAttribute('src',pathImageBlanche);
			imgtmp.setAttribute('height',divVideo.firstChild.getAttribute("height"));
			imgtmp.setAttribute('width',divVideo.firstChild.getAttribute("width"));
			
			divVideo.removeChild(divVideo.firstChild);
			ultmp.appendChild(litmp);
			divVideo.appendChild(ultmp);
			divVideo.appendChild(imgtmp);
		}
	}	
	setTimeout("chgtVideo()",1000);

}

function afficherLesTravaux(){
	if(fondEnCours == "lesTravaux" || ouvertLesTravaux == "true") {
		fondEnCours = "";
		$("imgs").style.visibility = "hidden";
		if(albumEnCours!="") {$(albumEnCours).style.visibility = "hidden";}
	} else {
		cacherFonds();
		fondEnCours = "lesTravaux";
		$("imgs").style.visibility = "visible";
	}
	
	if(ouvertLesTravaux == "false"){ouvertLesTravaux = "true";} else {ouvertLesTravaux = "false";}
}



function charger(nomalbum) {
	cacherFonds()
	albumEnCours = nomalbum;
	
	new Ajax.Updater(
		'imgs',
		domaineSite+'data/photo/photo.php?album='+nomalbum,
		{
			method: 'get'
		}
	);
	
	setOnMouseClick();
	majListePhoto(nomalbum);
	$(nomalbum).style.visibility = "visible";
	$("imgs").style.visibility = "visible";	
}

function setOnMouseClick() {
	var elements = document.getElementsByTagName("a");
	for (var i = 0; i < elements.length; i++) {
		switch(elements[i].className) {
			case "next":
				elements[i].onclick = function() {
					 imgs.move('next');
					 
					 return false;
				}
				break;
			case "prev":
				elements[i].onclick = function() {
					 imgs.move('prev');
					 return false;
				}
				break;
		}
	}
}

function majListePhoto(nomalbum) {
	new Ajax.Request(
		domaineSite+'data/photo/getXML.php',
		{
			onSuccess: function(transport, json) {				

				var x = 0;
				var docXml = transport.responseXML;
				
				var child = docXml.firstChild;
				imgsGallery = new Array();
								
				while (child) {
					if(child.nodeName == 'gallery'){
						gallery = child.firstChild;
					}
					child = child.nextSibling;
				}
				
				
				while(gallery){
					if(gallery.nodeName == 'album'){
						var album = gallery.firstChild;
						while(album){
														
							if(album.nodeName == 'albumname'){

								if(album.firstChild.nodeValue == nomalbum){
									
									image = gallery.firstChild;								
									
									while(image){
										if(image.nodeName == 'image'){
											file = image.firstChild;
											while(file){
												if(file.nodeName =='filename'){
														imgsGallery[x] = new Object();
														imgsGallery[x].image = new Image();
														imgsGallery[x].image.src ='data/photo/'+nomalbum+'/'+file.firstChild.nodeValue;
														x++;							
												}
												file = file.nextSibling;
											}																																	
										}
										image = image.nextSibling;
									}
								}
							}														
							album = album.nextSibling;
						}
					}
					gallery = gallery.nextSibling;
				}				
				
				imgs = new MudShiftContent('imgs', imgsGallery);	
														
			}
		}
	);		
}


function chargerVideo(nomalbum) {
	cacherFonds();
	scrollTo(0,0);
	document.body.scrollTop='0px';
	
	new Ajax.Updater(
		'flashcontent',
		domaineSite+'data/video/video.php?album='+nomalbum,
		{
			method: 'get'
		}
	);
	
	$("flashcontent").style.visibility = "visible";
}

function deleteVideo(){
	while ($("flashcontent").firstChild) {
	  $("flashcontent").removeChild($("flashcontent").firstChild);
	}

}



Event.observe(window, 'load', initFunc, false);