var map;
var GillMap;
var markersArray = [];
var openedWindow;
var centerLat;
var centerLng;

var setMarkers;
var addMarker;
var clearMarkers;

(function($) {
	var map_debug = false;
	var GillMapOverlay = function(map, image, imageWidth, imageHeight) {
		this.image_ = image;
		this.imageWidth_ = imageWidth;
		this.imageHeight_ = imageHeight;

		this.div_ = null;

		this.setMap(map);
	}

	GillMapOverlay.prototype = new google.maps.OverlayView();

	GillMapOverlay.prototype.onAdd = function() {
		var div = document.createElement('div');
		div.style.border = "none";
		div.style.borderWidth = "0px";
		div.style.position = "absolute";

		var img = document.createElement("img");
		img.src = this.image_;

		img.style.width = this.imageWidth_;	
		img.style.height = this.imageHeight_;

		div.appendChild(img);
		this.div_ = div;

		var panes = this.getPanes();
		panes.overlayLayer.appendChild(div);

		document.getElementById("map").removeChild(document.getElementById("loading"));
	}

	GillMapOverlay.prototype.draw = function() {
		var div = this.div_;

		div.style.left = '0px';
		div.style.top = '0px';
		div.style.width = this.imageWidth_ + 'px';
		div.style.height = this.imageHeight_ + 'px';

		if (map_debug) {
			div.style.backgroundColor = '#ffff00';
		} else {
			div.style.backgroundColor = '#000000';
		}
	}

	GillMapOverlay.prototype.onRemove = function() {
		this.div_.parentNode.removeChild(this.div_);
		this.div_ = null;
	}

	GillMapOverlay.prototype.remove = function() {
		this.setMap(null);
	}

	function overlayLoading() {
		var odiv = document.createElement('div');
		odiv.id = "loading";
		odiv.style.border = "none";
		odiv.style.borderWidth = "0px";
		odiv.style.position = "absolute";

		odiv.style.left = '0px';
		odiv.style.top = '0px';
		//div.style.width = this.imageWidth_ + 'px';
		//div.style.height = this.imageHeight_ + 'px';
		odiv.style.width = '100%';
		odiv.style.height = '100%';
		//odiv.style.backgroundColor = '#ffff00';
		odiv.style.backgroundColor = '#000000';
		odiv.style.zIndex = 99999;
		
		document.getElementById("map").appendChild(odiv);
	}


	setMarkers = function(stores) {
		if (stores.length != 0) {
			for (var i = 0; i < stores.length; i++) {
				marker = addMarker(stores[i], i);
				markersArray.push(marker);
			}
		}
	}



	addMarker = function(store, key) {
		var image;

		if (key == 0) {
			image = new google.maps.MarkerImage('/images/store-locator/nearest-store.png',
				new google.maps.Size(14,13), // size
				new google.maps.Point(0,0), // origin
				new google.maps.Point(6,7), // anchor
				new google.maps.Size(14,13) // scaled size
			);
		} else {
			image = new google.maps.MarkerImage('/images/store-locator/other-store.png',
				new google.maps.Size(5,5), // size
				new google.maps.Point(0,0), // origin
				new google.maps.Point(2,2), // anchor
				new google.maps.Size(5,5) // scaled size
			);
		}

		var zIndex = 100 - key;

		var myLatLng = new google.maps.LatLng(store['lat'], store['lon']);
		var marker = new google.maps.Marker({
			position: myLatLng,
			map: map,
			icon: image,
			title: store['title'],
			zIndex: zIndex
		});


		google.maps.event.addListener(marker, "click", function() {
			window.open(store['url']);
		});

		return marker;
	}

	clearMarkers = function() {
		if (markersArray) {
			for (i in markersArray) {
				markersArray[i].setMap(null);
			}
		}
		markersArray = [];
	}

	GillMap = {
		init: function(storeCountryCode) {
			var oThis = this;

			$(function() {
				oThis.onLoadInit(storeCountryCode);
			});
		},

		onLoadInit: function(storeCountryCode) {

			switch (storeCountryCode) {
				case "au":
					zoom = 3;
					centerLat = -24.20689;
					centerLng = 133.769531;
					imageWidth = 440;
					imageHeight = 440;
					break;
				case "de":
					zoom = 6;
					centerLat = 51.165567;
					centerLng = 10.458984;
					imageWidth = 440;
					imageHeight = 600;
					break;
				case "dk":
					zoom = 7;
					centerLat = 56.195;
					centerLng = 10.343;
					imageWidth = 440;
					imageHeight = 600;
					break;
				case "es":
					zoom = 5;
					centerLat = 40.480381;
					centerLng = -3.735352;
					imageWidth = 440;
					imageHeight = 440;
					break;
				case "fr":
					zoom = 5;
					centerLat = 46.94;
					centerLng = 1.72;
					imageWidth = 440;
					imageHeight = 436;
					break;
				case "gb":
					zoom = 5;
					centerLat = 55.37911;
					centerLng = -3.427734;
					imageWidth = 440;
					imageHeight = 500;
					break;
				case "ie":
					zoom = 7;
					centerLat = 53.501117;
					centerLng = -8.239746;
					imageWidth = 440;
					imageHeight = 650;
					break;
				case "it":
					zoom = 5;
					centerLat = 41.869561;
					centerLng = 12.568359;
					imageWidth = 440;
					imageHeight = 440;
					break;
				case "gr":
					zoom = 6;
					centerLat = 39.867588;
					centerLng = 23.159180;
					imageWidth = 440;
					imageHeight = 440;
					break;
				case "nl":
					zoom = 7;
					centerLat = 52.133488;
					centerLng = 5.29541;
					imageWidth = 440;
					imageHeight = 500;
					break;
				case "no":
					zoom = 4;
					centerLat = 64;
					centerLng = 20;
					imageWidth = 440;
					imageHeight = 500;
					break;
				case "nz":
					zoom = 5;
					centerLat = -41.49;
					centerLng = 173.62; 
					imageWidth = 440;
					imageHeight = 500;
					break;
				case "pt":
					zoom = 7;
					centerLat = 39.502244;
					centerLng = -8.217773;
					imageWidth = 440;
					imageHeight = 650;
					break;
				case "ru":
					zoom = 2;
					centerLat = 61.606396;
					centerLng = 103.46875;
					imageWidth = 440;
					imageHeight = 440;
					break;
				case "se":
					zoom = 4;
					centerLat = 62.10867;
					centerLng = 18.632813;
					imageWidth = 440;
					imageHeight = 550;
					break;
				case "tr":
					zoom = 5;
					centerLat = 39.707186;
					centerLng = 35.332031;
					imageWidth = 440;
					imageHeight = 440;
					break;
				default:
					zoom = 4;
					centerLat = 0;
					centerLng = 0;
					imageWidth = 440;
					imageHeight = 440;
			}

			// Loading Overlay
			if (map_debug == false) {
				overlayLoading();
			}

			// Options
			var map_options = {
				backgroundColor:		'#000000',
				zoom:					zoom,
				center:					new google.maps.LatLng(centerLat, centerLng),
				mapTypeId:				google.maps.MapTypeId.ROADMAP,
				navigationControl:		false,
				mapTypeControl:			false,
				draggable:				false,
				disableDoubleClickZoom:	true,
				disableDefaultUI:		true,
				keyboardShortcuts:		false,
				scrollwheel:			false,
				noClear:				true
			};
			

			if (map_debug) {
				// Debug Setting On:
				// Stops Map being locked in place
				var map_options_debug = {
					draggable:				true,
					disableDoubleClickZoom:	false,
					keyboardShortcuts:		true,
					scrollwheel:			true,
					noClear:				false
				};

				jQuery.extend(map_options, map_options_debug);
			}


			// Create map
			map = new google.maps.Map(document.getElementById("map"), map_options);

			if (map_debug == false) {
				// Debug Setting Off:
				// Adds the image overlay and store markers
				var image = '/images/store-locator/' + storeCountryCode + '.png';
				var overlay = new GillMapOverlay(map, image, imageWidth, imageHeight);
			}
		}
	};
})(jQuery);

