var html_content = [];
var tube_content = [];
var attraction_content = [];
var rail_content = [];
var num;

$(function()
{
	load();
	
	$(".odd.normal, .even.normal").mouseover(function(){
		$(this).addClass("over");
		num = $(this).attr("id").substring(3);
		var images = map.getContainer().getElementsByTagName("img");
		for(var i = 0; i<images.length; i++)
		{
			if (images[i].src.indexOf("img/greenmap" + num + ".gif") > -1)
			{
				images[i].src = "img/greenmap" + num + "_on.gif";
			}
		}
	}).mouseout(function(){
		$(this).removeClass("over");
		num = $(this).attr("id").substring(3);
		if (info_close != num)
		{
			var images = map.getContainer().getElementsByTagName("img");
			for(var i = 0; i<images.length; i++)
			{
				if (images[i].src.indexOf("img/greenmap" + num + "_on.gif") > -1)
				{
					images[i].src = "img/greenmap" + num + ".gif";
				}
			}
		}
	}).click(function(){
		num = $(this).attr("id").substring(3);
		rowClick(num);
		return false;
	});
	
	$(".odd.other, .even.other").mouseover(function(){
		$(this).addClass("over");
		num = $(this).attr("id").substring(3);
		var images = map.getContainer().getElementsByTagName("img");
		for(var i = 0; i<images.length; i++)
		{
			if (images[i].src.indexOf("img/othermap" + num + ".gif") > -1)
			{
				images[i].src = "img/greenmap" + num + "_on.gif";
			}
		}
	}).mouseout(function(){
		$(this).removeClass("over");
		num = $(this).attr("id").substring(3);
		if (info_close != num)
		{
			var images = map.getContainer().getElementsByTagName("img");
			for(var i = 0; i<images.length; i++)
			{
				if (images[i].src.indexOf("img/greenmap" + num + "_on.gif") > -1)
				{
					images[i].src = "img/othermap" + num + ".gif";
				}
			}
		}
	}).click(function(){
		num = $(this).attr("id").substring(3);
		rowClick(num);
		return false;
	});

	
	$(".map_check").click(function(){
		cat = $(this).attr("id").substring(4);
		if ($(this).attr("checked") == true)
			show(cat);
		else
			hide(cat);
	});
	
	$(".hotel_map_check").click(function(){
		cat = $(this).attr("id").substring(4);
		if ($(this).attr("checked") == true)
		{
			show(cat);
		}
		else
		{
			hide2(cat);
		}
	});
	
	$("#attraction, #rail, #tube").change(function(){
		val = $(this).val().substring($(this).val().indexOf("|") + 1);
		type = val.substring(0, val.indexOf("|"));
		num = val.substring(val.indexOf("|") + 1);
		attractionChange(num, type)
	});
	
	$(window).unload(function(){
		GUnload();
	});
});

/* Creates a marker at the given point with the given number label*/
function createMarker2(point, hname, url, imagename, station, rownumber)
{
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(17, 15);
	baseIcon.iconAnchor = new GPoint(17, 15);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	var icon = new GIcon(baseIcon);
	icon.image = "img/"+imagename+".gif";
	var marker = new GMarker(point,icon);
	marker.mycategory = imagename;
	marker.myname = imagename + rownumber;
	
	$("#attraction_icon").attr("src", "img/"+imagename+".gif");
	$("#attraction_name").text(hname);
	$("#attraction_link").attr("href", url);
	if (station==1) rail_content[rownumber] = $("#details_attraction").html();
	else attraction_content[rownumber] = $("#details_attraction").html();
	
	GEvent.addListener(marker, "mouseover", function(){
		/*
		$("#attraction").attr("selectedIndex", 0);
		$("#tube").attr("selectedIndex", 0);
		$("#rail").attr("selectedIndex", 0);
		$("#rail").addClass("green9");
		i=0;
		found=false;
		if (station==0)
		{
			while (i < $("#attraction")[0].length && !found)
			{
				if ($("#attraction option:eq("+i+")").text()==hname)
				{
					$("#attraction").attr("selectedIndex", i);
					$("#attraction").addClass("whitedropdown");
					found=true;
				}
				else
					i++;
			}
		}
		else
		{
			while (i<$("#rail")[0].length && !found)
			{
				if ($("#rail option:eq("+i+")").text()==hname)
				{
					$("#rail").attr("selectedIndex", i);
					$("#rail").addClass("whitedropdown");
					found=true;
				}
				else
					i++;
			}
		}
		*/
	});
	GEvent.addListener(marker, "mouseout", function(){
		//$("#attraction").attr("selectedIndex", 0);
		//$("#rail").attr("selectedIndex", 0);
	});

	gmarkers.push(marker);
	GEvent.addListener(marker, "click", function() {
		$("#attraction").attr("selectedIndex", 0);
		$("#tube").attr("selectedIndex", 0);
		$("#rail").attr("selectedIndex", 0);
		$("#rail").addClass("green9");
		i=0;
		found=false;
		if (station==0)
		{
			while (i < $("#attraction")[0].length && !found)
			{
				if ($("#attraction option:eq("+i+")").text()==hname)
				{
					$("#attraction").attr("selectedIndex", i);
					$("#attraction").addClass("whitedropdown");
					found=true;
				}
				else
					i++;
			}
		}
		else
		{
			while (i<$("#rail")[0].length && !found)
			{
				if ($("#rail option:eq("+i+")").text()==hname)
				{
					$("#rail").attr("selectedIndex", i);
					$("#rail").addClass("whitedropdown");
					found=true;
				}
				else
					i++;
			}
		}
		$("#orderby").val("distance");
		$("#direction").val("asc");
		if (station==1) marker.openInfoWindow(rail_content[rownumber]);
		else marker.openInfoWindow(attraction_content[rownumber]);
	});
	
	GEvent.addListener(marker, "infowindowopen", function() {
		var images = map.getContainer().getElementsByTagName("img");
		for(var i = 0; i<images.length; i++)
		{
			if (images[i].src=="http://maps.google.com/intl/en_ALL/mapfiles/iw_close.gif")
			{
				images[i].src = "img/map_close.gif";
			}
		}
	});
	return marker;
}

