// JavaScript Document

var overlay;
var galerie_offset = 0;
var galerie_step = 9;
var fenetre = 0;
var last_fenetre_overlay = 0;
var total_galerie = 0;
var detail_move = 100;
var overlay_respawn = 0;
var openOn = {'livre':'Charge_Livre()','galerie':'Charge_Galerie_Overlay()','peinture':'','biographie':'Charge_Biographie()','portrait':'Charge_Portrait()'}

function ChangeHash(a) {
	window.location.hash = a;
}

 $(document).ready(function(){
   //alert('all is loaded');

	// Lecture du HASH
	var hash = window.location.hash.slice(1);
	var hash_array = hash.split('/');
	hash1 = hash_array[0];
	hash2 = hash_array[1];
	if (hash1=='livre') setTimeout(openOn.livre,1000);
	if (hash1=='galerie') setTimeout(openOn.galerie,1000);
	if (hash1=='peinture') setTimeout('Charge_Galerie_Information('+hash2+')',1000);
	if (hash1=='biographie') setTimeout(openOn.biographie,1000);
	if (hash1=='portrait') setTimeout(openOn.portrait,1000);

    $.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

	/*****/
	/* Récupération des raccourcis clavier */
	document.onkeydown = function(e){
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		
		// 1 : Galerie
		// 2 : Detail
		// 3 : Actualite
		// 4 : Biographie
		// 5 : Portrait
		// 6 : Texte
		// 7 : Pied de page
		// 8 : Galerie(overlay)
		// 9 : Livre
		//$("#texte").html(keycode);
		// g37 h38 d39 b40 
		/* Deplacement des details */
		if(keycode == 37 && fenetre == 2) {
			$("#detail_inline").animate({left:"-="+detail_move+"px"}, 'fast');
		}
		if(keycode == 38 && fenetre == 2) {
			$("#detail_inline").animate({top:"-="+detail_move+"px"}, 'fast');
		}		
		if(keycode == 39 && fenetre == 2) {
			$("#detail_inline").animate({left:"+="+detail_move+"px"}, 'fast');
		}		
		if(keycode == 40 && fenetre == 2) {
			$("#detail_inline").animate({top:"+="+detail_move+"px"}, 'fast');
		}
		
		if(keycode == 27){
			if (fenetre == 1) {
				Decharge_Galerie_Information();
			}
			if (fenetre == 2) {
				Decharge_Detail();
			}
			if (fenetre == 3) {
				Decharge_Actualites();
			}
			if (fenetre == 4) {
				Decharge_Biographie();
			}
			if (fenetre == 5) {
				Decharge_Portrait();
			}
			if (fenetre == 6) {
				Decharge_Texte();
			}
			
			if (fenetre == 7) {
				Decharge_PDP();
			}
			if (fenetre == 8) {
				Decharge_Galerie_Overlay()
			}
			if (fenetre == 9) {
				Decharge_Livre()
			}
			
			
		}else if(keycode == 109 && Hacked == 1){
			overlay_show(0.75);
		}else if(keycode == 67){
			//afficher_mon_compte();
		}else if(keycode == 107 && Hacked == 1){
			overlay_hide();

		}else if(keycode == 112){
			alert('Raccourcis clavier:\n\nF1  : Cette aide\nC   : Mon compte\nESC : Fermer la fenêtre active');
		}else{
			//alert(keycode);
		}

		//$("#info").html("DEBUG MODE<br/><br/>keycode:"+keycode+"<br/>Hack:"+Hack+"<br/>Limit:"+Limit+"<br/>Fen&ecirc;tre:"+fenetre);

	};	
	/*****/

	$("#overlay").fadeTo(0, 0);

	/*$("#overlay").click(function(){
		//overlay_hide();
	});*/
	
	function texte_hide()
	{
		//$("#texte").hide('fast').html('Présentation du site').show('slow');
	}

	function changeTexte(value)
	{
		$("#texte").html(value);
	}

	$("#logo_weboa").mouseover(function() {
		$(this).animate(
			{top: "5px", right: "-=5px"}, 'fast');
		});
	$("#logo_weboa").mouseout(function() {
		$(this).animate({
			top: "10px", right: "+=5px"}, 'fast');
		});

	$("#menu_galerie").mouseover(function() {
		//$("#texte").hide('fast').html("Visitez la galerie, et contemplez la rigueur du travail, la possibilité d'avoir aussi la....").show('fast');
		//$("#texte").slideUp('slow');
		//$("#texte").slideUp('slow');
		//$("#texte").slideDown('slow');		

		// On fait disparaitre la zone de texte
		
		//$("#texte").wait(500);
		//$("#texte").animate( {height: "0px"}, 200, 'swing');
		//$("#texte").animate( {height: "50px"}, 1500, 'swing');
		
		//$("#texte").wait(500).html("ceci est un texte");
		//$("#texte").html("Visitez la galerie, et contemplez la rigueur du travail, la possibilité d'avoir aussi la....").slideDown('fast');
		});

	/* Animation des menus */
	$("#menu_galerie").mouseover(function() {
		$(this).animate({left:"+=10px"}, 'fast');
		});
	$("#menu_galerie").mouseout(function() {
		$(this).animate({left:"-=10px"}, 'normal');
		});

	$("#menu_biographie").mouseover(function() {
		$(this).animate({left:"-=10px"}, 'fast');
		});
	$("#menu_biographie").mouseout(function() {
		$(this).animate({left:"+=10px"}, 'normal');
		});

	$("#menu_portrait").mouseover(function() {
		$(this).animate({left:"+=10px"}, 'fast');
		});
	$("#menu_portrait").mouseout(function() {
		$(this).animate({left:"-=10px"}, 'normal');
		});	

	$("#menu_livre").mouseover(function() {
		$(this).animate({left:"-=10px"}, 'fast');
		});
	$("#menu_livre").mouseout(function() {
		$(this).animate({left:"+=10px"}, 'normal');
		});	

	$("#menu_actualite").mouseover(function() {
		$(this).animate({left:"+=10px"}, 'fast');
		});
	$("#menu_actualite").mouseout(function() {
		$(this).animate({left:"-=10px"}, 'normal');
		});	


	$("#menu_biographie").mouseover(function() {
		//$("#texte").hide('fast').html("Connaissez vous vraiment l'artiste ?").show('fast');
		});
	$("#menu_biographie").mouseout(function() {
		//$("#texte").hide('fast');
		//texte_hide();
		});		

	/* Animation des flèches */
	$("#arrow_left").mouseover(function() {
		$(this).animate({left:"-=10px"}, 'fast');
		});
	$("#arrow_left").mouseout(function() {
		$(this).animate({left:"+=10px"}, 'normal');
		});	
	$("#arrow_right").mouseover(function() {
		$(this).animate({right:"-=10px"}, 'fast');
		});
	$("#arrow_right").mouseout(function() {
		$(this).animate({right:"+=10px"}, 'normal');
		});	

});

