var map = null;
var dealerLocations = null;
var myLocationID = "xxx";
var myPolyID = "yyy";
var mySearch = "";
var aLat = null;
var aLong = null;
var resultIndex = -1;

function GetMap() {
	map = new VEMap('myMap');
	map.LoadMap();
	AddMyLayer(true, false);
	map.AttachEvent("onclick",MouseHandler);
}

function GetDetailMap(storeId) {
    map = new VEMap('myMap');
    map.LoadMap();
    AddDetailLayer(storeId);
}


function MouseHandler(e) {
	if (e.elementID != null) {
		if (e.eventName == "onclick") {
			if (e.leftMouseButton) {
				var myTitle = map.GetShapeByID(e.elementID).GetTitle();
				window.location = "StoreDetails.asp?store=" + escape(myTitle)
				return true;
			}
		}
	}
}


function BuildPolyPoints(CenterLat, CenterLong) {
    aLat = new Array(CenterLat - 0.15, CenterLat + 0.15, CenterLat + 0.4, CenterLat + 0.4,
                        CenterLat + 0.15, CenterLat - 0.15, CenterLat - 0.4, CenterLat - 0.4, CenterLat - 0.15);
    aLong = new Array(CenterLong - 0.45, CenterLong - 0.45, CenterLong - 0.15, CenterLong + 0.15,
                        CenterLong + 0.45, CenterLong + 0.45, CenterLong + 0.15, CenterLong - 0.15, CenterLong - 0.45);
}

function DisplaySearchArea() {
    var shape = new VEShape(VEShapeType.Polygon, [
                            new VELatLong(aLat[0], aLong[0]),
                            new VELatLong(aLat[1], aLong[1]),
                            new VELatLong(aLat[2], aLong[2]),
                            new VELatLong(aLat[3], aLong[3]),
                            new VELatLong(aLat[4], aLong[4]),
                            new VELatLong(aLat[5], aLong[5]),
                            new VELatLong(aLat[6], aLong[6]),
                            new VELatLong(aLat[7], aLong[7])
                        ]);
    dealerLocations.AddShape(shape);
    var myShape = dealerLocations.GetShapeByIndex(1);
    myPolyID = myShape.GetID();
}