function createMarker3(point, hname, url, imagename, rownumber)
{
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(17, 15);
	baseIcon.iconAnchor = new GPoint(17, 15);
	baseIcon.infoWindowAnchor = new GPoint(9, 1);
	var icon = new GIcon(baseIcon);
	icon.image = "img/"+imagename+".gif";
	var marker = new GMarker(point,icon);
	marker.mycategory = imagename;
	marker.myname = imagename + rownumber;

	$("#attraction_icon").attr("src", "img/"+imagename+".gif");
	$("#attraction_name").text(hname);
	$("#attraction_link").attr("href", url);
	tube_content[rownumber] = $("#details_attraction").html();
	
	GEvent.addListener(marker, "mouseover", function() {
		/*
		$("#attraction").attr("selectedIndex", 0);
		$("#tube").attr("selectedIndex", 0);
		$("#rail").attr("selectedIndex", 0);
		i=0;
		found=false;
		sel = 0;
		while (i<$("#tube")[0].length && !found)
		{
			if ($("#tube option:eq("+i+")").text().replace(" Underground Station", "")==hname.replace(" Underground Station", ""))
			{
				$("#tube").attr("selectedIndex", i);
				$("#tube").addClass("whitedropdown");
				sel = i;
				found=true;
			}
			else
				i++;
		}
		*/
	});
	GEvent.addListener(marker, "mouseout", function() {
		//$("#tube").attr("selectedIndex", 0);
	});
	gmarkers.push(marker);
	GEvent.addListener(marker, "click", function() {
		//$("#tube").attr("selectedIndex", sel);
		$("#attraction").attr("selectedIndex", 0);
		$("#tube").attr("selectedIndex", 0);
		$("#rail").attr("selectedIndex", 0);
		i=0;
		found=false;
		sel = 0;
		while (i<$("#tube")[0].length && !found)
		{
			if ($("#tube option:eq("+i+")").text().replace(" Underground Station", "")==hname.replace(" Underground Station", ""))
			{
				$("#tube").attr("selectedIndex", i);
				$("#tube").addClass("whitedropdown");
				sel = i;
				found=true;
			}
			else
				i++;
		}
		$("#orderby").val("distance");
		$("#direction").val("asc");

		marker.openInfoWindow(tube_content[rownumber]);
	});
	GEvent.addListener(marker, "infowindowopen", function() {
		var images = map.getContainer().getElementsByTagName("img");
		for(var i = 0; i<images.length; i++)
		{
			if (images[i].src=="http://maps.google.com/intl/en_ALL/mapfiles/iw_close.gif")
			{
				images[i].src = "img/map_close.gif";
			}
		}
	});
	return marker;
}

function createMarkerNoLink(point, address, imagename,postcode,city)
{
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(22, 29);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	var icon = new GIcon(baseIcon);
	icon.image = "img/"+imagename+".gif";
	var marker = new GMarker(point,icon);
	return marker;
}

