﻿if (language == "ua"){
	language = "ru";
}
var NameLang = language != "ru" ? "NameEn" : "NameRu";

var allAjax = [];

var BigCityHotelsCount = 10;
var MiddleCityHotelsCount = 5;

var objForm = null;
var objFieldsetCity = null;

var AllPoW = new MyList();
var Cities = new MyList();
var Airports = new MyList();
var Hotels = new MyList();
var HotelSearchCookie = null;
var HotelSearchDateCookie = null;

var map = null;

function MyList() {
	this.Hash = {};
	this.Arr = [];
}
MyList.prototype.Add = function(Obj) {
	this.Hash[Obj.Id] = Obj;
	this.Arr.push(Obj);
};
MyList.prototype.Get = function(Param) {
	if (typeof Param == "number") {
		return this.Arr[Param];
	}
	return this.Hash[Param];
};
MyList.prototype.GetLength = function() {
	return this.Arr.length;
};

$(function() {
	$(window).ajaxSend(function(evt, request, settings) {
		allAjax.push(request);
	});
	
	var __HotelSearchCookie = ReadCookie("HotelSearchCookie");
	if (__HotelSearchCookie != null) HotelSearchCookie = unescape(__HotelSearchCookie);
	var __HotelSearchDateCookie = ReadCookie("HotelSearchDateCookie");
	if (__HotelSearchDateCookie != null) HotelSearchDateCookie = unescape(__HotelSearchDateCookie);
	
	objForm = new Form('AviaSearchForm');
	var CheckInCalendar = new Calendar();
	
	if (HotelSearchCookie != null) {
		var Place = JSON.parse(HotelSearchCookie);
		objFieldsetCity.Fill(Place);
		InitMap(Place);
	} else {
		InitMap();
	}
});

function Form(FormId) {
	var self = this;
	this.Id = FormId;
	this.elForm = document.getElementById(this.Id);
	
	this.CheckInDateInput = this.elForm.ArriveDate;
	this.CheckOutDateInput = this.elForm.DepartureDate;
	this.CheckInDateInput.value = "";
	this.CheckOutDateInput.value = "";
	
	if (HotelSearchDateCookie != null) {
		var objHotelSearchDateCookie = JSON.parse(HotelSearchDateCookie);
		var CheckInDate = Date.parseAWADString(objHotelSearchDateCookie.CheckInDate);
		if (CheckInDate.cutTime() >= (new Date()).cutTime()) {
			this.CheckInDateInput.value = objHotelSearchDateCookie.CheckInDate;
			this.CheckOutDateInput.value = objHotelSearchDateCookie.CheckOutDate;
		}
	}
	
	objFieldsetCity = new FieldsetCity();
	
	this.elForm.onsubmit = function() {
		self.Check();
		return false;
	};
}
Form.prototype.Check = function() {
	this.Error = false;
	this.Select = [];
	this.Alert = [];
	
	this.Check_City();
	this.Check_Date(this.CheckInDateInput);
	
	if (this.Error == false) {
		AjaxAbort(allAjax);
		this.CreateCookie();
		this.Submit();
	} else {
		if (this.Alert[0] != '') {
			new AlertMessage(this.Alert[0]);
		}
		if (this.Select[0] instanceof Object) {
			this.Select[0].select();
		}
	}
};
Form.prototype.Check_Date = function() {
	if (this.CheckInDateInput.value == '') {
		this.Error = true;
		this.Select.push(this.CheckInDateInput);
		this.Alert.push(MO_str_ChooseArrivelDate);
	}
	if (this.CheckOutDateInput.value == '') {
		this.Error = true;
		this.Select.push(this.CheckOutDateInput);
		this.Alert.push(MO_str_ChooseDepartureDate);
	}
};
Form.prototype.Check_City = function() {
	if (objFieldsetCity.Input.value == '') {
		this.Error = true;
		this.Select.push(objFieldsetCity.Input);
		this.Alert.push(MO_str_ChooseDestinationCity);
	} else if (objFieldsetCity.Input.value != '' && objFieldsetCity.Input_CityCode.value == '') {
		this.Error = true;
		this.Select.push(objFieldsetCity.Input);
		this.Alert.push(MO_str_ChooseDestinationCity);
	}
};
Form.prototype.CreateCookie = function() {
	var DateCookie = {
		CheckInDate: this.CheckInDateInput.value,
		CheckOutDate: this.CheckOutDateInput.value
	};
	CreateCookie("HotelSearchDateCookie", JSON.stringify(DateCookie));
};
Form.prototype.Submit = function() {
	var self = this;
	$.ajax({
		cache: false,
		dataType: "xml",
		type: self.elForm.method,
		url: self.elForm.action,
		data: $(self.elForm).serialize(),
		success: function(Response) {
			var Status = Response.firstChild.getAttribute("Status");
			if (Status == "OK") {
				var href = language != "ru" ? "/" + language : "";
				href += "/hotels/searching/?RequestId=" + Response.firstChild.getAttribute("Id");
				location.href = href;
			} else if (Status == "Error") {
				alert("Ошибка!");
			} else {
				setTimeout(function() {
					self.Submit();
				}, 2000);
			}
		},
		error: function() {
			alert(MO_str_ajax_noResponse);
		}
	});
};

