/*if ($.browser.safari && $.browser.version < 530) {
	$('head').append('<link rel="stylesheet" type="text/css" href="styles/overflow_safari.css" media="screen, projection" />'); 
}*/
$(function() { 

$(".ElemC").css("display","block");
$(".me_sclear").css("display","block");
$(".me_pagination li.valid").css("display","none");

	$('.innerlistUne li').each(function(){
		$(this).show();
	});
	
	$('.me_blockUneOnglets ul li').each(function(){
		$(this).show();
		$(this).removeClass('active');
	});
	
	$('.inner_listWebTv li').each(function(){
		$(this).show();
	});
	
	$('.me_navWebTv li').each(function(i){
		if (i != 0) {
			$(this).removeClass('active');
		}
	});
	
	/* -- Gestions des videos une -- */

	$("#me_blockUne .video a").click(function(){
		var container = $(this).parent();
		var videoId = container.attr('id');
		if ($('#' + container.attr('id') + '_src')) {
			var videoURL = $('#' + container.attr('id') + '_src').attr('value');
		} else {
			var videoURL = '';
		}
		if ($('#' + container.attr('id') + '_audio')) {
			var audioURL = $('#' + container.attr('id') + '_audio').attr('value');
		} else {
			var audioURL = '';
		}
		if ($('#' + container.attr('id') + '_subtitle')) {
			var subtitleURL = $('#' + container.attr('id') + '_subtitle').attr('value');
		} else {
			var subtitleURL = '';
		}
		var videoContainer =  $('<p class="videoContainer" id="'+videoId+'"></p>');
		var sMedia = '';
		if (document.getElementById('sMedia')) {
			var sMedia = document.getElementById('sMedia').value;
		}
		container.hide();
		container.parent().prepend(videoContainer);
		
		$(".play").css("display","none");
		
		return loadVideo(videoURL, subtitleURL, audioURL, videoId, sMedia, 'webtv', 465, 310);
	});
	
	$("#me_blockUne .items li, #me_blockUne .next, #me_blockUne .prev").click(function() {
		$(".play").css("display","block");
		$('.video').show();
		$('object[id^=playervideo]').remove()
	});
	
/* -- Gestions des Carrousels -- */

if ($(".me_blockZoom").length) {
	// ZOOM
	cZoom = new (function(){
		var $this = this;
		
		$('.me_blockZoom li.me_zoomClear').each(function(){
			$(this).removeClass('me_zoomClear');
		});
		
		this.api = $(".me_blockZoom .scrollable")
		.css({
			 width:"693px", 
			 //height: "150px", 
			 overflow: "hidden", 
			 float: "left" 
		 })
		.scrollable({
			size:4,
			clickable: false
		}).circular({api: true});
		$(".me_blockZoom .scrollable").children("ul.items").css({width: "20000em"});
		// Création d'une boucle
		$this.api.begin();
		this.callTimer = function() {
			if (!$(".me_blockZoom .noAuto").length) {
				$this.timer = setInterval(function(){$this.api.next()},4000);
			}
		}
		this.callTimer();
		
		$(".me_contentZoom .me_linkBack a,.me_contentZoom .me_linkNext a").click(function(){
			var $target = $('.me_contentZoom .me_navControls a');
			clearInterval($this.timer);
			$target.parent().removeClass('me_btnStop').addClass('me_btnPlay');
			$titre = "Lire le bloc Zoom sur";
			$target.attr("title", $titre);
			$target.html("Lire");
		});

		
		$('.me_contentZoom .me_navControls a').click(function(e){
			var $target = $(e.target);
			var $titre = "";
			if($target.parent().hasClass("me_btnPlay")) {
				$(".me_blockZoom .noAuto").removeClass('noAuto');
				$this.callTimer();
				$target.parent().removeClass('me_btnPlay').addClass('me_btnStop');
				$titre = "Stopper le bloc Zoom sur";
				$target.attr("title", $titre);
				$target.html("Stopper");
			}
			else {
				clearInterval($this.timer);
				$target.parent().removeClass('me_btnStop').addClass('me_btnPlay');
				$titre = "Lire le bloc Zoom sur";
				$target.attr("title", $titre);
				$target.html("Lire");
			}
			return false;
		});
		return this;
	})();
}
	// ACCES RAPIDE
	if (document.getElementById("me_blockQuickAccessNb")) {
		var nbRapidAccess = document.getElementById("me_blockQuickAccessNb").value;
	} else {
		var nbRapidAccess = 7;
	}
	
	$(".me_blockQuickAccess .scrollable")
		.css({ 
			 width:"216px",
			 height: nbRapidAccess * 6 + "em",
			 overflow: "hidden" 
			 })
		.scrollable({
					size:nbRapidAccess, 
					vertical:true 
					});



/* -- Onglets A la Une -- */
	
	// Redimensionnement Bloc A la Une
	var containerUneHeight = function () {
		var indexEl = $(".me_blockUneOnglets .scrollable .items > li").index( $('.active')[0] );
		indexEl++;
		var elId = $(".me_listUne .innerlistUne > li#me_une"+indexEl);
		var heightLi = elId.height();
		if (heightLi < $(".me_blockUneOnglets").height()) {
			$(".me_listUne .innerlistUne > li#me_une"+indexEl+" > div > div > div > div").height($(".me_blockUneOnglets").height());
		}
		$(".me_listUne").animate({height:heightLi}, 100);
		/*if ($(".me_blockUneOnglets").height() > $("#me_blockUne").height()) {
		$("#me_blockUne").animate({height:$(".me_blockUneOnglets").height()}, 100);
		}*/
	}
	
	// Gestion Onglet
	if ($(".me_listUne").length) {
		var listUne = new (function () { 
		var $this = this;
			this.api = $(".me_listUne")
			.css({
				 height: "437px", 
				 overflow: "hidden"
				 })
			.scrollable({ 
						vertical: true, 
						size: 1, 
						clickable: false,
						keyboard: true
						})
			.navigator({
					   navi:".me_blockUneOnglets ul",
					   api: true
					   });
	/*		$this.api.onSeek(function() {
				$this.api.reload();
			});*/
		})();
		
		$(".me_listUne").children("ul.innerlistUne")
		.css({
			 position:"absolute",
			 overflow: 'hidden',
			 height: 'auto',
			 width: 'auto'
			 });
	
	
		// Gestion du Defilement des actus a la Une	
		var Une = new (function () { 
			var $this = this;
			this.api = $(".me_blockUneOnglets .scrollable")
				.css({ 
				 width:"234px", 
				 height: "430px", 
				 overflow: "hidden" 
				 })
				.scrollable({
					size:4,
					vertical:true
					});
	
			})();
		
		// exception pour safari 3
		if ($.browser.safari && $.browser.version < 530) {
		} else {
			var uneCallTimer = function() {
				setInterval(function(){containerUneHeight()},500);
			}
			uneCallTimer();
		}
	}
	
	
/* -- Onglets WebTv -- */
if ($(".inner_listWebTv li").length > 1) {
	var cWebTv = new (function() {
		var $this = this;
		this.el = $(".me_listWebTv");
		this.api = $(".me_listWebTv")
		.scrollable({ 
			vertical: true, 
			size: 1, 
			keyboard: true,
			clickable: false
		})
		.circular()
		.navigator({navi:".me_navWebTv", api:true});
		$('.inner_listWebTv li').unbind('click.scrollable');
		$(".me_listWebTv").css({
			 height: "225px", 
			 overflow: "hidden"
		 }).children(".inner_listWebTv").css({
			 position: "absolute"
		 });
		// Création d'une boucle
		this.callTimer = function() {
			$this.timer = setInterval(function(){$this.api.next()},4000);
		}
		this.callTimer();
		$(".me_navWebTv li").click(function(){
				var $target = $('.me_blockWebTv .me_navControls a');
				clearInterval($this.timer);
				$target.parent().removeClass('me_btnStop').addClass('me_btnPlay');
				$titre = "Lire le bloc Web TV";
				$target.attr("title", $titre);
				$target.html("Lire");
		});
		
		
		// Fonction des boutons play/stop
		$('.me_blockWebTv .me_navControls a').click(function(e){
			var $target = $(e.target);
			var $titre = "";
			if($target.parent().hasClass("me_btnPlay")) {
				$this.callTimer();
				$target.parent().removeClass('me_btnPlay').addClass('me_btnStop');
				$titre = "Stopper le bloc Web TV";
				$target.attr("title", $titre);
				$target.html("Stopper");
			}
			else {
				clearInterval($this.timer);
				$target.parent().removeClass('me_btnStop').addClass('me_btnPlay');
				$titre = "Lire le bloc Web TV";
				$target.attr("title", $titre);
				$target.html("Lire");
			}
			return false;
		});
		
		$('.me_listWebTv a').click(function(e){
			clearInterval($this.timer);
			$('.me_blockWebTv .me_navControls a').parent().removeClass('me_btnStop').addClass('me_btnPlay');
		});
/* -- Gestions des videos WebTV -- */

		$(".me_blockWebTv .video a").click(function(e){
			var container = $(this).parent();
			var videoId = container.attr('id');
			if ($('#' + container.attr('id') + '_src')) {
				var videoURL = $('#' + container.attr('id') + '_src').attr('value');
			} else {
				var videoURL = '';
			}
			if ($('#' + container.attr('id') + '_audio')) {
				var audioURL = $('#' + container.attr('id') + '_audio').attr('value');
			} else {
				var audioURL = '';
			}
			if ($('#' + container.attr('id') + '_subtitle')) {
				var subtitleURL = $('#' + container.attr('id') + '_subtitle').attr('value');
			} else {
				var subtitleURL = '';
			}
			var sMedia = '';
			if (document.getElementById('sMediaWebTv')) {
				var sMedia = document.getElementById('sMediaWebTv').value;
			}
			var videoContainer =  $('<p class="videoContainer" id="'+videoId+'Player"></p>');
			clearInterval($this.timer);
			container.hide();
			container.parent().prepend(videoContainer);
			
			$(".play").css("display","none");
			return loadVideo(videoURL, subtitleURL, audioURL, videoId+'Player', sMedia, 'webtv', 280, 222);
		});

		$(".me_navWebTv li").click(function() {
			$(".me_blockWebTv .miniPlay").css("display","block");
			$(".me_blockWebTv .video").show();
			$(".me_blockWebTv object[id^=playerwebTv]").remove()
		});

		return this;
	})();
}

/* -- Perte du Focus sur les liens des listes -- */

	$(".me_linkNext, .me_linkBack").show();
	$(".me_linkNext a span, .me_linkBack a span").css("display", "none");
	$(".me_linkNext a.next, .me_linkBack a.prev").click(function(){
		$(this).blur();
		return false;
	});
	


/* -- Ajout des boutons Imprimer et de grossissement/reduction du texte -- */

	var linkUpDownTypo = '<li class="me_pictoIncreaseFont"><a href="#" title="Agrandir la taille du texte">Agrandir</a></li>'
						+'<li class="me_pictoDecreaseFont"><a href="#" title="R&eacute;duire la taille du texte">Réduire</a></li>';
	
	var linkBtnPrint = '<li class="me_linkPrint"><a href="#" title="Imprimer la page en cours (ouverture d\'une bo&icirc;te de dialogue)">Imprimer</a></li>';
	
	$("#me_navTools .me_linkRss")
		.before(linkUpDownTypo)
		.after(linkBtnPrint);

	$(".me_linkPrint, .me_pictoIncreaseFont, .me_pictoDecreaseFont").show();

/* -- Bouton d'impression -- */

	$(".me_linkPrint a").click(function () {
		window.print();
		return false;
	});


/* -- Agrandissement/Reduction du texte de la page -- */
	var containerHeight = function () {
		var heightLi;
			heightLi = $(".me_listUne .innerlistUne > li:visible").height();
		$(".me_listUne .innerlistUne > li").not(':visible').css("margin", "0");
		$(".me_listUne").height(heightLi);
	}
	
	var txtSize = 11;
	var increase=0;
	$(".me_contentHeader").css("fontSize", "11px");
	$(".me_pictoIncreaseFont a").click(function () {
		if (increase < 3) {
			txtSize = txtSize+2;
			$("body").css("fontSize", txtSize+"px");
			containerHeight();
			containerUneHeight();
			increase++;
			$(".me_blockUneOnglets .scrollable").height($(".me_blockUneOnglets .scrollable").height()+100);
			/*if($(".me_listUne .innerlistUne").css("top") == "0px") {
				$("*").index($('.me_blockUneOnglets').find('li.active'));
				$(".me_listUne .innerlistUne").css("top", $(".me_listUne .innerlistUne").css("top"));
			}*/
		}
		return false;
	});
	$(".me_pictoDecreaseFont a").click(function () {
		if (increase > 0) {
			txtSize = txtSize-2;
			$("body").css("fontSize", txtSize+"px");
			containerHeight();
			containerUneHeight();
			increase--;
			$(".me_blockUneOnglets .scrollable").height($(".me_blockUneOnglets .scrollable").height()-100);
		}
		return false;
	});



/* -- Resize du bloc A la une lors de l'Agrandissement/Reduction du texte de la page -- */

	// setup function to listen for custom events
	ResizeEvents.eventElement.bind('x-initial-sizes x-text-resize x-window-resize x-window-width-resize x-window-height-resize', 
								   function (eventObj, emPixels, textHeight, windowWidth, windowHeight){
									   // append results of resize event
									   containerHeight();
									   
									});
	// Call init ASAP
	ResizeEvents.initialise();

	$('#form_access .me_inputCheckbox').click(function(){
		var picto = $(this).siblings('img')
		
		if (document.getElementById(picto.attr('id') + '_on')) {
			var picto_on = document.getElementById(picto.attr('id') + '_on').value;
		} else {
			picto_on = picto.attr('src');
		}
		if (document.getElementById(picto.attr('id') + '_off')) {
			var picto_off = document.getElementById(picto.attr('id') + '_off').value;
		} else {
			picto_off = picto.attr('src');
		}
		
		if (document.getElementById('me_' + picto.attr('id'))) {
			var bChecked = document.getElementById('me_' + picto.attr('id')).checked;
		} else {
			var bChecked = 0;
		}
		if (bChecked) {
			picto.attr('src',picto_on);
		} else {
			picto.attr('src',picto_off);
		}
	});

});