function Charge_Galerie_Overlay()
{
	$("#wait").show();
	overlay_show(0.8);
	$.get('/spip.php?page=galerie',
		   {}, function(data) {
			   $('#galerie_overlay').show().html(data);
			   fenetre = 8;
			   ChangeHash('galerie');
			   $("#wait").hide();
		   }, "html");
}

function Decharge_Galerie_Overlay()
{
	$("#galerie_overlay").hide();
	overlay_hide(0.8);
   fenetre = 0;	
   ChangeHash('');
}

function Charge_Actualites()
{
	$("#wait").show();
	overlay_show(0.8);
	$('#actualites').show().html('chargement actualite');
	$.get('/spip.php?page=AJAX_Charge_Actualites',
		   {}, function(data) {
			   $('#actualites').show().html(data);
			   fenetre = 3;
			   $("#wait").hide();
		   }, "html");
}

function Decharge_Actualites()
{
	$("#actualites").hide();
	overlay_hide(0.8);
   fenetre = 0;	
}

function Charge_Portrait()
{
	$("#wait").show();
	overlay_show(0.75);
	$.get('/spip.php?page=portrait',
		   {}, function(data) {
			   $('#info').show().html(data);
			   fenetre = 5;
			   $("#wait").hide();
			   ChangeHash('portrait');
		   }, "html");
}

function Decharge_Portrait()
{
	$("#info").hide();
	overlay_hide(0.8);
   fenetre = 0;
   ChangeHash('');
}

function Charge_Texte()
{
	$("#wait").show();
	overlay_show(1);
	$.get('/spip.php?page=texte',
		   {}, function(data) {
			   $('#texte').show().html(data);
			   fenetre = 6;
			   $("#wait").hide();
		   }, "html");
}
function Decharge_Texte()
{
	$("#texte").hide();
	overlay_hide(0.8);
   fenetre = 0;	
}

function Charge_PDP(id_article)
{
	$("#wait").show();
	overlay_show(0.8);
	$.get('/spip.php?page=pdp',
		   {
			   'id_article' : id_article
			}, function(data) {
			   $('#info').show().html(data);
			   fenetre = 7;
			   $("#wait").hide();
		   }, "html");
}
function Decharge_PDP()
{
	$("#info").hide();
	overlay_hide(0.8);
   fenetre = 0;	
}

