﻿//Add reference to get asp.net ajax script intellisense (use path for js file, name = assembly)
///<reference name = "MicrosoftAjax.js"/>

//********************Global Declairations**********************************

//var SleepCtrInfo = null; //JSON object that holds the address information.
var map; //Map Ojbect
var geocoder; 
//var currPageIndex = 0;

///*********************************Show Hide JS Functions***********************************************
//Follow the mouse script Ref. http://javascript.about.com/library/blfollow1.htm
var divName = 'msg'; var offX = 15;  var offY = 15;
function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}
function follow(evt) {if (document.getElementById) {var obj = document.getElementById(divName).style;
// obj.visibility = 'visible';
obj.left = (parseInt(mouseX(evt))+offX) + 'px';
obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}

//On Mouse over/out events on map items
function ShowAddress(key)
{
  var msgdiv = $get(divName);
  if(null != SleepCtrInfo[key])
   msgdiv.innerHTML = GetLocDetails(SleepCtrInfo[key]);
  else msgdiv.innerHTML = "Invalid Key";
   msgdiv.style.display = "";     
}
function HideAddress(){$get(divName).style.display = "none";}

/************** WEB SERVICE CODE [DO Search]*************************/
var CacheSearch = new Array();
function DoSearch(inputID, pIndex)
{
  $get(LinkGoID).disabled  = true;
  $get("spanBusy").innerHTML = "Working..";
  var inputstr = $get(inputID).value;
  if(!inputstr)
     inputstr = "";
  var tst = $get("tblResult").rows.length; 
  
  currPageIndex = pIndex;
  //Use client side cache
  if(null != CacheSearch[inputstr + "_" + pIndex])
  {
      SearchResults = CacheSearch[inputstr + "_" + pIndex];
      FillResultTable();
       $get(LinkGoID).disabled  = false;
       $get("spanBusy").innerHTML = "";

  }
  else
  {  //Cache does not exist, get it from web service"
     SleepCenterInfoWS.GetSearchResults(inputstr , pIndex, $get("tblResult").rows.length, onCompleteSearch, onErrorSearch);  
  }
}

function onCompleteSearch(result, userContext, methodName)
{
   $get(LinkGoID).disabled  = false;
   $get("spanBusy").innerHTML = "";

  SearchResults = result; 
  //Store in the cache
  
  CacheSearch[$get(InputTxtID).value + "_" + currPageIndex] = result;
  FillResultTable();
}

function onErrorSearch(exception, userContext, methodName)
{
   $get(LinkGoID).disabled  = false;
   $get("spanBusy").innerHTML = "";

  alert(String.format("Sorry! No results were found. \nException: '{0}'", exception.get_message()));
}

/*********** Fill Results Table ************************************/

function FillResultTable()
{
  var tbl = $get("tblResult");
  var i;
  //Remeber not to use the table rows count for the loop counter. as on the last page
  //number of results returned may be less then the total number of the rows on the table.
  for(i = 0; i < SearchResults.length; ++i)
  {
    if (SearchResults[i].Key != "OhioHealth Sleep Services - Columbus, Dublin, Grove City, Pickerington, Westerville, Worthington")
    {
        tbl.rows[i].cells[0].innerHTML = GetLocDetails( SleepCtrInfo[ SearchResults[i].Key ] );
        tbl.rows[i].cells[1].innerHTML = 
            "<b>" + SearchResults[i].Distance + " miles</b><br /><br />"+
            "<a href=\"javascript:void(0);\" onclick=\"ShowLocation('" + SearchResults[i].Key + "')\">" +
            "Map/Info</a>";
    }
  }
  
  //If number of results returned were less then intended, clear the rest of entires.
  for(i = SearchResults.length; i < tbl.rows.length; ++i)
  {
   tbl.rows[i].cells[0].innerHTML = "";
    tbl.rows[i].cells[1].innerHTML = "";
  }
}

//update the display of the current page shown
function SelectPagingLink(index, NPages)
{ var i = 0, lnk;
  for(i = 0; i < NPages; ++i)
  {
     lnk = $get("pa_" + i);
     if(i == index)
       lnk.className = "xmenuSelected";
     else
       lnk.className = "xmenu";
  }
}

//**********************Google Map JS***************************************************
    
 function LoadInitialGoogleMap()
 { map = new GMap2(document.getElementById("map_canvas"));
   map.addControl(new GMapTypeControl()) ;
   map.addControl(new GSmallMapControl());
   //map.addControl(new GLargeMapControl()); 
   map.addControl(new GMapTypeControl());
   map.enableScrollWheelZoom();   
   var bounds = new GLatLngBounds();
   //GEvent.addListener(map, "click", MarkerClickedEvent);
   
   //Add in all the locations in the map from the JSON object.
   for(var key in SleepCtrInfo)
   {
   if (key != "OhioHealth Sleep Services - Columbus, Dublin, Grove City, Pickerington, Westerville, Worthington")
   {
     var loc = SleepCtrInfo[key];
     var point = new GLatLng(loc.Lat, loc.Long);         
     map.setCenter(point, 11);
     var marker = createMarker(point, '<span style = \"color:#466186;\">' + GetLocDetails(loc) + GetGoogleDirections(loc) + '<span/>');
     map.addOverlay(marker);
     bounds.extend(point);
     }
     //GEvent.addListener(marker, "click", MarkerClickedEvent);             
   }
   //Center the default location. [Optional]
   //map.setCenter(new GLatLng(SleepCtrInfo["Key2"].Lat, SleepCtrInfo["Key2"].Long), 8);
   //map.setCenter(new GLatLng(0, 0), 8);
   geocoder = new GClientGeocoder();    
 }
 function ShowLocation(key)
 {
 $get("formLayer").style.visibility = "visible";
 var loc = SleepCtrInfo[key];
   if(null == loc)
   {alert("Invalid Loc");}
   else
   {  var bounds = new GLatLngBounds();
      var point = new GLatLng(loc.Lat, loc.Long);         
      map.setCenter(point, 11);
      var marker = createMarker(point, '<span style = \"color:#466186;\">' + GetLocDetails(loc) + GetGoogleDirections(loc) + '<span/>');
      map.addOverlay(marker);
      // NOTE: Add more info about the sleep labs here
      marker.openInfoWindowHtml('<span style = \"color:#466186;\">' + GetLocDetails(loc) + GetGoogleDirections(loc) + '<span/>');
      marker.show();
      bounds.extend(point);
      
    // map.setZoom(map.getBoundsZoomLevel(bounds));
    // var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    // var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
    // map.setCenter(new GLatLng(clat,clng));
      
      
      
      //map.panDirection(-1, -1) ;
      //map.panBy("20px");
     
     
      ShowTransparentBkgrd(600,400);
      map.checkResize()
   }
 }
 
 
 function createMarker(point, infoMsg) {
		var marker = new GMarker(point);
		GEvent.addListener(marker, 'mouseover', function() { marker.openInfoWindowHtml(infoMsg);});
		return marker;
	}
  
 function GetLocDetails(loc)
 {
    if (loc.Name == "OhioHealth Sleep Services - Columbus, Dublin, Grove City, Pickerington, Westerville, Worthington")
    {
        return   '<p><span class=\"msg1stLine\">' + loc.Name + '</span><br />'+
        '<span class="msg3rdLine">'+loc.Phone+'</span></p>';
    }
    else{
        return   '<p><span class=\"msg1stLine\">' + loc.Name + '</span><br />'+
        '<span class=\"msg2ndLine\">' + loc.Street + '</span><br />'+
        '<span class=\"msg2ndLine\">' + loc.City +',' + loc.State +',' + loc.Zip + '</span><br />'+
        '<span class="msg3rdLine">'+loc.Phone+'</span></p>';
    }
 }
 
 function GetGoogleDirections(loc)
 {
   var lnkHref = "http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=" + "CurrentZip" + 
        "&daddr=" + loc.Street.replace(" ", "+") + "," + loc.City + "," + loc.State +
        "," + loc.Zip + "&sll=" + loc.Lat + "," + loc.Long + "&sspn=0.0078,0.020256&ie=UTF8&z=7";
   return "<a href=\"" + lnkHref + "\" onmousedown=\"UpdateZip(this);\">Directions</a>";
 }
   
 function UpdateZip(glink)
 {
         var zip = $get(InputTxtID).value;
         glink.href = glink.href.replace("CurrentZip", zip);
         var tst = glink.href;

 }
/* Function for the menu settings.*/

//function RestoreMenuVisibility()
//     {
//       
//       { if(visibleSubMenuID == 1)
//            document.getElementById("trouble").style.visibility = "visible";
//         else if(visibleSubMenuID == 2)
//             document.getElementById("what1").style.visibility = "visible";
//         else if(visibleSubMenuID == 3)
//             document.getElementById("what2").style.visibility = "visible";
//       }    
//     }