function FieldsetCity() {
	var self = this;
	
	this.Fieldset = document.getElementById("Fieldset_City");
	this.Td = this.Fieldset.parentNode;
	this.Input = document.getElementById("CityName_input");
	this.Input_CityCode = document.getElementById("City_input");
	this.Input_Country = document.getElementById("Country_input");
	this.Input_CountryName = document.getElementById("CountryName_input");
	this.Input_State = document.getElementById("State_input");
	this.Input_PoW = document.getElementById("PoW_input");
	this.Input_Lat = document.getElementById("Lat_input");
	this.Input_Lon = document.getElementById("Lon_input");
	this.DetailValue = $("label.DetailValue", this.Fieldset)[0];
	this.CitiesTable = document.getElementById("CitiesTable");
	this.objList = null;
	this.OnLinkClickLink = function() {
		self.OnLinkClick(this);
	};
	this.Init();
}
FieldsetCity.prototype.Init = function() {
	var self = this;
	this.Input.Field = new FieldClass(this.Input);
	this.Input.Suggest = new Suggest(this.Input);
};
FieldsetCity.prototype.Fill = function(Place) {
	this.DropCityParams();
	if (Place != null) {
		this.Input.Suggest.LastValue = Place.Name;
		this.Input.value = Place.Name;
		this.Input_CityCode.value = Place.CityCode;
		this.Input_Country.value = Place.CountryId;
		this.Input_CountryName.value = Place.Country;
		this.Input_State.value = Place.StateProvince;
		this.Input_PoW.value = Place.PoW;
		this.Input_Lat.value = Place.Latitude;
		this.Input_Lon.value = Place.Longitude;
		this.Input.Field.Update();
		var DetailValue = "";
		if (Place.StateProvince != "") DetailValue += Place.StateProvince + ", ";
		// hide Asia for RU region
		if (Place.CountryId.toLowerCase()=="ru") {
			DetailValue += Place.Country;
		} else {
			DetailValue += Place.Country + ", " + Place.PoW;
		}
		this.DetailValue.appendChild(document.createTextNode(DetailValue));
		$(this.DetailValue).removeClass("invisible");
		CreateCookie("HotelSearchCookie", JSON.stringify(Place));
	}
};
FieldsetCity.prototype.DropCityParams = function() {
	this.DetailValue.innerHTML = "";
	$(this.DetailValue).addClass("invisible");
	this.Input_CityCode.value = "";
	this.Input_Country.value = "";
	this.Input_CountryName.value = "";
	this.Input_State.value = "";
	this.Input_PoW.value = "";
	this.Input_Lat.value = "";
	this.Input_Lon.value = "";
};
FieldsetCity.prototype.OnLinkClick = function(Link) {
	var CurrentPlaceId = $(Link).attr("ID");
	var CurrentPlace = this.objList.Get(CurrentPlaceId);
	if(map != null) map.ChangeView(CurrentPlace);
};
FieldsetCity.prototype.ClearTable = function() {
	while (this.CitiesTable.rows.length > 0) {
		this.CitiesTable.deleteRow(0);
	}
};
FieldsetCity.prototype.DrawTable = function(objList) {
	var self = this;
	this.objList = objList;
	this.ClearTable();
	var Row = this.CitiesTable.insertRow(0);
	var l = this.objList.GetLength() < 18 ? this.objList.GetLength() : 18;
	for (var c = 0; c < 3; c++) {
		var Cell = Row.insertCell(c);
		for (var g = c * Math.ceil(l / 3); g < c * Math.ceil(l / 3) + Math.ceil(l / 3); g++) {
			var CurrentPlace = this.objList.Arr[g];
			if (CurrentPlace != null) {
				var Div = document.createElement("div");
				var Link = document.createElement("span");
				Link.className = "link_psevdo";
				Link.appendChild(document.createTextNode(CurrentPlace.Name));
				Link.setAttribute("ID", CurrentPlace.Id);
				$(Link).click(self.OnLinkClickLink);
				
				Div.appendChild(Link);
				Cell.appendChild(Div);
			}
		}
	}
};

