
// Global Variables
var map = null;
var pinLocations = new PinLocations();
var zoom = 9;
var streetLevelZoom = 16;


//Basic Map load
function GetMap()
{
    var maptemp = document.getElementById("myMap");
    // make sure the div holding map is there before trying to use it
	if (maptemp != null)
	{
        //alert("called");
	    // looks for the 'myMap' div to load the map into
	    map = new VEMap('myMap');
	    //alert("1");
	    // Loads the pins from the search
	    map.onLoadMap = pinLocations.CreatePins;
	    //alert("2");
	    map.LoadMap();
	    //alert("3");
    	
	    // sets the center of the map to the first returned location and
	    // zooms into the default zoom level
	    if(pinLocations.locations.length > 0)
	    {
		    var point = new VELatLong(pinLocations.locations[0].Lat, pinLocations.locations[0].Long);
    		
		    map.SetCenterAndZoom(point, findZoomLevel());
	    }
	 }
} 

//dispose map etc
function DisposeMap() 
{
    if (map != null)
        map.Dispose();
}


// Sets the maps scale based on the default users selection - or 9 (20 miles) by default
function findZoomLevel()
{
	var qs = new Querystring();
	var radius = qs.get("radius");
	var zoomLevel = zoom;
	
	if(radius != "" && radius != null)
	{
		if(radius == 80)
			zoomLevel = 7;
		else if(radius == 40)
			zoomLevel = 7;
		else if(radius == 20)
			zoomLevel = 9;
		else if(radius == 10)
			zoomLevel = 10;
		else if(radius == 5)
			zoomLevel = 11; 
	}
	return zoomLevel;		
}

function DisplayLocation(latitude, longitude)
{
	var point = new VELatLong(latitude, longitude);
	map.SetCenterAndZoom(point, streetLevelZoom);
}

// This is the standard VE contols
function ChangeDashBoard(style)
{            
	//Dispose the map if it was previously loaded.
	if (map!=null)
		map.Dispose();
		
	map = new VEMap('myMap');
	
	if(style == "normal")
		map.SetDashboardSize(VEDashboardSize.Normal);
	else if (style == "small")
		map.SetDashboardSize(VEDashboardSize.Small);
	else
		map.SetDashboardSize(VEDashboardSize.Tiny);
		
	map.onLoadMap = pinLocations.CreatePins;		
	map.LoadMap();
	
	if(pinLocations.locations.length > 0)
	{
		var point = new VELatLong(pinLocations.locations[0].Lat, pinLocations.locations[0].Long);
		map.SetCenterAndZoom(point, zoom);
	}
}               

// Shows or hides the dashboard (map controls)
function ViewDashboard(visible)
{
	if(visible)
		map.ShowDashboard();
	else
		map.HideDashboard();
}

// Classes to help take the locator data from Mappoint  
// and populate the VE map

// PinLocation Class
// This stores the individual store information
function PinLocation()
{
	this.Index = 0;
	this.Lat = "";
	this.Long = "";
	this.Title = "";
	this.Address1 = "";
	this.Address2 = "";
	this.City = "";
	this.State = "";
	this.Zip = "";
	this.Phone = "";
}

//PinLocations Class
// Used to hold all the locations that are returned by Mappoint
function PinLocations()
{
	// Array used to store the PinLocation objects
	this.locations = [];
	
	// Used to add new locations to the array
	this.Add=function(pinLocation)
	{
		if(this.locations.length == 0)
			this.locations[0] = pinLocation;
		else
			this.locations[this.locations.length] = pinLocation;
	}
	
	// Iterates through all the PinLocation objects and creates the marker on the map
	this.CreatePins=function()
	{	
		for (var i=0; i<pinLocations.locations.length; i++) 
		{	
			var pin = pinLocations.locations[i];
			
			var point = new VELatLong(pin.Lat, pin.Long);
			var shape = new VEShape(VEShapeType.Pushpin,point); 
			var itemCnt = i + 1;
			shape.SetCustomIcon("<div class='pinStyle2' onclick='DisplayLocation(" + pin.Lat + " , " + pin.Long + ");'><div class='text'>" + pin.Index + "</div></div>"); 
			//shape.SetCustomIcon("<div class='pinStyle2' onclick='alert(\"here\");'><div class='text'>" + pin.Index + "</div></div>"); 
			shape.SetTitle(pin.Title);
			shape.SetDescription(pin.Address1 + "<br/><div>" + pin.Address2 + "</div>" + pin.City + ", " + pin.State + " " + pin.Zip);
			map.AddShape(shape);				
		}
	}
}
// Other functions used on the Search/Results page to help:
// 1. Retain the search criteria by the user - SetMappointViewState
// 2. Parse the querystring for the variables

