// JavaScript Document
//alert('appel du fichier js fonction cover');



$(document).ready(function() {
				
				//alert('alert dans le document ready');
				
				//alert(<?php echo $cat;?>);
				var counts = {
					clicks: 0,
					mouseovers: 0,
					mouseouts: 0,
					focuses: 0,
					blurs: 0,
					overalls: 0
				};
				
			
				
				 
				
				jQuery('ul#moveIt li').focus(function(e) {
					
					if (counts.focuses == 0) {
						
						pushItem('<span style="color: #666;"><strong>Curious</strong> event fired by <em>You</em>.</span>');
					}
					
					if (counts.blurs == 10) {
						
						pushItem('<span style="color: #666;"><strong>Love</strong> event fired by <em>You</em>.</span>');
					}

					counts.focuses++;
					pushItem('<span style="color: #392;"><strong>Focus</strong> event fired by <em>' + jQuery(this).html() + '</em>.</span>');
					
					if (counts.focuses == 2) {
						pushItem('<span style="color: #666;"><strong>Like</strong> event fired by <em>You</em>. :)</span>');
					}
				}).blur(function(e) {
					counts.blurs++;
					pushItem('<span style="color: #329;"><strong>Blur</strong> event fired by <em>' + jQuery(this).html() + '</em>.</span>');
					
					if (counts.blurs == 8) {
						pushItem('<span style="color: #666;"><strong>Thrilled</strong> event fired by <em>You</em>.</span>');
					}
				}).click(function(e) {
					var path="offres/";
					if (document.getElementById('noel').value == 1) {//pour les offres de noel
						path = "boutique_de_noel/";
					}
					//Faire la redirection ici
					redirection(jQuery(this).attr('name'));
					//alert(jQuery(this).attr('title'));
					$url = jQuery(this).attr('name');
					//alert(jQuery('.roundabout-moveable-item').val());
					if (url == "boutique_de_noel") {
						url = "";
					}
					else if (url == "vismaville") {
						path = "";
						url = "";
					}
					else if (isNaN(url)) {//si on a passé le nom d'une boutique
						path = 'boutique/';
					}
					document.location.href = jQuery('#root').val()+path+url;
					var cible = jQuery(this).attr('id');
					
					jQuery('.roundabout-moveable-item').removeClass('roundabout-in-focus');
					jQuery(this).addClass('roundabout-in-focus');
					jQuery(".puce_img").attr("src", "coverflow/puce.png");
					var id_theme = jQuery('#valeur_du_theme').val();
					//alert (id_theme);
					var src = "coverflow/puce_h_"+id_theme+".png";
					jQuery('#'+cible+'_puce_img').attr("src", src);
					
					counts.clicks++;
					pushItem('<span style="color: #923;"><strong>Click</strong> fired by <em>' + jQuery(this).html() + '</em>.</span>');
					
					if (counts.clicks == 4) {
						
						pushItem('<span style="color: #666;"><strong>Impressed</strong> event fired by <em>You</em>.</span>');
					}
				}).mouseover(function(e) {
					counts.mouseovers++;
					jQuery(this).css('border', '0px solid blue');
					pushItem('<span style="color: red;"><strong>Mouseover</strong> event fired by <em>' + jQuery(this).html() + '</em>.</span>');
					
					if (counts.mouseovers == 20) {
						pushItem('<span style="color: #666;"><strong>OMG</strong> event fired by <em>You</em>.</span>');
					}
				}).mouseout(function(e) {
					counts.mouseouts++;
					jQuery(this).css('border', '0px solid red');
					pushItem('<span style="color: #999;"><strong>Mouseout</strong> event fired by <em>' + jQuery(this).html() + '</em>.</span>');
				}); 
				
				
				
				hover_vignette_focus();
				clic_focus();
				clic_list();
				affiche_coverflow();
				initialisation();
				
				
				jQuery('ul#moveIt').roundabout();
				
				//jQuery('.roundabout-moveable-item .voile').css('opacity',0.5);
				//jQuery('.roundabout-in-focus .voile').css('opacity',0);
				
				if(jQuery('#noel').val()=='1'){
				jQuery('#6 .voile').css('opacity',0);
				}else{
					jQuery('.roundabout-moveable-item .voile').css('opacity',0.5);
					jQuery('.roundabout-in-focus .voile').css('opacity',0);
					}
				
				
				
				//alert(jQuery('.roundabout-in-focus').css("opacity",1));
				
				setInterval(function() {
					jQuery('.recorded li').each(function() {
						fadeOverTime(jQuery(this));
					});
				}, 50);
			});
			
			
			
			
			
			
