
// Global Variables
var map = null;
var pinLocations = new PinLocations();
var zoom = 9;
var streetLevelZoom = 16;


//Basic Map load
function GetMap()
{
	// looks for the 'myMap' div to load the map into
	map = new VEMap('myMap');
	// Loads the pins from the search
	map.onLoadMap = pinLocations.CreatePins;
	map.LoadMap(new VELatLong(62.36042812948593,16.29809807986021), 5 ,'r' ,false);
	
	// 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 = "";
	this.filter = "";
}
	
//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+ "<br />" + pin.Phone + "<br />" + pin.filter);
			map.AddShape(shape);				
		}
	}
}

// wrties in the icons for the different filters in the bubble popup on the map
function WriteProperties(PromotionalOffer,biolage,colorsync)
{
	var strReturn = "";
	if (PromotionalOffer == "True" || PromotionalOffer == "-1")
		strReturn = "<IMG SRC='/_sv/_se/salon/img/key_icon_promo.gif' ALT='' BORDER='0'>";
	if(biolage == "True" || biolage == "-1")
		strReturn = addPlus(strReturn,"<IMG SRC='/_sv/_se/salon/img/key_icon_biolage.gif' ALT='' BORDER='0'>");
	
	if (colorsync == "True" || colorsync == "-1")
		strReturn = addPlus(strReturn,"<IMG SRC='/_sv/_se/salon/img/key_icon_sync.gif' ALT='' BORDER='0'>");
	
	return strReturn;
}

// adds a space between the icons for the bubble popup
function addPlus(strCurrent, strAdd){
	if (strCurrent != "")
		strCurrent = strCurrent + "&nbsp;" + strAdd;
	else
		strCurrent = strAdd;
	
	return strCurrent;
}
// 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 promotionalOffer = qs3.get("PromotionalOffer");
	
	if(promotionalOffer != null)
	{
		if(promotionalOffer == "true")
			GetElement("PromotionalOffer", formName).checked = "true";
	}
	
	var Biolage = qs3.get("biolage");
	
	if(Biolage != null)
	{
		if(Biolage == "true")
			GetElement("biolage", formName).checked = "true";
	}
		
	var colorsync = qs3.get("colorsync");
	if(colorsync != null)
	{
		if(colorsync == "true")
			GetElement("colorsync", formName).checked = "true";
	}

}

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;
	}
}