$(document).ready(function(){
	
	// Datepicker
	var indate = new Date($('#HotelIny').val(), $('#HotelInm').val() - 1, $('#HotelInd').val());
	var outMinDate = new Date($('#HotelIny').val(), $('#HotelInm').val() - 1, $('#HotelInd').val());
	var outdate = new Date($('#HotelOuty').val(), $('#HotelOutm').val() - 1, $('#HotelOutd').val());
	outMinDate.setDate(outMinDate.getDate() + 1);
	
	$("#indate").datepicker({
		showOn: "button",
		buttonImage: "/img/calendar.gif",
		buttonImageOnly: true,
		dateFormat: "yy-m-d",
		maxDate: "+364d",
		minDate: "-0d",
		defaultDate: indate,
		onSelect: function(dateText, inst) { 
			populateSelects(dateText,'In');
		}
	});
	
	$("#outdate").datepicker({
		showOn: "button",
		buttonImage: "/img/calendar.gif",
		buttonImageOnly: true,
		dateFormat: "yy-m-d",
		maxDate: "+365d",
		minDate: outMinDate,
		defaultDate: outdate,
		onSelect: function(dateText, inst) { 
			populateSelects(dateText,'Out');
		}
	});
	
	function populateSelects(dateText, what) {
		var date = dateText.split('-');
		$('#Hotel'+what+'y').val(date[0]);
		$('#Hotel'+what+'m').val(date[1]);
		$('#Hotel'+what+'d').val(date[2]);
		
		var ind = new Date($('#HotelIny').val(), $('#HotelInm').val()-1, $('#HotelInd').val());
		var outd = new Date($('#HotelOuty').val(), $('#HotelOutm').val()-1, $('#HotelOutd').val());
		var minOut = new Date(ind.getFullYear(), ind.getMonth(), ind.getDate());
		minOut.setDate(minOut.getDate() + 1);

		if(what == 'In') {
			// if outdate < indate, set to indate + 1 day
			if(outd <= ind) {
				$('#HotelOuty').val(minOut.getFullYear());
				$('#HotelOutm').val(minOut.getMonth()+1);
				$('#HotelOutd').val(minOut.getDate());
			} 
		}
		$("#outdate").datepicker( "option", "minDate", minOut );
		return true;
	}
	
	
	$("#filter .active").click(function() {
		var id = $(this).attr('id');
		var url = $("#close_"+id).attr('href');
		window.location = url;
	});
	
	// toggle detail page
	$("#tab_availability").click(function() {
		$("#facilities").hide();
		$("#info").hide();
		$("#photos").hide();
		$("#availability").fadeIn();
		$("#tab_availability").addClass("active");
		$("#tab_facilities").removeClass("active");
		$("#tab_info").removeClass("active");
		$("#tab_photos").removeClass("active");
	});
	
	$("#tab_facilities").click(function() {
		$("#availability").hide();
		$("#info").hide();
		$("#photos").hide();
		$("#facilities").fadeIn();
		$("#tab_availability").removeClass("active");
		$("#tab_facilities").addClass("active");
		$("#tab_info").removeClass("active");
		$("#tab_photos").removeClass("active");
	});
	
	$("#tab_info").click(function() {
		$("#availability").hide();
		$("#facilities").hide();
		$("#photos").hide();
		$("#info").fadeIn();
		$("#tab_availability").removeClass("active");
		$("#tab_facilities").removeClass("active");
		$("#tab_info").addClass("active");
		$("#tab_photos").removeClass("active");
	});
	
	$("#tab_photos, #show_policies").click(function() {
		$("#availability").hide();
		$("#facilities").hide();
		$("#info").hide();
		$("#photos").fadeIn();
		$("#tab_availability").removeClass("active");
		$("#tab_facilities").removeClass("active");
		$("#tab_info").removeClass("active");
		$("#tab_photos").addClass("active");
	});

	$(".more").click(function() {
		var id = $(this).attr("id");	
		$("#room_"+id).toggle();
	});
	
	$("#OrderOrder").change(function() {
		var url = $("#OrderUrl").attr('value');
		var newOrder = $("#OrderOrder").attr('value');
		window.location = url + '/order:' + newOrder;
	});
	
	$('#check_radius').click( function () {
		
		var id = $('#AirportId').attr('value');
		var radius = $('#AirportRadius').attr('value');

		$.get("/airports/count/", { id: id, radius: radius }, function(data){
			$("#nr_hotels").html(data);
			$("#AirportNrHotels").attr('value',data);
			
		});
	});
	
	$('.thumb').click(function() {
	    var src = $(this).attr('id');
	    $('#bigImage').attr('src', src);
	});
	
	$(".mapLink").click(function() {
		var url = $(this).attr('id');
		$("#mapContainer").show();
		loadExternal(url,'buffer');
	});
	
	$("#mapClose").click(function() {
		$("#mapContainer").hide();
	});
	
	$("#mailFriend").click(function() {
		$("#mailFriend_form").toggle('slow');
	});
});

function popupWindow(url, name, width, height) {
  popupWin = window.open(url, name, 'scrollbars=1,resizable=1,height=' + height + ',width=' + width + ',left=50,top=50')
}


var cX = 0; var cY = 0;

function UpdateCursorPosition(e) { 
	cX = e.pageX; 
	cY = e.pageY;
}
function UpdateCursorPositionDocAll(evt) { 
	cX = event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	cY = event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}
if(document.all) { 
	
	document.onmousemove = UpdateCursorPositionDocAll; 
	//windows
}
else { 
	document.onmousemove = UpdateCursorPosition; 
	//mac
}
function AssignPosition(d) {
	if((cX-550)<0) { 
		cX = 15; 
		d.style.left = cX + "px";
	} else {
		d.style.left = (cX-550) + "px";
	}
	d.style.top = (cY-75) + "px";
}
function HideContent(d) {
	if(d.length < 1) { 
		return; 
	}
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	if(d.length < 1) {
		return; 
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
}
function ReverseContentDisplay(d) {
	if(d.length < 1) { 
		return; 
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	if(dd.style.display == "none") { 
		dd.style.display = "block"; 
	} else { 
		dd.style.display = "none"; 
	}
}


// Loading external page in iFrame
function loadExternal(url,layer) {
	if ( window.frames['buffer'] ) {
		window.frames['buffer'].location = url;
		/*ShowContent('display');
		var lyr = document.getElementById? document.getElementById('display'): document.all? document.all['display']: null;
		if ( lyr && typeof lyr.innerHTML != "undefined" ) {
			lyr.innerHTML = '<img src="/img/loading.gif" alt="" class="loading">';
		}*/
		return false;
	} 
	return true; // other browsers follow link
}

// called when documents loaded into iframe (from their body's onload attribute)
function displayExternal() {
	var lyr = document.getElementById? document.getElementById('display'): document.all? document.all['display']: null;
		if ( window.frames['buffer'] && lyr && typeof lyr.innerHTML != "undefined" ) {
			lyr.innerHTML = window.frames['buffer'].document.body.innerHTML;
		}
}

function DoIt() {
	getLoaded();
	setTimeout('parent.doMap(hotels,center)', 1);
    setTimeout('parent.focusMap(center)', 2);
}