function createMarker(point, hname, url, imagename, rownumber, thumb, rating, reviews, reviews_link, description, offer, details, price, shortlist, book_link)
{
	/* Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.*/
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(18, 23);
	baseIcon.iconAnchor = new GPoint(18, 23);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	var icon = new GIcon(baseIcon);
	icon.image = "img/"+imagename+rownumber+".gif";
	var marker = new GMarker(point,icon);
	if (imagename.indexOf("other") > -1)
	{
		marker.mycategory = "otherhotels";
	}
	else
	{
		marker.mycategory = "hotels";
	}
	
	$("#hotel_info_number").text(rownumber);
	$("#hotel_info_name").html(hname);
	$("#hotel_info_thumb").attr("src", thumb);
	$("#hotel_info_rating").text(rating);
	$("#hotel_info_reviews").attr("href", reviews_link);
	$("#hotel_info_reviews").text(reviews);
	$("#hotel_info_description").html(description);
	$("#hotel_info_offer").html(offer);
	$("#hotel_info_details").html(details);
	$("#hotel_info_price").html(price);
	$("#hotel_info_shortlist").attr("href", shortlist);
	$("#hotel_info_link").attr("hlink", url);
	$("#hotel_info_link").attr("onclick", "location.href=$(this).attr('hlink');");;
	$("#hotel_info_bookbrowse").attr("onclick", "location.href='" + book_link + "'");
	$("#hotel_info_book").attr("onclick", "$('#hotel_form"+rownumber+"').submit();");
	html_content[rownumber] = $("#details_hotel").html();
	
	marker.myname = "hotel" + rownumber;
	GEvent.addListener(marker, "mouseover", function() {
		marker.setImage("img/greenmap"+rownumber+"_on.gif");
		$("#row" + rownumber).addClass("over");
		$("#hotel_list").scrollTop(20*(rownumber-1));
	});
	GEvent.addListener(marker, "mouseout", function() {
		if (info_close != rownumber)
		{
			marker.setImage("img/"+imagename+rownumber+".gif");
			$("#row" + rownumber).removeClass("over");
		}
	});
	GEvent.addListener(marker, "click", function() {
		rowClick(rownumber);
	});
	GEvent.addListener(marker, "infowindowclose", function() {
		info_close = 0;
		marker.setImage("img/"+imagename+rownumber+".gif");
		$("#row" + rownumber).removeClass("over");
	});
	GEvent.addListener(marker, "infowindowopen", function() {
		info_close = rownumber;
		marker.setImage("img/greenmap"+rownumber+"_on.gif");
		$("#row" + rownumber).addClass("over");
		$("#hotel_list").scrollTop(20*(rownumber-1));
		var images = map.getContainer().getElementsByTagName("img");
		for(var i = 0; i<images.length; i++)
		{
			if (images[i].src=="http://maps.google.com/intl/en_ALL/mapfiles/iw_close.gif")
			{
				images[i].src = "img/map_close.gif";
			}
		}
	});
	gmarkers.push(marker);
	return marker;
}

function show(category)
{
	for (var i=0; i<gmarkers.length; i++)
	{
		if (gmarkers[i].mycategory == category)
		{
			gmarkers[i].show();
		}
	}
}

function hide(category)
{
	for (var i=0; i<gmarkers.length; i++)
	{
		if (gmarkers[i].mycategory == category)
		{
			gmarkers[i].hide();
		}
	}
	map.closeInfoWindow();
}

function hide2(category)
{
	for (var i=0; i<gmarkers.length; i++)
	{
		if (gmarkers[i].mycategory == category)
		{
			gmarkers[i].hide();
		}
	}
}

function rowClick(row)
{
	for (var i=0; i<gmarkers.length; i++)
	{
		if (gmarkers[i].myname == "hotel" + row)
		{
			gmarkers[i].openInfoWindowHtml(html_content[row]);
		}
	}
}

function attractionChange(row, imagename)
{
	for (var i=0; i<gmarkers.length; i++)
	{
		if (imagename == "binocularsmap")
		{
			if (gmarkers[i].myname == imagename + row)
			{
				gmarkers[i].openInfoWindowHtml(attraction_content[row]);
			}
		}
		if (imagename == "railmap")
		{
			if (gmarkers[i].myname == imagename + row)
			{
				gmarkers[i].openInfoWindowHtml(rail_content[row]);
			}
		}
		if (imagename == "tubemap")
		{
			if (gmarkers[i].myname == imagename + row)
			{
				gmarkers[i].openInfoWindowHtml(tube_content[row]);
			}
		}
	}
}

/* Creates a marker at the given point with the given number label*/
function createHotelMarker(point, hname, url,imagename,category,city)
{
	/* 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.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(13, 20);
	var icon = new GIcon(baseIcon);
	icon.image = "img/"+imagename;
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindow($("#hotel_details").html());
	});
	GEvent.addListener(marker, "infowindowopen", function() {
		var images = map.getContainer().getElementsByTagName("img");
		for(var i = 0; i<images.length; i++)
		{
			if (images[i].src=="http://maps.google.com/intl/en_ALL/mapfiles/iw_close.gif")
			{
				images[i].src = "img/map_close.gif";
			}
		}
	});
	return marker;
}

function createMarkerAttraction(point, hname, url, imagename, station)
{
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(17, 15);
	baseIcon.iconAnchor = new GPoint(17, 15);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	var icon = new GIcon(baseIcon);
	icon.image = "img/"+imagename+".gif";
	var marker = new GMarker(point,icon);
	marker.mycategory = imagename;
	marker.myname = imagename;

	gmarkers.push(marker);
	GEvent.addListener(marker, "click", function() {
		$("#attraction_icon").attr("src", "img/"+imagename+".gif");
		$("#attraction_name").text(hname);
		$("#attraction_link").attr("href", url);
		marker.openInfoWindow($("#details_attraction").html());
	});
	GEvent.addListener(marker, "infowindowopen", function() {
		var images = map.getContainer().getElementsByTagName("img");
		for(var i = 0; i<images.length; i++)
		{
			if (images[i].src=="http://maps.google.com/intl/en_ALL/mapfiles/iw_close.gif")
			{
				images[i].src = "img/map_close.gif";
			}
		}
	});
	return marker;
}
