var map = null;
var detailspanel = null;
var tiles=new Array();
var totalNumberOfTiles=0;
var currentView='r';
var currentViewBeforeBirsEye='';
var secondsToWait=3*1000;
var secondsToWaitSearching=5*1000;
var doPoly=false;
var returnedStartAddressInfos=null;
var startAddressIndexUsed=0;
var returnedStores=null;
var currentLeftHandDiv=null;
var isMapExpanded=false;
var currentSelectedStore=0;
var returnedDrivingAddressInfos=null;
var drivingDirectionsAddressIndexUsed=0;
var drivingDirectionSegments=null;
var currentlyShowingMap="";
var drivingDirectionsTime=0;
var drivingDirectionsMiles=0;
var showDetailView=true;
var scene=null;
var otherStoreCount=0;

var changeLocationHTML="<span class='changelocation'><a href='' onclick='SwitchToStartAddressFromListOfStores(); return false;'>search for a different location</a></span>";

var poly=null;
var pts = [];
var lineColors = ['red','green','blue','orange','yellow','gray','white'];
var currLineColor = 0;
var isPanning = false;
var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bMoz = (navigator.appName == 'Netscape');

var w = window; var d = document; var browserType; if (w.opera) { if (w.print && d.childNodes) { browserType = "opera7"; } else { if (w.print) { browserType = "opera6"; } else { browserType = "opera5orLess"; }}}

function TestAjax()
{
	// check and see if we are safari or not
	if(bSaf)
	{
		window.location="/storelocator/noajax.aspx";
	}
	var intTimeOut=self.setTimeout("TestAjax_Timeout()", 5*1000);
	// make a call off to AJAX
	ajax.SupportAjax(intTimeOut,TestAjax_Callback);
}
function TestAjax_Callback(response)
{	// clear out the timeout
	try
	{
		self.clearTimeout(response.value);
	}
	catch(ex)
	{
	}
	// start the process
	StartStore();
}
function TestAjax_Timeout()
{
	// we didn't get anything back from the ajax call in the time period
	window.location="/storelocator/noajax.aspx";
}
function GetSessionInfo()
{
	var historyObject=new Object();
	historyObject.ReturnedStores=returnedStores;
	historyObject.returnedStartAddressInfos=returnedStartAddressInfos;
	historyObject.startAddressIndexUsed=startAddressIndexUsed;
	historyObject.returnedDrivingAddressInfos=returnedDrivingAddressInfos;
	historyObject.drivingDirectionsAddressIndexUsed=drivingDirectionsAddressIndexUsed;
	historyObject.drivingDirectionSegments=drivingDirectionSegments;
	historyObject.drivingDirectionsTime=drivingDirectionsTime;
	historyObject.drivingDirectionsMiles=drivingDirectionsMiles;
	historyObject.scene=scene;
	historyObject.otherStoreCount=otherStoreCount;
	return historyObject;
}