function Suggest(Input) {
	var self = this;
	this.URL = "/_references/GeoSuggest/";
	this.LastRequestNumber = 0;
	this.Input = Input;
	this.inFocus = false;
	this.entElement = document.createElement("div");
	this.entElement.className = "Suggest";
	this.LastValue = '';
	this.isVisible = false;
	this.Results = null;
	this.ResultId = null;
	
	this.OnKeyDownLink = function(event) {
		self.OnKeyDown(event);
	};
	$(this.Input).focus(function(event) {
		self.OnFocus(event);
	});
	$(this.Input).blur(function(event) {
		self.OnBlur(event);
	});
	$(this.Input).keyup(function(event) {
		self.OnKeyUp(event);
	});
	$(this.Input).change(function(event) {
		self.OnChange(event);
	});
}
Suggest.prototype.OnFocus = function(event) {
	this.inFocus = true;
};
Suggest.prototype.OnBlur = function(event) {
	var self = this;
	self.inFocus = false;
	setTimeout(function() {
		if (self.isVisible) {
			if (self.Results != null) {
				self.SetCurrentResult();
			}
			self.Hide();
		}
	}, 100);
	if (this.LastValue != this.Input.value) {
		objFieldsetCity.DropCityParams();
	}
};
Suggest.prototype.OnKeyDown = function(event){
	switch (event.keyCode) {
		case 27: // <Esc>
			this.Hide();
			break;
		case 38: // <Up>
			this.SetPrevRowHover();
			break;
		case 40: // <Down>
			this.SetNextRowHover();
			break;
		case 13: // <Enter>
			event.preventDefault();
			if (this.Results!=null) this.SetCurrentResult();
			this.Hide();
			break;
	}
};
Suggest.prototype.OnKeyUp = function(event){
	if(this.LastValue != this.Input.value){
		this.OnChange(event);
	}
};
Suggest.prototype.OnChange = function(event) {
	var self = this;
	if (this.LastValue != this.Input.value) {
		objFieldsetCity.DropCityParams();
		this.Hide();
		this.LastValue = this.Input.value;
		var QueryString = this.LastValue;
		if (this.Input.value.length > 2) {
			setTimeout(function() {
				self.MakeSuggest(QueryString);
			}, 350);
		}
	}
};
Suggest.prototype.SetRowHover = function(Row) {
	if (this.SelectedRow) {
		this.DropRowHover(this.SelectedRow);
	}
	$(Row).addClass("selected");
	this.SelectedRow = Row;
};
Suggest.prototype.DropRowHover = function(Row) {
	$(Row).removeClass("selected");
};
Suggest.prototype.SetPrevRowHover = function() {
	if (this.SelectedRow != null) {
		var PrewRow = this.SelectedRow.previousSibling;
		if (PrewRow) {
			this.DropRowHover(this.SelectedRow);
			this.SetRowHover(PrewRow);
			this.SelectedRow = PrewRow;
		}
	}
};
Suggest.prototype.SetNextRowHover = function() {
	if (this.SelectedRow != null) {
		var NextRow = this.SelectedRow.nextSibling;
		if (NextRow) {
			this.DropRowHover(this.SelectedRow);
			this.SetRowHover(NextRow);
			this.SelectedRow = NextRow;
		}
	}
};
Suggest.prototype.SetCurrentResult = function() {
	if (this.SelectedRow != null) {
		this.ResultId = $(this.SelectedRow).attr("ID");
		var Result = this.Results.Get(this.ResultId);
		objFieldsetCity.Fill(Result);
		if (map != null) map.ChangeView(Result);
	}
};
Suggest.prototype.MakeSuggest = function(QueryString, Try) {
	if (QueryString == this.Input.value) {
		var self = this;
		var RequestNumber = ++this.LastRequestNumber;
		$.ajax({
			cache: false,
			dataType: "xml",
			type: "GET",
			url: self.URL,
			data: {
				filter: QueryString,
				language: language,
				zoom: 9
			},
			beforeSend: function() {
				$("label.wait", self.Input.parentNode).removeClass("invisible");
			},
			success: function(Response) {
				$("label.wait", self.Input.parentNode).addClass("invisible");
				if ((RequestNumber == self.LastRequestNumber) && (QueryString == self.Input.value)) {
					self.ApplySuggest(Response);
				}
			},
			error: function() {
				if (RequestNumber == self.LastRequestNumber) {
					Try = Try || 0;
					if (Try < 3) {
						Try += 1;
						setTimeout(function() {
							self.MakeSuggest(QueryString, Try);
						}, 350);
						return;
					}
					$("label.wait", self.Input.parentNode).addClass("invisible");
					alert(MO_str_ajax_noResponse);
				}
			}
		});
	}
};
Suggest.prototype.ApplySuggest = function(Response) {
	this.ParseResponse(Response);
	this.Draw("ascending");
	this.Draw("descending");
	if (this.Results == null || this.inFocus == true && this.Results.GetLength() != 1) {
		this.Show();
	} else {
		this.SetCurrentResult();
	}
};
Suggest.prototype.ParseResponse = function(Response) {
	var ResponseItems = Response.getElementsByTagName("City");
	if (ResponseItems.length > 0) {
		this.Results = new MyList();
		for (var i = 0, length = ResponseItems.length; i < length; i++) {
			var CurrentItem = ResponseItems[i];
			
			var Place = {
				Type: CurrentItem.tagName,
				Name: CurrentItem.getAttribute("Name"),
				Id: CurrentItem.getAttribute("Id"),
				Latitude: CurrentItem.getAttribute("Lat"),
				Longitude: CurrentItem.getAttribute("Lon")
			};
			if (Place.Type == "City") {
				Place.PoW = CurrentItem.parentNode.parentNode.getAttribute("Name");
				Place.Country = CurrentItem.parentNode.getAttribute("Name");
				Place.CountryId = CurrentItem.parentNode.getAttribute("Id");
				Place.StateProvince = CurrentItem.getAttribute("StateProvince");
				Place.CityCode = CurrentItem.getAttribute("BookingComCode");
				Place.HotelsCount = CurrentItem.getAttribute("HotelsCount");
			}
			
			this.Results.Add(Place);
		}
	} else {
		this.Results = null;
	}
};
Suggest.prototype.Draw = function(Sort) {
	var self = this;
	var Table = document.createElement("table");
	if (this.Results != null) {
		for (var i = 0, length = this.Results.GetLength(); i < length; i++) {
			if (Sort == "ascending") var Item = this.Results.Arr[i];
			else var Item = this.Results.Arr[length - i - 1];
			
			var ResponseRow = Table.insertRow(i);
			ResponseRow.setAttribute("ID", Item.Id);
			ResponseRow.onmouseover = function() {
				self.SetRowHover(this);
			};
			ResponseRow.onmouseout = function() {
				self.DropRowHover(this);
			};
			ResponseRow.onclick = function() {
				self.SetCurrentResult();
				self.Hide();
			};
			
			var QueryString = this.Input.value;
			var re = new RegExp(QueryString, 'ig');
			var Text = Item.Name;
			if ($.browser.opera) {
				var MatchStr = re.exec(Text);
				Text = Text.replace(re, function() {
					return '<span class="match">' + MatchStr + '</span>';
				});
			} else {
				Text = Text.replace(re, function() {
					return '<span class="match">' + re.exec(Text) + '</span>';
				});
			}
			if (Item.StateProvince != "") {
				Text += ", " + Item.StateProvince;
			}
			// hide Asia for RU region
			if (Item.CountryId.toLowerCase()=="ru") {
				Text += ", " + Item.Country;
			} else {
				Text += ", " + Item.Country + ", " + Item.PoW;
			}
			var ResponseCell = ResponseRow.insertCell(0);
			ResponseCell.innerHTML = Text;
		}
		Table.onmouseout = function() {
			self.SetRowHover(self.SelectedRow);
		};
	} else {
		self.noResults = true;
		var NoResultRow = Table.insertRow(i);
		var NoResultCell = NoResultRow.insertCell(0);
		NoResultCell.innerHTML = MO_str_SuggestNoResults;
	}
	
	if (Sort == "ascending") {
		this.entElement.innerHTML = "";
		this.ResultTable = Table;
		this.SetRowHover(this.ResultTable.firstChild.firstChild);
		this.entElement.appendChild(this.ResultTable);
	} else {
		this.ResultTableDesc = Table;
	}
};
Suggest.prototype.Show = function() {
	var self = this;
	document.body.appendChild(this.entElement);
	
	if (($(this.Input).offset().top - document.documentElement.scrollTop + this.Input.offsetHeight + this.entElement.offsetHeight) > document.documentElement.clientHeight) {
		$(this.entElement).removeClass("Down");
		$(this.entElement).addClass("Up");
		this.entElement.style.top = $(this.Input).offset().top - this.Input.scrollTop - this.entElement.offsetHeight + "px";
		
		this.entElement.innerHTML = "";
		this.ResultTable = this.ResultTableDesc;
		this.SetRowHover(this.ResultTable.firstChild.lastChild);
		this.entElement.appendChild(this.ResultTable);
	} else {
		$(this.entElement).removeClass("Up");
		$(this.entElement).addClass("Down");
		this.entElement.style.top = $(this.Input).offset().top - this.Input.scrollTop + this.Input.offsetHeight + "px";
	}
	this.entElement.style.left = ($(this.Input).offset().left - this.Input.scrollLeft) + "px";
	this.entElement.style.visibility = "visible";
	this.isVisible = true;
	
	$(this.Input).bind("keydown", self.OnKeyDownLink);
};
Suggest.prototype.Hide = function() {
	var self = this;
	
	if (this.entElement.parentNode == document.body) {
		$(this.Input).unbind("keydown", self.OnKeyDownLink);
		this.entElement.style.visibility = "hidden";
		document.body.removeChild(this.entElement);
		this.isVisible = false;
	}
};

