

jQuery(document).ready(function($) {

	var RetailMap = {
		portal: "music",
		map: null,
		markers: [],
		markerClusterer: null,
		mapShown: false,
		coords: false,
		country: false,
		state: false,
		locations: [],
		initialState: true
	};
	
	function getLocations() {
		
		RetailMap.markers = [];
		
		var url = '/inc/gmaplocations.php?portal='+RetailMap.portal+'&lon='+escape(RetailMap.coords[0])+'&lat='+escape(RetailMap.coords[1]);
	
		$.ajax({
			url: url,
			method: 'get',
			dataType: 'json',
			success: function(data) {
				RetailMap.locations = data;
				mapInit();
			},
			error: function() {
				alert('Something went wrong...');
			}
		});
	
	}
	
	function createMarker(point,location) {
	
		var icon = new GIcon(G_DEFAULT_ICON);
		//icon.image = "http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png";
		
		var marker = new GMarker(point);
				
		var html = "<div class='location_info'>";
		
		if (location.name.length > 0)     {html += "<span style='font-weight:bold;'>"+location.name+"</span><br>";}
		if (location.address1.length > 0) {html += location.address1+"<br>";}
		if (location.address2.length > 0) {html += location.address2+"<br>";}
		if (location.phone.length > 0)    {html += location.phone+"<br>";}
		if (location.website.length > 0)    {html += "<a href='http://"+location.website+"'>"+location.website+'</a>';}
		html += "<\/div>";
				
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		
		return marker;
	}
	
	function mapInit() {	
		
		if(GBrowserIsCompatible()) {

			if(!RetailMap.mapShown) {
				$('#map_canvas').show();
				RetailMap.mapShown = true;
			}
	
			RetailMap.map = new GMap2(document.getElementById('map_canvas'));
			
			RetailMap.map.setCenter(new GLatLng(RetailMap.coords[1], RetailMap.coords[0]), 12);
				
			RetailMap.map.addControl(new GLargeMapControl());
			RetailMap.map.addControl(new GMapTypeControl());
	
			for (var i = 0; i < RetailMap.locations.length; ++i) {
	
				var point = new GLatLng(RetailMap.locations[i].lat, RetailMap.locations[i].lon);
				var marker = createMarker(point,RetailMap.locations[i]);
				
				RetailMap.markers.push(marker);
			}
				
			if (RetailMap.markerClusterer !== null) {
				RetailMap.markerClusterer.clearMarkers();
			}
			
			RetailMap.markerClusterer = new MarkerClusterer(
				RetailMap.map, 
				RetailMap.markers, 
				{maxZoom: 17, gridSize: 20, styles: -1}
			);
		}
	}
		
	$('#zip_search_form').bind('submit', function() {
	
		var zip = this.zip.value,
			url = '/inc/gmapgeolocate.php?zip=' + zip;
		
		RetailMap.portal = this.portal.value;
	
		$.ajax({
	
			url: url,
			dataType: 'json',
			success: function (data) {
	
				RetailMap.coords = data.Placemark[0].Point.coordinates;
				RetailMap.country = data.Placemark[0].AddressDetails.Country.CountryNameCode;
	
				if (RetailMap.country != 'US' && RetailMap.country != 'CA') {
					alert ("Sorry, that's an invalid location.");
					return;
				} else {
					RetailMap.state = data.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
					getLocations();
				}
			},
			error: function () {
				alert('Something went wrong...');
			}
		});
	
		return false;
	});
	
	$('input[type="text"]').each(function(){

		this.value = $(this).attr('title');
		
		$(this).addClass('text-label');
		
		$(this).focus(function(){
			if(this.value == $(this).attr('title')) {
				this.value = '';
				$(this).removeClass('text-label');
			}
		});
		
		$(this).blur(function(){
			if(this.value == '') {
				this.value = $(this).attr('title');
				$(this).addClass('text-label');
			}
		});
	});

	
});

