﻿var map = null;
var mapElement = null;
var mapDirElement = null;
var mapErrorElement = null;
var icon = '<div style="position:relative; top:-28px;"><img src="http://image.coldwatercreek.com/StoreLoc/081118_CWC_marker.gif" /></div>';

var useLatLong = false;
var storeLat = '';
var storeLong = '';
var eAddressLine = '';
var eStoreName = '';
var storeDescription = '';
var fromAddress = '';
var mapScripts;

function SetMapVariables(storeID, storeName, address, city, state, zipCode) {
	var _storeAddrs = (address.indexOf('#') > 0) ? address.replace(address.substring(address.indexOf('#'),address.length),'') : address;
	_storeAddrs = _storeAddrs.replace(/^\s*/, '').replace(/\s*$/, '').replace(/,$/, '').replace(/\s*$/, '');
	
	var _mapElement = document.getElementById('myMap');
	var _mapDirElement = document.getElementById('mapDirs');
	var _mapErrorElement = document.getElementById('mapDirs');
	var _useLatLong = false;
	var _storeLat = '';
	var _storeLong = '';

	switch (storeID) {
		case 108:
			_useLatLong = true;
			_storeLat = '33.56162711864402';
			_storeLong = '-117.66891358024625';
			break;
		case 340:
			_useLatLong = true;
			_storeLat = '47.04451405870334';
			_storeLong = '-122.92682468891145';
			break;
		case 356:
			_useLatLong = true;
			_storeLat = '45.70757';
			_storeLong = '-111.06554';
			break;
		case 436:
			_useLatLong = true;
			_storeLat = '34.00217214490464';
			_storeLong = '-117.73236751556398';
			break;
		case 443:
			_useLatLong = true;
			_storeLat = '38.779614102631655';
			_storeLong = '-90.79041481018066';
			break;
		case 445:
			_useLatLong = true;
			_storeLat = '28.185669491525367';
			_storeLong = '-82.35362962962897';
			break;
		case 456:
			_useLatLong = true;
			_storeLat = '38.981338983050776';
			_storeLong = '-76.54322222222156';
			break;
		case 466:
			_useLatLong = true;
			_storeLat = '26.2529';
			_storeLong = '-81.7992';
			break;
		case 9139:
			_useLatLong = true;
			_storeLat = '39.20612027345347';
			_storeLong = '-81.52726113796236';
			break;
		case 484:
			_useLatLong = true;
			_storeLat = '33.678046';
			_storeLong = '-111.964508';
			break;
		default:
			break;
	}
	
	var _eAddressLine = _storeAddrs + ', ' + zipCode;
	var _eStoreName = storeName;
	var _storeDescription = '<span class="pushPinDesc">' + _storeAddrs + '<br />' + city + ', ' + state + '&#160;&#160;' + zipCode + '</span>';
	var _fromAddressElement = document.getElementById('myMap');
	
	SetVariablesAndRun(_mapElement,_mapDirElement,_mapErrorElement,_useLatLong,_storeLat,_storeLong,_eAddressLine,_eStoreName,_storeDescription);
}

/*function pageLoad()
{
	mapScripts = document.getElementById('mapScripts');
	if (mapScripts)
	{
		eval(mapScripts.innerHTML);
	}
	if (typeof SetMapVariables == 'function')
	{
		SetMapVariables();
	}
}

function ExecuteFunction(runFunction)
{
	if (mapScripts)
	{
		eval(mapScripts.innerHTML);
		eval(runFunction);
	}
}*/

function SetVariablesAndRun(_mapElement,_mapDirElement,_mapErrorElement,_useLatLong,_storeLat,_storeLong,_eAddressLine,_eStoreName,_storeDescription)
{
	mapElement = _mapElement;
	mapDirElement = _mapDirElement;
	mapErrorElement = _mapErrorElement;
	useLatLong = _useLatLong;
	storeLat = _storeLat;
	storeLong = _storeLong;
	eAddressLine = _eAddressLine;
	eStoreName = _eStoreName;
	storeDescription = _storeDescription;
	
	GetMap();
}

function GetMap()
{
	map = new VEMap('myMap');
	map.SetDashboardSize(VEDashboardSize.Small);
	map.LoadMap();
	try
	{
		if (useLatLong)
		{
			var nLatLong = new VELatLong(storeLat, storeLong);
			map.FindLocations(nLatLong,FindLoc_Callback);
		}
		else
		{
			map.Find(null, eAddressLine, null, null, null, null, null, null, null, null, Find_Callback);
		}
	}
	catch(ex)
	{
		OnCatchError(ex.message, 'There was an error initializing the map.');
	}
}

function Find_Callback(layer, resultsArray, places, hasMore, veErrorMessage)
{
	SetPinAndZoom(map.GetCenter());
}

function FindLoc_Callback(places)
{
	SetPinAndZoom(places[0].LatLong);
}

function SetPinAndZoom(latLong)
{
	try
	{
		
		//Adds push pin
		var centerPushPin = new VEShape(VEShapeType.Pushpin, latLong);
		centerPushPin.SetCustomIcon(icon);
		centerPushPin.SetTitle(eStoreName);
		centerPushPin.SetDescription(storeDescription);
		centerPushPin.SetZIndex(null,99999);
		map.AddShape(centerPushPin);
		
		//Sets center and better zoom level
		map.SetCenterAndZoom(latLong,'15');
		
		//Shows map once all is set
		mapElement.style.visibility = 'visible';
	}
	catch(ex)
	{
		OnCatchError(ex.message,'There was an issue setting up the map.');
	}
}

