var tab_photo = new Array();
var x = 0;

var IE6 = false, IE7 = false, IE8 = false, IE9 = false,
 FIREFOX2  = false, FIREFOX  = false,
 NETSCAPE7 = false, NETSCAPE = false, 
 OPERA9 = false, OPERA = false, 
 AUTRE = false; IE = false;
var strChUserAgent = navigator.userAgent;
var intSplitStart = strChUserAgent.indexOf("(",0);
var intSplitEnd = strChUserAgent.indexOf(")",0);
var strChStart = strChUserAgent.substring(0,intSplitStart);
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
var strChEnd = strChUserAgent.substring(strChEnd);
if(strChMid.indexOf("MSIE") != -1)
 IE = true;
if(strChMid.indexOf("MSIE 8") != -1)
 IE8 = true;
if(strChMid.indexOf("MSIE 9") != -1)
 IE9 = true; 
if(strChMid.indexOf("MSIE 7") != -1)
 IE7 = true;
else if(strChMid.indexOf("MSIE 6") != -1)
 IE6 = true;
else if(strChEnd.indexOf("Firefox/2") != -1)
 FIREFOX2 = true;
else if(strChEnd.indexOf("Firefox") != -1)
 FIREFOX = true;
else if(strChEnd.indexOf("Netscape/7") != -1)
 NETSCAPE7 = true;
else if(strChEnd.indexOf("Netscape") != -1)
 NETSCAPE = true;
else if(strChStart.indexOf("Opera/9") != -1)
 OPERA9 = true;
else if(strChStart.indexOf("Opera") != -1)
 OPERA = true;
else
 AUTRE = true;

$(document).ready(function(){
	if(!IE7 && !IE6){
		$(".list > li").hover(function(){
			$(this).addClass("degrade_menu");
		},function(){
			$(this).removeClass("degrade_menu");
		});
		
		$(".album").hover(function(){
			$(this).css("background","#F5F5F5");
		},function(){
			$(this).css("background","");
		});
	}
	
	$.ajax({
		type: "POST",
		url: "/lib/home_photo.asp",
		success: function(msg){
			eval( msg );
		}
	});	
	
	$.ajax({
		type: "POST",
		url: "/lib/home_photo2.asp",
		success: function(msg){
			eval( msg );
		}
	});	
});

function show_image(){
	var html = $("#animation").html();
	$("#animation").find("img").fadeOut(function(){
		$(this).attr("src",tab_photo[x]);
		$(this).load(function(){
			$("#animation").find("img").fadeIn();
		});
	});
	x++;
	if(x >= 10){
		x = 0;	
	}
}

function show_image2(){
	var html = $("#animation2").html();
	$("#animation2").find("img").fadeOut(function(){
		$(this).attr("src",tab_photo[x]);
		$(this).load(function(){
			$("#animation2").find("img").fadeIn();
		});
	});
	x++;
	if(x >= 10){
		x = 0;	
	}
}

this.reste = "";
this.i = 0;
this.total = 0

function view_photo(file,ref){
	file = unescape(file);
	var temp = file.replace(".jpg","");
	this.reste = file.substr(0,temp.length-4);
	this.i = temp.substr(temp.length-4) * 1;
	this.total = $("#total_album").val();
	var txt = '<div id="close" onclick="fermer_photo()"><img src="/img/fermer.png" /></div>'
	txt = txt + '<table><tr><td width="40" align="center">';
	txt = txt + '<div id="pic_left" onClick="pic_prec()"><img src="/img/gauche.png" border="0" /></div>';
	txt = txt + '</td><td><div><img id="thepic" src="' + file + '" /><div class="watermark2">LaboRacing.com</div><div><div style="margin-top:10px;float:left" id="ref">Ref. ' + ref + '</div><div style="float:right;margin-top:10px;"><a href=javascript:send_mail("' + ref + '","' + escape(file) + '") class="lien_mail">Recevoir cette photo par e-mail</a></div></td><td width="40" align="center">';
	txt = txt + '<div id="pic_right" onClick="pic_suiv()"><img src="/img/droite.png" /></div>';
	txt = txt + '</td></tr></table>'
	$("#picture").html(txt);
	
	$("#picture").find("img").load(function(){
		centrer("#picture");
		$("#picture").fadeIn();
		$("#bg").css("display","block");
		$("#bg").animate({
			opacity:0.8
		});
	});
	check_fleche(i,total,escape(reste));
}

var ref = "";
var file = "";

function pic_prec(){
	this.i = this.i - 1;
	check_fleche();	
}

