/*
*
* Gestion de l'interactivité avec jQuery
*
*/



function afficheInfos(texte){
	//console.log(texte);
}


		
		
var _ulrEnCours = "";	
var _indexEncours;


function chargeVideoYoutube(url, index) {
	
	//var url = "http://www.youtube.com/watch?v=hUYQzHnnLTc";
	//var numero = '8';
	
	var playerURL = _themePath + "/player.swf";
	
	if (swfobject.hasFlashPlayerVersion("6")) {
		// check if SWF hasn't been removed, if this is the case, create a new alternative content container
		var c = document.getElementById("container" + index);
		var att = { data:playerURL, width:"240", height:"160" };
		var par = { menu:"false", allowfullscreen:"true", allowscriptaccess:"always", wmode:"opaque", flashvars:"file="+ url +"&autostart=true&controlbar=bottom" };
		//var par = { menu:"false"};
		var id = "container" + index;
		swfobject.createSWF(att, par, id);
		_ulrEnCours = url;
	}
}


function supprimeVideoYoutube(url, index) {
	
	
	// Si il y a un objet Flash (la vidéo), on le supprime
	if( $('#container' + index).length > 0){
	
		//alert('SUPPRIME VIDEO ' + index);
		
		var element = document.getElementById("container" + index);
		jwplayer(element).stop();
		
		swfobject.removeSWF("container" + index);
		$('#container' + index ).remove();
		
		//var url = "http://www.youtube.com/watch?v=hUYQzHnnLTc";
		//var numero = '8';
		
		var d = document.createElement("div");
		d.setAttribute("id", "container" + index);
		d.setAttribute("class", "container-video");
		document.getElementById("content-container" + index).appendChild(d);
		// ON rajoute le lien
		var lien = document.createElement("a");
		lien.setAttribute("href", url);
		lien.setAttribute("rel", index);
		document.getElementById("container" + index).appendChild(lien);
	
	}
}


function stoppeToutesLesVideos(){
	
}




