/** * Ce fichier contient toutes les fonctions JavaScript utiles au site
 */

/**
 * Fonction pour charger une vidéo
 */
function loadVideo(video_path, subtitleUrl, audioUrl, div, sMedia, sPlugName, width, height) {
	var flashvars = {
		file:video_path,
		autostart:"true",
		playlistsize:"0",
		playlist:"bottom",
		shuffle:'false',
		enablejs:'true',
		autoscroll:'false',
		plugins:'captions-1,audiodescription-1',
		shuffle:"false",
		javascriptid:'videoItem',
		captions:subtitleUrl,
		audio:audioUrl
    }
        
    var params = {
        allowfullscreen:"true",
        allowscriptaccess:"always",
        seamlesstabbing:'true'
    }
    
    var attributes = {
        id:'player'+div,
        name:'player'+div
    }
   
    if (swfobject.hasFlashPlayerVersion("9.0.115")) {
    	width = 470;
    	height = 310;
    	urlswfplayer = 'http://media.enseignementsup-recherche.gouv.fr/plugin/webtv/jw_flv_player/phpsflvplayer.swf';
    	swfobject.embedSWF(urlswfplayer, div, width, height, "9.0.115", false, flashvars, params, attributes);
    	return false;
    } else {
    	return true;
    }
}

/**
 * Déclaration des fonctions JS appelées dans $(function(){})
 */
function hideFooterPage() {
	$(".footer_page_level_3").css("display", "none");
	$(".open_link .page_arrow_img").each(function (e){
		$(this).attr("src", $(this).attr("src").replace("fleche_verte_open", "fleche_verte"));
	});
	$(".open_link").addClass("close_link");
	$(".open_link").removeClass("open_link");
}



// Verifie dans le tableau de cookie si pas deja dans le programme
// type = 1 (manifestation), 2 (village),3 (operation envergure)
// action_id peut valoir 0
// project_id est toujours renseigne
function checkIsAlreadyAuProgramme(type, project_id, action_id) {
	var trouve = false;
	eval('programme = ' + getCookie('programme') +';')
	if(programme == null){
		return false;
	}	
	var cookieval = getCookie('programme');
	var tab_cookie = jQuery.parseJSON(cookieval);

	var i=0;
	if (typeof(tab_cookie) != 'undefined') {
		var ltab = tab_cookie.action_id.length;
		for (i = 0;i < ltab; i++) {
			if (tab_cookie.project_id[i] == project_id) {
				if (tab_cookie.type_id[i] == type) {
						var bool = tab_cookie.action_id[i] || 0;
						if(! bool && action_id == 0) {
	   						trouve = true;
	   						break;
						} else if (tab_cookie.action_id[i] == action_id) {
							trouve = true;
							break;
						}	
				}
			}
		}
	}
	return trouve;
}
/**
 * Cette fonction est lancée à la fin du chargement de la page
 * $(function() {
 */
