
$(document).ready(function () {
	
	$(".person_count").attr("style", "visibility:hidden");
	$(".person_text").attr("style", "visibility:hidden");
	$("#guest_span").attr("style", "visibility:visible");

	$(".detail_nahled").click(function() {	
		var data = new Object(); 
		data.fileName = $(this).attr("src"); 
		var dataString = $.toJSON(data);
		 
		$.post("request-hotel-detail/", {getPhoto: dataString}, function (result) {
			var data = result;
			$("#main_photo").attr("src", data.fileName);
		}, "json");
	});
	
	$("#btn_find").click(function() {
		if ($("#nights").val() > 31) {
        	alert("Nights limit reached. Maximum is 31 nights.");
			return false;
      	}
      	else if ($("#nights").val() == "-") {
      		alert("Check in has the same or later date than check out");
      		return false;
      	}
      	else {
      		$("#avail_checked").val(1);
      		return true;
      	}
    });
    
    $("#btn_book").click(function() {
		var isEmpty = true;
		var isError = false;
		
		$(".room_count").each(function() { 	
   		 	if (this.value > 0) {
   		 		isEmpty = false;
   		 		return;
   		 	}
   		});
   		if ($("#avail_checked").val() == 0) {
      		alert("You have changed check in or check out date. Please check availability before proceeding.");
      		isError = true;
      	}
		else if ($("#nights").val() > 31) {
        	alert("Nights limit reached. Maximum is 31 nights.");
			isError = true;
      	}
      	else if ($("#nights").val() == "-") {
      		alert("Check in has the same or later date than check out");
      		isError = true;
      	}
      	else if (isEmpty) {
   		 	alert("Please select at least one room");
   		 	isError = true;
   		}
      	
      	if (isError) {
      		return false;
      	}
      	else {
      		return true;
      	}
	});
	
	$(".room_count").change(function() {
		var id_rcount = new String(this.id);
		var index = id_rcount.substr(7);	// id maji format rcount_cislo
		var id_pcount = "pcount_" + index;
		var id_ptext = "ptext_" + index;
		var options = "";	
		
		if (this.value > 0) {
			for (var i = 1; i <= this.value * $("#person_room_" + index).val(); i++) {
	   			options += "<option value='" + i + "'>" + i + "</option>";
		    } 
		    $("#" + id_pcount).html(options);
			document.getElementById(id_pcount).style.visibility = "visible";
			document.getElementById(id_ptext).style.visibility = "visible";
		}
		else {
			document.getElementById(id_pcount).style.visibility = "hidden";
			document.getElementById(id_ptext).style.visibility = "hidden";
		}
		computeGuests();
	});
	
	$(".person_count").change(function() {
		computeGuests();
	});
	
	function computeGuests() {
   		var persons = 0;
		$(".person_count").each(function() {
			if (this.value > 0) {
				persons += parseInt(this.value);
			}
		});
		$("#guests").val(persons);
	}
});



