function InPoly(npol, xp, yp, x, y) {
      var i, j, c = 0;
      for (i = 0, j = npol-1; i < npol; j = i++) {
        if ((((yp[i] <= y) && (y < yp[j])) ||
             ((yp[j] <= y) && (y < yp[i]))) &&
            (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
          c = !c;
      }
      return c;
}

function onFeedLoad(layer) {
    var numShapes = layer.GetShapeCount();
    
    var myLatLong = null;
    var myLat = null;
    var myLong = null;
    var resultCount = 0;

    var locationHTML = "<ul>";
    
    for (var i = 0; i < numShapes; ++i) {
        var s = layer.GetShapeByIndex(i);
        if (s.GetID() != myLocationID && s.GetID() != myPolyID) {
            myLatLong = s.GetPoints();
            myLat = myLatLong[0].Latitude;
            myLong = myLatLong[0].Longitude;

            //Check to see if location is in the poly
            if (InPoly(9, aLat, aLong, myLat, myLong)) {
                locationHTML = locationHTML + '<li>' + FormatTitle(s.GetTitle(), myLat, myLong) + FormatDescription(s.GetDescription()) + '</li>';
                resultCount++;
            }

            //s.SetCustomIcon("<img src='images/LorellIcon.png'/>");
        }
    }
    if(resultCount==0) locationHTML = locationHTML + '<em>No locations nearby...</em>';
    locationHTML = locationHTML + '</ul>';
    document.getElementById("LocationList").innerHTML = locationHTML;
}

function FormatTitle(myTitle, myLat, myLong) {
	resultIndex++;
	
	var myTitleLink = '<div class="LocationName">' + myTitle + '</div>';

	myTitleLink = myTitleLink + '<a class="StoreDetails" href="StoreDetails.asp?store=' + escape(myTitle) + '">Store Details</a>';
	return myTitleLink;
}

function FormatDescription(myDescription) {
	var myDescriptionArray = myDescription.split("\n");
	var myNewDescription = '<div class="StoreInfo">' + myDescriptionArray[0] + '<br />' + myDescriptionArray[1] + '</div>';

	//myNewDescription = myNewDescription + '<div id="HiddenInfo' + resultIndex + '" class="StoreInfoHidden">' + myDescriptionArray[2] + '<br /><br />'+ FormatServices(myDescriptionArray[3]) + '</div>';
	return myNewDescription;
}

function FormatServices(myServices) {
/*
	A - Fuel Links Rewards		M - Copy Machine		Y - Video Department
	B - Custom Cake			N - Fax Services		Z - Postal Services
	C - Hot Foods			O - Gift Certificates		0 - Dollar Store
	D - Floral			P - Money Orders		1 - Pharmacy
	E - Lottery Machine		Q - Stamps			2 - Coinstar Machine
	F - Bill Paying			R - Rug Doctor (Cleaner)	3 - Post Office
	G - ATM				S - In-Store Photo Processing	4 - Dry Cleaning
	H - Seafood			T - Home Delivery		5 - Family Pac Section
	I - Greeting Cards		U - Key Making			6 - Instant Lotteries
	J - Tobacco Shop		V - Western Union		7 - Cookie Club Card
	K - 24hr Photo Developing	W - Catering			8 - Food/Court
	L - Double Coupons		X - Red Tag Specials		9 - Bank
*/
	var myServiceList = '<ul>';

	if (myServices.indexOf("A") > -1) myServiceList = myServiceList + '<li>Fuel Links Rewards</li>';
	if (myServices.indexOf("B") > -1) myServiceList = myServiceList + '<li>Custom Cake</li>';
	if (myServices.indexOf("C") > -1) myServiceList = myServiceList + '<li>Hot Foods</li>';
	if (myServices.indexOf("D") > -1) myServiceList = myServiceList + '<li>Floral</li>';
	if (myServices.indexOf("F") > -1) myServiceList = myServiceList + '<li>Bill Paying</li>';
	if (myServices.indexOf("G") > -1) myServiceList = myServiceList + '<li>ATM</li>';
	if (myServices.indexOf("H") > -1) myServiceList = myServiceList + '<li>Seafood</li>';
	if (myServices.indexOf("I") > -1) myServiceList = myServiceList + '<li>Greeting Cards</li>';
	if (myServices.indexOf("K") > -1) myServiceList = myServiceList + '<li>24hr Photo Developing</li>';
	if (myServices.indexOf("L") > -1) myServiceList = myServiceList + '<li>Double Coupons</li>';
	if (myServices.indexOf("J") > -1) myServiceList = myServiceList + '<li>Tobacco Shop</li>';
	if (myServices.indexOf("E") > -1) myServiceList = myServiceList + '<li>Lottery Machine</li>';
	if (myServices.indexOf("R") > -1) myServiceList = myServiceList + '<li>Rug Doctor (Cleaner)</li>';
	if (myServices.indexOf("M") > -1) myServiceList = myServiceList + '<li>Copy Machine</li>';
	if (myServices.indexOf("N") > -1) myServiceList = myServiceList + '<li>Fax Services</li>';
	if (myServices.indexOf("O") > -1) myServiceList = myServiceList + '<li>Gift Certificates</li>';
	if (myServices.indexOf("P") > -1) myServiceList = myServiceList + '<li>Money Orders</li>';
	if (myServices.indexOf("Q") > -1) myServiceList = myServiceList + '<li>Stamps</li>';
	if (myServices.indexOf("S") > -1) myServiceList = myServiceList + '<li>In-Store Photo Processing</li>';
	if (myServices.indexOf("T") > -1) myServiceList = myServiceList + '<li>Home Delivery</li>';
	if (myServices.indexOf("U") > -1) myServiceList = myServiceList + '<li>Key Making</li>';
	if (myServices.indexOf("V") > -1) myServiceList = myServiceList + '<li>Western Union</li>';
	if (myServices.indexOf("W") > -1) myServiceList = myServiceList + '<li>Catering</li>';
	if (myServices.indexOf("X") > -1) myServiceList = myServiceList + '<li>Red Tag Specials</li>';
	if (myServices.indexOf("Y") > -1) myServiceList = myServiceList + '<li>Video Department</li>';
	if (myServices.indexOf("Z") > -1) myServiceList = myServiceList + '<li>Postal Services</li>';
	if (myServices.indexOf("0") > -1) myServiceList = myServiceList + '<li>Dollar Store</li>';
	if (myServices.indexOf("1") > -1) myServiceList = myServiceList + '<li>Pharmacy</li>';
	if (myServices.indexOf("2") > -1) myServiceList = myServiceList + '<li>Coinstar Machine</li>';
	if (myServices.indexOf("3") > -1) myServiceList = myServiceList + '<li>Post Office</li>';
	if (myServices.indexOf("4") > -1) myServiceList = myServiceList + '<li>Dry Cleaning</li>';
	if (myServices.indexOf("5") > -1) myServiceList = myServiceList + '<li>Family Pac Section</li>';
	if (myServices.indexOf("6") > -1) myServiceList = myServiceList + '<li>Instant Lotteries</li>';
	if (myServices.indexOf("7") > -1) myServiceList = myServiceList + '<li>Cookie Club Card</li>';
	if (myServices.indexOf("8") > -1) myServiceList = myServiceList + '<li>Food/Court</li>';
	if (myServices.indexOf("9") > -1) myServiceList = myServiceList + '<li>Bank</li>';

	myServiceList = myServiceList + '</ul>';
	return myServiceList;
}

function AddMyLayer(setBestView,doOnLoad) {
    var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, "Locations.asp", dealerLocations);
    if(doOnLoad) {
    	map.ImportShapeLayerData(veLayerSpec, onFeedLoad, setBestView);
    } else {
    	map.ImportShapeLayerData(veLayerSpec, null, setBestView);
    }
}

function AddDetailLayer(storeId) {
	var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, "Locations.asp?storeid=" + storeId, dealerLocations);
	map.ImportShapeLayerData(veLayerSpec, null, true);
}

