/*var states = new Array();

states['AK'] = new Array();
states['AK']['latS'] = 51.210000000000001; 
states['AK']['lngW'] = -179.06;
states['AK']['latN'] = 71.359999999999999;
states['AK']['lngE'] = -129.97999999999999;

states['KY'] = new Array();
states['KY']['latS'] = 36.490000000000002; 
states['KY']['lngW'] = -89.579999999999998;
states['KY']['latN'] = 39.18; 
states['KY']['lngE'] = -81.959999999999994;

states['CO'] = new Array();
states['CO']['latS'] = 36.990000000000002; 
states['CO']['lngW'] = -109.06999999999999;
states['CO']['latN'] = 41.009999999999998; 
states['CO']['lngE'] = -102.04000000000001;

states['ND'] = new Array();
states['ND']['latS'] = 45.93; 
states['ND']['lngW'] = -104.05;
states['ND']['latN'] = 49.009999999999998; 
states['ND']['lngE'] = -96.549999999999997;

states['PR'] = new Array();
states['PR']['latS'] = 17.879999999999999; 
states['PR']['lngW'] = -67.959999999999994;
states['PR']['latN'] = 18.52; 
states['PR']['lngE'] = -65.219999999999999;

states['CT'] = new Array();
states['CT']['latS'] = 40.979999999999997; 
states['CT']['lngW'] = -73.730000000000004;
states['CT']['latN'] = 42.060000000000002; 
states['CT']['lngE'] = -71.780000000000001;

states['DC'] = new Array();
states['DC']['latS'] = 38.789999999999999; 
states['DC']['lngW'] = -77.120000000000005;
states['DC']['latN'] = 39; 
states['DC']['lngE'] = -76.900000000000006;

states['RI'] = new Array();
states['RI']['latS'] = 41.140000000000001; 
states['RI']['lngW'] = -71.872;
states['RI']['latN'] = 42.020000000000003; 
states['RI']['lngE'] = -71.120000000000005;

states['DE'] = new Array();
states['DE']['latS'] = 38.450000000000003; 
states['DE']['lngW'] = -75.790000000000006;
states['DE']['latN'] = 39.840000000000003; 
states['DE']['lngE'] = -75.040000000000006;

states['AL'] = new Array();
states['AL']['latS'] = 30.219999999999999; 
states['AL']['lngW'] = -88.480000000000004;
states['AL']['latN'] = 35.009999999999998; 
states['AL']['lngE'] = -84.890000000000001;

states['LA'] = new Array();
states['LA']['latS'] = 28.920000000000002; 
states['LA']['lngW'] = -94.049999999999997;
states['LA']['latN'] = 33.020000000000003; 
states['LA']['lngE'] = -88.810000000000002;

states['AR'] = new Array();
states['AR']['latS'] = 33.009999999999998; 
states['AR']['lngW'] = -94.620000000000005;
states['AR']['latN'] = 36.5; 
states['AR']['lngE'] = -89.640000000000001;

states['SC'] = new Array();
states['SC']['latS'] = 32.030000000000001; 
states['SC']['lngW'] = -83.359999999999999;
states['SC']['latN'] = 35.219999999999999; 
states['SC']['lngE'] = -78.540000000000006;

states['MA'] = new Array();
states['MA']['latS'] = 41.299999999999997; 
states['MA']['lngW'] = -73.510000000000005;
states['MA']['latN'] = 42.890000000000001; 
states['MA']['lngE'] = -69.920000000000002;

states['HI'] = new Array();
states['HI']['latS'] = 18.66; 
states['HI']['lngW'] = -160.35;
states['HI']['latN'] = 22.75; 
states['HI']['lngE'] = -154.78;

states['MD'] = new Array();
states['MD']['latS'] = 37.909999999999997; 
states['MD']['lngW'] = -79.489999999999995;
states['MD']['latN'] = 39.729999999999997; 
states['MD']['lngE'] = -75.048000000000002;

states['ME'] = new Array();
states['ME']['latS'] = 43.049999999999997; 
states['ME']['lngW'] = -71.090000000000003;
states['ME']['latN'] = 47.460000000000001; 
states['ME']['lngE'] = -66.939999999999998;

states['MI'] = new Array();
states['MI']['latS'] = 41.689999999999998; 
states['MI']['lngW'] = -90.420000000000002;
states['MI']['latN'] = 48.200000000000003; 
states['MI']['lngE'] = -82.409999999999997;

states['NE'] = new Array();
states['NE']['latS'] = 39.990000000000002; 
states['NE']['lngW'] = -104.06;
states['NE']['latN'] = 43.009999999999998; 
states['NE']['lngE'] = -95.299999999999997;

states['NH'] = new Array();
states['NH']['latS'] = 42.689999999999998; 
states['NH']['lngW'] = -72.560000000000002;
states['NH']['latN'] = 45.310000000000002; 
states['NH']['lngE'] = -70.700000000000003;

states['AZ'] = new Array();
states['AZ']['latS'] = 31.329999999999998; 
states['AZ']['lngW'] = -114.81999999999999;
states['AZ']['latN'] = 37.009999999999998; 
states['AZ']['lngE'] = -109.04000000000001;

states['NJ'] = new Array();
states['NJ']['latS'] = 38.920000000000002; 
states['NJ']['lngW'] = -75.560000000000002;
states['NJ']['latN'] = 41.359999999999999; 
states['NJ']['lngE'] = -73.890000000000001;

states['NM'] = new Array();
states['NM']['latS'] = 31.329999999999998; 
states['NM']['lngW'] = -109.06;
states['NM']['latN'] = 37.009999999999998; 
states['NM']['lngE'] = -103;

states['NV'] = new Array();
states['NV']['latS'] = 35; 
states['NV']['lngW'] = -120.01000000000001;
states['NV']['latN'] = 42.009999999999998; 
states['NV']['lngE'] = -114.03;

states['SD'] = new Array();
states['SD']['latS'] = 42.469999999999999; 
states['SD']['lngW'] = -104.06;
states['SD']['latN'] = 45.950000000000003; 
states['SD']['lngE'] = -96.430000000000007;

states['TX'] = new Array();
states['TX']['latS'] = 25.829999999999998; 
states['TX']['lngW'] = -106.65000000000001;
states['TX']['latN'] = 36.509999999999998; 
states['TX']['lngE'] = -93.510000000000005;

states['UT'] = new Array();
states['UT']['latS'] = 36.990000000000002; 
states['UT']['lngW'] = -114.06;
states['UT']['latN'] = 42.009999999999998; 
states['UT']['lngE'] = -109.04000000000001;

states['CA'] = new Array();
states['CA']['latS'] = 32.530000000000001; 
states['CA']['lngW'] = -124.41;
states['CA']['latN'] = 42.009999999999998; 
states['CA']['lngE'] = -114.13;

states['TN'] = new Array();
states['TN']['latS'] = 34.979999999999997; 
states['TN']['lngW'] = -90.319999999999993;
states['TN']['latN'] = 36.68; 
states['TN']['lngE'] = -81.640000000000001;

states['FL'] = new Array();
states['FL']['latS'] = 24.350000000000001; 
states['FL']['lngW'] = -87.640000000000001;
states['FL']['latN'] = 31.010000000000002; 
states['FL']['lngE'] = -80.030000000000001;

states['GA'] = new Array();
states['GA']['latS'] = 30.350000000000001; 
states['GA']['lngW'] = -85.609999999999999;
states['GA']['latN'] = 35.009999999999998; 
states['GA']['lngE'] = -80.840000000000003;

states['IA'] = new Array();
states['IA']['latS'] = 40.369999999999997; 
states['IA']['lngW'] = -96.640000000000001;
states['IA']['latN'] = 43.509999999999998; 
states['IA']['lngE'] = -90.140000000000001;

states['ID'] = new Array();
states['ID']['latS'] = 41.979999999999997; 
states['ID']['lngW'] = -117.25;
states['ID']['latN'] = 49.009999999999998; 
states['ID']['lngE'] = -111.04000000000001;

states['IL'] = new Array();
states['IL']['latS'] = 36.969999999999999; 
states['IL']['lngW'] = -91.519999999999996;
states['IL']['latN'] = 42.509999999999998; 
states['IL']['lngE'] = -87.489999999999995;

states['IN'] = new Array();
states['IN']['latS'] = 37.770000000000003; 
states['IN']['lngW'] = -88.060000000000002;
states['IN']['latN'] = 41.770000000000003; 
states['IN']['lngE'] = -84.780000000000001;

states['KS'] = new Array();
states['KS']['latS'] = 36.990000000000002; 
states['KS']['lngW'] = -102.06;
states['KS']['latN'] = 40.009999999999998; 
states['KS']['lngE'] = -94.579999999999998;

states['MN'] = new Array();
states['MN']['latS'] = 43.490000000000002; 
states['MN']['lngW'] = -97.239999999999995;
states['MN']['latN'] = 49.390000000000001; 
states['MN']['lngE'] = -89.480000000000004;

states['MO'] = new Array();
states['MO']['latS'] = 35.990000000000002; 
states['MO']['lngW'] = -95.780000000000001;
states['MO']['latN'] = 40.619999999999997; 
states['MO']['lngE'] = -89.090000000000003;

states['MS'] = new Array();
states['MS']['latS'] = 30.170000000000002; 
states['MS']['lngW'] = -91.650000000000006;
states['MS']['latN'] = 35; 
states['MS']['lngE'] = -88.099999999999994;

states['MT'] = new Array();
states['MT']['latS'] = 44.350000000000001; 
states['MT']['lngW'] = -116.06;
states['MT']['latN'] = 49.009999999999998; 
states['MT']['lngE'] = -104.03;

states['NY'] = new Array();
states['NY']['latS'] = 40.490000000000002; 
states['NY']['lngW'] = -79.769999999999996;
states['NY']['latN'] = 45.020000000000003; 
states['NY']['lngE'] = -71.849999999999994;

states['OH'] = new Array();
states['OH']['latS'] = 38.399999999999999; 
states['OH']['lngW'] = -84.829999999999998;
states['OH']['latN'] = 41.979999999999997; 
states['OH']['lngE'] = -80.510000000000005;

states['OK'] = new Array();
states['OK']['latS'] = 33.609999999999999; 
states['OK']['lngW'] = -103.01000000000001;
states['OK']['latN'] = 37.009999999999998; 
states['OK']['lngE'] = -94.430000000000007;

states['OR'] = new Array();
states['OR']['latS'] = 41.990000000000002; 
states['OR']['lngW'] = -124.56;
states['OR']['latN'] = 46.280000000000001; 
states['OR']['lngE'] = -116.45999999999999;

states['VA'] = new Array();
states['VA']['latS'] = 36.539999999999999; 
states['VA']['lngW'] = -83.680000000000007;
states['VA']['latN'] = 39.469999999999999; 
states['VA']['lngE'] = -75.239999999999995;

states['VT'] = new Array();
states['VT']['latS'] = 42.719999999999999; 
states['VT']['lngW'] = -73.439999999999998;
states['VT']['latN'] = 45.020000000000003; 
states['VT']['lngE'] = -71.459999999999994;

states['WA'] = new Array();
states['WA']['latS'] = 45.539999999999999; 
states['WA']['lngW'] = -124.73999999999999;
states['WA']['latN'] = 49.009999999999998; 
states['WA']['lngE'] = -116.91;

states['WI'] = new Array();
states['WI']['latS'] = 42.490000000000002; 
states['WI']['lngW'] = -92.890000000000001;
states['WI']['latN'] = 47.079999999999998; 
states['WI']['lngE'] = -86.799999999999997;

states['WV'] = new Array();
states['WV']['latS'] = 37.200000000000003; 
states['WV']['lngW'] = -82.650000000000006;
states['WV']['latN'] = 40.640000000000001; 
states['WV']['lngE'] = -77.709999999999994;

states['WY'] = new Array();
states['WY']['latS'] = 40.990000000000002; 
states['WY']['lngW'] = -111.06;
states['WY']['latN'] = 45.009999999999998; 
states['WY']['lngE'] = -104.05;

states['NC'] = new Array();
states['NC']['latS'] = 33.840000000000003; 
states['NC']['lngW'] = -84.329999999999998;
states['NC']['latN'] = 36.590000000000003; 
states['NC']['lngE'] = -75.450000000000003;

states['PA'] = new Array();
states['PA']['latS'] = 39.710000000000001; 
states['PA']['lngW'] = -80.519999999999996;
states['PA']['latN'] = 42.270000000000003; 
states['PA']['lngE'] = -74.680000000000007;
*/
addLoadEvent(initialize);
var baseIcon = new GIcon();
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);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

