// perso.js 
// Copyright (c) 2008 Bulteau Fran¨ois 

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

var albumEnCours = "";


function initFunc(){
	//Evenements logo
	Event.observe('logoAccueil', 'click', afficherFlottante);
	
	//Evenements Menu
	Event.observe($('slideStudio').previous(), 'mousedown', slideMenu);
	Event.observe($('slideTravaux').previous(), 'mousedown',slideMenu);
	Event.observe($('slidePhoto2').previous(), 'mousedown',slideMenu);
	Event.observe($('slideGens').previous(), 'click',slideMenu);
	Event.observe($('slideBook').previous(), 'mousedown',slideMenu);
	Event.observe($('slideContact').previous(), 'mousedown',testGM);
	Event.observe($('hide'), 'mousedown',hideFlottante);
}

function slideMenu(){
	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 'slidePhoto2':
			afficherLesTravaux();
			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 hideFlottante(){
	//if(albumEnCours!="") {$(albumEnCours).style.visibility = "hidden";}
	Effect.toggle(this.next().next().id,'slide')
	
	
}


function afficherFlottante(){
	$("flottanteBox").style.visibility = "visible";
	$("logoAccueil").style.visibility = "hidden";
}

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>");
}

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 testGM(){
	setTimeout('afficherGoogleMap()', 500); 
	$("fond").style.visibility = "visible";
}

function afficherGoogleMap(){
		cacherFonds();
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("fond"));
			map.setCenter(new GLatLng(48.86204,2.351031), 12);
			map.addControl(new GLargeMapControl());
			var mapControl = new GMapTypeControl();
			map.addControl(mapControl);
			var point = new GLatLng(48.86204,2.351031);
			var Marqueur = new GMarker(point);
			var html = "<p><span class='googleTitre'><b><i>Sono alla drogeria.</i></b></span></p>";
			GEvent.addListener(Marqueur, "click", function(){
					map.openInfoWindowHtml(point,html,{noCloseOnClick: true});
			});
			map.addOverlay(Marqueur);
			GEvent.trigger(Marqueur,"click");
			
			fondEnCours = "googleMaps";
			$("fond").style.visibility = "visible";
		}
}

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',
		'/data/dossierphoto1/photo.php?album='+nomalbum,
		{
			method: 'get'
		}
	);
	
	setOnMouseClick();
	var dossier = 'dossierphoto1';
	majListePhoto(nomalbum,dossier);
	$(nomalbum).style.visibility = "visible";
	$("imgs").style.visibility = "visible";	
}

function charger2(nomalbum) {
	cacherFonds()
	albumEnCours = nomalbum;
	
	new Ajax.Updater(
		'imgs',
		'/data/dossierphoto2/photo.php?album='+nomalbum,
		{
			method: 'get'
		}
	);
	
	setOnMouseClick();
	var dossier = 'dossierphoto2';
	majListePhoto(nomalbum,dossier);
	$(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, dossier) {
	new Ajax.Request(
		'/data/'+dossier+'/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/'+dossier+'/'+nomalbum+'/'+file.firstChild.nodeValue;
														x++;							
												}
												file = file.nextSibling
											}																																	
										}
										image = image.nextSibling
									}
								}
							}														
							album = album.nextSibling
						}
					}
					gallery = gallery.nextSibling
				}				
				imgs = new MudShiftContent('imgs', imgsGallery);												
			}
		}
	);		
}




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