$(document).ready(function() {
	/**
	 * Déclaration des variables de toutes les variables
	 */
	var txtSize = 11;
	var increase = 0;
	
	/**
	 * JS associé aux outils en haut de page
	 */
	$("#fontsizeUp").click(function () {
		if (increase < 3) {
			txtSize = txtSize+2;
			$("body").css("fontSize", txtSize+"px");
			increase++;
		}
		return false;
	});
	$("#fontsizeDown").click(function () {
		if (increase > 0) {
			txtSize = txtSize-2;
			$("body").css("fontSize", txtSize+"px");
			increase--;
		}
		return false;
	});
	$("#print").click(function () {
		window.print();
		return false;
	});
	
	/**
	 * On masque les rubriques de niveau 3 dans le footer
	 */
	hideFooterPage();
	$(".show_pages").click(function () {
		$(this).siblings(".footer_page_level_3").slideToggle("slow");
		if ($(this).hasClass("page_arrow")) {
			var img = $(this).children(".page_arrow_img");
		} else {
			var img = $(this).siblings(".page_arrow").children(".page_arrow_img");
		}
		if (img && img.attr("src") && $(this).parent().hasClass("open_link")) {
			img.attr("src", img.attr("src").replace("fleche_verte_open", "fleche_verte"))
		} else if (img && img.attr("src")) {
			img.attr("src", img.attr("src").replace("fleche_verte", "fleche_verte_open"))
		}
		$(this).parent().toggleClass("open_link close_link");
		return false;
    });
    
    /**
     * Gestion du scroll du bloc Other Sites
     */
	if ($(".other_sites_list li").length > 1) {	
	    var sites = new (function () { 
			var $this = this;
			this.api = $(".other_sites_scroll")
				.scrollable({
					size:1,
					clickable: false,
					vertical:true,
					circular:true
				})
		})();
	}
	
	/**
	 * Gestion du scroll du bloc A la une
	 */
	if ($(".a_la_une_onglets_list li").length > 1) {
		var listUne = new (function () { 
					var $this = this;
					this.api = $(".a_la_une_content")
							.scrollable({
								vertical: true, 
								size: 1, 
								clickable: false,
								keyboard: false
							})
							.navigator({
								navi:".a_la_une_onglets_list",
								api: true
							})
				})();
			
				var Une = new (function () { 
					var $this = this;
					this.api = $(".a_la_une_onglets")
							.scrollable({
								size:3,
								clickable: false,
								vertical:true
							});
				})();
		
		$(".a_la_une_content .hide").each(function() {
			$(this).removeClass("hide");
		});
		$(".a_la_une_onglets .hide").each(function() {
			$(this).removeClass("hide");
		});
	}
		
	/**
	 * Gestion du scroll du bloc WebTV
	 */
	if ($(".webtv_bloc_list li").length > 1) {
		var webTV = new (function () { 
			var $this = this;
			
			this.api = $(".webtv_bloc_player_nav")
				.scrollable({ 
					vertical: true, 
					size: 1, 
					keyboard: true,
					clickable: false,
					circular: true
				})
				.navigator({navi:".webtv_bloc_list", api:true});
				
			/**
			 * Création de la boucle automatique
			 */
			//$this.api.begin();
			/*
			this.callTimer = function() {
				if (!$(".webtv_bloc .noAuto").length) {
					$this.timer = setInterval(function(){$this.api.next()},4000);
				}
			}
			$this.callTimer();
			*/
			
			/**
			 * Arrêt du défilement au clic sur un item de la nav
			 */
			$(".webtv_bloc_list li").click(function(){
				$(".webtv_bloc_player_item a").css("display", "block");
				$('object[id^=playerwebtv]').remove();
				
				$(this).children("a").blur();
				var $target = $(".webtv_bloc .webtv_bloc_btn a");
				clearInterval($this.timer);
				$target.parent().removeClass("stop").addClass("play");
				$titre = "Lire le bloc Web TV";
				$target.attr("title", $titre);
				$target.html("Lire");
			});
			
			/**
			 * Arrêt du défilement au clic sur l"item central
			 */
			$(".webtv_bloc_player_nav a").click(function(e){				
				clearInterval($this.timer);
				$(".webtv_bloc .webtv_bloc_btn a").parent().removeClass("stop").addClass("play");
			});
			
			/**
			 * Gestion du bouton d"action Lecture/Pause
			 */
			$(".webtv_bloc .webtv_bloc_btn a").click(function(e){
				$(".webtv_bloc_player_item a").css("display", "block");
				$('object[id^=playerwebtv]').remove();
				
				$(this).blur();
				var $target = $(e.target);
				var $titre = "";
				if($target.parent().hasClass("play")) {
					$(".webtv_bloc .noAuto").removeClass("noAuto");
					$this.callTimer();
					$target.parent().removeClass("play").addClass("stop");
					$titre = "Stopper le bloc WebTV";
					$target.attr("title", $titre);
					$target.html("Stopper");
				} else {
					clearInterval($this.timer);
					$target.parent().removeClass("stop").addClass("play");
					$titre = "Lire le bloc WebTV";
					$target.attr("title", $titre);
					$target.html("Lire");
				}
				return false;
			});
			
			/**
			 * Affichage du bouton play/pause
			 */
			$(".webtv_bloc .hide").each(function() {
				$(this).removeClass("hide");
			});
			$(".webtv_bloc_nav li").each(function(i){
				if (i != 0) {
					$(this).removeClass("active");
				}
			});
			
			return this;
		})();
		
		/**
		 * Lancement d'une vidéo
		 */
		$(".play_video").click(function(e){
			var videoSrc = "";
			var subtitleUrl = "";
			var audioUrl = "";
			var destDivId = $(this).parent().attr("id") + "_container";
			var videoContainer =  $('<p class="videoContainer" id="'+destDivId+'"></p>');
						
			if ($(this).children(".video_src")) {
				videoSrc = $(this).children(".video_src").attr("value");
			}
			if ($(this).children(".video_subtitle")) {
				subtitleUrl = $(this).children(".video_subtitle").attr("value");
			}
			if ($(this).children(".video_audio")) {
				audioUrl = $(this).children(".video_audio").attr("value");
			}
			$(this).parent().prepend(videoContainer)
			$(this).css("display", "none");
			
			return loadVideo(videoSrc, subtitleUrl, audioUrl, destDivId, "http://education.dev.media", "webtv", 480, 310)
		});
	}
	
	/**
	 * Gestion du scroll du bloc Zoom Sur
	 */
	if ($(".zoom_sur_bloc_list li").length > 1) {		
	    var zoomSur = new (function () {
			var $this = this;
			this.api = $(".zoom_sur_bloc_scroll")
				.scrollable({
					size:4,
					clickable: false,
					vertical:false,
					circular:true
				})
			$(".zoom_sur_bloc_scroll.scrollable").children("ul.items").css({width: "20000em"});
			$(".zoom_sur_bloc_scroll li.clear").each(function(){
				$(this).removeClass("clear");
			});
				
	
			/**
			 * Gestion du bouton d"action Lecture/Pause
			 */
			$(".zoom_sur_bloc .zoom_sur_bloc_btn a").click(function(e){
				$(this).blur();
				var $target = $(e.target);
				var $titre = "";
				if($target.parent().hasClass("play")) {
					$(".zoom_sur_bloc .noAuto").removeClass("noAuto");
					$this.callTimer();
					$target.parent().removeClass("play").addClass("stop");
					$titre = "Stopper le bloc Zoom sur";
					$target.attr("title", $titre);
					$target.html("Stopper");
				}
				else {
					clearInterval($this.timer);
					$target.parent().removeClass("stop").addClass("play");
					$titre = "Lire le bloc Zoom sur";
					$target.attr("title", $titre);
					$target.html("Lire");
				}
				return false;
			});
			
			/**
			 * Affichage du bouton play/pause
			 */
			$(".zoom_sur_bloc .hide").each(function() {
				$(this).removeClass("hide");
			});
			
			return this;
		})();
	}
	
	$(".link_next a.next, .link_prev a.prev").click(function(){
		$(this).blur();
		return false;
	});
	
		$('li.firstLevel ul').each(function(){
			$(this).css('display', 'none');
		});
		$('li.firstLevel').mouseover(function(){
			$(this).addClass('over');
			$('ul', this).css('display', 'block');
		});
		$('li.firstLevel').mouseout(function(){
			$(this).removeClass('over');
			$('ul', this).css('display', 'none');
		});


	// recuperer un swf dans le dom	
	function thisMovie(movieName) {
		if(navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	};	
		
	$('.hidevideoune').click(function() {
		if ($('#webtv_video_tmp').length)
			$('#webtv_video_tmp').remove();
		
		if (typeof(WebTVdansPlayer) != 'undefined') {
			var hrefval = $(this).attr('href');
			//webtv=1#webtv_bloc_nav
			var numBloc = hrefval.substr(7,1);
			
			
			if ($('#webtvContainer_tmpplayer').length)	
				thisMovie('webtvContainer_tmpplayer').sendEvent('stop');
			
			// on boucle pour activer/desactiver les videos		
			for (i=0;i<=3;i++) {
				if ($('#webtvContainer_' + i + 'player').length) {	
					if (numBloc == i) {
						thisMovie('webtvContainer_' + i + 'player').sendEvent('playpause');
					} else {
						thisMovie('webtvContainer_' + i + 'player').sendEvent('stop');
					}
				}
			}
		}
	});
	$("a.print").click(function () {
		window.print();
		return false;
	});

		
});

