var boutique_link;
$(document).ready(function(){
	$("<link>").appendTo("head").attr({rel: "stylesheet", type: "text/css", href: base_url+"/styles/colorbox.css"});
	$.getScript(base_url+"/scripts/jquery.colorbox.js", function() {
		var v_link = $("a.video_link");
		if(v_link.length > 0) {
			v_link.colorbox({
						width:"500px",
						height:"350px",
						iframe:true,
						scrolling:false
				});			
		}
		
		var img_link = $("a.image_link");
		if(img_link.length > 0) {
			img_link.colorbox({maxWidth:"75%"});			
		}
		boutique_link = $("a.boutique_link");
		
		if(boutique_link.length > 0) { 
			boutique_link.colorbox({iframe:true,width:"500px", height:"400px", speed:0});
		}
		
		alert_link = $("a.alert_link");
		if(alert_link.length > 0) { 
			alert_link.colorbox({width:"220px", height:"150px", inline:true, href:"#bientot"});			
		}
	});
	
});

/*
function openLightBox(largeur,hauteur){
	boutique_link.colorbox({iframe:true, width:largeur, height:hauteur, transition:"none", onComplete:function(){redimLightBox(largeur, hauteur);}});

}

function nextLightBox(largeur,hauteur){
	redimLightBox(largeur,hauteur);
	//boutique_link.colorbox({iframe:true, width:largeur, height:hauteur});	
}
*/
/*
function redimLightBox(largeur,hauteur){
	largeur = parseInt(largeur)+30;
	hauteur = parseInt(hauteur)+50;
	mi_larg = Math.floor(largeur/2);
	mi_hauteur = Math.floor(hauteur/2);
	$("#colorbox").css({width:largeur,height:hauteur, top:'50%', left:'50%', 'marginTop':'-'+mi_hauteur+'px', 'marginLeft':'-'+mi_larg+'px'});
	$("#cboxIframe").css("width",(largeur-30)).css("height",(hauteur-50));
	$("#cboxLoadedContent").css("width",largeur).css("height",hauteur);
	$("#cboxContent").css("width",largeur).css("height",hauteur);
	$("#cboxWrapper").css("width",largeur).css("height",hauteur);
	
	$(window).unbind('resize.cbox_resize resize.cboxie6 scroll.cboxie6');
}
*/

var largeurCB = 0;
var hauteurCB = 0;

function nextLightBox(largeur,hauteur){
	redimLightBox(largeur,hauteur);
}
function redimLightBox(largeur,hauteur){
	
	
	largeurCB = largeur;
	hauteurCB = hauteur;
	
	setTimeout(function(){
	
	$('#colorbox').css('width',largeur+'px');
	$('#colorbox').css('height',hauteur+'px').css("position", "fixed");
	/*$('#colorbox').css('top','50%');
	$('#colorbox').css('left','50%');
	$('#colorbox').css('marginTop','-'+(hauteur/2)+'px');
	$('#colorbox').css('marginLeft','-'+(largeur/2)+'px');*/
	
	
	$('#colorbox').css('top',(($(window).height() - hauteur)/2)+'px');
	$('#colorbox').css('left',(($(window).width() - largeur)/2)+'px');
	
	$('#cboxWrapper').css('width',largeur+'px');
	$('#cboxWrapper').css('height',hauteur+'px');
	
	$('#cboxTopCenter').css('width',largeur+'px');
	$('#cboxMiddleLeft').css('height',hauteur+'px');
	
	$('#cboxContent').css('width',largeur+'px');
	$('#cboxContent').css('height',hauteur+'px');
	
	$('#cboxLoadedContent').css('width',(largeur-20)+'px');
	$('#cboxLoadedContent').css('height',(hauteur-57)+'px');
	
	$('#cboxLoadingOverlay').css('height',hauteur+'px');
	$('#cboxLoadingGraphic').css('height',hauteur+'px');
	$('#cboxMiddleRight').css('height',hauteur+'px');
	
	
	$('#cboxBottomCenter').css('width',largeur+'px');
	},100);
	
	
}

$(document).bind('cbox_closed', function(){
    
	$('#colorbox').css('top','0');
	$('#colorbox').css('left','0');
	$('#colorbox').css('marginTop','0');
	$('#colorbox').css('marginLeft','0');
});


$(window).bind('resize', function(){
	redimLightBox(largeurCB, hauteurCB);
});