// The beginning section can be used for all sites
// The bottom section is more related to the brands needs
function SetMappointViewState()
{
	// Basic gerneric items
	var qs = new Querystring();
	var state = qs.get("state");
	var city = qs.get("city");
	var zip = qs.get("zipcode");
	var radius = qs.get("radius");
	var results = qs.get("results");
	var formName = GetMainFormName();
	
	// set the state in drop down
	if(state != "" && state != null)
		document.getElementById(state).selected = "true";
	// set the radius in drop down
	if(radius != "" && radius != null)
		document.getElementById("radius_" + radius).selected = "true";
	// set the results in drop down
	if(results != "" && results != null)
		document.getElementById("results_" + results).selected = "true";
	// set the city textbox
	if(city != "" && city != null)
		GetElement("city", formName).value = city;
	// set the zipcode textbox
	if(zip != "" && zip != null)	
		GetElement("zipcode", formName).value = zip;
	
	// OTHER SITE SPECIFIC PROPERTIES	
	var searchFilter = qs.get("SearchFilter");

	var qs2 = new Querystring(searchFilter, ";", "?");
	var entryType = qs2.get("EntryType");
	if(entryType != null)
	{
		if(entryType == "P")
			document.getElementById("Entry_P").checked = "true";
		if(entryType == "S")
			document.getElementById("Entry_S").checked = "true";

		GetElement("EntryType", formName).value = entryType;
	}
		
	var qs3 = new Querystring(searchFilter, ";", "|");
	var hasAnthelios = qs3.get("HasAnthelios");
	
	if(hasAnthelios != null)
	{
		if(hasAnthelios == "true")
			GetElement("HasAnthelios", formName).checked = "true";
	}
	
	var isAdvanced = qs3.get("IsAdvanced");
	
	if(isAdvanced != null)
	{
		if(isAdvanced == "true")
			GetElement("IsAdvanced", formName).checked = "true";
	}
		
	var circle_Of_Excellence = qs3.get("Circle_Of_Excellence");
	if(circle_Of_Excellence != null)
	{
		if(circle_Of_Excellence == "true")
			GetElement("Circle_Of_Excellence", formName).checked = "true";
	}


    // set the hidden fields..to store them on post back, otherwise they were getting lost
    var selState = GetElement("selState", formName);
    Sethiddenvalue(selState[selState.selectedIndex].value,'state'); 
    
    var selRadius = GetElement("Select1", formName);
    Sethiddenvalue(selRadius[selRadius.selectedIndex].value,'radius');
    
    var selResults = GetElement("Select2", formName);
    Sethiddenvalue(selResults[selResults.selectedIndex].value,'results');
}

function Querystring(qs, delimiter, splitter) { 
	this.params = new Object()
	this.get=Querystring_get;
	
	if(delimiter == null) delimiter = "&";
		
	if(splitter == null) splitter = "=";
		
	if (qs == null)	qs=location.search.substring(1,location.search.length);

	if (qs.length == 0) return;

	// Turn <plus> back to <space>
	qs = qs.replace(/\+/g, ' ');
	// parse out name/value pairs separated via &
	var args = qs.split(delimiter) ;
	
	// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split(splitter);
		var name = unescape(pair[0]);

		if (pair.length == 2)
			value = unescape(pair[1]);
		else
			value = name;
		
		this.params[name] = value;
	}
}

function Querystring_get(key, default_) {
	// This line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key];
	if (value==null) value=default_;
	
	return value;
}

// set the hidden field
function Sethiddenvalue(val,field)
{
	var _f = document.forms[0];
	for (i=0;i<_f.elements.length;i++)
	{
		if (_f.elements[i].id.indexOf(field) != -1)
			_f.elements[i].value=val;
	}
}

function GetUrl(tCity,tCountry,tState,tLatitude,tLongitude)
{
    var formName = GetMainFormName();
    var selState = GetElement("selState", formName);
    var tSelState = selState[selState.selectedIndex].value;
    window.location.href = "/stores/index.aspx?zipcode=&city=" + tCity + "&cntry=" + tCountry + "&state=" + tSelState + "&latlong=" + tLatitude + "," + tLongitude + "";
}