$(document).ready(function(){
	 $('a[href=#top_site]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });
	 
	ListEmi.init(); // Chargment jquery pour le bloc de listing des emissions d'une thematique
	Last10.init(); // Chargement  jquery pour le bloc des 10 dernieres videos affiché en page d'accueil
	 
	$(".title_flv").each(function(){
		if($.flash.hasVersion(10)) {
			$(this).flash({
				swf: '/flash/titreDynamique-'+$(this).attr("size")+'.swf',
				flashvars: {
					titre: $(this).text(),
					couleur: $(this).attr("flv_color")
				},
				width:$(this).width(),
				height:$(this).height(),
				wmode:'transparent'
			});
		} else {
			$(this).addClass("no_flash").css({
				backgroundColor:$(this).attr("flv_color").replace(/0x/, "#"),
				width:'auto',
				height:'auto'
			});
			drawarrow(this);
		}
	});
});

function drawarrow(elem) {
	var canvas = document.createElement("canvas");
	$(canvas).css({position:'absolute', top:29, right:"30%",width:25,height:25});
	elem.appendChild(canvas);
	try {
		var context = canvas.getContext("2d");
		context.fillStyle   = elem.style.backgroundColor;
		context.beginPath();
		context.moveTo(0, 0);
		context.lineTo(200, 0);
		context.lineTo(200, 80);
		context.lineTo(0, 0);
		context.fill();
	}catch(e){}
}


var ListEmi = {
	thema_selected:'all',
	init:function(){
		if(document.getElementById("pick_thema")) {
			this.set_active_thema();
			ListEmi.loadContent(0);
			$("#pick_thema div").click(function(){
				ListEmi.thema_selected = this.getAttribute("pick");
				ListEmi.set_active_thema();
				ListEmi.loadContent(0);
			});
		}
	},
	set_active_thema:function(){
		$("#pick_thema div").removeClass("actif");
		$('#pick_thema div[pick="'+this.thema_selected+'"]').addClass("actif");
	},
	loadContent:function(page){
		$("#liste_emissions").load("/ajax/get_emissions.php", {thema:this.thema_selected, page:page});
	},
	showDetails:function(id) {
		$.post("/ajax/get_emissions.php", {detail_emi:id}, function(res){
			$.colorbox({
				html:$("content", res).html(),
				inline:false,
				width:450,
				title:$("title", res).html(),
				open:true
			});
		}, "html");
	}
};

var Last10 = {
	pos:0,
	moving:false,
	offset:648,
	duration:1000,
	max_pos:2,
	init:function() {
		if(document.getElementById("last10_vids")) {
			$("#last10_vids .arrow_left").click(function(){Last10.move(-1)});
			$("#last10_vids .arrow_right").click(function(){Last10.move(1)});
		}
	},
	move:function(direc) {
		if(this.moving || (this.pos + direc < 0) || (this.pos + direc > this.max_pos)) return;
		this.moving = true;
		this.pos += direc;
		$("#last10_scroller").animate({marginLeft:-(this.pos * this.offset)}, this.duration, function(){Last10.moving = false;});
	}
};

// Fonction permettant la pagination dans une page d'emission
function loadPageVideos(num) {
	$("#videos_from_emission").load(document.location.href+" #videos_loading_content", {page:num});
}


function zoomTexte(elem, size){
	$("#accessibilite a").removeClass("actif");
	$(elem).addClass("actif");
	$(document.body).css("fontSize", size + 'px');
}


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

    if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
    }
    else {
	    return document[movieName];
    }
} 
/*
function afficherPartage(cle){
	appelFlash("laVisionneuse_"+cle).afficherPartage();	
}*/

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

var base_url = "";
function afficherPartage(cle){
	var vid = $("#my_video_"+cle+"");
	if(vid.length > 0) {
		
		var popup = document.getElementById("my_popup");
		if(!popup) {
			popup = "<div id='my_popup' ></div>";
		}
		
		$(popup).load(base_url+"/visionneuse/ajax/partagerVideo.php?key="+cle).appendTo(vid);
		$("#my_popup").css({width:vid.width(), height:0, overflow:"hidden", position:'absolute', bottom:0, left:0, background:'url('+base_url+'/visionneuse/ajax/bg_overlay.png)'})
						.animate({height:140}, 600, "swing");
	}
}

