// JavaScript Document
$(document).ready(function() {
		setInterval('swapMasthead()',5000);
		$('#imageHead .mastheadImage').bind('mouseover', function() {
			$(this).find('span').hide();
			$(this).find('span').css('visibility','visible');
			$(this).find('span').fadeIn(300);
		 });
		$('#imageHead .mastheadImage').bind('mouseout', function() {
			$(this).find('span').fadeOut(300);
		 });
		
		//arrange navigation elements
		distributeNav();
		
		//initiate nyro
		$('a.nyroModal').nyroModal();
		
		//display offer is there is one
		if($('#offer')) {
			$('#offer').show('slide', {direction: 'up'}, 1200);	
			//setTimeout(function() {$('#offer').hide('slide', {direction: 'up'}, 800);}, 16000); (special offer stays)
		}
 });




function distributeNav() {
	
	var w =982,
	n =8, //number of items in nav
	li =$('#navigation ul li'),
	lW =getLiTotalWidth();
	if(lW>8000) lW =getLiTotalWidth();
	if(lW>8000) lW =860;
	var r =w-lW;
	
	if(r>0) { //space left
		var p =Math.floor((r/n));
		for(var i=0; i<n; i++) {
			liW =li.eq(i).width();
			if(isNaN(liW)) liW =new Number(liW.replace(/px/g, ''));
			if(liW +p>500) {
				return;
			}
			li.eq(i).width((liW + p) + 'px');
		}
		
		
		
	}else { //make space
		var s =Math.ceil(r/n);
		for(i =0; i<n; i++) {
			liW =li.eq(i).width();
			if(isNaN(liW)) liW =new Number(liW.replace(/px/g, ''));
			li.eq(i).width((liW -s) + 'px');
		}
	}
	
	
	function getLiTotalWidth() {
		var liW =0,
		lW =0,
		n =$('#navigation li').length;
		for(var i=0; i<n; i++) {
			liW =$('#navigation ul li:eq('+i+')').width();
			if(isNaN(liW)) liW =new Number(liW.replace(/[px]/g, ''));
			lW +=liW;
		}
		return lW;
	}
	
}

function swapMasthead() {
	var currID =$('#imageHead .currentImage').attr('id');
	var nextID =$('#' + currID).next('.mastheadImage').attr('id');	
	if(!nextID) nextID =$('#imageHead .mastheadImage').attr('id');
	if(nextID!=currID) {
		$('#' + currID).removeClass('currentImage');
		$('#' + nextID).addClass('currentImage');
		$('#' + nextID).hide();
		$('#' + nextID).css('visibility', 'visible');
		$('#' + currID).fadeOut(800);
		$('#' + nextID).fadeIn(800);
	}
}

function swapLang(lang) {
	var url =window.location.href;
	var urlParts =url.split('/');
	urlParts[3] =lang;
	document.location.href =urlParts.join('/');
}

function displayErrors(e,t) {
	$('#dialog').dialog('destroy');
	if(!t) t ='Errors';
	var str ='';
	for(var i =0; i<e.length; i++) {
		str	+='<div class="errorItem warning">' + e[i] + '</div>';
	}
	$('#dialog').html(str);
	
	$("#dialog").dialog({
		title: t,
		modal: true,
		bgiframe: true,
		width: 400,
		height: 300,
		autoOpen: false
		
	});
	$('#dialog').dialog('open');
}

function displayAsset(o,id) {
	//redundant
}

function renderBookingFormGuests(o,c) {
	var c =$(o).val();
	for(var i=1; i<=10;i++) {
		if(i<=c) $('#guest-'+i+':hidden').fadeIn();
		else $('#guest-'+i+':visible').fadeOut();
	}
	
}

function autoSetDepartureDate() {
	var a =$('#arrivalDate').val();
	if(!a) return;
	var d =a.split('/');
	var n =new Date(new Number(d[2]), new Number(d[1])-1, new Number(d[0])+7);
	$('#departureDate').val(n.getDate() + '/' + (n.getMonth()+1) + '/' + n.getFullYear());
}

function checkChalet(n) {
	var max =0;
	if($(n).val()=='Chalet du Vallon') max =8;
	else max =10;
	
	if($('#guests').val()>max) {
		$('#guests option:selected').removeAttr('selected');
		$('#guests option:eq('+max+')').attr('selected', 'selected');
	}
	$('#guests option:gt('+(max)+')').remove();
	$('fieldset.guest:gt('+(max-1)+')').hide();
	if($('#guests option').length<max) {
		var c =$('#guests option').length;
		for(var i=c; i<=max; i++) {
			$('#guests').append('<option>'+i+'</option>');	
		}
	}
	
}



