﻿// JScript File
//Get all the Love's Stores that are under construction

//Virtual Earth Map Construction Store Layer
var ConstructionStoreLayer;

//Whenever the map view changes call the webservice for the latest data
function GetPinData_ConstructionStores(bounds, zoom) 
{  
    //reset the display total store count
    var totalCount = document.getElementById("totalCount");
    if (totalCount != null){totalCount.innerText = "";}

    //call webservice to get Current Stores
    Loves.VirtualEarthAjax.MapService.GetClusteredMapData(bounds, zoom, "Construction", OnMapDataSucceeded_ConstructionStores, OnFailed);
}

//callback when data returns from web service
function OnMapDataSucceeded_ConstructionStores(results, eventArgs, StoreType)
{
    //decode pins
    var result=results.split(",");
    var locs = decodeLine(result[0]);
    
    //clear existing pins
    constructionStorePins = new Array();
    
    //Create Construction Store layer if it doesn't exists or clear it out
    if (ConstructionStoreLayer == null)
    {
        ConstructionStoreLayer = new VEShapeLayer();
        map.AddShapeLayer(ConstructionStoreLayer);
    }
    else
    {
        ConstructionStoreLayer.DeleteAllShapes();
    }

    //put data into memory
    for(x = 0; x < locs.length; x++)
    {
         constructionStorePins.push(new clusteredPin(locs[x],result[x+1]));
         AddPin_ConstructionStores(x, locs[x]);
    }
    
    //Get the total store count
    displayStoreCount_ConstructionStores(locs.length);
    
    HideLoading();
}


//helper to add a VE pin.
function AddPin_ConstructionStores(pinID, latlon)
{
    //we use the title field to store the pin ID.
    var shape = new VEShape(VEShapeType.Pushpin, latlon);  
    shape.Bounds = pinID;
    shape.StoreType = "Construction";  
    shape.SetCustomIcon("DesktopModules/Loves.VirtualEarthAjax/images/Diamond14.png");
    ConstructionStoreLayer.AddShape(shape);
}   


//display the current visible stores
function displayStoreCount_ConstructionStores(VisibleCount)
{
    var VisbleStoreCount = document.getElementById("StoreCount");
    var totalCount = document.getElementById("totalCount");
    
    var total;

    //check if there is already a number there, if so add to it
    if ((totalCount != null) && (totalCount.innerText != ""))
    {
        total = totalCount.innerText;
        total = parseInt(total) + VisibleCount;
    }
    else
    {
        total = VisibleCount;
    }   
   
    VisbleStoreCount.innerHTML = "Displaying <span ID='totalCount' class='ControlsHeaderText'>" + total + "</span> of Love's stores";
}


//request content for popup
function getAJAXContent_ConstructionStores(ID, bounds, startIndex)
{
    //store the current pin ID to validate what popup for what data 
    CurrentPopupID=ID;
    //call the web service
    Loves.VirtualEarthAjax.MapService.GetPushPin(bounds, startIndex, "Construction", OnContentSucceeded_ConstructionStores, OnFailed, ID);
} 


//Receive content for popup
function OnContentSucceeded_ConstructionStores(result, ID)
{
    //verify this is the data for the current popup.
    if (ID==CurrentPopupID)
    {
       //set the title
       $get(PopupPrefix + ID + "_Title").innerHTML = "<span style='font-size: 9pt; font-weight: bold;'>Love's " + result.StoreType + " #" + result.StoreNumber + "</span>";
       
       //set the content       
       $get(PopupPrefix + ID + "_Desc").innerHTML = "<table style='border: 0px; font-size: 7pt; margin: 0px; padding: 0px;'>" +
                                                       "<tr>" +
                                                            "<td style='padding-right: 5px;'>" + checkNull(result.Address) + "<br>" + checkNull(result.City) + ", " + checkNull(result.State) + " " + checkNull(result.Zip) + "<br><br>Ph. (" + checkNull(result.AreaCode) + ")" + checkNull(result.Phone) + "" + checkEmptyReturnValue(result.PhoneRoadSide, "<br>Road Side <br/>(" + checkNull(result.AreaCode) + ") " + checkNull(result.PhoneRoadSide) + "") + "</td>" +
                                                            "<td><img src='StoreImages/" + result.StoreImage + "' alt='' border='0'></td>" +
                                                        "</tr>" +
                                                        "<tr>" +
                                                            "<td colspan='2' style='padding-top: 10px; text-align: left;'><b>This store is currently under construction!</b></td>" + 
                                                        "</tr>" +
                                                    "</table>";
                        
    }
}