Shadowbox.init();

var openShadowbox = function(content, player, width, height, title){
	Shadowbox.open({
		content:    content,
		player:     player,
		title:      title,
		width: 		width,
		height:		height
	});
};


var $j = jQuery.noConflict();

var minHeightColumnText = 370
var maxHeightColumnText = 370;

var map = null;
var markersPointArray = [];
var listPoint		  = [];


var initLatitude = 46.754917;
var initLongitude = 2.874755;
var initZoom = 1;

$j(document).ready(function(){
	$j('img.rollover').rollOver();
	
	/**
	 * Changement de background
	 */
    if($j('#backgroundfade').length > 0){
    	$j('#backgroundfade').fadeOut(1500);
    }
	
	/**
	 * ACCORDION
	 */
	 
	 /* Menu gauche */
	if($j('.containerAccordion').length > 0){
		$j('.containerAccordion').wfAccordion();
	}
	
	if($j('.retaillerAccordion').length > 0){
		$j('.retaillerAccordion').wfAccordion();
	}
	/**
	 * INIT CYCLES
	 */
	 
	 /* Cycle listing produit */
	if($j('#slider_product').length>0){
		$j('#slider_product').cycle({
			fx:     'scrollVert',
			timeout: 0,
			next:   '#slider_next_product', 
			prev:   '#slider_previous_product'
		});
	}
	
	/* Cycle thumb descriptif produit */
	if($j('#slider_descriptif').length>0){
		$j('#slider_descriptif').cycle({
			fx:     'scrollHorz',
			timeout: 0,
			next:   '#slider_next_descriptif', 
			prev:   '#slider_previous_descriptif'
		});
	}
	
	/* Cycle thumb couleur produit */
	if($j('#slider_couleur').length>0){
		$j('#slider_couleur').cycle({
			fx:     'scrollHorz',
			timeout: 0,
			next:   '#slider_next_couleur', 
			prev:   '#slider_previous_couleur'
		});
	}
	
	/* Cycle listing presse */
	if($j('#slider_presse').length>0){
		$j('#slider_presse').cycle({
			fx:     'fade',
			timeout: 0,
			next:   '#slider_next_presse', 
			prev:   '#slider_previous_presse'
		});
	}
	
	
	if($j('#scroll_text').length>0){
	
		if($j('#scroll_text_content').height()>=maxHeightColumnText){
			
			/**
			 * récupération du nombre d'éléments et calcul de la hauteur moyenne
			 */
			var itm = $j('#scroll_text_content .txt_content');
			var n	= itm.length;
			if(n > 0){
				h = $j('#scroll_text_content').height() / n;
			}else{
				h = $j('#scroll_text_content').height()
			}
			
			/**
			 * determination de la vitesse de défilement et du step en fonction de h
			 */
			var nb 		= 1;
			var speed	= 200;
			
			/**/
			if(h > maxHeightColumnText){
				nb 		= 6;
				speed	= 600;
			}else if(h > (maxHeightColumnText / 2)){
				nb 		= 3;
				speed	= 400;
			}else{
				nb 		= 3;
				speed	= 200;
			}
			/**/
			
			$j('#scroll_text').serialScroll({
				items:'div',
				prev:'#prev',
				next:'#next',
				event: 'click',
				duration:speed,
				step:nb,
				offset:-20,
				force:true,
				stop:false,
				lock:false,
				axis:'y',
				cycle:false
			});	
						
		}else{
			$j('#next').css('visibility','hidden');
			$j('#prev').css('visibility','hidden');
		}
	}

	
	
	/* Cycle listing texte */
	if($j('#slider_text').length>0){
		
		
		if($j('#slider_text').height() >= maxHeightColumnText){
			
			$j('#slider_text').columnize({balanced: false, height: maxHeightColumnText, minHeight: minHeightColumnText});

			$j('#slider_text div.column').each(function(index){
				$j(this).data('num', index);
			});
			
			$j('#slider_text').cycle({
				fx:     			'fade',
				next:   			'#btn_text_next',
				prev:   			'#btn_text_prev',
				rev: 				1,
				timeout:			0,
				before: 			function(currSlideElement, nextSlideElement, options, forwardFlag){
					if($j(currSlideElement).data('num') == $j(nextSlideElement).data('num')){
						// Init
						$j('#btn_text_prev').css('visibility', 'hidden');
					}else{
						// Desactive active prev buttom
						if($j(nextSlideElement).data('num') <= 0){
							$j('#btn_text_prev').css('visibility', 'hidden');
						}else{
							$j('#btn_text_prev').css('visibility', 'visible');
						}
						// Desactive active next buttom
						if( ($j(nextSlideElement).data('num') + 1) >=  $j('#slider_text div.column').length ){
							$j('#btn_text_next').css('visibility', 'hidden');
						}else{
							$j('#btn_text_next').css('visibility', 'visible');
						}
					}
				}
			});
		}else{
			$j('#btn_text_prev').css('visibility', 'hidden');
			$j('#btn_text_next').css('visibility', 'hidden');
		}
	};
	
	
	/**
	 * modification image principaleSlug
	 */
	$j('.vignette_apercu').click(function(){
		$j.post("/getPhoto.html",
				{itm: this.id},
				function(data){
					if (data.content){
						$j('.contener_img_produit').html(data.content);
					}
				},
				'json'
		);
	});
	
	$j('.fade')
	    .live('mouseenter',function(){
	                    $j(this).find('div:first').stop().fadeTo(250, 1);
	    })
	    .live('mouseleave', function(){
	                    $j(this).find('div:first').stop().fadeTo(50, 0);
    });

	/**
	 * GOOGLE MAP POINT DE VENTE
	 */
	if ($j('.box_gmap').length > 0) {
		
		var latlng = new google.maps.LatLng(initLatitude, initLongitude);
		
		myOptions = {
				zoom: initZoom,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				disableDefaultUI: true
		};
		
		map = new google.maps.Map(document.getElementById('box_gmap'), myOptions);
		map.bounds = new google.maps.LatLngBounds ();
	
	}
	
	/**
	 * JEUX
	 * 
	 */
	/**
	 * FORM INSCRIPTION
	 */
	if ($j('form.inscription').length == 1) {
		
		/**
		 * INIT SUBMIT BUTTON
		 */
		$j('input.submit')
			.css('opacity', 0.5)
			.live('click', function(){
				return false;
			});
		
		/**
		 * CLICK ON SUBMIT BUTTON
		 */
		$j('input[name=agree_reglement_check]').live('click', function(){
			if ($j(this).filter(':checked').length == 1) {
				$j('input.submit')
					.css('opacity', 1)
					.die('click')
					.live('click', function(){
						return true;
					});
			}
			else {
				$j('input.submit')
					.css('opacity', 0.5)
					.die('click')
					.live('click', function(){
						return false;
					});
			}
		});
		
		/**
		 * ON SUMBIT FORM
		 *
		$j('input[name^=user_email]')
			.live('focus', function(){
				$j(this).removeClass('error');
			});
		$j('form.inscription').live('submit', function(){
			var email 				= $j('input[name=user_email]').val();
			var emailConfirmation 	= $j('input[name=user_email2]').val();
			
			if ( (email.length > 0) && (email == emailConfirmation) ) {
				return true;
			}
			else {
				$j('input[name^=user_email]').addClass('error');
				return false;
			}
		});
		/**/
		/**
		 * DATEPICKER
		 */
		$j('input.datepicker')
			.datepicker({
				defaultDate: 	'01-09-2003',
				minDate:		'01-09-2003',
				maxDate:		'01-09-2010',
				dateFormat: 	'dd-mm-yy',
				changeMonth: 	true,
				changeYear: 	true,
				constrainInput: true
			}, $j.datepicker.regional['fr']);
		

	}
	
	/**
	 * FIN JEUX
	 */
});	