function Calendar() {
	var self = this;
	this.date = (new Date()).cutTime();
	this.today = (new Date()).cutTime();
	this.elPeriodLegend = document.getElementById("PeriodLegend");
	this.CheckInDate = objForm.CheckInDateInput.value != "" ? Date.parseAWADString(objForm.CheckInDateInput.value) : null;
	this.CheckOutDate = objForm.CheckOutDateInput.value != "" ? Date.parseAWADString(objForm.CheckOutDateInput.value) : null;
	
	this.monthsLength = 1;
	this.CreateStructure();
	this.Draw();
}
Calendar.prototype.CreateStructure = function() {
	var self = this;
	
	this.rootElement = document.createElement("div");
	this.rootElement.className = "Calendar";
	this.Table = [];
	
	var TableContainer = document.createElement("table");
	TableContainer.className = "TableContainer";
	var ContainerRow = TableContainer.insertRow(0);
	this.rootElement.appendChild(TableContainer);
	
	for (var i = 0; i <= this.monthsLength; i++) {
		var ContainerRowCell = ContainerRow.insertCell(i);
		ContainerRowCell.className = "TdContainer";
		if (i == 0) ContainerRowCell.className += " first-child";
		
		this.Table[i] = document.createElement("table");
		this.Table[i].className = "Calendar";
		this.Table[i].insertRow(0);
		var THead = this.Table[i].createTHead(0);
		var MonthRow = THead.insertRow(0);
		var MonthArLeft = MonthRow.insertCell(0);
		var MonthArLeftSpan = document.createElement("span");
		MonthArLeftSpan.className = "MonthArLeft";
		MonthArLeftSpan.onclick = function() {
			self.PrevMonth();
		};
		MonthArLeft.appendChild(MonthArLeftSpan);
		var MonthCell = MonthRow.insertCell(1);
		MonthCell.colSpan = 5;
		MonthCell.className = "MonthCell";
		var MonthArRight = MonthRow.insertCell(2);
		var MonthArRightSpan = document.createElement("span");
		MonthArRightSpan.className = "MonthArRight";
		MonthArRightSpan.onclick = function() {
			self.NextMonth();
		};
		MonthArRight.appendChild(MonthArRightSpan);
		var DlabelRow = THead.insertRow(1);
		DlabelRow.className = "DlabelRow";
		for (var wd = 0; wd < 7; wd++) {
			var DayLabelCell = DlabelRow.insertCell(wd);
			DayLabelCell.appendChild(document.createTextNode(this.GetDayName(wd)));
		}
		ContainerRowCell.appendChild(this.Table[i]);
	}
	
	document.getElementById("CalendarContainer").appendChild(this.rootElement);
};
Calendar.prototype.GetDayName = function(k) {
	var q = k + texts_calendar.weekstart;
	if (q > 6) q = q - 7;
	return texts_calendar.daysSNames[q];
};
Calendar.prototype.Draw = function() {
	this.DrawPeriodLegend();
	for (var i = 0; i <= this.monthsLength; i++) {
		this.DrawMonth(i);
		this.DrawDays(i);
	}
};
Calendar.prototype.DrawPeriodLegend = function() {
	this.elPeriodLegend.innerHTML = "";
	if (this.CheckInDate != null) {
		this.elPeriodLegend.innerHTML = " " + MO_cmnStr_From + " " + this.CheckInDate.getDate();
		if (this.CheckOutDate == null || this.CheckOutDate != null && this.CheckInDate.getMonth().valueOf() != this.CheckOutDate.getMonth().valueOf()) {
			this.elPeriodLegend.innerHTML += " " + texts_calendar.monthesDNames[this.CheckInDate.getMonth()];
		}
		if (this.CheckOutDate != null) {
			this.elPeriodLegend.innerHTML += " " + MO_cmnStr_To + " " + this.CheckOutDate.getDate() + " " + texts_calendar.monthesDNames[this.CheckOutDate.getMonth()];
		}
	}
};
Calendar.prototype.DrawMonth = function(n) {
	var self = this;
	var tempDate = new Date(this.date);
	tempDate.setMonth(tempDate.getMonth() + n);
	
	if (n == 0 && tempDate.valueOf() != this.today.valueOf()) {
		this.Table[n].rows[0].cells[0].firstChild.style.visibility = "visible";
	} else {
		this.Table[n].rows[0].cells[0].firstChild.style.visibility = "hidden";
	}
	
	this.Table[n].rows[0].cells[1].innerHTML = "";
	var MonthStr = texts_calendar.monthesFNames[tempDate.getMonth()] + " " + tempDate.getFullYear();
	this.Table[n].rows[0].cells[1].appendChild(document.createTextNode(MonthStr));
	
	if (n == this.monthsLength) {
		this.Table[n].rows[0].cells[2].firstChild.style.visibility = "visible";
	} else {
		this.Table[n].rows[0].cells[2].firstChild.style.visibility = "hidden";
	}
};
Calendar.prototype.NextMonth = function() {
	this.date = new Date(this.date.setMonth(this.date.getMonth() + 1));
	this.Draw();
};
Calendar.prototype.PrevMonth = function() {
	this.date = new Date(this.date.setMonth(this.date.getMonth() - 1));
	this.Draw();
};
Calendar.prototype.DrawDays = function(n) {
	var self = this;
	for (var r = 0; this.Table[n].tBodies[0].rows.length > 0; r++) {
		this.Table[n].tBodies[0].deleteRow(0);
	}
	
	var tempDate = new Date(this.date);
		tempDate.setMonth(tempDate.getMonth() + n);
		tempDate.setDate(1);
	var currentMonth = tempDate.getMonth();
	
	var DayShift = (tempDate.getDay() - texts_calendar.weekstart) % 7;
	if (DayShift < 0) {
		DayShift += 7;
	}
	tempDate.setDate(tempDate.getDate() - DayShift);
	
	for (var weekNumber = 0; weekNumber < 6; weekNumber++) {
		var row = this.Table[n].tBodies[0].insertRow(-1);
		for (var i = 0; i < 7; i++) {
			var cell = row.insertCell(-1);
			if (tempDate.getMonth() == currentMonth) {
				var DateStr = tempDate.getDate();
				if (tempDate < this.today) {
					cell.className += " PastDay";
				}
				if (tempDate > this.today) {
					cell.className += " FutureDay";
					cell.onclick = function(event) {
						self.OnDayClick(event, this);
					};
				}
				if (tempDate.valueOf() == this.today.valueOf()) {
					cell.className += " Today";
					cell.onclick = function(event) {
						self.OnDayClick(event, this);
					};
				}
				cell.appendChild(document.createTextNode(DateStr));
				cell.setAttribute('Date', tempDate.toAWADString());
				
				if (this.CheckInDate != null && this.CheckOutDate != null) {
					if (tempDate >= this.CheckInDate && tempDate <= this.CheckOutDate) {
						cell.className += " selected";
					}
					if (tempDate.valueOf() == this.CheckInDate.valueOf()) {
						cell.className += " CheckInDate";
					}
					if (tempDate.valueOf() == this.CheckOutDate.valueOf()) {
						cell.className += " CheckOutDate";
					}
					
				} else if (this.CheckInDate != null && this.CheckOutDate == null) {
					if (tempDate.valueOf() == this.CheckInDate.valueOf()) {
						cell.className += " selected CheckInDate";
					}
				} else {
				
				}
			} else {
				cell.appendChild(document.createTextNode("\u00A0"));
			}
			tempDate.setDate(tempDate.getDate() + 1);
		}
	}
};
Calendar.prototype.OnDayClick = function(event, cell) {
	var NewDate = Date.parseAWADString(cell.getAttribute("Date"));
	var TempDate = new Date(this.CheckOutDate);
	if (this.CheckInDate == null) {
		this.CheckInDate = NewDate;
		objForm.CheckInDateInput.value = cell.getAttribute("Date");
		this.CheckOutDate = null;
		objForm.CheckOutDateInput.value = "";
	} else if (this.CheckOutDate == null) {
		if (NewDate > this.CheckInDate) {
			this.CheckOutDate = NewDate;
			objForm.CheckOutDateInput.value = cell.getAttribute("Date");
		} else {
			this.CheckInDate = NewDate;
			objForm.CheckInDateInput.value = cell.getAttribute("Date");
		}
	} else if (TempDate.setDate(TempDate.getDate() + 1).valueOf() == NewDate.valueOf()) {
		this.CheckOutDate = NewDate;
		objForm.CheckOutDateInput.value = cell.getAttribute("Date");
	} else {
		this.CheckInDate = NewDate;
		objForm.CheckInDateInput.value = cell.getAttribute("Date");
		this.CheckOutDate = null;
		objForm.CheckOutDateInput.value = "";
	}
	this.Draw();
};
function InitMap(Place) {
	/*var mapOptions = {
		zoom: 3,
		center: new google.maps.LatLng(48, 0),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		scrollwheel: false,
		disableDefaultUI: true
	};
	map = new google.maps.Map(document.getElementById("map"), mapOptions);*/
	if (GBrowserIsCompatible()) {
		map = new google.maps.Map2(document.getElementById("map"));
		if (Place != null) {
			map.ChangeView(Place);
		} else {
			var Zoom = 3;
			var Center = new GLatLng(48, 0);
			map.setCenter(Center, Zoom);
		}
		map.BuildMap();
	}
}
google.maps.Map2.prototype.BuildMap = function() {
	var self = this;
	
	var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(23,70));
	this.addControl(new GLargeMapControl3D(), topLeft);
	
	this.SetCitiesLayer();
	this.SetAirportsLayer();
	this.SetHotelsLayer();
	this.PanoramioLayer = new GLayer("com.panoramio.all");
	this.CreateSwitchControls();
	
	this.GetGeoObjects();
	
	GEvent.addListener(this, 'zoomend', function() {
		self.OnZoomEnd();
	});
	GEvent.addListener(this, 'moveend', function() {
		self.OnEndChangeView();
	});
};
google.maps.Map2.prototype.ChangeView = function(Place) {
	var Zoom = 0;
	if (Place != null) {
		if (Place.Type == "PoW") {
			Zoom = 6;
		} else if (Place.Type == "Country") {
			Zoom = 9;
		} else if (Place.Type == "City") {
			objFieldsetCity.Fill(Place);
			Zoom = 15;
		}
		var Center = new GLatLng(Place.Latitude, Place.Longitude);
		this.setCenter(Center, Zoom);
	}
};
google.maps.Map2.prototype.OnZoomEnd = function() {
	var ZoomLevel = this.getZoom();
	if (ZoomLevel < 14) {
		this.HideSwitchControls();
	} else {
		this.ShowSwitchControls();
	}
};
google.maps.Map2.prototype.OnEndChangeView = function() {
	var self = this;
	var Zoom = this.getZoom();
	objFieldsetCity.ClearTable();
	if (Zoom > 4 || AllPoW.GetLength() === 0) {
		this.GetGeoObjects();
	} else {
		objFieldsetCity.DrawTable(AllPoW);
	}
	if (Zoom >= 8) {
		this.GetAirports();
	}
};
google.maps.Map2.prototype.SetCitiesLayer = function(){
	this.CityIcon = new GIcon(G_DEFAULT_ICON, "/images/map/pin-city.png");
	this.CityIcon.shadow = "";
	this.CityIcon.iconSize = GSize(26, 27);
	this.CitiesLayer = new GMarkerManager(this);
};
google.maps.Map2.prototype.GetGeoObjects = function() {
	var self = this;
	var Zoom = this.getZoom();
	var Center = this.getCenter();
	var HotelsCount = 0;
	if (Zoom >= 8 && Zoom <= 9) {
		HotelsCount = BigCityHotelsCount;
	} else if (Zoom >= 10 && Zoom <= 11) {
		HotelsCount = MiddleCityHotelsCount;
	}
	$.ajax({
		cache: false,
		dataType: "xml",
		type: "GET",
		url: "/_references/GeoSuggest/",
		data: {
			language: language,
			zoom: Zoom,
			lat: Center.lat(),
			lon: Center.lng(),
			hotels: HotelsCount
		},
		success: function(Response) {
			var CurrentZoomLevel = self.getZoom();
			if (CurrentZoomLevel < 5) {
				PoWList = Response.getElementsByTagName("PoW");
				if (PoWList.length > 0) self.CreateObjList(PoWList);
			} else if (CurrentZoomLevel < 8) {
				CountryList = Response.getElementsByTagName("Country");
				if (CountryList.length > 0) self.CreateObjList(CountryList);
			} else {
				CityList = Response.getElementsByTagName("City");
				if (CityList.length > 0) {
					self.CreateObjList(CityList);
					if (CurrentZoomLevel >= 14) {
						for (var i = 0, length = CityList.length; i < length; i++) {
							var CurrentItem = CityList[i];
							var CurrentItemId = CurrentItem.getAttribute("Id");
							var CurrentCity = Cities.Get(CurrentItemId);
							if (CurrentCity.Hotels == null) {
								self.GetHotels(CurrentCity);
							}
						}
					}
				}
			}
		},
		error: function() {
			//alert(MO_str_ajax_noResponse);
		}
	});
};
google.maps.Map2.prototype.CreateObjList = function(ItemsList) {
	var objList = new MyList();
	var Type = ItemsList[0].tagName;
	var NewCitiesMarkers = 0;
	for (var i = 0, length = ItemsList.length; i < length; i++) {
		var CurrentItem = ItemsList[i];
		var Place = {
			Type: Type,
			Name: CurrentItem.getAttribute("Name"),
			Id: CurrentItem.getAttribute("Id"),
			Latitude: CurrentItem.getAttribute("Lat"),
			Longitude: CurrentItem.getAttribute("Lon")
		};
		if (Place.Type == "PoW") {
			objList.Add(Place);
			AllPoW = objList;
		} else if (Place.Type == "Country") {
			Place.PoW = CurrentItem.parentNode.getAttribute("Name");
			objList.Add(Place);
		} else if (Place.Type == "City") {
			Place.PoW = CurrentItem.parentNode.parentNode.getAttribute("Name");
			Place.Country = CurrentItem.parentNode.getAttribute("Name");
			Place.CountryId = CurrentItem.parentNode.getAttribute("Id");
			Place.StateProvince = CurrentItem.getAttribute("StateProvince");
			Place.CityCode = CurrentItem.getAttribute("BookingComCode");
			Place.HotelsCount = CurrentItem.getAttribute("HotelsCount");
			objList.Add(Place);
			
			if (Cities.Hash[Place.Id] == null) {
				Cities.Add(Place);
				this.SetCity(Place);
				NewCitiesMarkers++;
			}
		}
	}
	if(NewCitiesMarkers >= 0){
		this.CitiesLayer.refresh();
	}
	if (this.getZoom() < 14) {
		objFieldsetCity.DrawTable(objList);
	}
};
google.maps.Map2.prototype.SetCity = function(Place) {
	var self = this;
	var Point = new GLatLng(Place.Latitude, Place.Longitude);
	var MarkerOptions = {
		icon: this.CityIcon,
		title: Place.Name
	};
	var Marker = new GMarker(Point, MarkerOptions);
	Marker.Place = Place;
	GEvent.addListener(Marker, "dblclick", function() {
		self.ChangeView(this.Place);
	});
	GEvent.addListener(Marker, "click", function() {
		this.openInfoWindowHtml("<div>" + MO_str_PinCity_City + "</div><div style='margin-bottom: 0.5em;'><b>" + this.Place.Name + "</b></div><div>" + MO_str_PinCity_Hotels + ": " + this.Place.HotelsCount + "</div>");
	});
	var MinZoom = 12;
	if (Place.HotelsCount >= BigCityHotelsCount) {
		MinZoom = 8;
	} else if(Place.HotelsCount >= MiddleCityHotelsCount){
		MinZoom = 10;
	}
	this.CitiesLayer.addMarker(Marker, MinZoom, 14);
};
google.maps.Map2.prototype.SetAirportsLayer = function(){
	this.AirportIcon = new GIcon(G_DEFAULT_ICON, "/images/map/pin-airport.png");
	this.AirportIcon.shadow = "";
	this.AirportIcon.iconSize = GSize(26, 27);
	this.AirportsLayer = new GMarkerManager(this);
};
google.maps.Map2.prototype.GetAirports = function() {
	var self = this;
	var Center = this.getCenter();
	$.ajax({
		cache: false,
		dataType: "xml",
		type: "GET",
		url: "/_references/AirportFinder/",
		data: {
			Radius: 200,
			Latitude: Center.lat(),
			Longitude: Center.lng()
		},
		success: function(Response) {
			var AirportsList = Response.getElementsByTagName("Airport");
			var NewAirports = 0;
			for (var i = 0, length = AirportsList.length; i < length; i++) {
				var CurrentAirport = AirportsList[i];
				var objAirport = {
					Name: CurrentAirport.getElementsByTagName("Name")[0].getElementsByTagName(NameLang)[0].firstChild.data,
					Id: CurrentAirport.getElementsByTagName("Code")[0].firstChild.data,
					Latitude: CurrentAirport.getElementsByTagName("Latitude")[0].firstChild.data,
					Longitude: CurrentAirport.getElementsByTagName("Longitude")[0].firstChild.data,
					City: CurrentAirport.parentNode.getAttribute(NameLang),
					Country: CurrentAirport.parentNode.parentNode.getAttribute(NameLang)
				};
				if (Airports.Hash[objAirport.Id] == null) {
					Airports.Add(objAirport);
					self.SetAirport(objAirport);
				}
			}
			if (NewAirports > 0){
				this.AirportsLayer.refresh();
			}
		},
		error: function() {
			//alert(MO_str_ajax_noResponse);
		}
	});
};
google.maps.Map2.prototype.SetAirport = function(Airport) {
	var Point = new GLatLng(Airport.Latitude, Airport.Longitude);
	var MarkerOptions = {
		icon: this.AirportIcon,
		title: Airport.Name
	};
	var Marker = new GMarker(Point, MarkerOptions);
	GEvent.addListener(Marker, "click", function() {
		this.openInfoWindowHtml("<div>" + MO_str_PinAirport + "</div><div><b>" + Airport.Name + ",</b> " + Airport.City + ", " + Airport.Country + "</div>");
	});
	this.CitiesLayer.addMarker(Marker, 8);
};
google.maps.Map2.prototype.SetHotelsLayer = function(){
	this.HotelIcon = new GIcon(G_DEFAULT_ICON, "/images/map/pin-hotel-small.png");
	this.HotelIcon.shadow = "";
	this.HotelIcon.iconSize = GSize(17, 20);
	this.HotelsLayer = new MarkerManager(this);
	this.HotelsLayer.Visible = true;
	this.HotelsLayer.Markers = [];
	this.HotelsLayer.show = function(){
		this.Visible = true;
		for(var i in this.Markers){
			this.addMarker(this.Markers[i], 14);
		}
	};
};
google.maps.Map2.prototype.GetHotels = function(City) {
	var self = this;
	$.ajax({
		cache: false,
		dataType: "xml",
		type: "GET",
		url: "/hotels/getbycity/",
		data: {
			CityId: City.Id,
			language: language
		},
		success: function(Response) {
			City.Hotels = [];
			var HotelsList = Response.getElementsByTagName("H");
			for (var i = 0, length = HotelsList.length; i < length; i++) {
				var CurrentHotel = HotelsList[i];
				var objHotel = {
					Name: CurrentHotel.getAttribute("N"),
					Id: CurrentHotel.getAttribute("Id"),
					Latitude: CurrentHotel.getAttribute("Lat"),
					Longitude: CurrentHotel.getAttribute("Lon"),
					Stars: CurrentHotel.getAttribute("S"),
					PicUrl: CurrentHotel.getAttribute("PicUrl")
				};
				City.Hotels.push(objHotel);
			}
			self.SetHotels(City);
		},
		error: function() {
			//alert(MO_str_ajax_noResponse);
		}
	});
};
google.maps.Map2.prototype.SetHotels = function(City) {
	for(var h in City.Hotels){
		this.SetHotel(City.Hotels[h]);
	}
};
google.maps.Map2.prototype.SetHotel = function(Hotel) {
	var Point = new GLatLng(Hotel.Latitude, Hotel.Longitude);
	var MarkerOptions = {
		icon: this.HotelIcon,
		title: Hotel.Name
	};
	var Marker = new GMarker(Point, MarkerOptions);
	var imgHTML = '';
	if(Hotel.PicUrl != null){
		imgHTML = '<div><img src="' + Hotel.PicUrl + '" style="height:90px;width:90px;" height="90" width="90" alt=""/></div>';
	}
	GEvent.addListener(Marker, "click", function() {
		this.openInfoWindowHtml('<div><span class="icoStarsSmall icoStars' + Hotel.Stars + '"></span></div><div><b>' + Hotel.Name + '</b></div>' + imgHTML);
	});
	this.HotelsLayer.Markers.push(Marker);
	if (this.HotelsLayer.Visible === true) {
		this.HotelsLayer.addMarker(Marker, 14);
	}
};
google.maps.Map2.prototype.CreateSwitchControls = function() {
	var self = this;
	this.SwitchControls = document.getElementById("MapControls2");
	this.HotelsSwitch = document.getElementById("HotelsSwitch");
	this.PanoramioSwitch = document.getElementById("PanoramioSwitch");
	
	this.HotelsSwitch.onclick = function() {
		self.ToggleHotelsLayer();
	};
	this.PanoramioSwitch.onclick = function() {
		self.TogglePanoramioLayer();
	};
	this.OnZoomEnd();
};
google.maps.Map2.prototype.HideSwitchControls = function() {
	this.removeOverlay(this.PanoramioLayer);
	$(this.SwitchControls).addClass("invisible");
};
google.maps.Map2.prototype.ShowSwitchControls = function() {
	$(this.SwitchControls).removeClass("invisible");
	this.ToggleHotelsLayer();
	this.TogglePanoramioLayer();
};
google.maps.Map2.prototype.ToggleHotelsLayer = function() {
	if (this.HotelsSwitch.checked == true) {
		this.HotelsLayer.show();
	} else {
		this.HotelsLayer.Visible = false;
		this.HotelsLayer.clearMarkers();
	}
};
google.maps.Map2.prototype.TogglePanoramioLayer = function() {
	if (this.PanoramioSwitch.checked == true) {
		this.addOverlay(this.PanoramioLayer);
	} else {
		this.removeOverlay(this.PanoramioLayer);
	}
};