﻿/* Functions to generate Bing Map */
var map = null;
var pinid = 0;
            
function GetMap(mapName)
{
    var max = 40;
    
    map = new VEMap(mapName);
    map.SetCredentials(bingMapsKey);
    map.SetDashboardSize(VEDashboardSize.Small);
    map.LoadMap();
    
    if(window['obj'] != undefined) {
        for(var i = 0; i < obj.Stores.Store.length; i++) {
            CreateMarker(i, obj.Stores.Store[i].Latitude, obj.Stores.Store[i].Longitude, obj.Stores.Store[i]);
            max = obj.Stores.Store[i].DistanceFormated;
        }
        
        SetCenterZoom(obj.Latitude, obj.Longitude, max);
    }
    
    map.AttachEvent("onmouseover", ShapeHandlerOver);
    map.AttachEvent("onmouseout", ShapeHandlerOut);
}

function GetMapDetails(mapName)
{
    var max = 0;
    
    map = new VEMap(mapName);
    map.SetCredentials(bingMapsKey);
    map.SetDashboardSize(VEDashboardSize.Small);
    map.LoadMap();
    
    CreateMarker(0, Store_Latitude, Store_Longitude, null);
    SetCenterZoom(Store_Latitude, Store_Longitude, max);
}

function CreateMarker(index, latitude, longitude, store)
{
    var imageNumber = index + 1;
    var icon = "<img src='../images/jl_locator/jl_locator_tab_0" + imageNumber + ".gif' width='25' border='0' />";
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));

    //Set the icon
    shape.SetCustomIcon(icon);

    //Set the info box
    if (store != null)
    {
        shape.SetTitle("<span style='font-size: 11px; font-weight: bold'>Jiffy Lube<sup>&reg;</sup> Service Center #" + store.Store_Number + "</span>");
        shape.SetDescription(GenerateHtml(store.Store_Id, store.Store_Number, store.Address_Line_1, store.City, store.Region_State, store.Postal_Code, store.Country, store.Phone_Number));
    }

    //Add the shape the the map
    map.AddShape(shape);
    pinid++;
}

function GetMapForMetro(mapName) {
    var max = 40;

    var maxLong = 0;
    var minLong = 2000;
    var maxLat = 0;
    var minLat = 2000;

    map = new VEMap(mapName);
    map.SetCredentials(bingMapsKey);
    map.SetDashboardSize(VEDashboardSize.Small);
    map.LoadMap();
    if (window['obj'] != undefined) {
        for (var i = 0; i < obj.Stores.Store.length; i++) {
            CreateMetroMarker(i, obj.Stores.Store[i].Latitude, obj.Stores.Store[i].Longitude, obj.Stores.Store[i]);
            max = obj.Stores.Store[i].DistanceFormated;

            if (obj.Stores.Store[i].Longitude > maxLong)
                maxLong = obj.Stores.Store[i].Longitude;
            if (obj.Stores.Store[i].Longitude < minLong)
                minLong = obj.Stores.Store[i].Longitude;

            if (obj.Stores.Store[i].Latitude > maxLat)
                maxLat = obj.Stores.Store[i].Latitude;
            if (obj.Stores.Store[i].Latitude < minLat)
                minLat = obj.Stores.Store[i].Latitude;

        }

        SetCenterZoom(maxLat, minLong, 90);
    }

    map.AttachEvent("onmouseover", ShapeHandlerOver);
    map.AttachEvent("onmouseout", ShapeHandlerOut);
}

function CreateMetroMarker(index, latitude, longitude, store) {
    var imageNumber = index + 1;
    var icon = "<img src='../images/jl_locator/jl_locator_tab_0" + imageNumber + ".gif' width='25' border='0' />";
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));

    //Set the icon
    shape.SetCustomIcon(icon);

    //Set the info box
    if (store != null) {
        shape.SetTitle("<span style='font-size: 11px; font-weight: bold; color: #97002D;'>Jiffy Lube<sup>&reg;</sup> Service Center #" + store.Store_Number + "</span>");
        shape.SetDescription(GenerateMetroHtml(store.Store_Id, store.Store_Number, store.Address_Line_1, store.City, store.Region_State, store.Postal_Code, store.Country, store.Phone_Number, store.Website,store.PPC_Count, store.PPC_Code));
    }

    //Add the shape the the map
    map.AddShape(shape);
    pinid++;
}

