function str_pad (input, pad_length, pad_string, pad_type) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + namespaced by: Michael White (http://getsprink.com)
    // +      input by: Marco van Oort
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
    // *     returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
    // *     example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
    // *     returns 2: '------Kevin van Zonneveld-----'

    var half = '', pad_to_go;

    var str_pad_repeater = function (s, len) {
        var collect = '', i;

        while (collect.length < len) {collect += s;}
        collect = collect.substr(0,len);

        return collect;
    };

    input += '';
    pad_string = pad_string !== undefined ? pad_string : ' ';
    
    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }

    return input;
}

var langs = new Array();
	langs['fr'] = 'http://www.poitou-charentes-vacances.com/';
	langs['en'] = 'http://www.holidays-france-atlantic.com/';
	langs['de'] = 'http://www.atlantikkuste-frankreich.de/';
	langs['es'] = 'http://www.cognac-francia-atlantica.es/';


$(document).ready(function(){
	
	//over sur les pub
	$('a.theimage').hover(function(){
		$(this).next().show();
		
		$(this).next().mouseout(function(){
			$(this).hide();
		});
	});
	
	
	$('.jsaddtocarnet').click(function(){
		
		$. ajax({
			type: "POST",
			url: '/layout/set/addcarnetlayout/',
			data: 'add='+$(this).attr('href').replace('/Mon-voyage/Mon-carnet-de-voyage?add=',''),
			async: false,
			success: function(msg){
				try {
					var pageTracker = _gat._getTracker("UA-15509786-1");
					pageTracker._trackPageview('/layout/set/addcarnetlayout/');
				} catch(err){}
				
				
				
				$('body').append('<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1015696742/?label=6DHICIq0ywEQ5pqp5AM&amp;guid=ON&amp;script=0"/>');

				
				// var temp = '<script type="text/javascript">var google_conversion_id = 1015696742;var google_conversion_language = "fr";var google_conversion_format = "3";var google_conversion_color = "ffffff";	var google_conversion_label = "6DHICIq0ywEQ5pqp5AM";var google_conversion_value = 0; </script>';

				// var ga = document.createElement('script'); ga.type = 'text/javascript'; 
				// ga.src = 'http://www.googleadservices.com/pagead/conversion.js';
				// var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(temp+ga, s);
				
				// $('#body-wrapper').html('<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script>');
								
				alert('La destination vient d\'être ajoutée à votre carnet');
			}
			

		}); 
		
		return false;
	});
	
	$('#select-lang').sSelect().change(function(){
		//alert(langs[$('#select-lang').val()])
		location.replace(langs[$('#select-lang').val()]);
	
	});
								
	$('#content #map-option span').click(function(){
		if($(this).next().hasClass('block')){
			$(this).next().removeClass('block').addClass('none');
		}else{
			$('#content #map-option span').next().removeClass('block').addClass('none');
			$(this).next().removeClass('none').addClass('block');
		}
	});				
	$('#content #map-option img').click(function(){
		if($(this).next().next().hasClass('block')){
			$(this).next().next().removeClass('block').addClass('none');
		}else{
			$('#content #map-option img').next().next().removeClass('block').addClass('none');
			$(this).next().next().removeClass('none').addClass('block');
		}
	});

	
	if($('#btn-agenda').hasClass('open')){
		$('#form-search-agenda').show('normal',function(){
			$('#agenda-datepicker .ui-datepicker').css('width','188px');
		});
		
	}
	else if($('#btn-agenda').hasClass('close')){
		$('#form-search-agenda').hide();
	}
		
	$('#btn-agenda').click(function(){
		if($(this).hasClass('open')){
			$(this).addClass('close');
			$(this).removeClass('open');
			$('#form-search-agenda').hide();
		}
		else if($(this).hasClass('close')){
			$('#agenda-datepicker .ui-datepicker').css('width','188px');
			$(this).addClass('open');
			$(this).removeClass('close');
			$('#form-search-agenda').show();
		}
	});
	
});


