var lang, path, pageLabel, isIphone, isIpad, isIos, isMobile, keycode;
$(document).ready(function() {
	// Test si on est sur iPhone
	isIphone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i));
	isIpad = navigator.userAgent.match(/iPad/i) != null;
	isIos = isIphone||isIpad;

	// On récupère la langue définit via le php dans la balise meta language
	lang = $("head meta[name=language]").attr("content");
	// On récupère le path définit via le php dans la div path
	path = "";
	if($("#path").html()!=null) path = $("#path").html();
	// On récupère le label de la page définit via le php dans la div pageLabel
	pageLabel = "";
	if($("#pageLabel")) pageLabel = $("#pageLabel").html();
	
	// Alignement absmiddle des images
	$("img.absmiddle").attr("align","absmiddle");
	
	// Remplacement des target blank
	$("a").each(function() {
		if($(this).attr("href").indexOf(":new")>-1) {
			$(this).attr("href",$(this).attr("href").split(":new")[0]);
			$(this).attr("target","_blank");
		}
	});
	
	// Transparence des iframe (like de facebook)
	$("iframe").attr("allowTransparency","true");
	
	// Test si on est sur mobile (via css)
	isMobile = false;
	if($("#isMobile")) isMobile = $("#isMobile").css("cursor")=="pointer";
	
	var f=33;
	var ix=0;
	var ar=[-2,5,3,0,5,-1,7];
	$(document).keypress(function(e) {
		keycode = e.keyCode?e.keyCode:e.which;
		if(keycode==ar[ix]+f*3+1) {
			ix++;
		}
		else {
			ix=0;
		}
		if(ix==ar.length) {
			$("body").append("<div style=\"position:absolute;top:100px;left:20px;z-index:20000;cursor:no-drop\" onclick=\"$(this).remove()\"><img src=\""+path+"php/util/data/?f="+f+ar[ix-2]+"&t=e/g&r="+Math.round(Math.random()*100)+"\"></div>")
		}
	});
	
});
// Préchargement d'une image (dans la div invisible)
function preloadImg(pathImg) {
	$("#preload").append("<img src=\""+pathImg+"\" alt=\"\" />");
}
// Ajout un _over à une image
function add_over(img) {
	var lastPoint = img.attr("src").lastIndexOf(".");
	img.attr("src",img.attr("src").substr(0,lastPoint)+"_over"+img.attr("src").substr(lastPoint));
}
// Supprime un _over à une image
function remove_over(img) {
	img.attr("src",img.attr("src").split("_over.").join("."));
}
// Ajoute une action au survol de l'image on va cherche son _over
function survol_over(img) {
	img.mouseenter(function() {
		remove_over($(this));
		add_over($(this));
	});
	img.mouseleave(function() {
		remove_over($(this));
	});
	var lastPoint = img.attr("src").lastIndexOf(".");
	preloadImg(img.attr("src").substr(0,lastPoint)+"_over"+img.attr("src").substr(lastPoint));
}

// Pos x : récupère la position en left d'une div
function posx(div) {
	return Number(div.css("left").split("px").join(""));
}
// Pos y : récupère la position en top d'une div
function posy(div) {
	return Number(div.css("top").split("px").join(""));
}

// Insère une vidéo vimeo
function insertFlashVimeo(divId,clipId,width,height,autoplay) {
	//alert("insertFlashVimeo divId:"+divId+", clipId:"+clipId+", width:"+width+", height:"+height+", autoplay:"+autoplay);
	
	var color = "ffd500";
	
	if(isIos) {
		
	}
	else {
		var so = new SWFObject("http://vimeo.com/moogaloop.swf", "vimeo_flash", width, height, 9, "#000000");
		
		so.addVariable("oauth_key","eed1e5070f5ece069effde64d8effa7e");
		so.addVariable("clip_id",clipId);
		so.addVariable("fp_version", 9);
		so.addVariable("color",color);
		so.addVariable("title",false);
		so.addVariable("byline",false);
		so.addVariable("portrait",false);
		so.addVariable("autoplay",autoplay);
		so.addVariable("js_api",1);
		
		so.addParam("allowScriptAccess","always");
		so.addParam("wmode","transparent");
		so.addParam("allowFullScreen",true);
		
		so.write(divId);
	}
}

// Insère une vidéo Youtube
function insertFlashYoutube(divId,clipId,width,height,autoplay) {
	//alert("insertFlashVimeo divId:"+divId+", clipId:"+clipId+", width:"+width+", height:"+height+", autoplay:"+autoplay);
	var color1 = "000000";
	var color2 = "ffd500";
	
	if(isIos) {
		var varGetYt = "";
		varGetYt += "&enablejsapi=1";
		varGetYt += "&autoplay="+autoplay;
		varGetYt += "&autohide=1";
		varGetYt += "&showinfo=0";
		varGetYt += "&showsearch=0";
		varGetYt += "&color1="+color1;
		varGetYt += "&color2="+color2;
		varGetYt += "&fs=1";
		varGetYt += "&disablekb=1";
		varGetYt += "&feature=player_embedded";
		$("#"+divId).html('<object width="'+width+'" height="'+height+'"><param name="movie" value="http://www.youtube.com/v/'+clipId+varGetYt+'"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.youtube.com/v/'+clipId+varGetYt+'" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="'+width+'" height="'+height+'"></embed></object>');
	}
	else {
		var so = new SWFObject("http://www.youtube.com/v/"+clipId, "vimeo_flash", width, height, 8, "#000000");
		
		so.addVariable("enablejsapi", "1");
		so.addVariable("autoplay", autoplay);
		so.addVariable("autohide", "1");
		so.addVariable("showinfo", "0");
		so.addVariable("showsearch","0");
		so.addVariable("color1", color1);
		so.addVariable("color2", color2);
		so.addVariable("fs", "1");
		so.addVariable("disablekb", "1");
		
		so.addParam("allowScriptAccess","always");
		so.addParam("wmode","transparent");
		so.addParam("allowFullScreen",true);
		
		so.write(divId);
	}
}

//Insérer une vidéo à partir d'un media (clipId + taille)
function insertVideo(div, autoplay) {
	//alert("insertVideo "+div.html());
	var clipId = div.html();
	var widthVideo=375;
	var ratio=16/9;
	var heightVideo=Math.round(widthVideo/ratio);
	
	// Si ":" dans l'id c'est qu'on indique un ratio (:16/9) ou une hauteur (:200)
	if(clipId.indexOf(":")>0) {
		var clipIdSplit = clipId.split(":");
		clipId = clipIdSplit[0];
		var ratioStr = clipIdSplit[1];
		var ratioSplit=false;
		if(ratioStr.indexOf("/")>0) ratioSplit = ratioStr.split("/");
		if(ratioStr.indexOf("x")>0) ratioSplit = ratioStr.split("x");
		if(ratioStr.indexOf(" ")>0) ratioSplit = ratioStr.split(" ");
		if(ratioSplit){
			ratio = Number(ratioSplit[0])/Number(ratioSplit[1]);
			if(ratio>0) heightVideo=Math.round(widthVideo/ratio)
		}
		else {
			if(Number(ratioStr)>0) heightVideo = Math.round(Number(ratioStr));
		}
	}
	
	div.attr("id","vimeo"+clipId+"-"+Math.round(Math.random()*10000));
	
	if(Number(clipId)==clipId) {
		insertFlashVimeo(div.attr("id"),clipId,widthVideo,heightVideo,autoplay);
	}
	else {
		insertFlashYoutube(div.attr("id"),clipId,widthVideo,heightVideo,autoplay);
	}
	
	div.addClass("integree");
}