function ShapeHandlerOver(e)
{
    if (e.elementID != null)
    {
        var rowIndex = parseInt(e.elementID.substring(e.elementID.length - 1));
        ShowSelectedLoc(rowIndex);
    }
    else
    {
        HideAllLoc();
    }
}

function ShapeHandlerOut(e)
{
    HideAllLoc();
}

function ShowInfoBox(shapeIndex)
{
    shapeLayer = map.GetShapeLayerByIndex(0);
    
    HideAllInfoBox(shapeLayer);
    
    shapeShow = shapeLayer.GetShapeByIndex(shapeIndex);
    ShowSelectedLoc(shapeIndex);
    map.ShowInfoBox(shapeShow);
}

function HideAllInfoBox(shapeLayer)
{
    for (i = 0; i < shapeLayer.GetShapeCount(); i++)
    {
        shapeHide = shapeLayer.GetShapeByIndex(i);
        map.HideInfoBox(shapeHide);
    }
}

function ShowSelectedLoc(rowIndex)
{    
    HideAllLoc();
    
    document.getElementById( 'row_' + rowIndex ).className = "SelectedLoc";
}

function HideAllLoc()
{
    var objs = document.getElementsByTagName( 'div' );
    
    for(var i=0; i < objs.length; i++) {
        if( objs[i].className == 'SelectedLoc' )
            objs[i].className = 'UnselectedLoc';
    }
}


function GenerateHtml( store_id, storeNumber, street, city, state , zip, country , phone )
{
    return "<span class='popUp'>" + street + "</span><br><span class='popUp'>" + city + ", " + state + " " + zip + " " + country + "</span><br><br/><span class='popUp'>Phone: " + phone + "</span><br/><br/><a class='jiffyLink2' href='findlocation/StoreDetail.aspx?s_id=" + store_id + "'>View Store Details</a>"
}

function GenerateMetroHtml(store_id, storeNumber, street, city, state, zip, country, phone, website,ppc_count, ppc_code) {
    if(ppc_count > 0)
        return "<span class='popUp'>" + street + "</span><br><span class='popUp'>" + city + ", " + state + " " + zip + " " + country + "</span><br><span class='popUp'>Phone: " + phone + "</span><br/><br/><strong><a class='pageHyperLink' href='http://" + website + "'>Website</a>&nbsp;|&nbsp;<a class='pageHyperLink' href='http://" + website + "/ppc_coupon.aspx?s_id=" + storeNumber + "' onclick='" + ppc_code + "'>Coupon</a></strong>"
    else 
        return "<span class='popUp'>" + street + "</span><br><span class='popUp'>" + city + ", " + state + " " + zip + " " + country + "</span><br><span class='popUp'>Phone: " + phone + "</span><br/><br/><strong><a class='pageHyperLink' href='http://" + website + "'>Website</a>"
  
}

function SetCenterZoom(latitude, longitude, max)
{
    var lat = 37.0625;
    var lon = -95.677068;
    var zoom = 3;
    
    if (latitude != undefined ) { lat = latitude; }
    if (longitude != undefined ) { lon = longitude; }
    
    if (latitude != undefined && longitude != undefined)
    {
        if( max > 39.5 )
        {
            zoom = 8;
        }
        else if( max > 15.5 )
        {
            zoom = 9;
        }
        else if( max > 9.5 )
        {
            zoom = 10;
        }
        else if( max > 4.5 )
        {
            zoom = 11;
        }
        else if ( max == 0)
        {
            zoom = 15;
        }            
        else
        {
            zoom = 12;
        }
    }
    
    map.SetCenterAndZoom(new VELatLong(lat, lon), zoom);
}


