
function IPoint(lat, long) {
   this.lat = (lat)?lat:0;
   this.long = (long)?long:0;
}

IPoint.prototype.toString = function() {
	return "lat: "+this.lat+",long: "+this.long;
};

IPoint.prototype.setLatLong = function(lat, long) {
	this.lat = lat;
	this.long = long;
};

function IMarker(point, title, description, icon, checkBox){
   this.point = point;
   this.title = (title)?title:"";
   this.description = (description)?description:"";
   this.icon = (icon)?icon:"";
   this.isVisible = false;   
   this.pin = null;
   this.checkBox = (checkBox)?checkBox:null;
}

function initMap(fnc){
	if(getLayer("map")){
		var iMap = new IMap("map");
		gMap = iMap;
		iMap.show();
		iMap.setScaleSystem((gShowMiles)?'imperial':'metric');
		//Need to get list of plottable hotel codes in order to get Geocodes
		var codes = "";
		for(code in gHotelPins){
			if(gHotelPins[code].point.lat == 0 && gHotelPins[code].point.long == 0){
				codes += code + '|';
			}
		}
		if(codes.length > 0){
			httpRequest(function(){
				if(httpResponded()){
					var rspString = getHttpResponseString();
					if(rspString.length > 0){
						var hotelPoints = eval('('+getHttpResponseString()+')');
						for(pointName in hotelPoints){
							var point = hotelPoints[pointName];
							gHotelPins[pointName].point.setLatLong(point.lat, point.long);
						}
					}
				}
			}, gGetLocationCodesURL, true, "codes="+codes);
		}
		iMap.hideControl();
		if(fnc){
		iMap.addEventHandler('ondoubleclick', fnc);
		}
		for(hotelName in gHotelPins){
			var hotel = gHotelPins[hotelName];
			if(hotel.point.lat != 0 && hotel.point.long != 0){
				gMapPoints[hotelName] = hotel;
				iMap.addPin(gMapPoints[hotelName]);
			}
		}
		iMap.setView(gMapPoints);
		for(poi in gPOIs){
			iMap.addPin(gPOIs[poi]);
			if(gNoHotels){iMap.setView(gPOIs);}
		}
		if(iMap.isDefaultZoomLevel()){
			try{
				var lastCity = getCookieData("lastCity");
				if(lastCity != null && trim(lastCity) == trim(gCity)){
				var centre = new IPoint(getCookieData("mapLatitude"),getCookieData("mapLongitude"));
				iMap.setCentreZoom(centre, getCookieData("mapZoomLevel"));
				}
			}catch(e){}
		}else{
			saveSettings(iMap);
		}
	}
}

function toggleHotel(point, resetView){
	try{
		gMapPoints[point].togglePin();
		if(resetView) gMap.setView(gMapPoints);
	}catch(e){}
}

function toggleMappable(point){
	try{
		if(!gMapPoints[point]){
			gMapPoints[point] = gPlottablePins[point];
			gMap.addPin(gMapPoints[point]);
		}else{
			gMapPoints[point].togglePin();
		}
		gMap.setView(gMapPoints);
	}catch(e){}
}

function saveSettings(iMap){
	var centre = iMap.getCentre();
	try{
		setCookie("lastCity",gCity);
	setCookie("mapLatitude",centre.lat);
	setCookie("mapLongitude",centre.long);
	setCookie("mapZoomLevel",iMap.getZoomLevel());
	}catch(e){}
}

function deselectAll(){
	for(hotelName in gHotelPins){
		var hotel = gHotelPins[hotelName];
		if(hotel.checkBox.checked){
			toggleHotel(hotelName,false);
			hotel.checkBox.checked = false;
		}
	}
}

function toggleAll(){
	for(hotelName in gHotelPins){
		toggleHotel(hotelName,false);
		hotel.checkBox.checked = !hotel.checkBox.checked;
	}
}

function refresh() {
	if(gLoaded){
		window.location.replace(gNewPage+getSortFilterParms() + '&mapProvider='+gMapProvider);
		gLoaded=false;
	}
	return false;
}

function doSort(sortSeq) {
	var sort;
	switch (sortSeq) {
		case "PR":
			sort = (gSort == gSortPRICEASC) ? gSortPRICEDESC : gSortPRICEASC;
			break;
		case "NM":
			sort = (gSort == gSortNAMEASC) ? gSortNAMEDESC : gSortNAMEASC;
			break;
		case "SR":
			sort = (gSort == gSortSTARASC) ? gSortSTARDESC : gSortSTARASC;
			break;
		case "TP":
			sort = gSortTOPPICKS;
			break;
		case "SO":
			sort = gSortSPECIALOFFERS;
			break;
	}
	document.subset.sort.value = sort;
	gSort = sort;
	gSorted = true;
	return refresh();
}


