$(function()
{
	load();
	
	$("a.close").click(function(){
		window.close();
		return false;
	});
	
	$("a.hotels").click(function(){
		cat = $(this).attr("name");
		if (cat == 'all')
		{
			showAll(this);
		}
		else
		{
			if ($(this).hasClass("selected"))
			{
				hideHotel(cat);
				$(this).removeClass("selected");
				$(this).next("span").removeClass("selected");
				$("a.hotels.all").removeClass("selected");
			}
			else
			{
				showHotel(cat);
				$(this).addClass("selected");
				$(this).next("span").addClass("selected");

			}
		}
		return false;
	});
	
	$("a.attraction").click(function(){
		cat = $(this).attr("name");
		if ($(this).hasClass("selected"))
		{
			hideAttraction(cat);
			$(this).removeClass("selected");
			$(this).next("span").removeClass("selected");
		}
		else
		{
			showAttraction(cat);
			$(this).addClass("selected")
			$(this).next("span").addClass("selected");
		}
		return false;
	});
	
	$("a.region").click(function(){
		region = $(this).attr("name");
		
		if ($(this).hasClass("selected"))
		{
			$("a.region").removeClass("selected");
			hideRegions();
		}
		else
		{
			$("a.region").removeClass("selected");
			hideRegions();
			if (region == 'polygon1')
			{
				showRegion1();
				$(this).addClass("selected");
			}
			if (region == 'polygon2')
			{
				showRegion2();
				$(this).addClass("selected");
			}
			if (region == 'polygon3')
			{
				showRegion3();
				$(this).addClass("selected");
			}
		}
		
		return false;
	});
	
	$(window).unload(function(){
		GUnload();
	});
});

/* Creates a marker at the given point with the given number label*/
function createHotelMarker(point, hotel_name, address, rating, cat_name)
{
	/* Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.*/
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(79, 74);
	//baseIcon.iconSize = new GSize(28, 39);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(13, 20);
	var icon = new GIcon(baseIcon);
	icon.image = "img/hotel_map_icon.png";
	//icon.image = "img/hotel_map_icon_small.png";
	icon.shadow = "img/hotel_map_icon_shadow.png";
	icon.shadowSize = new GSize(79, 74);

	var marker = new GMarker(point,icon);
	marker.mycategory = cat_name;
	
	$("#tip_rating").show();
	$("#tip_rating").attr("src", "img/" + rating);
	$("#tip_title").html(hotel_name);
	$("#tip_text").hide();
	$("#tip_address").html(address);
	marker.tooltip = $("div#tip").html();

	GEvent.addListener(marker,"mouseover", function() {
		showTooltip(marker);
	});        
	GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden"
	});
	hotel_markers.push(marker);
	return marker;
}

function createMainMarkerAttraction(point, attraction_name, attraction_text, address, cat_name)
{
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(79, 74);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(13, 20);
	var icon = new GIcon(baseIcon);
	icon.image = "img/attraction_map_icon.png";
	var marker = new GMarker(point,icon);
	marker.mycategory = cat_name;
	
	$("#tip_rating").hide();
	$("#tip_title").html(attraction_name);
	$("#tip_text").show();
	$("#tip_text").html(attraction_text);
	$("#tip_address").html(address);
	marker.tooltip = $("div#tip").html();

	GEvent.addListener(marker,"mouseover", function() {
		showTooltip(marker);
	});        
	GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden"
	});
	att_markers.push(marker);
	return marker;
}

function showTooltip(marker)
{
	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x - width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
}

function showHotel(category)
{
	for (var i=0; i<hotel_markers.length; i++)
	{
		if (hotel_markers[i].mycategory == category)
		{
			hotel_markers[i].show();
		}
	}
}

function hideHotel(category)
{
	for (var i=0; i<hotel_markers.length; i++)
	{
		if (hotel_markers[i].mycategory == category)
		{
			hotel_markers[i].hide();
		}
	}
}

function showAttraction(category)
{
	for (var i=0; i<att_markers.length; i++)
	{
		if (att_markers[i].mycategory == category)
		{
			att_markers[i].show();
		}
	}
}

function hideAttraction(category)
{
	for (var i=0; i<att_markers.length; i++)
	{
		if (att_markers[i].mycategory == category)
		{
			att_markers[i].hide();
		}
	}
}
