/* Main scripts for the website */


function getDaysNr(m,y) {
	switch(m) {
	case '01': return 31;case '03': return 31;case '04': return 30;case '05': return 31;case '06': return 30;
	case '07': return 31;case '08': return 31;case '09': return 30;case '10': return 31;case '11': return 30;case '12': return 31;
	case '02': return (((y%4==0 && y%100!=0)||y%400==0)?29:28);	
	}
}

$(document).ready(function() {
	$('#checkin-date').datePicker();
	$('#checkout-date').datePicker();
	
	$('#checkin-day').trigger('change');
	$('#checkout-day').trigger('change');

	$('#checkin-day').change(function() {
		if($('#checkin-month').val()!=0)  {
			selm=$('#checkin-month').val().split("-");
			$('#checkin-date').val($('#checkin-day').val()+'/'+selm[1]+'/'+selm[0]);
			$('#checkin-date').trigger('change');
		}
	});

	$('#checkin-month').change(function() {
		if($('#checkin-day').val()!=0)  {
			selm=$('#checkin-month').val().split("-");
			$('#checkin-date').val($('#checkin-day').val()+'/'+selm[1]+'/'+selm[0]);
			dnr=getDaysNr(selm[1],selm[0]);
			$('#checkin-day option:hidden').show();
			
			for (i=0;i<(31-dnr);i++) {
				$('#checkin-day option').eq(31-i).hide();	
			}	
			cm=parseInt($('#checkin-day option:selected').val(),10);
			
			if (cm>dnr) {
			 	$('#checkin-day option').eq(dnr).attr("selected","selected");
				$('#checkin-date').val(dnr+'/'+selm[1]+'/'+selm[0]);
			}
			$('#checkin-date').trigger('change');
		}
	});
	
	$('#checkin-date').change(function() {
		cdm=$(this).val().substring(3).split('/');
		$('#checkin-month option[value='+cdm[1]+'-'+cdm[0]+']').attr("selected","selected");
		$('#checkin-day option[value='+$(this).val().substring(0,2)+']').attr("selected","selected");
		if($('#checkout-date').val()=='') {	
			selm=$('#checkin-month').val().split("-");
			dnr=getDaysNr(selm[1],selm[0]);
			com=parseInt(selm[1],10);
			if(parseInt($('#checkin-day').val(),10)==dnr) { cod=1; com+=1; }else cod=parseInt($('#checkin-day').val(),10)+1;
			coy=parseInt(selm[0],10);

			if (com==13) { com=1; coy+=1; }
			$('#checkout-date').val((cod<10?'0':'')+cod+'/'+(com<10?'0':'')+com+'/'+coy);
			$('#checkout-date').trigger('change');
		}
	});


	$('#checkout-day').change(function() {
		if($('#checkout-month').val()!=0)  {
			selm=$('#checkout-month').val().split("-");
			$('#checkout-date').val($('#checkout-day').val()+'/'+selm[1]+'/'+selm[0]);
			$('#checkout-date').trigger('change');
		}
	});

	$('#checkout-month').change(function() {
		if($('#checkout-day').val()!=0)  {
			selm=$('#checkout-month').val().split("-");
			$('#checkout-date').val($('#checkout-day').val()+'/'+selm[1]+'/'+selm[0]);
			dnr=getDaysNr(selm[1],selm[0]);
			
			$('#checkout-day option:hidden').show();
			
			for (i=0;i<(31-dnr);i++) {
				$('#checkout-day option').eq(31-i).hide();	
			}	
			cm=parseInt($('#checkout-day option:selected').val(),10);
			
			if (cm>dnr) {
			 	$('#checkout-day option').eq(dnr).attr("selected","selected");
				$('#checkout-date').val(dnr+'/'+selm[1]+'/'+selm[0]);
			}
			$('#checkout-date').trigger('change');
		}
	});
	
	$('#checkout-date').change(function() {
		cdm=$(this).val().substring(3).split('/');
		$('#checkout-month option[value='+cdm[1]+'-'+cdm[0]+']').attr("selected","selected");
		$('#checkout-day option[value='+$(this).val().substring(0,2)+']').attr("selected","selected");
	});


	$('#checkin-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#checkout-date').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);

// Calendar for hotel availability
	$('#checkin-date2').datePicker();
	$('#checkout-date2').datePicker();
	
	$('#checkin-day2').change(function() {
		if($('#checkin-month2').val()!=0)  {
			selm=$('#checkin-month2').val().split("-");
			$('#checkin-date2').val($('#checkin-day2').val()+'/'+selm[1]+'/'+selm[0]);
			$('#checkin-date2').trigger('change');
		}
	});

	$('#checkin-month2').change(function() {
		if($('#checkin-day2').val()!=0)  {
			selm=$('#checkin-month2').val().split("-");
			$('#checkin-date2').val($('#checkin-day2').val()+'/'+selm[1]+'/'+selm[0]);
			dnr=getDaysNr(selm[1],selm[0]);
			$('#checkin-day2 option:hidden').show();
			
			for (i=0;i<(31-dnr);i++) {
				$('#checkin-day2 option').eq(31-i).hide();	
			}	
			cm=parseInt($('#checkin-day2 option:selected').val(),10);
			
			if (cm>dnr) {
			 	$('#checkin-day2 option').eq(dnr).attr("selected","selected");
				$('#checkin-date2').val(dnr+'/'+selm[1]+'/'+selm[0]);
			}
			$('#checkin-date2').trigger('change');
		}
	});
	
	$('#checkin-date2').change(function() {
		cdm=$(this).val().substring(3).split('/');
		$('#checkin-month2 option[value='+cdm[1]+'-'+cdm[0]+']').attr("selected","selected");
		$('#checkin-day2 option[value='+$(this).val().substring(0,2)+']').attr("selected","selected");
		if($('#checkout-date2').val()=='') {	
			selm=$('#checkin-month2').val().split("-");
			dnr=getDaysNr(selm[1],selm[0]);
			com=parseInt(selm[1],10);
			if(parseInt($('#checkin-day2').val(),10)==dnr) { cod=1; com+=1; }else cod=parseInt($('#checkin-day2').val(),10)+1;
			coy=parseInt(selm[0],10);

			if (com==13) { com=1; coy+=1; }
			$('#checkout-date2').val((cod<10?'0':'')+cod+'/'+(com<10?'0':'')+com+'/'+coy);
			$('#checkout-date2').trigger('change');
		}
	});


	$('#checkout-day2').change(function() {
		if($('#checkout-month2').val()!=0)  {
			selm=$('#checkout-month2').val().split("-");
			$('#checkout-date2').val($('#checkout-day2').val()+'/'+selm[1]+'/'+selm[0]);
			$('#checkout-date2').trigger('change');
		}
	});

	$('#checkout-month2').change(function() {
		if($('#checkout-day2').val()!=0)  {
			selm=$('#checkout-month2').val().split("-");
			$('#checkout-date2').val($('#checkout-day2').val()+'/'+selm[1]+'/'+selm[0]);
			dnr=getDaysNr(selm[1],selm[0]);
			
			$('#checkout-day2 option:hidden').show();
			
			for (i=0;i<(31-dnr);i++) {
				$('#checkout-day2 option').eq(31-i).hide();	
			}	
			cm=parseInt($('#checkout-day2 option:selected').val(),10);
			
			if (cm>dnr) {
			 	$('#checkout-day2 option').eq(dnr).attr("selected","selected");
				$('#checkout-date2').val(dnr+'/'+selm[1]+'/'+selm[0]);
			}
			$('#checkout-date2').trigger('change');
		}
	});
	
	$('#checkout-date2').change(function() {
		cdm=$(this).val().substring(3).split('/');
		$('#checkout-month2 option[value='+cdm[1]+'-'+cdm[0]+']').attr("selected","selected");
		$('#checkout-day2 option[value='+$(this).val().substring(0,2)+']').attr("selected","selected");
	});


	$('#checkin-date2').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#checkout-date2').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);


	$('#lang_selector, #currency_selector').change(function() {
		$('#form_lc').trigger('submit');									
	});
	
// World map
	var cur_img = "images/map_as.png";
	$('#world_map').attr("src",cur_img);

	$('#world_map').mouseout(function() {
		$('#world_map').attr("src",cur_img);
	});
	
	$('#Map area').click(function() {
		cur_img = "images/map_"+$(this).attr("alt")+".png";
		$('.center-e div:visible').hide();
		$('.center-e #'+$(this).attr("alt")+'_list').show();
		return false;	
	});	
	
	$('#Map area').mouseover(function() {
		$('#world_map').attr("src","images/map_"+$(this).attr("alt")+".png");	
	});
// Advanced search:

	$('#adv_search').hide();
	$('#adv_search').css('height', $('#adv_search').height() + 'px');
	$('#adv_expand').click(function () {
		$('#adv_search').slideToggle(200);
		return false;
	});
	
// Expand room info	
	$('.ntf_center').hide();
	$('.r_exp').click(function() {
		rinfo_div = '#rmd_'+$(this).attr('alt');
		$(rinfo_div).css('height', $(rinfo_div).height() + 'px');
		$(rinfo_div).slideToggle(300);
		return false;
	});

	$('#btn_search').click(function() {
		if (!$('#nodates').is(':checked')&&($('#checkin-date').val()=='' || $('#checkout-date').val()=='')) {
			$('#dates_error').show();
			return false;
		}
	});
});

function trim (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
