/* Vacances Krusoe by zephyr-os.fr */
jQuery.fn.stripTags = function() { return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') ); };

$.fn.replaceUrl = function() {
    var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
	
	
	
    this.each(function() {   
        $(this).html($(this).html().replace(regexp,'<a href="$1">$1</a>'));
		$(this).html(stripTags($(this)));					   			   
    });
	
	
	
  return $(this);
}

/*
** Ajout a la selection de camping
*/
function add_to_selection(idproduit){
	displayOverlib('Veuillez patienter','Ajouter à la selection','Fermer','od_ad_to_cart','DRAGGABLE');
	$.ajax({
	   type: "POST",
	   url: "/scripts/specifs/add_to_selection.php",
	   data: "idproduit="+idproduit,
	   success: function(msg){
		 displayOverlib(msg,'Ajouter à la selection','Fermer','od_ad_to_cart','DRAGGABLE');
	   }
	 });
}
function add_to_selection_en(idproduit){
	displayOverlib('Please wait','Add to my selection','Close','od_ad_to_cart','DRAGGABLE');
	$.ajax({
	   type: "POST",
	   url: "/scripts/specifs/add_to_selection.php",
	   data: "idproduit="+idproduit,
	   success: function(msg){
		 displayOverlib(msg,'Add to my selection','Close','od_ad_to_cart','DRAGGABLE');
	   }
	 });
}
function add_to_comparateur(idproduit){
	cClick();
	displayOverlib('Patientez svp','Ajouter au comparateur','Fermer','od_ad_to_cart','DRAGGABLE');
	$.ajax({
	   type: "POST",
	   url: "/scripts/specifs/add_to_comparateur.php",
	   data: "idproduit="+idproduit,
	   success: function(response){
		   var data=response.split("@@!@@");
		   var ajout=data[0];
		   var msg=data[1];
		   if(msg!=""){
			   if(ajout=="0"){
				   $('#comparateur_'+idproduit).attr("checked",false);
			   }
			   displayOverlib(msg,'Ajouter au comparateur','Fermer','od_ad_to_cart','DRAGGABLE');
	   	   }else{
			   cClick();
		   }
	   }
	 });
}
function add_to_comparateur_en(idproduit){
	cClick();
	displayOverlib('Please wait','Add to comparator','Close','od_ad_to_cart','DRAGGABLE');
	$.ajax({
	   type: "POST",
	   url: "/scripts/specifs/add_to_comparateur.php",
	   data: "idproduit="+idproduit,
	   success: function(response){
		   var data=response.split("@@!@@");
		   var ajout=data[0];
		   var msg=data[1];
		   if(msg!=""){
			   if(ajout=="0"){
				   $('#comparateur_'+idproduit).attr("checked",false);
			   }
			  displayOverlib(msg,'Add to comparator','Close','od_ad_to_cart','DRAGGABLE');
	   	   }else{
			   cClick();
		   }
	   }
	 });
}
function delete_from_comparateur(idproduit){
	$.ajax({
	   type: "POST",
	   url: "/scripts/specifs/add_to_comparateur.php",
	   data: "idproduit="+idproduit,
	   success: function(response){
		  $('#short_product_comparateur_'+idproduit).fadeOut(300,function() { $('#short_product_comparateur_'+idproduit).remove(); });
	   }
	 });
}
// Rafraichit la page selon le tri cliqué
function updateListResult(tri,region){
	$('#trierpar').val(tri);
	if(region!="") $('#recherche_region').val(region);
	$('#formsearch').submit();
}
// Ouverture du lien "+ de photos"
function plusdephoto(idproduit){
	$.ajax({
	   type: "POST",
	   url: "/scripts/specifs/camping_plusdephoto.php",
	   data: "idproduit="+idproduit,
	   success: function(response){
		  $('#diaporama-camping .simple_overlay_content').html(response);
		  
		  apiOverlay = $("#diaporama-camping").overlay({
				    api:true,
				    mask: {
						color: '#000',
						loadSpeed: 200,
						opacity: 0.7
					},
					onBeforeLoad : function() {
						var that = this;
						$(this.getOverlay()).remove().appendTo("body");
						$(".close", this.getOverlay()).click(function(e){
							that.close();
						});
						
						$(".scrollable").scrollable();
						  var length = $('#diaporama-camping #scrollableitems > div > img').length;
						  if(length<=5){
							$('.browse').css("visibility","hidden");
						}
						
						$("#diaporama-camping .items img").click(function() {
							// see if same thumb is being clicked
							if ($(this).hasClass("active")) { return; }
							// calclulate large image's URL based on the thumbnail URL
							var url = $(this).attr("src").replace("/thumbnails", "");
							// get handle to element that wraps the image and make it semi-transparent
							var wrap = $("#image_wrap").fadeTo("medium", 0.5);
							// the large image
							var img = new Image();
							// call this function after it's loaded
							img.onload = function() {
								// make wrapper fully visible
								wrap.fadeTo("fast", 1);
								// change the image
								wrap.find("img").attr("src", url);
							};
							// begin loading the image
							img.src = url;
							// activate item
							$(".items img").removeClass("active");
							$(this).addClass("active");
							// when page loads simulate a "click" on the first image
						  }).filter("#diaporama-camping .items img:first").click();						
					},
					
					onLoad: function() { 
					$('#diaporama-camping').css('position', 'absolute');
					 $('body').scrollTo(0);
					},
					closeOnClick: true
		   });
		  
		  
		  apiOverlay.load();
		  
			  
	   }
	 });
	 
}
/* Pimp my CSS */
$(document).ready(function() {	

	$('.panelIframe').parent().css("display", "none");// Suppression div iframe pour la version evaluation
	
	$('.nav-services a, .bt-box-ensavoirplus').click(function(){
		$.scrollTo( "#product-intro", 500, {easing:'easein'} );	
	});	
	$('.nav-mobilhomes a').click(function(){
		$.scrollTo( "#product-mobilhomes", 500, {easing:'easein'} );	
	});	
	$('.nav-acces a').click(function(){
		$.scrollTo( "#product-acces", 500, {easing:'easein'} );	
	});	
	$('.nav-proximite a').click(function(){
		$.scrollTo( "#product-proximite", 500, {easing:'easein'} );	
	});	
	$('.nav-dispo a').click(function(){
		$.scrollTo( "#product-dispo", 500, {easing:'easein'} );	
	});
	
	$("a.bt-fiche-top").each(function(i){
		$(this).click(function(){
			$.scrollTo("#page", 500, {easing:'easein'} );	
		});	
	});
	
	
	// Recup image HD	
/*	$(".short_product .prod-pict a, .short_product_comparateur .prod-pict-img").each(function(i){
			var grdeimage = $(this).children("img").attr('src');
			
			if (grdeimage) {
			  var src_grdeimage = grdeimage.replace("thumbnails/", "");
			  $(this).children("img").attr("src", src_grdeimage);
			}
	});	*/
	
	
	
	
	// Page comparateur 
	
	function sortNumber(a,b)
	{
	return b - a;
	}


	var tableau = new Array();			
	$("body.comparateur .prod-activitesservices").each(function(i){
		tableau[i] = $(this).height();		
	});	
	tableau.sort(sortNumber);
	$("body.comparateur .prod-activitesservices").height(tableau[0]);	


	var tableauB = new Array();			
	$("body.comparateur .desc").each(function(i){
		tableauB[i] = $(this).height();		
	});	
	tableauB.sort(sortNumber);
	$("body.comparateur .desc").height(tableauB[0]);	
	
	
	var tableauTitre = new Array();			
	$("body.comparateur .prod-content h2").each(function(i){
		tableauTitre[i] = $(this).height();		
	});	
	tableauTitre.sort(sortNumber);
	$("body.comparateur .prod-content h2").height(tableauTitre[0]);	


	var tableauProximite = new Array();			
	$("body.comparateur .prod-proximite").each(function(i){
		tableauProximite[i] = $(this).height();		
	});	
	tableauProximite.sort(sortNumber);
	$("body.comparateur .prod-proximite").height(tableauProximite[0]);		
	
	
	// Fin Page comparateur
	
	$("body.comparateur .desc").height(tableauB[0]);	
	
	$(".coups_de_coeur #main").append('<div class="simple_overlay" id="diaporama-camping"><div class="simple_overlay_content"></div></div><div class="situation_overlay" id="situation_overlay"><div class="simple_overlay_content"></div></div>');
	
});

$(function() {
	// if the function argument is given to overlay,
	// it is assumed to be the onBeforeLoad event listener
	$(".coups_de_coeur a[rel], .bloc-listing a[rel], .prod-pict a[rel]").overlay({
																				 
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.7
		},																				 
		onBeforeLoad: function() {
			
			var that = this;
			$(this.getOverlay()).remove().appendTo("body");
			$(".close", this.getOverlay()).click(function(e){
				that.close();
			});			
			
			
			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".simple_overlay_content");
			// load the page specified in the trigger
			//wrap.load(this.getTrigger().attr("href"));
			wrap.html('<iframe id="iframe-gmap" width="626" scrolling="No" height="580" frameborder="0" src="'+this.getTrigger().attr("href")+'" marginheight="0" marginwidth="0" vspace="0" allowtransparency="true"></iframe>');
		}		
	});
});