function ProcessBack(newLocation,historyObject)
{
	// init all the vars
	returnedStores=historyObject.ReturnedStores;
	returnedStartAddressInfos=historyObject.returnedStartAddressInfos;
	startAddressIndexUsed=historyObject.startAddressIndexUsed;
	returnedDrivingAddressInfos=historyObject.returnedDrivingAddressInfos;
	drivingDirectionsAddressIndexUsed=historyObject.drivingDirectionsAddressIndexUsed;
	drivingDirectionSegments=historyObject.drivingDirectionSegments;
	drivingDirectionsTime=historyObject.drivingDirectionsTime;
	drivingDirectionsMiles=historyObject.drivingDirectionsMiles;
	scene=historyObject.scene;
	otherStoreCount=historyObject.otherStoreCount;

	switch(newLocation)
	{
	case 'startaddress':
		RemoveDrivingDirectionsPopup();
		UpdateDiv(currentLeftHandDiv,"none",0,"");
		UpdateDiv("enterStartAddressDiv","block",0,"");
		UpdateDiv("mapContainerDiv","none",0,"");
		UpdateDiv("instructionsDiv","block",0,"");
		break;
	case 'startaddresslist':
		RemoveDrivingDirectionsPopup();
		UpdateDiv(currentLeftHandDiv,"none",0,"");
		UpdateDiv("matchingAddressDiv","block",0,CreateMoreThenOneStartAddressReturnedHtml());
		UpdateDiv("mapContainerDiv","none",0,"");
		UpdateDiv("instructionsDiv","block",0,"");
		break;
	case 'listofstores':
		RemoveDrivingDirectionsPopup();
		UpdateDiv(currentLeftHandDiv,"none",0,"");
		UpdateDiv("instructionsDiv","none",0,"");
		UpdateDiv("storesListDiv","block",0,CreateReturnedStoresListHtml());
		UpdateDiv("mapContainerDiv","block",0,"");
		DrawStoresMap();
		break;
	case 'drivingaddresslist':
		UpdateDiv(currentLeftHandDiv,"none",0,"");
		UpdateDiv("drivingMatchingAddressDiv","block",0,CreateMoreThenOneDrivingAddressReturnedHtml());
		UpdateDiv("mapContainerDiv","block",0,"");
		DrawStoresMap();
		break;
	case 'drivingdirections':
		// hide whatever is listed on the left, and display the matchingaddress
		UpdateDiv(currentLeftHandDiv,"none",0,"");
		UpdateDiv("drivingDirectionDiv","block",0,CreateDrivingDirectionsHtml(0,0));
		UpdateDiv("mapContainerDiv","block",0,"");
		DrawDrivingMap();
	case 'detailview':
		currentlyShowingMap="BirdsEye";
		currentViewBeforeBirsEye=currentView;
		map.SetObliqueScene(scene.GetID());
		var intTimeOut=self.setTimeout("ShowDetailedViewStep2()", .5*1000);
	}
}
function RemoveDrivingDirectionsPopup()
{
	// get rid of the panel
	if (detailspanel != null)
	{
		document.body.removeChild(detailspanel);
		detailspanel = null;
	}
}
function DisplayDrivingDirectionsPopup(z,div,storeOrDriving)
{
	// set the current store
	currentSelectedStore=z;
	
	var temp=document.getElementById("mapdrawing");
	temp=div;
	var oNode=temp;
	//alert(oNode);
	
	var offsetX=0;
	var offsetY=0;
	
	while (oNode!=null)
	{
		offsetX+=oNode.offsetLeft;
		offsetY+=oNode.offsetTop;
		//alert(offsetX + ',' + offsetY);
		oNode=oNode.offsetParent;
	}
	
	// set the location of the panel
	var placeX=offsetX+20;
	var placeY=offsetY+20;	

	// make sure we have only one panel at a time 
	RemoveDrivingDirectionsPopup()	
	
	// id,x,y,width,height,color,zIndex,title,body,footer,mouseovertitle,bOpaque
	detailspanel=document.createElement("div");
	detailspanel.id="storePopup";
	// set its location	
	detailspanel.style.top=(placeY)+ 'px';
	detailspanel.style.left=(placeX)+ 'px';

	//detailspanel.style.width='150px'; taken care of in OverRideMapControl.css - see .VE_Panel_el
	//detailspanel.style.height='150px';
	detailspanel.style.position="absolute";
	detailspanel.style.zIndex=31;
	detailspanel.className='VE_Panel_el';
	
	if(storeOrDriving==0)
	{
		detailspanel.innerHTML=GetStoreText();
	}
	else
	{
		detailspanel.innerHTML=CreateBlankDrivingDirectionHtml();
	}

	// append the div to the body
	document.body.appendChild(detailspanel);
}
function ShowDrivingDirectionsPopup(num,storeOrDriving)
{
	// remove the panel
	RemoveDrivingDirectionsPopup();
	var temp=document.getElementById("store" + num);
	DisplayDrivingDirectionsPopup(num,temp,storeOrDriving);
}
function centermapstores(num,storeOrDriving)
{
	// remove the panel
	RemoveDrivingDirectionsPopup();
	// move the image
	//map.SetCenterAndZoom(returnedStores[num].Latitude,returnedStores[num].Longitude,13);
	map.SetCenter(returnedStores[num].Latitude,returnedStores[num].Longitude);
	// set the flag to show the popup when the move is complete
	self.setTimeout("ShowDrivingDirectionsPopup(" + num + "," + storeOrDriving + ")", 1*1000);
}
function centermapdriving(num)
{
	// move the image
	if(num<drivingDirectionSegments.length)
	{
		map.SetCenterAndZoom(drivingDirectionSegments[num].Latitude,drivingDirectionSegments[num].Longitude,16);
	}
	else
	{
		map.SetCenterAndZoom(returnedStores[currentSelectedStore].Latitude,returnedStores[currentSelectedStore].Longitude,16);
	}
}
function GetCurrentView(currentView)
{
	switch(currentView)
	{
	case 'r':
		return 'Road';
	case 'h':
		return 'Hybrid';
	case 'a':
		return 'Aerial';
	case 'o':
		return 'BirdsEye';
	}
}
function onerror(e)
{
	if(e!="too much recursion")
	{
		//alert(e.error);
		//alert(e.view.sceneID);
		//var e1 = 