$(document).ready(
	function(){
		//alert(_themePath);
		
		/*
        * ----------------------------------------------------------------------
        * Initialisation
        * ----------------------------------------------------------------------
        */
        
        var _fenetreLargeur; // variable pour largeur fenêtre
        var _fenetreHauteur; // variable pour hauteur fenêtre
        
		
		
		/*
        * ----------------------------------------------------------------------
        * Newsletter
        * ----------------------------------------------------------------------
        */
		
		$('#edit-mail').focus(
			function(){
				if( $(this).attr('value') == 'Inscrire votre email'){
						$(this).attr('value','');	
				}
			}
		);

	   
	   /*
        * ----------------------------------------------------------------------
        * Menu accordéon
        * ----------------------------------------------------------------------
        */
	   
	   
	   var _indexActif = 0;
	   $('#colonne-gauche #navigation .rubrique').hide();
	   $('#colonne-gauche #navigation h2').css('height','20px');
	   // --------
	   $('#colonne-gauche #navigation .rubrique').eq(0).show();
	   $('#colonne-gauche #navigation h2').eq(0).css('height','30px');
       $('#colonne-gauche #navigation h2').click( 
			function(){
				
				var newIndex = $('#colonne-gauche #navigation h2').index(this);
				if( newIndex != _indexActif){
					$('#colonne-gauche #navigation .rubrique').slideUp();
					$('#colonne-gauche #navigation h2').css('height','20px');
					// -----------
					$(this).next('.rubrique').slideDown();
					$(this).css('height','30px');
					_indexActif = newIndex;
				}
				return false;
			}
		);
        
        /*
        * ----------------------------------------------------------------------
        * Dimensions de la fenêtre
        * ----------------------------------------------------------------------
        */
        
        // Met à jour les dimensions de la fenêtre
        function actualiseDimensions(){
			//alert('actualise');
			
			if( $('#content-inner').length > 0){
				var contentHeight = $('#content-inner').height();
				//console.log("contentHeight " + contentHeight);
				if( contentHeight < 640 ){
					$('#content').height("720px");
				}else{
					$('#content').height(contentHeight + 100 + "px");
				}
			}
			
			
			if( $('#content .view-id-calendar_custom').length > 0){
				var contentHeight = $('#content-inner').height();
				$('#content').height(contentHeight + 100 + "px");
			}
			
			
			
			
			
			/*
			if( $('.page-calendar').length == 1){
				
				var contentHeight = $('.view-calendar-custom').height();
				if( contentHeight < 640 ){
					$('#content').height("640px");
				}else{
					$('#content').height(contentHeight + 200 + "px");
				}
			}
			*/
  
			
        } 



		 /*
		* ----------------------------------------------------------------------
		* CHARGEMENT DES ZONES GAUCHES
		* ----------------------------------------------------------------------
		*/

		/*$('#vue-cesoir').load('http://cluster011.ovh.net/~bobino/pointvirgule/rebus_ajax/cesoir', function() {
			Cufon.replace('.view-cesoir p.date');
			Cufon.replace('.view-cesoir p.title a');
		});*/
		
		
		
		
		$('#vue-demain').load('/rebus_ajax/demain', function() {
		//$('#vue-demain').load('http://cluster011.ovh.net/~bobino/pointvirgule/rebus_ajax/demain', function() {

			Cufon.replace('.view-demain .views-field-field-spectacle-all-value span.date-display-single');
			Cufon.replace('.view-demain .views-field-title span a');
		});
		
		//$('#vue-exceptionnel').load('http://cluster011.ovh.net/~bobino/pointvirgule/rebus_ajax/exceptionnel', function() {
		$('#vue-exceptionnel').load('/rebus_ajax/exceptionnel', function() {																											   
			Cufon.replace('.view-exceptionnel p.date');
			Cufon.replace('.view-exceptionnel p.title a');		
		});
		
		
		/*
		* ----------------------------------------------------------------------
		* CALENDRIER (TEMPORAIRE)
		* ----------------------------------------------------------------------
		*/

		$('#colonne-gauche .calendrier-left td a').each(
			function(i){
				var chaine = $(this).attr('href');
				var reg  = new RegExp("(calendrier)", "g");
				var newChaine = chaine.replace(reg, "calendrier_jour");
				//console.log(newChaine);
				$(this).attr('href', newChaine);
			}
		);
		
		
		
		


		if( $('body.front').length == 0){
			actualiseDimensions();
			
			
			
			// =================================================================
			// PAGE PROGRAMMATION / NOS SPECTACLES
			// =================================================================
			
			/*alert('programmation' + $('#programmation-affichage').length());
			if( $('#programmation-affichage').length() == 1){
				$('.programmation-liste').hide();
				$('ul.programmation-affichage li a').eq(0).addClass('active');
				actualiseDimensions();
				// ----------------------
				$('ul.programmation-affichage li a').eq(0).click(
					function(){
						$('.programmation-mosaique').show();
						$('.programmation-liste').hide();
						$(this).addClass('active');
						$('ul.programmation-affichage li a').eq(1).removeClass('active');
						actualiseDimensions();
						return false;
					}
				);
				// ----------------------
				$('ul.programmation-affichage li a').eq(1).click(
					function(){
						$('.programmation-mosaique').hide();
						$('.programmation-liste').show();
						$(this).addClass('active');
						$('ul.programmation-affichage li a').eq(0).removeClass('active');
						actualiseDimensions();
						return false;
					}
				);
			}*/
			
			
			
			
			
		}else{
			// ACCUEIL
			/*
			* ----------------------------------------------------------------------
			* Initialisation
			* ----------------------------------------------------------------------
			*/
			
			var animWidth = 683;
			var animHeight = 1024;
			var ratio = animHeight / animWidth;
			
			
			/*
			* ----------------------------------------------------------------------
			* Flash / Swfobject
			* ----------------------------------------------------------------------
			*/
			
			var flashvars = {};
			
			var flashparams = {};
			flashparams.wmode = "opaque";
			flashparams.scale = "exactFit";
			
			var attributes = {};
			attributes.styleclass = "flash";
			
			// PAGE 1 
			swfobject.embedSWF("fullpicturevert.swf", "flashAnimation", "100%", "100%", "9.0.0", false, flashvars, flashparams, attributes);
			
			
			/*
			* ----------------------------------------------------------------------
			* NEWS
			* ----------------------------------------------------------------------
			*/

			var contentTotal = $('#news').height() + 300;
			if( contentTotal > 640 ){
				$('#content').height( contentTotal + 120  + "px" );
			}
	

			
			 /*
			* ----------------------------------------------------------------------
			* CHARGEMENT DU TEASER
			* ----------------------------------------------------------------------
			*/
			
			
			// Calendrier (TEMP) 
			// remplace les liens
			$('#soon #calendrier td a').each(
				function(i){
					var chaine = $(this).attr('href');
					var reg  = new RegExp("(calendrier)", "g");
					var newChaine = chaine.replace(reg, "calendrier_jour");
					//console.log(newChaine);
					$(this).attr('href', newChaine);
				}
			);
			
			
			
			// Animation de chargement
			
			
			function fadeInLoading(){
				$('p.chargement span').fadeIn(1600, null, 
				function(){
					$('p.chargement span').fadeOut(1000, null, fadeInLoading);
				});	
				
			}
			
			fadeInLoading();
			
			
			$('#scroll-left').hide();
			$('#scroll-right').hide();
			
			$('#scroll').load('/rebus_ajax/view', function() {
			//$('#scroll').load('http://cluster011.ovh.net/~bobino/pointvirgule/rebus_ajax/view', function() {
				//alert('TEASER chargé.');
				//$('p.chargement').hide();
				initialiseTeaser();
				$('p.chargement').hide();
				$('#voile').hide();
				$('#scroll-left').fadeIn();
				$('#scroll-right').fadeIn();
				
				
			});
	   
			var _voletOuvert = false;
			var anciennePositionX;
			
			var _pas = 10;
			var _vitesse = 40;
			
			
			function ouvreVolet(thisClic, numero){
				// OUVRE

				anciennePositionX = $('#scroll').css('left');

				var _this = thisClic;
				var index = _this.next('.descriptif').children('.video').children('div').children('a').attr('rel');
				var url = _this.next('.descriptif').children('.video').children('.container-video').children('a').attr('href');
				
				_ulrEnCours = url;
				_indexEncours = index;
				
				
				$('#teaser .descriptif').animate({width:0, padding:0}, 0);
				
				
				// Scroll des affiches
				var posAffiche = calculePositionAffiche(numero) * -1;
				//alert('Position affiche numero ' + numero + " " + posAffiche);
				

				$('#scroll').animate({left:posAffiche + 'px'}, 1000);
				_this.next('.descriptif').animate({width:600, padding:0}, 500);
				
				chargeVideoYoutube(url, index);
				
				$('#scroll-left').fadeOut();
				$('#scroll-right').fadeOut();

				
				//afficheInfos("LARGEUR SCROLL " + $('#scroll').width() +  "LARGEUR TEASER " + $('#teaser').width() + " " + calculeLargeur());

			
			}
			
			
			function fermeVolet(thisClic){
				// FERME
				var _this = thisClic;
				//console.log( _this + " " + _this.next('.descriptif').children('.video') + " urlencours " + _urlEncours);
				// ON supprime le player
				/*
				swfobject.removeSWF('player' + _indexEncours);
				_this.next('.descriptif').children('.video').append('<div id="container'+ (_indexEncours) +'"><a href="' + _ulrEnCours + '">video</a></div>');
				*/
				
				//alert(_ulrEnCours + " " + _indexEncours);
				supprimeVideoYoutube(_ulrEnCours, _indexEncours);
				
				
				/*var element = document.getElementById("player" + _indexEncours);
				jwplayer(element).stop();*/
				
				$('#teaser .descriptif').animate({width:0, padding:0}, 500);			
				$('#scroll').animate({left:anciennePositionX}, 500);
				
				
				//$('#scroll').animate({left:anciennePositionX + 'px'}, 1000);
				
				
				//$('#teaser').scrollTo( 0 , anciennePositionX, {axis:'x'} );
				//$('#teaser').scrollTo( 0 , 1000, {axis:'x'} );
				$('#scroll-left').fadeIn();
				$('#scroll-right').fadeIn();

			}
			
			
			
			// déplacement
			function deplacement(direction, pas) {
					//var pos  = $('#scroll').offset() - 240;
					//console.log("SCROLL OFFSET GAUCHE " + $('#scroll').offset().left);
					//var posLeft = pos.left + pas;
					
					var pos = $('#scroll').css('left');
					var numPos = Number(pos.substring(0, pos.length -2));
					var posLeft = numPos + pas;
					
					/*if( (direction == "right") && (posLeft >= 0)){
						
						$('#scroll').css('left', posLeft + "px");
						console.log("RIGHT NUMPOS " + numPos);
						}*/
					
					var largeurTotale = calculeLargeur();
					//alert(largeurTotale);
					var posLimite = -1 * (largeurTotale - $('#teaser').width());
					
					if( (direction == "left") && (posLeft <= 0) && (posLeft >= posLimite)){
						
						$('#scroll').css('left', posLeft + "px");
						//console.log(" LEFT NUMPOS " + numPos + " / " + posLimite + " " + $('#teaser').width());
					}
			}
		
			
		
			
		   
			
			// ------------------
			// CALCULE LARGEUR
			
		
			
			function calculeLargeur(){
				var largeur = 0;
				$('#teaser img.imagecache-affiche').each(function(index) {
					//alert(index + ': ' + $(this).text());
					largeur += $(this).attr('width');
					
				 });
				
				return largeur; 
			}
			
			
			 
			
			// ------------------
			// CALCULE POSITION AFFICHE
			
		
			
			function calculePositionAffiche(index){
				var largeur = 0;
				$('#teaser img.imagecache-affiche:lt(' + (index) + ')').each(
					function(index) {
						//alert(index + ': ' + $(this).text());
						//console.log();
						largeur += $(this).attr('width');
				 	}
				);
				
				return largeur; 
			}
			
			
			
			
			
			function initialiseTeaser(){

				$('#teaser .descriptif').animate({width:0, padding:0}, 0);

				$('#teaser img.imagecache-affiche').click(
					function(){
						
						var numero = $('#teaser img.imagecache-affiche').index(this);
						if( _voletOuvert == false){
							// on ouvre
							ouvreVolet( $(this), numero );
							_voletOuvert = true;
						}else{
							// on ferme
							fermeVolet( $(this) );
							_voletOuvert = false;
						}
						return false;
					}
					
				);
				
				
				$('.descriptif .ferme-volet').click(
					function(){
						var img = $(this).parent('.video').parent('.descriptif').parent('.view-rows').children('img');
						fermeVolet(img);
						_voletOuvert = false;
						return false;
					}
				);
				
				
				// Survol des zones
				$('#scroll-right').hover(
					function(){
						$('p#aide').show();
						$('#teaser .descriptif').animate({width:0, padding:0}, 0);
						//console.log('debut hover');
						// on lance l'annimation en boucle toute les secondes (1000 miliseconde)
						_deplacement = setInterval(deplacement, _vitesse, "left", -1 * _pas);
					},
					function(){
						$('p#aide').hide();
						//console.log('fin hover');
						// arrêt de l'animation
						clearInterval(_deplacement);
					}
				);
				
				// Survol des zones
				$('#scroll-left').hover(
					function(){
						$('p#aide').show();
						$('#teaser .descriptif').animate({width:0, padding:0}, 0);
						//console.log('debut hover');
						// on lance l'annimation en boucle toute les secondes (1000 miliseconde)
						_deplacement = setInterval(deplacement, _vitesse, "left", _pas);
					},
					function(){
						$('p#aide').hide();
						//console.log('fin hover');
						// arrêt de l'animation
						clearInterval(_deplacement);
					}
				);
				
				
				
			
			}
			
			
		
			
			
			
		
		}
		// ----------------------------------------------------------------------
		
});