//Fonction exterieur au document ready			
			
			function pushItem(message) {
				var now = new Date();
				var hours, minutes, seconds, milli;
				
				hours = (now.getHours() < 10) ? '0' + now.getHours() : now.getHours();
				minutes = (now.getMinutes() < 10) ? '0' + now.getMinutes() : now.getMinutes();
				seconds = (now.getSeconds() < 10) ? '0' + now.getSeconds() : now.getSeconds();
				milli = (now.getMilliseconds() < 10) ? '00' + now.getMilliseconds() : (now.getMilliseconds() < 100) ? '0' + now.getMilliseconds() : now.getMilliseconds();
				
				var theTime = hours + ':' + minutes + ':' + seconds + '.<small>' + milli + '</small>';
				
				jQuery('.recorded ul').prepend('<li style="display: none;" timing="' + now + '"><small>' + theTime + '</small>' + message + '</li>');
				jQuery('.recorded li:first-child').show('fast');
				
				if (jQuery('.recorded li').length > 8) {
					jQuery('.recorded li').each(function(i) {
						if (i < 8) {
							return;
						}
						
						jQuery(this).hide('fast', function() { jQuery(this).remove(); });
					});
				}
			}
			
			function fadeOverTime(el) {
				var now = new Date();
				var timing = new Date(el.attr('timing'));
				var diff = now - timing;
				var opacity = (100 - (diff - 4000) / 60) / 100;
				
				opacity = (opacity > 1) ? 1 : opacity;
				
				if (opacity <= 0) {
					el.hide('fast', function() { jQuery(this).remove() });
				} else {
					el.css('opacity', opacity);
				}
				
			}
			
			
			//Faire apparaitre le cadre grisé en hover de la vignette en focus	
			function hover_vignette_focus(){
			
			jQuery('.bloc_hover').hide();
			jQuery(".roundabout-in-focus").attr("src", "coverflow/puce_h_"+jQuery('#valeur_du_theme').val()+".png");
					
					jQuery(".roundabout-in-focus").live('mouseover',function() {
					var cible_hover = jQuery(".roundabout-in-focus").attr('id');
					//alert('bloc_hover_focus_'+cible_hover);
					 jQuery('#bloc_hover_focus_'+cible_hover).show();
					 });
					 
					 jQuery(".bloc_hover").live('mouseover',function() {
					var cible_hover = jQuery(".roundabout-in-focus").attr('id');
					//alert('bloc_hover_focus_'+cible_hover);
					 jQuery('#bloc_hover_focus_'+cible_hover).show();
					 });
					 
					 var cible_hover = jQuery(".roundabout-in-focus").attr('id');
					 jQuery('.bloc_hover').live('mouseout',function() {
					 jQuery('.bloc_hover').hide();
					});
					
					var cible_hover = jQuery(".roundabout-in-focus").attr('id');
					 jQuery(".roundabout-in-focus").live('mouseout',function() {
					 jQuery('.bloc_hover').hide();
					});
					
					}
					
			
			//redirection après le clic sur le masque de l'image en focus		
			function clic_focus(){
				var path="offres/";
				if(document.getElementById('noel').value==1){path="boutique_de_noel/";}//pour les offres de noel
					jQuery(".bloc_hover").live('click',function() {
						var url = jQuery(".roundabout-in-focus").attr('name');
						//alert(url);
						var root = jQuery('#root').val();
						//alert(url);
						if(url=='blog'){
							document.location.href='http://blog.vis-ma-ville.com'; 
						}
						else{
							if (url == '2heuresavant') {
								document.location.href = 'http://www.2heuresavant.com/'; 
							}
							else if (url == 'boutique_de_noel') {
								document.location.href = root+url;
							}
							else if (url == 'vismaville') {
								document.location.href = root;
							}
							else {
								if (isNaN(url)) {//si c'est le nom d'une boutique qui est passé
									document.location.href = root+'boutique/'+url;
								}
								else {
									document.location.href = root+path+url;
								}
							}
						}
					 });
					
					}
					
					
			function initialisation(){
				
				//alert('interieur de la fonction initialisation');
				
				if(jQuery('#noel').val()=='1'){
				var vignette_act = parseInt(jQuery('#nb_puces').val())-1;
				}else{
					//special noel
				//var vignette_act = parseInt(jQuery('#nb_puces').val())-1;
				//affichage normal
				var vignette_act = parseInt(jQuery("#index_offre_affiche").html())+1;//j'ai remplacé la ligne du dessus par celle-là pour rendre le focus à l'offre centrale au lieu de la boutique de noel
				}
				///alert(vignette_act);
				jQuery('.roundabout-moveable-item').removeClass('roundabout-in-focus');
				jQuery('#'+vignette_act).removeClass('roundabout-moveable-item');
				jQuery('#'+vignette_act).addClass('roundabout-in-focus');
				//class="roundabout-moveable-item";
				//alert(jQuery('#'+vignette_act).attr('title'));
			}
			
			
			//Afficher le coverflow seulement après le chargement de la page
			function affiche_coverflow(){
			//alert('interieur de affiche coverflow');
				jQuery('#coverflow').css('visibility','visible');
			}
			
			
			
			
			
			function redirection(offre){
			
				var root = jQuery('#root').val();
				var path="offres/";
				if(document.getElementById('noel').value==1){path="boutique_de_noel/";}//pour les offres de noel
				//alert(offre);
				if(offre=='blog')	{
					document.location.href='http://blog.vis-ma-ville.com'; 
				}
				else{
					if(offre=='2heuresavant')	{
						window.open('http://www.2heuresavant.com/'); 
						//document.location.href='http://www.2heuresavant.com/'; 
					}
					else {
						if (offre == 'boutique_de_noel')	{
							document.location.href = root+offre;
						}
						else if (offre == 'vismaville')	{
							document.location.href = root;
						}
						else {
							if (isNaN(offre)) {//si c'est un nom de boutique qui est passé
									document.location.href = root+'boutique/'+offre;
							}
							else {
								document.location.href = root+path+offre;
							}
						}
					}
				}
				//document.location.href=root+"offres/"+offre; 
			}
			
			//Clic sur les boutons sous le diapo pour amenner la bonne image en focus			
			function clic_list(){
					
					//var id_theme = jQuery('#valeur_du_theme').live().val();
					//alert (id_theme);
					//jQuery('.roundabout-moveable-item').val();
					
					
					if(jQuery('#noel').val()=='1'){
				var pos_puce =  parseInt(jQuery('#nb_puces').val())-1;
				}else{
					//var pos_puce =  parseInt(jQuery('#nb_puces').val())-1;}
					var pos_puce = parseInt(jQuery("#index_offre_affiche").html())+1;}
					
					//alert(pos_puce);
					jQuery('#'+pos_puce+'_puce_img').attr("src", "coverflow/puce_h_"+jQuery('#valeur_du_theme').val()+".png");
					
					jQuery(".list_btn").live('click',function() {
						
						//alert('1');
						var id_theme = jQuery('#valeur_du_theme').val();
						
						var to_go = jQuery(this).attr('name');
						var cible_hover = parseInt(to_go)+1;
						
						jQuery('.roundabout-moveable-item').removeClass('roundabout-in-focus');
						jQuery('#'+cible_hover).addClass('roundabout-in-focus');
						jQuery('#moveIt').roundabout_animateToChild(to_go, "600", "swing");
						
						jQuery(".puce_img").attr("src", "coverflow/puce.png");
						var src = "coverflow/puce_h_"+id_theme+".png";
						jQuery('#'+cible_hover+'_puce_img').attr("src", src);
						
						jQuery('.roundabout-moveable-item .voile').css('opacity',0.5);
						//Animate ne fonctionne pas sur IE7??????? Mit en commentaire mais il faut trouver comment faire pour cela passe sur ie 7(modo)
						//jQuery('.roundabout-in-focus .voile').animate({opacity: 0,},500);
						//Test en attendant avec le changement d'opacité sans animate
						jQuery('.roundabout-in-focus .voile').css('opacity',0);
						
						
						//jQuery('.roundabout-moveable-item #vignette').css('opacity',0.5);
						// jQuery('.roundabout-in-focus #vignette').animate({opacity: 1,},500);
						
						
					 });
					
					}