function pic_suiv(){
	this.i = this.i + 1;
	check_fleche();	
}

function check_fleche(){
	nbr = "000" + this.i;
	ref = this.reste + nbr.substr(nbr.length-4);
	file = ref + ".jpg";
	ref = ref.substr(ref.length-12);
	$("#thepic").attr("src",file);
	$("#ref").html("Ref. " + ref);
	$(".lien_mail").attr("href","javascript:send_mail('" + ref + "','" + file + "')");
	if(i==1){
		$("#pic_left").css("visibility","hidden");
	}else{
		$("#pic_left").css("visibility","visible");
	}
	if(i==total){
		$("#pic_right").css("visibility","hidden");
	}else{
		$("#pic_right").css("visibility","visible");
	}
}
function send_mail(ref,lien){
	var W = $("#picture").width();
	var H = $("#picture").height();
	var txt = '<div id="close" onclick="fermer_photo()"><img src="/img/fermer.png" /></div>'
	txt = txt + "<p class='titre'>Recevoir cette photo par e-mail</p>";
	txt = txt + "<div style='float:left;margin-right:10px;'><img src='" + lien.replace("logo","petite") + "' /></div>";
	txt = txt + "<div style='float:left;'><div style='font-size:16px;font-weight:bold;'>" + $("#album_titre").html() + "</div><p>Ref. " + ref + "</p></div>";
	txt = txt + "<p style='clear:both;'></p><br /><hr size='1' color='#E5E5E5'>";
	txt = txt + "<div id='info'><div style='margin-left:130px;width:320px;'><p style='font-size:14px;'>Votre adresse e-mail : <input type='text' id='email' style='width:100%;margin-top:5px;' /></p>"
	txt = txt + "<p><input type='checkbox' id='condition' /> J'accepte les <a href='' target='_blank'>conditions d'utilisation</a> de Labo Racing<br /><input type='checkbox' id='memory' /> Retenir mon adresse e-mail</p>";
	txt = txt + "<br /><p align='right'><input type='button' value='Annuler' class='bouton' onclick=view_photo('" + escape(lien) + "','" + ref + "') /> <input type='button' value='Envoyer la photo' class='bouton' onclick=send_pic('" + ref + "','" + escape(lien) + "') /></p></div></div>";
	$("#picture").html(txt);
	$("#picture").width(W);
	var email_cookie = $("#email_cookie").val();
	var condition_cookie = $("#condition_cookie").val();
	$("#email").val(email_cookie);
	if(email_cookie != ""){
		$("#memory").attr("checked","checked");
	}
	if(condition_cookie=="ok"){
		$("#condition").attr("checked","checked");
	}
	$("#email").focus();
}

function send_pic(ref,lien){
	email = $("#email").val();
	album = $("#album").val();
	memory = $("#memory").attr("checked");
	condition = $("#condition").attr("checked");
	if(email.indexOf("@") > 1 && email.indexOf(".") > 1){
		if(condition){
			$("#info").css("text-align","center");
			$("#info").html("<br/><br/><br/><br/><img src='/img/loadingpt.gif' />");
			$.ajax({
				type: "POST",
				url: "/lib/send_photo.asp",
				data: "ref=" + ref + "&lien=" + lien + "&email=" + email + "&memory=" + memory + "&condition=" + condition + "&album=" + album,
				success: function(msg){
					eval( msg );
					setTimeout("view_photo('" + lien + "','" + ref + "')",2000);
				}
			});	
		}else{
			alert("Vous devez accepter les conditions d'utilisation de labo Racing");	
		}
	}else{
		alert("Veuillez indiquer une adresse e-mail valide.")	
		$("#email").focus();
	}
}

function fermer_photo(){
	$("#picture").fadeOut("fast");
	$("#bg").fadeOut("fast");	
}

function centrer(e){
	var W = $(e).width();
	var H = $(e).height();
	$(".watermark").css("width",(W-95)+"px");
	if (document.body && document.body.offsetHeight) {
	 winW = document.body.offsetWidth;
	 winH = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' &&
	    document.documentElement &&
	    document.documentElement.offsetHeight ) {
	 winW = document.documentElement.offsetWidth;
	 winH = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight) {
	 winW = window.innerWidth;
	 winH = window.innerHeight;
	}
	var left = (winW - W)/2;
	var top = ((winH - H)/2)-40;
	
	if(top < 0){
		top = 0;	
	}
	$(e).css("top",top + "px");
	$(e).css("left",left + "px");
	$("#bg").css("width",winW+"px");
	$("#bg").css("height",winH+"px");
}
