var marker_descriptions = [];
var markers = [];
var marker_urls = [];

function warehouseOnLoad() {
	var map = new GMap2(document.getElementById('map'));
	var center = new GLatLng(25, -30);
	map.setCenter(center, 2);

	// Google Example Code: 
	// http://code.google.com/apis/maps/documentation/overlays.html#Icons_overview

	var labels = [];
	var ids = [];
	var markerCount = 0;
	var point = new GLatLng(/*44.987931,-73.445578*/40.743095, -74.051971);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "This is our primary warehouse in the United States for all Teddy Mountain products. We ship worldwide.";
	marker_urls[markerCount] = "http://www.teddyzoo.com/en/onlinestore.aspx";
	labels[markerCount++] = "Teddy Zoo US";
	/*
	point = new GLatLng(42.52342,-83.53446);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount]  = "This is our secondary warehouse in the United States. It only ships items that don't feature the Teddy Mountain.";
	marker_urls[markerCount] = "http://www.teddyzoo.com/en/onlinestore.aspx";
	labels[markerCount++] = "Teddy Zoo US (2)";
	*/

	/*
	point = new GLatLng(20.759324, -156.44702);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "OPENING SOON - Teddy Zoo Hawaii. This distribution center will dramatically reduce shipping costs for retailers in Hawaii.";
	marker_urls[markerCount] = "http://www.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "Hawaii";
	labels[markerCount++] = "Teddy Zoo Hawaii";
	*/

	point = new GLatLng(45.573678, -73.689423);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "Our Canadian warehouse stocks the same inventory as our primary US warehouse. However, shipping costs are greatly reduced for our Canadian customers as products are shipped directly from Canada, without any associated border fees.";
	marker_urls[markerCount] = "http://ca.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "Canada";
	labels[markerCount++] = "Teddy Zoo Canada";

	point = new GLatLng(19.050111, -98.221893);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "Teddy Zoo Mexico carries Teddy Mountain brand items, charges in pesos and provides reduced shipping costs for customers in Mexico and all of Central America.";
	marker_urls[markerCount] = "http://mx.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "Mexico";
	labels[markerCount++] = "Teddy Zoo Mexico";

	point = new GLatLng(56.138042, 9.15659);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "Teddy Zoo Europe is our primary European distribution center and features the most diverse stock offering in Europe. Products are shipped out of Denmark.";
	marker_urls[markerCount] = "http://eu.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "Europe";
	labels[markerCount++] = "Teddy Zoo Europe";

	point = new GLatLng(39.91, 116.379204);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "NOW OPEN - Teddy Zoo China reduces shipping costs for our customers in Asia.";
	marker_urls[markerCount] = "http://cn.teddyzoo.com";
	ids[markerCount] = "China";
	labels[markerCount++] = "Teddy Zoo China";

	point = new GLatLng(50.4220235, -3.5935978);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "Teddy Zoo England is the primary distributor for the England, Ireland and Wales.";
	marker_urls[markerCount] = "http://england.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "England";
	labels[markerCount++] = "Teddy Zoo England";

	point = new GLatLng(53.363665, -6.240234);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "Teddy Zoo Ireland is served by Teddy Zoo England.";
	marker_urls[markerCount] = "http://england.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "Ireland";
	labels[markerCount++] = "Teddy Zoo Ireland";

	point = new GLatLng(52.519564, -3.806763);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "Teddy Zoo Wales is served by Teddy Zoo England.";
	marker_urls[markerCount] = "http://england.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "Wales";
	labels[markerCount++] = "Teddy Zoo Wales";

	
	point = new GLatLng(57.480772, -4.2267329);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "Teddy Zoo Scotland carries Teddy Mountain brand items, charges in British Pounds and provides reduced shipping costs for customers in Scotland.";
	marker_urls[markerCount] = "http://sct.teddyzoo.com/en/onlinestore.aspx";
	ids[markerCount] = "Europe";
	labels[markerCount++] = "Teddy Zoo Scotland";
	

	point = new GLatLng(-25.878994, 134.472656);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "LOOKING FOR A MASTER DISTRIBUTOR - Teddy Zoo is currently looking for a master distributor in Australia.";
	marker_urls[markerCount] = "http://www.teddyzoo.com";
	ids[markerCount] = "Australia";
	labels[markerCount++] = "Teddy Zoo Australia";

	point = new GLatLng(-31.278551, 24.609375);
	marker = new GMarker(point);
	map.addOverlay(marker);
	markers[markerCount] = marker;
	marker_descriptions[markerCount] = "LOOKING FOR A MASTER DISTRIBUTOR - Teddy Zoo is currently looking for a master distributor in South Africa.";
	marker_urls[markerCount] = "http://www.teddyzoo.com";
	ids[markerCount] = "Africa";
	labels[markerCount++] = "Teddy Zoo South Africa";

	

	$(markers).each(function(i, marker) {
		$("<li />").addClass('ui-state-default ui-corner-all')
						.uiHover()
						.html(labels[i])
						.click(function() {
							displayPoint(marker, i);
						})
						.appendTo("#list");

		GEvent.addListener(marker, "click", function() {
			displayPoint(this, i);
		});
	});

	$('<div id="message" />').appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));

	function displayPoint(marker, index) {
		$("#message").hide().empty();

		var closeButton = $(iconHTML("close"))
						.click(
							function() {
								$("#message").fadeOut();
							})
						.css({ top: '5px', right: '5px' })
						.uiHover();

		var setCookie = function() {
			setDistCenterCookie(ids[index]);
			
			if(redirectToSite == true)
				navigateTo(currentBrand, ids[index], currentPage);
		};

		$("#tabs-template").clone().show()
						.attr("id", "").appendTo("#message")
						.find(".description").html(marker_descriptions[index]).end()
						.find(".dialog").attr("href", marker_urls[index]).click(setCookie).end()
						.tabs()
						.find(".ui-tabs-nav").append(closeButton);

		var moveEnd = GEvent.addListener(map, "moveend", function() {
			var markerOffset = map.fromLatLngToDivPixel(marker.getPoint());
			setTimeout(function() {
				setCookie();
			}, 1000);
			$("#message")
							.css({ top: markerOffset.y, left: markerOffset.x - 100 })
							.show("drop", { direction: "right" });

			GEvent.removeListener(moveEnd);
		});

		left = map.getBounds().getSouthWest().lat();
		right = map.getBounds().getNorthEast().lat();
		offset = (right - left) * .25;
		map.panTo(new GLatLng(marker.getPoint().lat(), marker.getPoint().lng() + offset));
	}

	GEvent.addListener(map, 'zoomend', function() {
		$("#message").hide();
	});

	$("#dialog").show().dialog({
		autoOpen: false,
		modal: true,
		overlay: { background: "#000", opacity: 0.7 },
		width: 350, height: 300
	});

	$("#list").appendTo("#map").css({ top: '10px', right: '10px' });

	/* Build Controls */
	$(iconHTML("up"))
					.css({ top: '10px', left: '32px' })
					.click(function() {
						map.panDirection(0, 1);
					})
					.appendTo("#map");

	$(iconHTML("left"))
					.css({ top: '32px', left: '10px' })
					.click(function() {
						map.panDirection(1, 0);
					})
					.appendTo("#map");

	$(iconHTML("right"))
					.css({ top: '32px', left: '54px' })
					.click(function() {
						map.panDirection(-1, 0);
					})
					.appendTo("#map");

	$(iconHTML("down"))
					.css({ top: '54px', left: '32px' })
					.click(function() {
						map.panDirection(0, -1);
					})
					.appendTo("#map");

	$(iconHTML("plus"))
					.css({ top: '84px', left: '32px' })
					.click(function() {
						map.zoomIn();
						$("#map-slider").slider("value", map.getZoom());
					})
					.appendTo("#map");

	$(iconHTML("minus"))
					.css({ top: '325px', left: '32px' })
					.click(function() {
						map.zoomOut();
						$("#map-slider").slider("value", map.getZoom());
					})
					.appendTo("#map");

	$("<div />").attr('id', 'map-slider').height(200)
					.slider({
						orientation: "vertical",
						min: 0, max: 19, step: 1, value: map.getZoom(),
						change: function() {
							map.setZoom($(this).slider("value"));
						}
					})
					.css({ top: '115px', left: '38px', position: 'absolute' })
					.appendTo("#map");

}

// in codebehind
//$(document).ready(warehouseOnLoad);

function iconHTML(type) {
	switch (type) {
		case "up": iconClass = 'ui-icon-circle-arrow-n'; break;
		case "down": iconClass = 'ui-icon-circle-arrow-s'; break;
		case "left": iconClass = 'ui-icon-circle-arrow-w'; break;
		case "right": iconClass = 'ui-icon-circle-arrow-e'; break;
		case "plus": iconClass = 'ui-icon-circle-plus'; break;
		case "minus": iconClass = 'ui-icon-circle-minus'; break;
		case "close": iconClass = 'ui-icon-closethick'; break;
	}
	return '<div class="icon ui-state-default ui-corner-all"><span class="ui-icon ' + iconClass + '" /></div>';
}

$.fn.uiHover = function() {
	return this.each(function() {
		$(this).hover(
						function() { $(this).addClass('ui-state-hover'); },
						function() { $(this).removeClass('ui-state-hover'); }
					);
	});
}

