﻿// JScript File
//Get all the Love's Stores that are currently open

//Virtual Earth Map Current Store Layer
var CurrentStoreLayer;

//Whenever the map view changes call the webservice for the latest data
function GetPinData_CurrentStores(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, "Current", OnMapDataSucceeded_CurrentStores, OnFailed);
}

//callback when data returns from web service
function OnMapDataSucceeded_CurrentStores(results, eventArgs, StoreType)
{
    //decode pins
    var result=results.split(",");
    var locs = decodeLine(result[0]);
    
    //clear existing pins
    currentStorePins = new Array();
    
    //Create Current Store layer if it doesn't exists or clear it out
    if (CurrentStoreLayer == null)
    {
        CurrentStoreLayer = new VEShapeLayer();
        map.AddShapeLayer(CurrentStoreLayer);
    }
    else
    {
        CurrentStoreLayer.DeleteAllShapes();
    }

    //put data into memory
    for(x = 0; x < locs.length; x++)
    {
        currentStorePins.push(new clusteredPin(locs[x],result[x+1]));
        AddPin_CurrentStores(x, locs[x]);
    }
    
    //Get the total store count
    displayStoreCount_CurrentStores(locs.length);
    
    HideLoading();
}


//helper to add a VE pin.
function AddPin_CurrentStores(pinID, latlon)
{
    //we use the title field to store the pin ID.
    var shape = new VEShape(VEShapeType.Pushpin, latlon);  
    shape.Bounds = pinID;
    shape.StoreType = "Current";
    shape.SetCustomIcon("DesktopModules/Loves.VirtualEarthAjax/images/Heart12.png");
    CurrentStoreLayer.AddShape(shape);
}   


//display the current visible stores
function displayStoreCount_CurrentStores(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_CurrentStores(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, "Current", OnContentSucceeded_CurrentStores, OnFailed, ID);
} 


//Receive content for popup
function OnContentSucceeded_CurrentStores(result, ID)
{
    //verify this is the data for the current popup.
    if (ID==CurrentPopupID)
    {
       //document.getElementById("lbl_Debug").innerHTML = document.getElementById("lbl_Debug").innerHTML + PopupPrefix + ID + "_Title-";
       //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='text-align: left;'><a href='#' onclick='getStoreDetail(\"" + result.StoreID + "\");' style='text-decoration: underline;'>more info</a></td>" + 
                                                        "</tr>" +
                                                    "</table>";
                        
    }
}