function submit_form_partage() {
	var key = $("#current_video").val();
	var name = $("#your_name").val();
	var email = $("#friend_email").val();
	
	$.post(base_url+"/visionneuse/ajax/partagerVideo.php", {idVideo:key, mail:email, sender:name}, function(ret){
		var status = ret.substring(0, 1);
		var msg = ret.substring(1);
		
		var dialog_otpions = {title:"Partage de vid&eacute;o", modal:true};
		if(status == "1") {
			dialog_otpions.close = function(){close_form_partage();};
		}
		$("<div>"+msg+"</div>").dialog(dialog_otpions);
	}, "text");
}

function close_form_partage() {
	$("#my_popup").animate({height:0}, 300, "swing", function(){$(this).remove();});
}


// jQuery SWFObject v1.1.1 MIT/GPL @jon_neal
// http://jquery.thewikies.com/swfobject
(function(f,h,i){function k(a,c){var b=(a[0]||0)-(c[0]||0);return b>0||!b&&a.length>0&&k(a.slice(1),c.slice(1))}function l(a){if(typeof a!=g)return a;var c=[],b="";for(var d in a){b=typeof a[d]==g?l(a[d]):[d,m?encodeURI(a[d]):a[d]].join("=");c.push(b)}return c.join("&")}function n(a){var c=[];for(var b in a)a[b]&&c.push([b,'="',a[b],'"'].join(""));return c.join(" ")}function o(a){var c=[];for(var b in a)c.push(['<param name="',b,'" value="',l(a[b]),'" />'].join(""));return c.join("")}var g="object",m=true;try{var j=i.description||function(){return(new i("ShockwaveFlash.ShockwaveFlash")).GetVariable("$version")}()}catch(p){j="Unavailable"}var e=j.match(/\d+/g)||[0];f[h]={available:e[0]>0,activeX:i&&!i.name,version:{original:j,array:e,string:e.join("."),major:parseInt(e[0],10)||0,minor:parseInt(e[1],10)||0,release:parseInt(e[2],10)||0},hasVersion:function(a){a=/string|number/.test(typeof a)?a.toString().split("."):/object/.test(typeof a)?[a.major,a.minor]:a||[0,0];return k(e,a)},encodeParams:true,expressInstall:"expressInstall.swf",expressInstallIsActive:false,create:function(a){if(!a.swf||this.expressInstallIsActive||!this.available&&!a.hasVersionFail)return false;if(!this.hasVersion(a.hasVersion||1)){this.expressInstallIsActive=true;if(typeof a.hasVersionFail=="function")if(!a.hasVersionFail.apply(a))return false;a={swf:a.expressInstall||this.expressInstall,height:137,width:214,flashvars:{MMredirectURL:location.href,MMplayerType:this.activeX?"ActiveX":"PlugIn",MMdoctitle:document.title.slice(0,47)+" - Flash Player Installation"}}}attrs={data:a.swf,type:"application/x-shockwave-flash",id:a.id||"flash_"+Math.floor(Math.random()*999999999),width:a.width||320,height:a.height||180,style:a.style||""};m=typeof a.useEncode!=="undefined"?a.useEncode:this.encodeParams;a.movie=a.swf;a.wmode=a.wmode||"opaque";delete a.fallback;delete a.hasVersion;delete a.hasVersionFail;delete a.height;delete a.id;delete a.swf;delete a.useEncode;delete a.width;var c=document.createElement("div");c.innerHTML=["<object ",n(attrs),">",o(a),"</object>"].join("");return c.firstChild}};f.fn[h]=function(a){var c=this.find(g).andSelf().filter(g);/string|object/.test(typeof a)&&this.each(function(){var b=f(this),d;a=typeof a==g?a:{swf:a};a.fallback=this;if(d=f[h].create(a)){b.children().remove();b.html(d)}});typeof a=="function"&&c.each(function(){var b=this;b.jsInteractionTimeoutMs=b.jsInteractionTimeoutMs||0;if(b.jsInteractionTimeoutMs<660)b.clientWidth||b.clientHeight?a.call(b):setTimeout(function(){f(b)[h](a)},b.jsInteractionTimeoutMs+66)});return c}})(jQuery,"flash",navigator.plugins["Shockwave Flash"]||window.ActiveXObject);