function initialize() {
	//alert(sSearchType);
	var bHideMap = document.getElementById("tdMap") == null;
	//alert(bHideMap);
	if (window.aCenters === undefined || window.aCenters < 1 || bHideMap == true){
		return;
	}

	else if (!(aCenters[0][9] == 'USA' || aCenters[0][9] == 'US' || aCenters[0][9] == 'CAN' || aCenters[0][9] == 'CA')){
		hideMapView()
	}
	else{
		if (GBrowserIsCompatible()) {

			//this line was added to remove the background image of the table cell when the map displays
			document.getElementById('collapsemap').style.backgroundImage = 'none';
	
			function createMarker(point,html,index) {
	          // Create a lettered icon for this point using our icon class
	          //var letter = String.fromCharCode("A".charCodeAt(0) + index);

	          var letter = String.fromCharCode("A".charCodeAt(0) + (index-1));
      	    var letteredIcon = new GIcon(baseIcon);
var marker;
			if (sSearchType == 'state'){
				
				letteredIcon.image = "/_assets/images/markers/marker" + index + ".png";
		      	    	markerOptions = { icon:letteredIcon };
			}
			else
			{
          			letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		      	    	markerOptions = { icon:letteredIcon };
	  	        	
			}
	          	// Set up our GMarkerOptions object

  	        	var marker = new GMarker(point, markerOptions);
//		        	var marker = new GMarker(point);
			        GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(html);
				        });
			         return marker;
		       }


           		var map = new GMap2(document.getElementById("map"));
           	//	map.setCenter(new GLatLng(37.4419, -122.1419), 0);
	   		map.addControl(new GSmallMapControl());
	   		map.addControl(new GMapTypeControl());
	  
			if (sSearchType == 'state'){
			   //alert(sSearchType)
				var thisState = aCenters[0][7];
				var bounds = new GLatLngBounds();
				//var bounds = new GLatLngBounds(new GLatLng(states[thisState]['latS'], states[thisState]['lngW']), new GLatLng(states[thisState]['latN'], states[thisState]['lngE']));				
			}
			else{
   	   			var bounds = new GLatLngBounds();

			}
                
            	var coords = new GLatLng(aCenters[0][0], aCenters[0][1]);      				
		    map.setCenter ( coords, 0 );
			var icntr = 0;
			for (var loc in aCenters){
			       //alert(aCenters[loc][0]);
				//create point
				// I am not sure I am getting some dummy values so I am eliminating them here				
				if (aCenters[loc][0] === undefined)
				{
				    
				}
				else
				{
				   if (aCenters[loc][0] >0)
				   {
				   
      				    var point = new GLatLng(aCenters[loc][0], aCenters[loc][1]);      						               
				        //create the popup
				        //var html = aCenters[loc][2]+'<br />';
					var html = '<a href="/store' + aCenters[loc][5] + '">' + aCenters[loc][2] +'</a><br />'

				        var locaddr = ""; 
        				
				        if (aCenters[loc][3] != 'n/a' && aCenters[loc][3] != ''){
					        html += aCenters[loc][3]+'<br />';						    
					        locaddr +=  aCenters[loc][3] + ', ';
				        }
				        /*if (aCenters[loc][5] != 'n/a' && aCenters[loc][5] != ''){
					        html += aCenters[loc][5]+'<br />';					         
				        }
					*/
				        
				        html += aCenters[loc][6]+', '+aCenters[loc][7]+' '+aCenters[loc][8]+' '+aCenters[loc][9]+'<br /> ';
                        		locaddr +=  aCenters[loc][6]+', '+aCenters[loc][7]+' '+aCenters[loc][8];
				        if (aCenters[loc][10] != 'n/a' && aCenters[loc][10] != ''){
					        html += aCenters[loc][10]+'<br />';	
				        }
        				
    				    
    				    	html += '<a href="http://maps.google.com/maps?saddr=' + aCenters[loc][11] + '&daddr='+ locaddr + '&hl=en" target="new">Driving Directions</a><br />'
        				
				        if (aCenters[loc][11] != 'n/a' && aCenters[loc][10] !=''){
					        //html += '<a href="'+aCenters[loc][11]+'" target="new">Visit Our Website</a>';
				        }             
    						icntr = icntr + 1; 
      				    var marker = createMarker(point, html,icntr )
      				    map.addOverlay(marker);
 				           
                        
				        //if (sSearchType != 'state')
				        {
					        bounds.extend(point);
				        }
				    }
				else
				{
				          icntr = icntr + 1;  
				}
				}
			}
				//set zoom level according to bounds
				var newZoom;				
				{
				    newZoom = map.setZoom(map.getBoundsZoomLevel(bounds)-1);
				    map.setCenter(bounds.getCenter(),newZoom);
				}
				//set center according to bounds
				
		}

	}
}

function showListView() {
	document.getElementById("storelist").style.display = "block";
	document.getElementById("storemap").style.visibility = "hidden";
}
function showMapView() {
	document.getElementById("storelist").style.display = "none";
	document.getElementById("storemap").style.visibility = "visible";
}
function hideMapView()
{
  document.getElementById("storemap").style.visibility = "hidden"; 
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
