var DisplayLocator = {

	defLocation : { 'lat' : parseFloat(document.getElementById("initLatitude").title), 'lng' : parseFloat(document.getElementById("initLongitude").title), 'zoom' : parseInt(document.getElementById("initZoom").title) },
	proxRange : { 'default' : 20, 'big' : 50 },
	zoomLevels : { 'full' : 3, 'state' : 7, 'city' : 9, 'suburb' : 13 },	
	init: function(){
		DisplayLocator.bindEvents();
		DisplayLocator.Map.init();
	},
	bindEvents: function(){
		// Click on Spa VCARD, center on spa
		$('.vcard').click(function(){
			DisplayLocator.Map.focusOnSpa($(this));
			$('.vcard').css('background-position', '0px -24px');
			$(this).css('background-position', '-24px 0px');
			return false;
		});
		
	},
	getVcardGeo: function(vcard){
		var geo = {
			'spaName'     : vcard.find('h2').html(),
			'address'     : vcard.find('.mapAddress').html(),
			'address2'    : vcard.find('.mapAddress2').html(),
			'phone'       : vcard.find('.mapPhone').html(),
			'siteName'    : $('#dealerName').html(),
			'lng'         : parseFloat(vcard.find('.longitude').children().first().attr('title')),
			'lat'         : parseFloat(vcard.find('.latitude').children().first().attr('title'))
		}
		return geo;
	},
	Map: {	
		latlng: {},
		gMap: {},
		spaMarkers: [],
		spaInfoWindows: [],
		
		init: function(){
			// initialize google map
			DisplayLocator.Map.latlng = new google.maps.LatLng(DisplayLocator.defLocation.lat, DisplayLocator.defLocation.lng);
			var settings = {
				zoom: DisplayLocator.defLocation.zoom,
				center: DisplayLocator.Map.latlng,
				mapTypeControl: false,
				mapTypeControlOptions: {
					style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
				},
				navigationControl: true,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.ZOOM_PAN,
					position: google.maps.ControlPosition.DEFAULT
				},
				panControl: false,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			DisplayLocator.Map.gMap = new google.maps.Map(document.getElementById('displayMap'), settings);
			DisplayLocator.Map.drawAllMarkers();
		},
		
		focusMap: function(mapLocation){
			DisplayLocator.Map.closeAllInfoWindows();
			DisplayLocator.Map.gMap.setCenter(new google.maps.LatLng(mapLocation.lat, mapLocation.lng));
			DisplayLocator.Map.gMap.setZoom(mapLocation.zoom);
		},	
		drawAllMarkers: function(){
			
			allDisplays = $('.vcard');
			
			allDisplays.each(function(i){
				var _this = $(this),
					geo = DisplayLocator.getVcardGeo(_this);

				DisplayLocator.Map.setMarker(geo);
			});	
		},
		
		setMarker: function(val){
			
			var spaPos = new google.maps.LatLng(val.lat,val.lng),
				spaMarker = new google.maps.Marker({
					position : spaPos,
					map : DisplayLocator.Map.gMap,
				   	title : val.spaName
				}),
				popupWindowHTML = '<div class="info_window_text"><h2>' + val.siteName + '</h2><h3>'+ val.spaName +'</h3><p>'+ val.address +'</p><p>'+ val.address2 +'</p><p>'+val.phone+'</p></div>',
				infowindow = new google.maps.InfoWindow({
					content: popupWindowHTML
				});
				
			google.maps.event.addListener(spaMarker, 'click', function(){
				DisplayLocator.Map.closeAllInfoWindows();
				infowindow.open(DisplayLocator.Map.gMap,spaMarker);
				$('.vcard').css('background-position', '0px -24px');
				$('.vcard').eq(this.__gm_id -1).css('background-position', '-24px 0');
			});
			google.maps.event.addListener(infowindow, 'closeclick', function(){
				$('#spa_data').children('.vcard').removeClass('hover');
			});
			DisplayLocator.Map.spaMarkers.push(spaMarker);
			DisplayLocator.Map.spaInfoWindows.push(infowindow);
		},
		
		closeAllInfoWindows: function()
		{
			for(var i=0, max = DisplayLocator.Map.spaInfoWindows.length; i < max; i++)
			{
				DisplayLocator.Map.spaInfoWindows[i].close();
			}
		},
		
		focusOnSpa: function(vcard){
			var _geo   = DisplayLocator.getVcardGeo(vcard),
				_index = vcard.index();
				
			_geo.zoom = DisplayLocator.zoomLevels.suburb;
			
			DisplayLocator.Map.focusMap(_geo);
			DisplayLocator.Map.closeAllInfoWindows();
			DisplayLocator.Map.spaInfoWindows[_index].open(DisplayLocator.Map.gMap,DisplayLocator.Map.spaMarkers[_index]);
		}
	}	
};


$(function(){
	DisplayLocator.init();
});