/**************************
 * Fonctions google map   *
 **************************/

function updateSelection(clearVille){
	var pays_id 		= $j('#pays_id').val();
	var departement_id	= $j('#departement_id').val();
	var ville			= $j('#ville').val();
	
	$j.post("/pointvente/ajax",
				{	
					pays_id: pays_id,
					departement_id: departement_id,
					ville: ville,
					clearVille: clearVille
				},
				function(data){
					deleteOverlaysPoint();
					$j('#dept').html(data.selectDept);
					$j('#city').html(data.selectVille);	
					$j('#openMap').val(data.openMap);
					listPoint = data.listPoint;
					addMarker(listPoint);
					doCenterMap();
				},
				'json'
	);
}

function doCenterMap(){
	
	if(markersPointArray.length > 1){
		  /* On recentre la carte de façon a voir tout les points de la carte */
		map.fitBounds(map.bounds);
	}
	else if(markersPointArray.length == 1){
	   /* On recentre la carte de façon à voir tout les points de la carte */
		map.setCenter(map.bounds.getCenter());
		if(listPoint[0]['type'] == 'pays'){
			map.setZoom(5);
		}else if(listPoint[0]['type'] == 'dept'){
			map.setZoom(8);
		}else{
			map.setZoom(15);
		}
		  
	}else{
	  /* On recentre la carte sur l'origine */
	  var localisation = new google.maps.LatLng(initLatitude, initLongitude);
	  map.setCenter(localisation);
	  map.setZoom(initZoom);
	}
}


function deleteOverlaysPoint() {
	if (markersPointArray.length > 0) {
		for (var i in markersPointArray){
			   markersPointArray[i].setMap(null);
		}
		markersPointArray.length = 0;
	}
	listPoint = [];
	map.bounds = new google.maps.LatLngBounds ();
}

/**
 * Creation d'un marqueur
 */
function addMarker(poseur, i){
	if(poseur.length > 0){
		var j = 0;
		for(var i = 0 ;  i < poseur.length ;  i++ ){
			if(poseur[i]['lat'] != 0 && poseur[i]['lng'] != 0){
				var latlng = new google.maps.LatLng(poseur[i]['lat'], poseur[i]['lng']);
				
				if(poseur[i]['type'] == 'pdv'){
					var iconImg = '/images/picto_gmap.png';
					var myInfoWindow = new google.maps.InfoWindow({
						content: poseur[i]['name']
					});
					
					var option = {
						position: latlng,
						map: map,
						title: poseur[i]['name'],
						id:poseur[i]['id'],
						infoWindow:myInfoWindow,
						icon: iconImg
					};
				}else{
					var iconImg = '/images/picto_gmap_invisible.png';
					
					var option = {
						position: latlng,
						map: map,
						title: poseur[i]['name'],
						id:poseur[i]['id'],
						icon: iconImg
					};
					
				}
				
				var marker = new google.maps.Marker(option);
				marker.setMap(map);
				map.bounds.extend(marker.position);
								
				if(poseur[i]['type'] == 'pdv'){
					google.maps.event.addListener(marker, 'click', function() {
						displayInfoPdv(this.id);
					});
					
				}
				markersPointArray[j] = marker;
				
				
				j++;
			}
		}
	}
}