function seletedTab(index){
	if(recettePDF!="" || !mark){
		if(index==0){ cacherOverlaysVisio(); }
		else if(index==1){ cacherOverlaysVisio("#autresVideos"); $("#autresVideos").slideDown(); }
		else if(index==2){ cacherOverlaysVisio("#descVideo");$("#descVideo").slideDown(); }
		else if(index=='panier'){ 
			add_cart(id_panier, '', '');
			$(this).colorbox({href:"boutique/ajout.php?ajout="+id_panier, title:"Vous venez d'ajoutez un produit &agrave; votre panier", open:true,iframe:true, speed:0}); 
			
		}
		else if(index=='recette'){ window.open(recettePDF, "", "");  }
	}
	else {
		if(index==0){ cacherOverlaysVisio(); }
		else if(index==1){ cacherOverlaysVisio("#sommaire"); $("#sommaire").slideDown(); }
		else if(index==2){ cacherOverlaysVisio("#autresVideos"); $("#autresVideos").slideDown(); }
		else if(index==3){ cacherOverlaysVisio("#descVideo");$("#descVideo").slideDown(); }
		else if(index=='panier'){ 
			add_cart(id_panier, '', '');
			$(this).colorbox({href:"boutique/ajout.php?ajout="+id_panier, title:"Vous venez d'ajoutez un produit &agrave; votre panier", open:true,iframe:true, speed:0}); 
		}
	}
}

function add_facebook_link(id) {
	var url = "http://www.facebook.com/sharer.php?u=" + urlSite + "/index.php?video="+id;	
	window.open(url, "facebook", "width=650,height=350,top=150,left=200");
}



function cacherOverlaysVisio(nePasCacher){
	fermerPanneauxVisio();
	if(nePasCacher!="#sommaire"){ $("#sommaire").slideUp(); }
	if(nePasCacher!="#autresVideos"){ $("#autresVideos").slideUp(); }
	if(nePasCacher!="#descVideo"){ $("#descVideo").slideUp();	 }
	if(nePasCacher!="#panneau_form_commentaires"){ $("#panneau_form_commentaires").slideUp(); }
	if(nePasCacher!="#panneau_form_alerte"){ $("#panneau_form_alerte").slideUp(); }
	if(nePasCacher!="#panneau_form_suggestions"){ $("#panneau_form_suggestions").slideUp(); }
	if(nePasCacher!="#panneau_form_rep"){ $("#panneau_form_rep").slideUp(); }
	if(nePasCacher!="#panneau_form_partage"){ $("#panneau_form_partage").slideUp(); }
	if(nePasCacher!="#panneau_form_ret"){ $("#panneau_form_ret").slideUp(); }
	appelFlash("laVisionneuse_"+cle_visio).pauseVideo();	
}


/*Fonction Visionneuse*/
function appelFlash(movieName) {

    if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
    }
    else {
	    return document[movieName];
    }
} 

function afficherPartage(cle){
	cle_visio = cle;
	cacherOverlaysVisio();
	appelFlash("laVisionneuse_"+cle).afficherPartage();	
}

function afficherExport(cle){
	cacherOverlaysVisio();
	appelFlash("laVisionneuse_"+cle).afficherExport();	
}

function fermerPanneauxVisio(){
	if(cle_visio!=null)
	appelFlash("laVisionneuse_"+cle_visio).fermerPanneaux();	
}


function ajouterPlaylist(cle){
	cle_visio = cle;
	cacherOverlaysVisio();
	$.post("visionneuse/ajax/playlist/addVideo.php", {idVideo:cle}, function(){ affichePlaylist(); });
	
}

function affichePlaylist(){
	
	appelFlash("laVisionneuse_"+cle_visio).afficherPlaylist();
	
}

function allerMarqueur(cle,timecode){
	cacherOverlaysVisio();
	appelFlash("laVisionneuse_"+cle).seekVideo(timecode);
}


function send_commentaire() {
	var params = $("#send_comment").serialize();
	$.post("ajax/send_comment.php", params, function(res) {
		if(res.substring(0, 2) == "1:") {
			display_message(res.substring(2));
			$("#send_comment .inp_field").val("");
			cacherOverlaysVisio();
			refresh_captcha();
		}
		else {
			display_message(res);
		}
	}, "text");
}

function subscribe_alert() {
	var frm = document.form_alerte;
	var ml = frm.elements["email"].value;
	if(ml == "" || ml.indexOf("@") == -1 || ml.indexOf(".") == -1) {
		display_message("Vous devez saisir un email valide");
		return;
	}
	
	$.post("ajax/subscribe_alert.php", $(frm).serialize(), function(res) {
		if(res.substring(0, 2) == "1:") {
			display_message(res.substring(2));
			document.form_alerte.elements["email"].value = "";
			cacherOverlaysVisio();
		}
		else {
			display_message(res);
		}
	}, "text");
	
}

function display_message(text) {
	alert($("<div>"+text+"</div>").text());	
}

function refresh_captcha() {
	document.getElementById('captcha_img').src = 'captcha/securimage_show.php?sid='+Math.random();	
}