$(window).load(function(){

	// Rollover sur destinations et passions
	
	// Gestion module meteo
	$("#meteo-cities-list ul li").each(function(i){
		$(this).attr('id','meteo-city-'+i);
	});
	meteoCitiesCount = $("#meteo-cities-list ul li").length;
	$("#meteo-cities-list").jCarouselLite({
   		speed: 500,
		visible: 1,
		circular:true,
		btnPrev: "#meteo-btn-prev",
		btnNext: "#meteo-btn-next",
		beforeStart:function(a,b) {
			itemsInfos = $('#meteo-info-list li');
			idNext = b.attr('id').replace(/.*(\d+)/,"$1");
			idCurr = a.attr('id').replace(/.*(\d+)/,"$1");
			if(idNext!=idCurr) {
				itemCurr = $('#meteo-info-list li.current');
				itemCurr.fadeOut('normal',function(){$(this).removeClass('current');});
				$(itemsInfos[idNext]).fadeIn('normal',function(){$(this).addClass('current');});
			}
		}
	});
	
	// Gestion visionneuse
	$("#hc-thumbs ul li").each(function(i){
		$(this).attr('id','hc-thumb-'+i);
		$(this).click(function(){
			$("#hc-but-go-"+i).click();	
			return false;
		});
	});
	$("#hc-visus ul li").each(function(i){
		$(this).attr('id','hc-visu-'+i);
	});
	buttons = new Array();
	$("#hc-buttons-go-wrapper button").each(function(i){
		$(this).attr('id',"hc-but-go-"+i);
		buttons.push("#hc-but-go-"+i);
	});
		
	nbItems = $("#hc-visus ul li").length;
	hcDescriptions = $('#hc-descriptions .hc-description');
	$("#home-carousel").append('<div id="hc-nav-numbers">1 / '+nbItems+'</div>');
	$("#hc-visus").jCarouselLite({
		auto: 8000,
   		speed: 500,
		visible: 1,
		circular:true,
		pauseOnHover:"#home-carousel",
		btnPrev: "#hc-buttons-prevnext-wrapper #hc-but-prev",
		btnNext: "#hc-buttons-prevnext-wrapper #hc-but-next",
		btnGo:buttons,
		beforeStart:function(a,b) {
			idNext = b.attr('id').replace(/.*(\d+)/,"$1");
			idCurr = a.attr('id').replace(/.*(\d+)/,"$1");
			if(idNext!=idCurr) {
				itemCurr = $('#hc-descriptions .hc-description.current')
				itemCurr.fadeOut('normal',function(){$(this).removeClass('current');});
				$(hcDescriptions[idNext]).fadeIn('normal',function(){$(this).addClass('current');});
			}
		
		},
		afterEnd: function(a) {
			idNext = a.attr('id').replace(/.*(\d+)/,"$1");
			
			$('#hc-nav-numbers').html((parseInt(idNext)+1)+' / '+nbItems);
			$('#hc-thumbs .current').removeClass('current');
			$('#hc-thumb-'+idNext).addClass('current');
			$('#hc-buttons-go-wrapper button').removeClass('selected');
        	$('#hc-buttons-go-wrapper #hc-but-go-'+idNext).addClass('selected');
    	}
	});
	
	// Champ de recherche
	elementInSearch = $('#input-search-text');
	elementInSearch.focus(function() {
		if(elementInSearch.val()== 'Rechercher dans le site')
			elementInSearch.val('');
	}).blur(function() {
		if ($(this).val() == '') {
			elementInSearch.val('Rechercher dans le site');
		}
	});
	
	elementInSearch2 = $('#theSearchOi');
	elementInSearch2.focus(function() {
		if(elementInSearch2.val()== 'ex : Hôtel La Rochelle')
			elementInSearch2.val('');
	}).blur(function() {
		if ($(this).val() == '') {
			elementInSearch2.val('ex : Hôtel La Rochelle');
		}
	});
	
	// Visionneuse pour PC az
	verticalDispo = false;
	pcazVisible = 6;
	scrollNumber = 6;
	if($("#bloc-pcaz-wrapper").hasClass('vertical-dispo')) {
		verticalDispo = true;
		pcazVisible = 8;
		scrollNumber = 3;
	}
	$("#bloc-pcaz-wrapper").jCarouselLite({
		speed: 500,
		scroll: scrollNumber,
		visible: pcazVisible,
		circular:true,
		vertical:verticalDispo,
		btnPrev: "#pcaz-btn-prev",
		btnNext: "#pcaz-btn-next"
	});
	
	// Formulaire de contact
	$('#form-contact input.txt, #form-contact textarea,#form-contact select.select').focus(
		function(){
			$(this).addClass('focus');
		}).blur(
		function(){
			$(this).removeClass('focus');
		}
	);
	$('#form-contact #attachement-wrapper').hide();
	$('#form-contact .btn-attachement').show();
	$('#form-contact .btn-attachement').toggle(
			  function () {
				  $('#form-contact #attachement-wrapper').slideDown();
				  },
				  function () {
					  $('#form-contact #attachement-wrapper').slideUp();
				  }
				);
	
	// Champ message
	elementInContactTA = $('#form-contact textarea');
	elementInContactTA.focus(function() {
		if(elementInContactTA.val()== 'Votre message')
			elementInContactTA.val('');
	}).blur(function() {
		if ($(this).val() == '') {
			elementInContactTA.val('Votre message');
		}
	});
	
	
	var data = new Array();
	var selected = false;
	

	/*
	$.get('/layout/set/searchlayout/Mon-voyage', function(data) {
		$('#select-type').append(data);
			var data = new Array();
			var selected = false;
			
			
			
			// Charge les donnees du select dans la variable data
			opts = $('#select-type option')
			var i=0;
			for(i=0;i<opts.length;i++)
			{
				if(i>0) {
					customer = {id:$(opts.get(i)).attr('value'),name:$(opts.get(i)).text()};
					data.push(customer);
				}
				if(	$(opts.get(i)).is(':selected') ) {
					selected = {id:$(opts.get(i)).attr('value'),name:$(opts.get(i)).text()}				
				}
			}
			
			field_id_select = $('#bloc-search-type-wrapper select').attr('name');
			$('#bloc-search-type-wrapper select').remove();
			$('#bloc-search-type-wrapper').append('<input type="text" class="text" id="view-'+field_id_select+'" name="'+field_id_select+'" style="" value="'+selected.name+'" />');
			//$('#bloc-search-type-wrapper').append('<input type="hidden" class="text" id="'+field_id_select+'" name="'+field_id_select+'" value="'+selected.id+'" /> ');
			//if( selected.id!='' ) {
				//$('#admin-menu form').append('<input type="reset" class="submit" value="Reset" /> ');
				//$("#admin-menu form input[type='reset']").click(function() {
					//$('#'+field_id_select).val('');
					//$('#admin-menu form').submit();
				//});
			//}
			
			$('#view-'+field_id_select).click(function(){
				$(this).val('');
			});
			
			// Parametrage de l'autocompletion
			$('#view-'+field_id_select).autocomplete(data,{
				minChars: 0,
				width: 250,
				matchContains: true,
				autoFill: false,
				//max:false,
				formatItem: function(row) {
					return row.name;
				},
				formatMatch: function(row) {
					return row.name;
				},
				formatResult: function(row) {
					return row.name;
				}
			});
	});*/

	function resetPubNav(){
		$(this).css('opacity','1');
		$(this).css('display','none');
	}

	$("#hc-thumbs").hover(function(){
			var fade = $('#hct-wrapper',this);
			if($('#hc-map:visible').length==0) {
				// if the element is currently being animated (to fadeOut)...
				if (fade.is(':animated')) {
					// ...stop the current animation, and fade it to 1 from current position
					fade.stop().fadeTo(500, 1);
				} else {
					fade.fadeIn(500);
				}
			}
		
		}, function () {
			
			var fade = $('#hct-wrapper',this);
			if($('#hc-map:visible').length==0) {
				if (fade.is(':animated')) {
					fade.stop().fadeTo(500, 0, resetPubNav);
				} else {
					fade.fadeOut(500, resetPubNav);
				}
			}
		}
	);
	
	$('#produit_contact').submit(function(){
		var retour = true; 
		$('input',this).each(function(){
			if($(this).val() == 'Sujet' || $(this).val() == 'Email'){
				retour = false;
			}
		});
		if(!retour){
			alert('Veuillez remplir correctement le formulaire avant de l\'envoyer.');
			return false;
		}
	});
	$('#produit_contact textarea').each(function(){
		$(this).focus(function() {
			if($(this).html()== 'Message')
			$(this).html('');
		}).blur(function() {
			if ($(this).html() == '') {
				$(this).html('Message');
			}
		});
	});
	$('#produit_contact input').each(function(){
		if($(this).val()== 'Sujet'){
			$(this).focus(function() {
			if($(this).val()== 'Sujet')
				$(this).val('');
			}).blur(function() {
				if ($(this).val() == '') {
					$(this).val('Sujet');
				}
			});
		}
		if($(this).val()== 'Email'){
			$(this).focus(function() {
				if($(this).val()== 'Email')
					$(this).val('');
			}).blur(function() {
				if ($(this).val() == '') {
					$(this).val('Email');
				}
			});
		}
	});
	
	
	// elementAct = $("#bloc-search-type-wrapper input[type='text']");
	// elementAct.each(function(){
		// $(this).focus(function() {
			// if($(this).val()== 'Activité')
			// $(this).val('');
		// }).blur(function() {
			// if ($(this).val() == '') {
				// $(this).val('Activité');
			// }
		// });
	// }); 
	
	// elementCity = $("#bloc-search-city-wrapper input[type='text']");
	// elementCity.each(function(){
		// $(this).focus(function() {
			// if($(this).val()== 'Lieu')
			// $(this).val('');
		// }).blur(function() {
			// if ($(this).val() == '') {
				// $(this).val('Lieu');
			// }
		// });
	// }); 
	
	forms = $('#date-search').parents('form');
	if(forms.length) {
		$(forms[0]).change(function(){
			$(this).submit();
		});
	}

	
	$.datepick.setDefaults($.datepick.regional['fr']);
	$("#agenda-popup").hover(
		function(){}
		,function(){
			$(this).hide();
		}
	);
	$("#agenda-datepicker").datepick({
		altField: '#date-search',
		altFormat: 'yymmdd',
		dateFormat: 'yymmdd',
		onChangeMonthYear: function(year, month, inst) { 
			regex = /(\/.*)\?.*/gi
			
			var oldhref = $('#btn-show-all-events').attr('href');
			$('#btn-show-all-events').attr('href',oldhref.replace(regex,'$1')+'?month='+year+str_pad(month,2,'0','STR_PAD_LEFT'))
		},
		changeMonth: true,
		changeYear: false,
		showOtherMonths: true,
		nextText:'',
		prevText:'',
		beforeShowDay: function(date) {
			var cssclass = 'date-nohighlight';
			dateFormat = date.getFullYear()+str_pad(date.getMonth()+1,2,'0','STR_PAD_LEFT')+str_pad(date.getDate(),2,'0','STR_PAD_LEFT');
			ite = 0;
			for(ite=0;ite<eventsArray.length;ite++)
			{
				if (dateFormat == eventsArray[ite]) {
					cssclass = 'date-highlight';
					break;
				}
				else {
					cssclass = 'date-nohighlight';
				}
			}
			return [true,cssclass];
		},
		onHover: function(value, date, inst) { 
			html = '';
			valueInt = parseInt(value);
			if(valueInt>0) {
				//alert(valueInt);
				if(eventsArrayEvent[valueInt]) {
					html += '<ul>';
					for(var i=0;i<eventsArrayEvent[valueInt].length;i++)
					{
						name = eventsArrayEvent[valueInt][i][0];
						if(name.length>23)
							name = name.substr(0,20)+' ...';
						html += '<li><a href="'+eventsArrayEvent[valueInt][i][1]+'">'+name+'</a></li>';
					}
					html += '</ul>';
				}
				html += '<a class="lnk-more" href="'+$('#form-search-agenda').attr('action')+'?date='+valueInt+'">en savoir plus</a>';
				$('#agenda-popup .pop-content').html(html);
				$('#agenda-popup').show();
				$('#agenda-popup').css('top',(mouseY-($('#agenda-popup').height())+2)+'px');
				$('#agenda-popup').css('left',(mouseX-($('#agenda-popup').width()/2))+'px');
			}

		},
		useThemeRoller:true
	});
	$("#agenda-datepicker").datepick($.datepick.regional['fr']);
	regex2 = /(\d{4})(\d{2})(\d{2})/gi
	regex = /(\/.*)\?.*/gi
	if($("#agenda-datepicker").length>0 && dateSelect.length>0 && dateSelect.match(regex2)) {
		selectYear = dateSelect.replace(regex2,'$1');
		selectMonth = dateSelect.replace(regex2,'$2');
		selectDay = dateSelect.replace(regex2,'$3');
		$("#agenda-datepicker").datepicker( "setDate" , new Date(selectYear,selectMonth-1,selectDay) );
		var oldhref = $('#btn-show-all-events').attr('href');
		$('#btn-show-all-events').attr('href',oldhref.replace(regex,'$1')+'?month='+selectYear+selectMonth);
	}
	else {
		dateNow = new Date();
		var oldhref = $('#btn-show-all-events').attr('href');
		dateFormat = dateNow.getFullYear()+str_pad(dateNow.getMonth()+1,2,'0','STR_PAD_LEFT');
		$('#btn-show-all-events').attr('href',oldhref.replace(regex,'$1')+'?month='+dateFormat);
	}
	
	$('#home-carousel .hcm-bloc').hover(
		function(){
			$('.hcm-popup',this).show();
		},
		function(){
			$('.hcm-popup',this).hide();
		}
	)
	
	$('#hcm-btn-close').click(function(){$('#hc-btn-map').click();});
	$('#hc-btn-map').toggle(
		function(){
			$('#hc-map').show();
		},
		function(){
			$('#hc-map').hide();
		}
	)
	
	
});

var mouseX = mouseY = 0;

$(document).mousemove(function(e){
	mouseX = e.pageX; 
	mouseY = e.pageY;
   }); 