function FindNearbyCB(a, b, c, d, e) {
    var myLatLong = map.GetCenter();
    var myLat = myLatLong.Latitude;
    var myLong = myLatLong.Longitude;

    BuildPolyPoints(myLat, myLong);

    var shape = new VEShape(VEShapeType.Pushpin, myLatLong);
    shape.SetTitle('Your search entry:');
    shape.SetDescription(mySearch);
    dealerLocations.AddShape(shape);
    var myShape = dealerLocations.GetShapeByIndex(0);
    myLocationID = myShape.GetID();
    map.SetZoomLevel(10);

    //DisplaySearchArea();
    AddMyLayer(false, true);
}

function FindNearby() {
    document.getElementById("LocationList").innerHTML = "Searching for locations in your area...";

    map.DeleteAllShapeLayers();
    dealerLocations = null;
    dealerLocations = new VEShapeLayer();
    mySearch = document.getElementById("txtWhat").value;
    try {
        map.Find(null, mySearch, null, dealerLocations, 0, 1, true, true, true, null, FindNearbyCB);
    }
    catch (e) {
        alert(e.message);
    }
}

function MoveToLocation(myLat, myLong, myIndex) {
    var myLatLong = new VELatLong(myLat, myLong);
    map.SetCenter(myLatLong);
    map.SetZoomLevel(13);
    
    HideDetails();
    document.getElementById("HiddenInfo" + myIndex).className = "StoreInfo";
}

function HideDetails() {
	for(i=resultIndex; i>-1; i--) document.getElementById("HiddenInfo" + i).className = "StoreInfoHidden";
}