/* ------------------------------------------------------------

	AdamsGolf.com
	@file:			retail-search.js
	@copyright:		Sean Foushee	
	@version:		0.4
	@date:			3 Jul 2010	
	@dependencies: 	jquery-1.4.2.min.js (v 1.4.2)
					supersleight.plugin.js
 
   ------------------------------------------------------------ */

// using $(function(){}); as opposed to $(document).ready...
$(function() {
	// js active, change html class
	$('html').removeClass('nojs');
	$('html').addClass('js');
	
	// fix IE6 PNG with supersleight plugin
	$('#zipsearch-options-btn img').supersleight();
	
	if (GBrowserIsCompatible()) {
		var state_coords = [
			{"state":"AK","latitude":"61.3850","longitude":"152.2683"},
			{"state":"AL","latitude":"32.7990","longitude":"86.8073"},
			{"state":"AR","latitude":"34.9513","longitude":"92.3809"},
			{"state":"AS","latitude":"14.2417","longitude":"170.7197"},
			{"state":"AZ","latitude":"33.7712","longitude":"111.3877"},
			{"state":"CA","latitude":"36.1700","longitude":"119.7462"},
			{"state":"CO","latitude":"39.0646","longitude":"105.3272"},
			{"state":"CT","latitude":"41.5834","longitude":"72.7622"},
			{"state":"DC","latitude":"38.8964","longitude":"77.0262"},
			{"state":"DE","latitude":"39.3498","longitude":"75.5148"},
			{"state":"FL","latitude":"27.8333","longitude":"81.7170"},
			{"state":"GA","latitude":"32.9866","longitude":"83.6487"},
			{"state":"HI","latitude":"21.1098","longitude":"157.5311"},
			{"state":"IA","latitude":"42.0046","longitude":"93.2140"},
			{"state":"ID","latitude":"44.2394","longitude":"114.5103"},
			{"state":"IL","latitude":"40.3363","longitude":"89.0022"},
			{"state":"IN","latitude":"39.8647","longitude":"86.2604"},
			{"state":"KS","latitude":"38.5111","longitude":"96.8005"},
			{"state":"KY","latitude":"37.6690","longitude":"84.6514"},
			{"state":"LA","latitude":"31.1801","longitude":"91.8749"},
			{"state":"MA","latitude":"42.2373","longitude":"71.5314"},
			{"state":"MD","latitude":"39.0724","longitude":"76.7902"},
			{"state":"ME","latitude":"44.6074","longitude":"69.3977"},
			{"state":"MI","latitude":"43.3504","longitude":"84.5603"},
			{"state":"MN","latitude":"45.7326","longitude":"93.9196"},
			{"state":"MO","latitude":"38.4623","longitude":"92.3020"},
			{"state":"MP","latitude":"14.8058","longitude":"145.5505"},
			{"state":"MS","latitude":"32.7673","longitude":"89.6812"},
			{"state":"MT","latitude":"46.9048","longitude":"110.3261"},
			{"state":"NC","latitude":"35.6411","longitude":"79.8431"},
			{"state":"ND","latitude":"47.5362","longitude":"99.7930"},
			{"state":"NE","latitude":"41.1289","longitude":"98.2883"},
			{"state":"NH","latitude":"43.4108","longitude":"71.5653"},
			{"state":"NJ","latitude":"40.3140","longitude":"74.5089"},
			{"state":"NM","latitude":"34.8375","longitude":"106.2371"},
			{"state":"NV","latitude":"38.4199","longitude":"117.1219"},
			{"state":"NY","latitude":"42.1497","longitude":"74.9384"},
			{"state":"OH","latitude":"40.3736","longitude":"82.7755"},
			{"state":"OK","latitude":"35.5376","longitude":"96.9247"},
			{"state":"OR","latitude":"44.5672","longitude":"122.1269"},
			{"state":"PA","latitude":"40.5773","longitude":"77.2640"},
			{"state":"PR","latitude":"18.2766","longitude":"66.3350"},
			{"state":"RI","latitude":"41.6772","longitude":"71.5101"},
			{"state":"SC","latitude":"33.8191","longitude":"80.9066"},
			{"state":"SD","latitude":"44.2853","longitude":"99.4632"},
			{"state":"TN","latitude":"35.7449","longitude":"86.7489"},
			{"state":"TX","latitude":"31.1060","longitude":"97.6475"},
			{"state":"UT","latitude":"40.1135","longitude":"111.8535"},
			{"state":"VA","latitude":"37.7680","longitude":"78.2057"},
			{"state":"VI","latitude":"18.0001","longitude":"64.8199"},
			{"state":"VT","latitude":"44.0407","longitude":"72.7093"},
			{"state":"WA","latitude":"47.3917","longitude":"121.5708"},
			{"state":"WI","latitude":"44.2563","longitude":"89.6385"},
			{"state":"WV","latitude":"38.4680","longitude":"80.9696"},
			{"state":"WY","latitude":"42.7475","longitude":"107.2085"}
		];
		
		if ( state_search ) 
		{
			$.each(state_coords,function(i,state){
				if ( state.state == state_search )
				{
					origin_lat = state.latitude;
					origin_lon = state.longitude;
				}
			});
		}
		
		var mapdiv 		= document.getElementById("mapcanvas");
		var mapwidth 	= parseInt(mapdiv.style.width);
		
		var map 		= new GMap2(mapdiv);
		var mapcenter	= new GLatLng(origin_lat, origin_lon); // origin_lon and origin_lat set at bottom of search results page
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GScaleControl());
		map.addControl(new GMenuMapTypeControl());
		map.addControl(new GNavLabelControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(5,0)));
		map.setCenter(mapcenter, 10);
		
		var selectedmarker = [];
		var gmarkers = [];
		
		//  Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.
		var baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		
		function unselectTable() {
			var numselected = selectedmarker.length;
			if ( numselected > 0 ) {
				for (var i = 0; i < numselected; i++) {
					document.getElementById(selectedmarker[i]).className = '';
				}
			}
		}
		
		function showMarker(i) {
			GEvent.trigger(gmarkers[i], "click");
		}
		
		// Creates a marker whose info window displays the letter corresponding to the given index.
		function createMarker(point, index, store) {
			
			// Create a lettered icon for this point using our icon class
			var letter = String.fromCharCode("A".charCodeAt(0) + index);
			var letteredIcon = new GIcon(baseIcon);
			letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

			// Set up our GMarkerOptions object
			markerOptions = { icon:letteredIcon };
			var marker = new GMarker(point, markerOptions);
			gmarkers[index] = marker;
			
			GEvent.addListener(gmarkers[index], "click", function() {
				unselectTable();
				// distance_unit_name below set at the bottom of the search results page
				// '<a href="#">more info &raquo;</a><br>' +
				var store_web = ( store.store_web != '' ) ? ' | <a href="'+store.store_web+'" target="_blank">Website</a>' : ''
				var storeinfo = '<div class="retailers-infowindow">' +
								'<h3>' + store.store_name + '</h3>' + 
										 store.store_address + 
								'<br>' + store.store_city + ', ' + store.store_state + ' ' + store.store_zip + 
								'<br>' + store.distance + ' ' + distance_unit_name +
								' | <a href="http://maps.google.com/maps?daddr=' + point.toUrlValue() + '" target="_blank">Directions</a>' + store_web +
								'</div>';
				marker.openInfoWindowHtml(storeinfo,{noCloseOnClick: true});
				
				var selected = "store-"+store.store_id;
				document.getElementById(selected).className = 'selected';
				selectedmarker = []
				selectedmarker.push(selected);
			});
			
			return marker;
		}
		
		var infowindow = map.getInfoWindow();
		GEvent.addListener(infowindow, "closeclick", function() {
			unselectTable();
		});
		
		// Add markers to the map
		var markers 	= map_markers;	// map_markers set at the bottom of the search results page
		var bounds 		= new GLatLngBounds;	
		var nummarkers 	= markers.length;
		
		for (var i = 0; i < nummarkers; i++) {
			var latlng = new GLatLng(markers[i].store_latitude,markers[i].store_longitude);
			map.addOverlay(createMarker(latlng, i, markers[i]));
			bounds.extend(latlng);
		}

		var recenter = map.getBoundsZoomLevel( bounds );
		recenter = ( recenter <= 14 ) ? recenter : 14;
		map.setCenter( bounds.getCenter(), recenter );
	}
	
	$(".markerlink").bind({
		click: function(event) {
			var markerindex = this.id.split('-')[1];
			showMarker(markerindex);
			event.preventDefault();
		},
		mouseenter: function(event) {
			event.preventDefault();
		}
	});
	var optionsstatus = true;
	$("#zipsearch-options-btn").bind({
		click: function(event) {
			$("#zipsearch-options-btn").toggleClass("optionsbtnclose");
			$(".options").toggleClass("hideoptions");
			if ( optionsstatus ) {
				optionsstatus = false;
				$("#zipsearch-options-btn img").attr('src','icons/info_black.png');
			} else {
				optionsstatus = true;
				$("#zipsearch-options-btn img").attr('src','icons/info_white.png');
			}
			// fix IE6 PNG with supersleight plugin
			$('#zipsearch-options-btn img').supersleight();
			event.preventDefault();
		}
	});
	
	// delete cache file
	if ( cache_file )
	{
		$("#cacheicon").click(function(){
			// send AJAX request to server to kill the selected cache file
			$.post(  
				"/retail/deletecache.php",  
				{file: cache_file},  
				function(responseText){  
					alert(responseText);  
				},  
				"html"  
			);
		});
	}
});