function SetVarsGetDir(_fromAddress)
{
	fromAddress = _fromAddress;
	GetStoreDirs();
}

function GetStoreDirs()
{
	try
	{
		////Shows loading screen
		//document.getElementById('_divLoadingContent').style.display = 'block';
		//document.getElementById('_divLoadingMessage').style.display = 'block';
		//$find('_pnlLoadingModalPopupBehavior').show();
		
		//Runs a function that you can place on your page if it exists
//		if (mapScripts)
//		{
//			eval(mapScripts.innerHTML);
//		}
//		if (typeof DoUponGetStoreDirs == 'function')
//		{
			DoUponGetStoreDirs();
//		}
		
		//var fromAddress = document.getElementById('_txtAddress').value;
		map.DeleteAllShapes();
		
		var options = new VERouteOptions();
		options.RouteCallback = OnGotRoute;
		options.ShowDisambiguation = false;
		options.ShowErrorMessages = false;
		
		if (useLatLong)
		{
			var toLatLong = new VELatLong(storeLat, storeLong);
			map.GetDirections([fromAddress, toLatLong], options);
		}
		else
		{
		map.GetDirections([fromAddress, eAddressLine], options);
		}

	}
	catch(ex)
	{
		OnCatchError(ex.message, 'There was an error retrieving directions from the address provided.' + ex.message);
	}
}

function OnGotRoute(route)
{
	//$find('_pnlLoadingModalPopupBehavior').hide();
//	if (mapScripts)
//	{
//		eval(mapScripts.innerHTML);
//	}
//	if (typeof DoUponOnGotRoute == 'function')
//	{
		DoUponOnGotRoute();
//	}
	
	try
	{
		// Unroll route
		var legs     = route.RouteLegs;
		var turns    = '<div>Total distance: ' + route.Distance.toFixed(1) + ' miles</div>';
		var numTurns = 0;
		var leg      = null;
		
		// Get intermediate legs
		for(var i = 0; i < legs.length; i++)
		{
			 // Get this leg so we don't have to derefernce multiple times
			 leg = legs[i];  // Leg is a VERouteLeg object
			 
			 //Sets Icon for Final Destination.
			 leg.Itinerary.Items[(leg.Itinerary.Items.length) - 1].Shape.SetCustomIcon(icon);
			 
			 // Unroll each intermediate leg
			 var turn = null;  // The itinerary leg
			 for(var j = 0; j < leg.Itinerary.Items.length; j++)
			 {
				turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
				numTurns++;
				turns += '<div class="mapDir_turns">' + numTurns + '. ' + turn.Text + ' (' + turn.Distance.toFixed(1) + ' mi)</div>';
				
				//Shows hints if any
				var hints = turn.Hints;
				var hint = null;
				if (hints)
				{
					for (var k = 0; k < hints.length; k++)
					{
						hint = hints[k];
						turns += '<div class="mapDir_hints">' + hint.Text + '</div>';
					}
				}
			 }
		}
		
		mapDirElement.innerHTML = turns;
	}
	catch(ex)
	{
		OnCatchError(ex.message, 'There was an error retrieving directions from the address provided.');
	}
}

function OnCatchError(errorMessage, errorDescription)
{
	if (mapErrorElement)
	{
		mapErrorElement.innerHTML = 'Error: ' + errorDescription;
		mapErrorElement.style.visibility = 'visible';
	}
}

function getDirsEnterKey(e,runFunction)		//this function keeps the form from submitting and instead performs the function passed in
{
	var key;
	if(window.event)
	{
		key = e.keyCode;		//IE
	}
	else
	{
		key = e.which;		//firefox
	}
	if(key == 13)
	{
		//eval(runFunction);
		runFunction();
		return false;
	}
	else
	{
		return true;
	}
}

//Store Availability specific functions

function GetMapDirs()
{
	var fromAddress = '';
	fromAddress += (document.getElementById('_txtFromAddress').value != '') ? document.getElementById('_txtFromAddress').value + ', ' : '';
	fromAddress += (document.getElementById('_txtFromCity').value != '') ? document.getElementById('_txtFromCity').value + ', ' : '';
	fromAddress += document.getElementById('_ddlFromState').value;
	fromAddress += (document.getElementById('_txtFromZip').value != '') ? ', ' + document.getElementById('_txtFromZip').value : '';
	
	SetVarsGetDir(fromAddress);
}

function DoUponGetStoreDirs()
{
	document.getElementById('mapDirsWrapper').style.display = 'block';
	ShowStoreAvailabilityLoadingModal();
	//$find('_storeAvailabilityPopupBehavior').show();
}

function DoUponOnGotRoute()
{
	//$find('_storeAvailabilityPopupBehavior').hide();
	document.getElementById('_divStoreLoadingContent').style.display = 'none';
	document.getElementById('_divStoreMainContent').style.display = 'block';
	$find('_storeAvailabilityPopupBehavior').show();
}
