﻿

// Define Global variables.
var myMap;
var myMQPoiCollection;
var myMapInit;
var searchCriteria;
var pageNumber;
var sortBy;
var mapType;
var stateName;
var marketName;

// Use ASP.NET Ajax to do the page load and unload.
Sys.Application.add_unload(page_unload);


function ShowStateMap()
{
  if (typeof(postbackElement) !== "undefined" && postbackElement.id.indexOf("SpotlightControl")>-1) 
   { 
      return;
   }
        searchCriteria = $get('SearchCriteriaField').value;
        mapType="state";
        UpdateHeader(mapType);
        NHS.CW.Mapping.GetStatePoints(searchCriteria,onGetPointsSuccess, onGetPointsFailure);
   
}

function ShowMarketMap()
{
   if (typeof(postbackElement) !== "undefined" && postbackElement.id.indexOf("SpotlightControl")>-1) 
   { 
      return;
   }
  
        searchCriteria = $get('SearchCriteriaField').value;
        pageNumber = $get('PageNumberField').value;
        sortBy =  $get('SortCriteriaField').value;
        mapType="market";
        UpdateHeader(mapType);
        //myMap = new MQTileMap($get('mapWindow'), 5, new MQLatLng(31.196878, -99.346703), "map", myMapInit);
        NHS.CW.Mapping.GetMarketPoints(searchCriteria,pageNumber,sortBy,onGetPointsSuccess, onGetPointsFailure);
   
}
function IsMarketLevel()
{
    return (window.location.search.substring(1).indexOf("mid")>-1);    
}

function IsMarketFacetSelected(postbackElement)
{
   if (typeof(postbackElement) === "undefined")
   {
        return false;
   }
   else
   { 
        return(postbackElement.id.indexOf("AreaFacetLink")>-1 && postbackElement.id.indexOf("AreaFacetLink0")==-1);
   }
}

function IsAllMarketFacetSelected(postbackElement)
{
  if (typeof(postbackElement) === "undefined") 
  {
    return false;
  }
  else
  { 
    return(postbackElement.id.indexOf("AreaFacetLink0")>-1);
  }
       
}

function IsStateMap()
{
     var queryString = window.location.search.substring(1);
     if(queryString.indexOf("mid")>-1)
     {
        return false;
     }
     else
     {
       
       return true;
     }
}

function onGetPointsSuccess(result, context, methodName) {

   if(result!=null)
   {
        myMQPoiCollection = new MQPoiCollection();    

        // Parse thru all the items in the array of points.
        for (var i = 0; i < result.length; i++) {

        // Create a new object point based on the array item.
        var point = result[i];
        // Assign Latitude and Longitude to the MapQuest Point.
        var myPoint = new MQPoi(new MQLatLng(point.Latitude, point.Longitude));
        var communityNameList = point.CommunityName.split(';');
        if(mapType=='state')
        {
            var titleHTML='';
            if(communityNameList.length>1)
            {
                for( var index=0;index<communityNameList.length;index++)
                {
                    titleHTML+=communityNameList[index]+"<br/>";
                }
            }
            else
            {
            titleHTML=point.CommunityName;
            }
            myPoint.setInfoTitleHTML(titleHTML);
            MQEventManager.addListener(myPoint, 'click', doSomething);
        }
        // Set the unique identifier for the Point.
        myPoint.setKey(point.PointID);
        // Create an Icon object.
      var myIcon = new MQMapIcon();
//        // Set the Icon's properties.
      myIcon.setImage(point.ImageUrl, 19, 19, true, false);
      myIcon.setShadow("",0,0,0,0,false);
//        // Associate the Icon to the Point.
       myPoint.setIcon(myIcon);
      
        // Add the Point to the Point Collection.
        myMQPoiCollection.add(myPoint);
    }
    mapInit = new MQMapInit();
    mapInit.setBestFitRect( myMQPoiCollection.getBoundingRect() );
    document.getElementById('mapWindow').innerHTML="";
    myMap = new MQTileMap(document.getElementById('mapWindow'),null,null,"map",mapInit);
    myMap.addPois(myMQPoiCollection);
    var zoomLevel =  myMap.getZoomLevel() ;
    // If zoom level is too high , zoom out. 12 is too high
    if(zoomLevel>12)
    {
        myMap.setZoomLevel(9);
    }

    myMap.addControl(new MQZoomControl());
   }
 

}

function doSomething(e)
{
        var protocol = window.location.protocol;
        var hostName = window.location.host;
        var hostPath = window.location.pathname;
        var queryString = window.location.search.substring(1);
        if(queryString.indexOf("mid")==-1)
        {
            url = protocol+"//"+hostName+hostPath+"?"+queryString+"&mid="+this.getKey();
        }
        else
        {
           url = protocol+"//"+hostName+hostPath+"?"+queryString.substring(0,queryString.indexOf("&mid"))+"&mid="+this.getKey();
        }
        window.location.href = url;
 }
 


function onGetPointsFailure() {       

}

function page_unload(sender, e) {
    if(myMap!=null)
    {
        myMap.removeAllPois();
        myMap=null;
    }
}
//Have to add this hack to fix shell issues with ajax. Shell is written in such a way that
// it break page life cycle. So for now I am adding this hack.
function UpdateHeader(mapType)
{
  var headerID = $get("HeaderClientID");
  if(headerID!=null)
  {
     var header =  $get(headerID.value);
     if (header!=null)
     {
        header.innerText = $get('TitleTextField').value;
     }
  }
}


