/**
 * @author frukt
 */

var hotelName, hotelId, newHotel, bookingSys, selPersons, selNights, languageName, languageId;
var queryHotel, queryPersons, queryNights;
var raw_day, raw_month, raw_year, day, month, year, curr_day_raw, curr_month_raw, curr_day, curr_month, curr_year;
var configFile = "/extensions/ext_uniquestay/config/quickbook.xml";

//since the hotels are booked via different systems, different booking system paths are required
var queryPath = new Array();
//system 0: ihotelier
queryPath[0] = "https://reservations.ihotelier.com/istay.cfm";
//system 1: ibooking
queryPath[1] = "http://www.clearclick.net/hotel/book/externalbook.asp";
//queryPath[1] = "http://hotelapp.ibooking.com/find.asp?id_prov=3301";

function init_form(language){



languageName = language;
var startDate="";
var depDate="";

	$.get(configFile, function(XMLdata){
		$(XMLdata).find("hotel").each(function(){
			var hotel = $(this);
			var hotelName = hotel.children("name").text();
			var hotelId = hotel.children("id").text();
			bookingSys = hotel.children("bookingsys").text();

			languageId = 0;

			hotel.children("language").each(function(){
				if ($(this).children("name").text() == languageName) {
					languageId = $(this).children("id").text();
				}
			});
			newHotel = '<li><a href="" id="' + bookingSys + '"><span id="' + hotelId + '" rel="' + languageId + '">' + hotelName + '</span></a></li>';
			$("ul.hotels").append(newHotel);
		});

		//randomize hotels list		
		$("ul.hotels").jShuffle();

		hotelName = $("ul.hotels li:first a span").text();
		queryHotel = $("ul.hotels li span:first").attr("id");
		if ($("ul.hotels li span:first").attr("rel")) {
			queryLanguage = $("ul.hotels li span:first").attr("rel");
		} else {
			queryLanguage = 0;
		}

		$(".hotel-sel span:first").text(hotelName);

		//bind click with hotel selection
		$("ul.hotels li a").bind("click", function(el){
			var selHotel = $(this).text();
			$(".hotel-sel span:first").text(selHotel);
			queryHotel = $(this).children("span").attr("id");
			bookingSys = $(this).attr("id");
			queryLanguage = $(this).attr("rel");
			if (menu_open == true) {
				$(".dropdown").slideUp(75);
				menu_open = false;
			}
			return false;
		});
		
		$("ul.persons li a").bind("click", function(el){
			selPersons = $(this).text();
			$(".persons-sel span:first").text(selPersons);
			queryPersons = selPersons;
			if (menu_open == true) {
				$(".dropdown").slideUp(75);
				menu_open = false;
			}
			return false;
		});
		
		$("ul.nights li a").bind("click", function(el){
			selNights = $(this).text();
			$(".nights-sel span:first").text(selNights);
			queryNights = selNights;
			if (menu_open == true) {
				$(".dropdown").slideUp(75);
				menu_open = false;
			}
			return false;
		});
	});

	//set today's date to input field
	var currentDate = new Date();
	curr_day_raw = currentDate.getDate();
	curr_month_raw = currentDate.getMonth() + 1;
	curr_year = currentDate.getFullYear();
	curr_day = curr_day_raw + "";
	curr_month = curr_month_raw + "";
	
	if (curr_day.length == 1) {	curr_day = "0" + curr_day;}
	if (curr_month.length == 1) {curr_month = "0" + curr_month;}
	
	day = curr_day;
	month = curr_month;
	year = curr_year;
	
	startDate=month + "/" + day + "/" + year;
	depDate=month + "/" + day + "/" + year;
	$("#date").val(curr_day + "/" + curr_month + "/" + curr_year);
	$("#dateDep").val(curr_day + "/" + curr_month + "/" + curr_year);
	
	$("#date").datepicker({
	     dateFormat: 'dd/mm/yy',
	     minDate: currentDate,
	     showOn: 'button',
	     buttonImage: '/extensions/ext_uniquestay/gfx/calendar.gif', 
	     buttonImageOnly: true,
	     onClose: function(date) { 
		    startDate= date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear();
	     }
	});

	$("#dateDep").datepicker({
	     dateFormat: 'dd/mm/yy',
	     minDate: currentDate,
	     showOn: 'button',
	     buttonImage: '/extensions/ext_uniquestay/gfx/calendar.gif', 
	     buttonImageOnly: true,
	     onClose: function(date) {
		    depDate= (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear();
	     },
	     beforeShow: function(){
		   return{ minDate: $('#date').datepicker("getDate") };
           }
	});

	$(".submit-booking").bind("click", function(el){
		//set default values in case form isn't modified
		queryHotel = (queryHotel) ? queryHotel : $("ul.hotels li:first span").attr("id");
		var sDate=new Date(startDate);
		var lDate=new Date(depDate);
		queryNights = (days_between(sDate,lDate) > 1) ? days_between(sDate,lDate) : 1;
		queryPersons = (queryPersons > 1) ? selPersons : 1;
		queryLanguage = (queryLanguage != 0) ? queryLanguage : 0;

		//core for multiple bookingsystems
		switch(bookingSys) {
		case "0":
			var queryString = "HotelID=" + queryHotel + "&datein=" + startDate + "&Length=" + queryNights + "&Adults=" + queryPersons + "&languageid="+ queryLanguage;
			window.open(queryPath[bookingSys] + "?" + queryString, "Title", "menubar=no,width=1015,height=610,toolbar=no");
			break;
		case "1":
			var queryString = "hotel_id=" + queryHotel;
			window.open(queryPath[bookingSys] + "?" + queryString, "Title", "menubar=no,width=525,height=465,toolbar=no");
			break;
		default:
			break;
		}
		return false;
	});
	

	
      function days_between(date1, date2) {
		 // The number of milliseconds in one day
		 var ONE_DAY = 1000 * 60 * 60 * 24

		 // Convert both dates to milliseconds
		 var date1_ms = date1.getTime()
		 var date2_ms = date2.getTime()

		 // Calculate the difference in milliseconds
		 var difference_ms = Math.abs(date2_ms - date1_ms)
		 
		 // Convert back to days and return
		 return Math.round(difference_ms/ONE_DAY)

	}

}


