$(function() { // on ready

	// get all links then assign pop if have class popup
	$('a.popup').click(function() {
		var win = window.open($(this).attr('href'),"newwindow","scrollbars=1, menubar=1, width=600, height=500");
		win.focus();
		return false;
	});
	
	// get all links then assign pop if have class popup
	$('a.popupMap').click(function() {
		var win = window.open($(this).attr('href'),"newwindow","scrollbars=1, menubar=1, width=760, height=600");
		win.focus();
		return false;
	});
	
	/* toggle divs
	$('.arrowForward').show().click(function() {
		$(this).parent().next().toggle();
		$(this).toggleClass('arrowDown');	
		return false;
	});
	$('.arrowForward').parent().next().hide();*/
	
	
	if (!$.browser.opera)
	{
		$('#steps div.steps').hover(function() {
				$(this).find(':first').show();
			}, function() {
				$(this).find(':first').hide();
		});
	}
	
	// get lang cookie for day names in cal
	_lang = '';
	if ($.cookie('language') && $.cookie('language') != 'en') {
		_lang = $.cookie('language');
	}
	$('#dFrom').datepicker({ 
		mandatory: true,  // no clear option
		firstDay: 1, // monday
		changeFirstDay: false, // no change for monday
		dateFormat: 'yy-mm-dd',
		showOn: 'both', 
		//speed: 'fast', 
		buttonImage: '/hotels/graphics/calendar_blue.gif', 
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		beforeShow: customRangeFrom,
		onSelect: setStart
	});
	$('#dTo').datepicker({
		//closeAtTop: false,
		mandatory: true,
		firstDay: 1, // monday
		changeFirstDay: false, // no change for monday
		dateFormat: 'yy-mm-dd',
		showOn: 'both', 
		//speed: 'fast', 
		buttonImage: '/hotels/graphics/calendar_blue.gif', 
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		beforeShow: customRangeTo,
		onSelect: setEnd // callback
	});
	
	
	// inititiate functions on menu change
	 
	$('#start_day, #start_month').change(setOnChangeEntrada); 
	$('#end_day, #end_month').change(setOnChangeSalida); 
	// initiate onload
	checkDaysInMonth('start_month', 'start_day', 'end_month', 'end_day'); 
	setNightsEntrada();
	highlightDays();
	disableContinue();
	$('#jsDisabled').val('');
	
	/// search
	$('#lastminuteform').submit(function() {
		if (!checkValidDates()) {	
			$('#errorMessage').show();	
			return false;
		}
		$('#errorMessage').hide();	
		$('#sendform').hide();
		$('#loading').show();		
		setTimeout('document.lastminuteform.submit();', 500);
		return false;
	});
	
	$(".popDown").show();	
	$(".popDown").next().hide();
	$(".popDown a").click(function() {
		$(this).parent().next().toggle();
		$(this).toggleClass("arrowDown");	
		return false;	
	});
	
	// show first popdown prices
	$('#autoPrices').find('.popDown').eq(0).find('a:first-child').trigger("click");
	
	$('.pricePopUp').toggle(function() {
		$('#popUp1').remove();							 	
		$(this).parent().after('<div style="width: 500px; padding: 5px; border: 1px solid #ccc; background-color: white; line-height: 1.5; text-align: left; margin: 5px 0" id="popUp1" class="small">'+$('#priceInfo').html()+'</div>').show();
	}, function() {
		$('#popUp1').remove();
	}).css({'cursor' : 'pointer',
			'text-decoration' : 'underline'});
});


//////////////////////////////////// functions before show
	
  
function customRangeFrom() { 
	$('#dFrom').val( 
		$('#start_month').val() + '-' +  
		$('#start_day').val() 
	); 
	return {minDate: new Date(), 
		maxDate: 426
	}; 
} 

function customRangeTo(input) { 
	$('#dTo').val(
		$('#end_month').val() + '-' +  
		$('#end_day').val() 
	); 
	return {minDate: $('#dFrom').datepicker('getDate'), 
		maxDate: 426
	}; 
}

//////////////////////////////////// functions after selecting start date using calendar or select menus
 
// on first cal set day/month fields, and check days in month
function setStart(date) { 
	
	$('#start_month').val(date.substring(0, 4)+'-'+date.substring(5, 7)); 
	$('#start_day').val(date.substring(8, 10)); 
	
	// set end day one after start
	
	setCalDate();
	checkDaysInMonth('start_month', 'start_day', 'end_month', 'end_day'); 
	setNightsEntrada();
	disableContinue();
} 

// onchange of any of start day menus
function setOnChangeEntrada() { 
	
	$('#dFrom').val($('#start_month').val()+'-'+$('#start_day').val());

	setCalDate();
	checkDaysInMonth('start_month', 'start_day', 'end_month', 'end_day'); 	
	setNightsEntrada();
	disableContinue();
	
}

//////////////////////////////////// functions after selecting end date using calendar or date fields

// on second cal: set day/month fields
function setEnd(date) {
	
	
	$('#end_month').val(date.substring(0, 4)+'-'+date.substring(5, 7));
	$('#end_day').val(date.substring(8, 10));
	//alert($('#end_month').val()+'-'+$('#end_day').val());
	checkDaysInMonth('start_month', 'start_day', 'end_month', 'end_day'); 
	setNightsSalida();
	disableContinue();
}

// on change of any of end day menus
function setOnChangeSalida() {
	
	$('#dTo').val($('#end_month').val()+'-'+$('#end_day').val());
	checkDaysInMonth('start_month', 'start_day', 'end_month', 'end_day', $('#dTo').val());
	setNightsSalida(); 
	disableContinue();
}

// set iframe cal date
// using jquery is a bit slower
function setCalDate() {
	if (window.frames['cal']) {
		// delete olddate and reset
		var olddate = $('#cal').contents().find('#olddate').val();
		$('#cal').contents().find('td #'+olddate).css('border', '');
		var olddate = window.frames['cal'].document.getElementById('olddate').value;
		if (window.frames['cal'].document.getElementById(olddate)) {
			window.frames['cal'].document.getElementById(olddate).style.border = "";
		}
		var dFrom = $('#start_month').val()+'-'+$('#start_day').val();
		//alert(dFrom);
		$('#cal').contents().find('td #'+dFrom).css("border", "2px solid #3333cc");
		if (window.frames['cal'].document.getElementById(dFrom)) {
			window.frames['cal'].document.getElementById(dFrom).style.border = "2px solid #3333cc";
			//$('#cal').contents().find('#olddate').val(dFrom);
			window.frames['cal'].document.getElementById('olddate').value = dFrom;
		}
		
	}
	
}









function disableContinue()	{
	// disable continue			
	if (document.getElementById('botoncontinuar')) {
		$('#botoncontinuar').attr('disabled', true);
		$('td .tdShowPrice').empty();
		$('#Submit2').addClass('searchAlert');
	}
}

function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
	xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
		  xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
		  try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch(e) {
			xhr = false;
		  }
		}
	}
	return xhr;
}