﻿var map = null;
var url = window.location.href;
var initial_load = true;


$(document).ready(function() {


    $('#showMap').live("click", function() {
        $('#myMap').slideToggle(700, function() {
            updateMapText();
        });
    });


    bindCategory();

    $('#lbBack').live("click", function() {
        $('#liBack').hide();
        $('#ctl00_cphMainBody_txtSearchTerms').val("Search SuperGraphics");
        $('#divSearch').hide();
        $('#divMaster').show();


        bindCategory();

        $('#ctl00_cphMainBody_drpClassic').fadeIn(1);

        GetMapData();
    });


    $("#liBack").hide();
    $("#divSearch").hide();

    LoadDefaultMap();
});

function bindCategory() {

 
    $('#ctl00_cphMainBody_drpClassic').change(function() {
        $('#divMaster').show();
        $('#divSearch').hide();
        GetMapData();
   
    });
}


function hideMaster() {
    $('#divMaster').hide();
}

function updateMapText() {
    if ($('#myMap').is(':visible'))
        $('#showMap').text('Hide Map');
    else
        $('#showMap').text('Show Map');
}

function LoadDefaultMap() {
 
    $('#myMap').show();
    map = new VEMap('myMap');
    map.LoadMap();

    if ($('#showMap').html() == "Show Map")
        $('#myMap').hide();

    if ($('#ctl00_cphMainBody_lbBack').html() == null) {
        GetMapData();
    }
}


function GetMapData() {
    var seriesId = $("#ctl00_cphMainBody_drpClassic").val();

    // Call web service, returns a JSON serialized object of List<MapLocation>.  OnGetMapDataComplete added as callback.
    $.ajax({
        type: "POST",
        url: "svc/wsMapData.asmx/GetMapData",
        data: '{ "categoryId" : "' + seriesId + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        begin: function() {
            // load ajax loader gif
        },

        success: function(msg) {
            
            OnGetMapDataComplete(eval(msg.d), false);
        },

        error: function(msg) {

        }
    });
}
   
// Callback for GetMapData web service call.
function OnGetMapDataComplete(myData, is_search) {
    locs = new Array;  
    
    if (is_search) {
        $("#ctl00_cphMainBody_drpClassic").hide();
        $("#divMaster").hide();
        $("#divSearch").show();
        $("#liBack").show();
    }
    
    // Loops through returned List<MapLocation> and adds them to map.
    var shapes = new Array();
    //map.DeleteAllShapeLayers();
 
    map.DeleteAllShapeLayers();
    
    for (var i = 0; i < myData.length; i++) {
        var curLatLong = new VELatLong(myData[i].Lat, myData[i].Long);
        var Shape = new VEShape(VEShapeType.Pushpin, curLatLong);

        Shape.SetCustomIcon("images/pushpin1.gif");
        locs.push(curLatLong);
        Shape.SetDescription(BuildInfoBoxHtml(myData[i]));
        shapes.push(Shape);      
    }

    if (myData.length > 0) {
        var pushLayer = new VEShapeLayer();
        map.AddShapeLayer(pushLayer);
        pushLayer.AddShape(shapes);  
        
        map.SetMapView(locs);

        if (initial_load) {
            map.SetCenterAndZoom(new VELatLong(50, -97), 3);
            initial_load = false;
        }
        else {
            if (map.GetZoomLevel() > 5)
                map.SetZoomLevel(5);
        }
    }
}

function BuildInfoBoxHtml(myData) {


    var html = "<div style='width: 440px;'>";
    html += "<div style='float: left; width: 140px;'>";
    html += "<a href='landing.aspx?site_id=" + myData.SiteId + "&sort_order=0'>";
    html += "<img src='image_handler.ashx?site_id=" + myData.SiteId + "'></a></div>";
    html += "<div style='float: left; width: 200px;'>";
    html += "<a href='landing.aspx?site_id=" + myData.SiteId + "&sort_order=0'>";
    html += "<h2>" + myData.State + "</h2></a></div>";
    html += "<div style='float: left; width: 300px; margin-top: -1em;'>";
    html += myData.ShortDesc + "</div><br/>";


    // If the site has a "Did you know?" Display it.  Otherwise nothing.
    if (myData.DidYouKnow != null) {
    
        html += "<br/><br/><h4>Did you know?</h4>";
        html += "<br/>";

        var didYouKnow = myData.DidYouKnow;

        if (didYouKnow.length > 100)
            didYouKnow = didYouKnow.substring(0, 100) + "...";
          
        html += didYouKnow + "<br/>";
    }
        
    html += "<a href='landing.aspx?site_id=" + myData.SiteId + "&sort_order=0' class='more' style='float: right; margin-right: 10px;'>Learn More</a></div>";

    return html;
}

function WatermarkFocus(txtElem, strWatermark) {

    if (txtElem.value == strWatermark)
        txtElem.value = "";

}

function WatermarkBlur(txtElem, strWatermark) {
    if (txtElem.value == "")
        txtElem.value = strWatermark;
}

function PushPin_Click(e) {

    var shape = map.GetShapeByID(e.elementID);
    if (shape != null)
        window.open(shape.Description);
}