function Charge_Biographie()
{
	$("#wait").show();
	overlay_show(0.75);
	$.get('/spip.php?page=biographie',
		   {}, function(data) {
			   $('#biographie').show().html(data);
			   fenetre = 4;
			   $("#wait").hide();
			   ChangeHash('biographie');
		   }, "html");
}

function Decharge_Biographie()
{
	$("#biographie").hide();
	overlay_hide(0.8);
   fenetre = 0;
   ChangeHash('');
}

function Charge_Livre()
{
	$("#wait").show();
	overlay_show(0.75);
	$.get('/spip.php?page=livre',
		   {}, function(data) {
			   $('#livre').show().html(data);
			   fenetre = 9;
			   ChangeHash('livre');
			   $("#wait").hide();
		   }, "html");
}

function Decharge_Livre()
{
	$("#livre").hide();
	overlay_hide(0.8);
   fenetre = 0;	
   ChangeHash('');
}

function Charge_Galerie_Information(id)
{
	if (fenetre == 8) {
		last_fenetre_overlay = 8;
		$("#galerie_overlay").fadeTo('normal', 0, function() {
			$(this).hide();
		});
	}
	overlay_show(0.75);
	$("#wait").show();	
	$('#info').html('chargement...['+id+']');
	$.get('/spip.php?page=AJAX_Charge_Galerie_Information',
		   {
			   id_article : id
		   }, function(data) {
			   $('#info').show().html(data);
			   	$("#wait").hide();
		   		fenetre = 1;
				ChangeHash('peinture/'+id);
		   }, "html");
}

function Decharge_Galerie_Information()
{
	if (last_fenetre_overlay == 8) {
		$('#info').hide();
		fenetre = 8;
		last_fenetre_overlay = 0;
		$("#galerie_overlay").show().fadeTo('normal', 1);
		ChangeHash('galerie');
	}else {
		$('#info').hide();
		overlay_hide();
   		fenetre = 0;
		ChangeHash('');
	}
}

/* Affiche le détail d'une galerie
*/
function Charge_Detail(image, info, overlay)
{
  if (overlay == 1) {
	  overlay_respawn = 1;
  }else{
	  overlay_respawn = 0;
  }
  overlay_show(1);
  $("#wait").show();
	$("#info").fadeTo('slow', 0.0, function() {
		$("#detail").show().fadeTo('slow', 1, function() {
			$(this).html('<table width="100%" height="100%"><tr><td><p id="detail_inline" align="center"><img style="cursor:move;" src="'+image+'" /><br/><br/>'+info+'<br/><span style="font-size:8pt;">double click pour fermer cette fen&ecirc;tre.</span><script>$("#detail_inline").draggable();</script></p><div onclick="Decharge_Detail()" class="bicone_close" id="bclose_image_detail" style="left:50px;top:50px;"></div></td></tr></table>');
			last_fenetre = fenetre;
			fenetre = 2;
			$("#wait").hide();
			})
		});
}

function Decharge_Detail()
{
  if (overlay_respawn != 1) {
	  overlay_show(0.8);
  }else{
	  overlay_hide();
  }
	$("#detail").fadeTo('slow', 0, function() {
		$(this).html(' ').hide(function() {
			$("#info").fadeTo('fast',1);
			fenetre = last_fenetre;
			})});
}

function overlay_show(opacity)
{
	opacity = opacity || 1;
	$("#overlay")
		.show()
		.fadeTo('normal', opacity);
		overlay = 1;
}

function overlay_hide()
{
	$("#overlay")
		.fadeTo('normal', 0, function(){
			$(this).hide();
			overlay = 0;			
			});
}

function Charge_Galerie (direction) {
	// id : galerie
	move=false;
	if (direction == "+" && galerie_offset+galerie_step < total_galerie) {
		galerie_offset = galerie_offset + galerie_step;
		move=true;
	}else if (direction == "-" && (galerie_offset-galerie_step) >= 0) {
		galerie_offset = galerie_offset - galerie_step;
		move=true;
	}
	if (move==true) {
		$("#wait").show();
		//overlay_show();
		
		$.get('/spip.php?page=vignettes',
				{
					debut_galerie : galerie_offset
				}, function(data) {
					$("#galerie").animate({'top':'-=800px' }, 'normal', function() {
						$("#galerie").html(data);
						$("#galerie").animate({'top':'+=800px' }, 'normal', function() {
						$("#wait").hide();
						//overlay_hide();
						})
					});				
				}, 'html');
	}
	//$("#info").show().html("direction:"+direction+"<br/>galerie_offset:"+galerie_offset+"<br/>galerie_step:"+galerie_step+"<br/>total_galerie:"+total_galerie);
}

function goUrl(url) {
	window.open("/redirect.php?url=" + url);
}
