﻿var zoomListener = null;
var dragListener = null;
var showWeather = false;
function ShowWeather()
{

        if(document.getElementById("btnShowWeather").innerHTML == "Show Weather")
    {
        document.getElementById("btnweather").innerHTML = "Weather <img  border='0' src='images/Tick3.gif'/>";
        showWeather = true;
    }
    else
    {
        document.getElementById("btnweather").innerHTML = "Weather";
        showWeather = false;
    }
    WeatherClick()
}
function CloseWeather()
{
    document.getElementById("Weather").style.display = 'none';
}
function WeatherClick()
{
   if(document.getElementById("btnShowWeather").innerHTML == "Show Weather")
   {
        document.getElementById("btnShowWeather").innerHTML = "Hide Weather";
        CloseWeather();
        GetWeather();
        //g_map.AddExecutingFunction("GetWeather");
        if (zoomListener == null && dragListener == null)
        {
            zoomListener = new AGEvent.addListener(g_map, "onzoomend", function(prevZoomLvl, curZoomLvl){GetWeather();});
            dragListener = new AGEvent.addListener(g_map, "ondragend", function(){GetWeather();});
        }
    }
    else
    {
        document.getElementById("btnShowWeather").innerHTML = "Show Weather";
        RemoveWeather();
        if(document.getElementById("btnShowTraffic").innerHTML == "Hide Traffic Incidents")
        {
            GetTraffic();
        }
    }
}
function RemoveWeather()
{
 //g_map.RemoveAllObjects();
 g_map.removeGroupOverlay("Weather");
 if (zoomListener != null && dragListener != null)
 {
    AGEvent.removeListener(zoomListener);
    AGEvent.removeListener(dragListener);
 }
 CloseWeather();
}
function GetWeather()
{ 
            var url =""
            if(document.getElementById("btnShowWeather").innerHTML == "Hide Weather")
            {
                    var i = g_map.getZoomLevel()
                    if(i < 3)
                     {
		                             url = "WeatherMain.aspx?jsonp=callbackFunctionWeather";
		                          }
		                          else
		                          {
		                             var oBoundingBox = g_map.getBoundingBox();
                                     var oSouthWestCoords = oBoundingBox.getSouthWest();
                                     var oNorthEastCoords = oBoundingBox.getNorthEast();
                                     var boundingBox = oSouthWestCoords.longitude() + "," + oSouthWestCoords.latitude() + "," + oNorthEastCoords.longitude() + "," + oNorthEastCoords.latitude();
		                             url = "Weather.aspx?jsonp=callbackFunctionWeather&bbox=" + boundingBox;
                      }
                  $.getJSON(url,
        function(json)
            {
                callbackFunctionWeather(json);
            }
        );
    return false;
            }
}
function callbackFunctionWeather(Result)
{
   if (showWeather)
   {
       document.getElementById("ResultsDiv").innerHTML ="";
       var oLucene = eval("("+Result+")");
       var w = document.createElement('div');
       var elHtml = "";
       if(oLucene)
       {
           for(var i =0;i < oLucene.WeatherResults.length;i++)
           {
                                sResult = "{|StationName|:|" + oLucene.WeatherResults[i].StationName + "|,";
                                sResult += "|StationID|:|" + oLucene.WeatherResults[i].StationID + "|,";
                                sResult += "|Min|:|" + oLucene.WeatherResults[i].Min + "°C|,";
                                sResult += "|Max|:|" + oLucene.WeatherResults[i].Max + "°C|,";
                                sResult += "|SunRise|:|" + oLucene.WeatherResults[i].SunRise + "|,";
                                sResult += "|SunSet|:|" + oLucene.WeatherResults[i].SunSet + "|,";
                                sResult += "|MoonRise|:|" + oLucene.WeatherResults[i].MoonRise + "|,";
                                sResult += "|MoonSet|:|" + oLucene.WeatherResults[i].MoonSet + "|,";
                                sResult += "|MoonPhaze|:|" + oLucene.WeatherResults[i].MoonPhaze + "|,";
                                sResult += "|WeatherDesc|:|" + oLucene.WeatherResults[i].WeatherDesc + "|,";
                                sResult += "|WindDesc|:|" + oLucene.WeatherResults[i].WindDesc + "|,";
                                sResult += "|icon|:|" + oLucene.WeatherResults[i].icon + "|,";
                                sResult += "|Latitude|:|" + oLucene.WeatherResults[i].Latitude + "|,";
                                sResult += "|Longitude|:|" + oLucene.WeatherResults[i].Longitude + "|}";
                                icon = oLucene.WeatherResults[i].icon;
                              imgObj=document.createElement("div");
                             // sResult = sResult.replace('"','|');
                              imgObj.innerHTML = "<img src ='images/"+icon+".gif' title='"+oLucene.WeatherResults[i].StationName+" Min "+oLucene.WeatherResults[i].Min+"°C Max "+oLucene.WeatherResults[i].Max+"°C' onclick='ShowDiv(\""+sResult+"\");'/>";
                              //str = "lat:"+oLucene.WeatherResults[i].Latitude+"lon:"+oLucene.WeatherResults[i].Longitude+"<strong>ID :</strong>"+oLucene.WeatherResults[i].StationID+"<br/><strong>Station Name :</strong>"+oLucene.WeatherResults[i].StationName+"<br/><strong>Min Temp :</strong>"+oLucene.WeatherResults[i].Min+"<br/><strong>Max Temp :</strong>"+oLucene.WeatherResults[i].Max+"<br/><strong>Wind :</strong>"+oLucene.WeatherResults[i].WeatherDesc+"<br/><strong>Moon Phase :</strong>"+oLucene.WeatherResults[i].MoonPhaze;
                              //g_map.AddPoint("images/"+icon+".gif", "img1",oLucene.WeatherResults[i].Latitude , oLucene.WeatherResults[i].Longitude, -5,-32,"<strong style='color:Brown;'>Weather Station</strong>", str)
                              //g_map.AddObject(imgObj,parseFloat(oLucene.WeatherResults[i].Latitude),parseFloat(oLucene.WeatherResults[i].Longitude),-5,-5);
                              var myHTMLOptions = new AGHTMLOverlayOptions(new AGPoint(-8, -8));
                              var myHTMLOverlay = new AGHTMLOverlay(imgObj, new AGCoord(parseFloat(oLucene.WeatherResults[i].Latitude), parseFloat(oLucene.WeatherResults[i].Longitude)), myHTMLOptions);
                              g_map.addOverlay(myHTMLOverlay, "Weather"); 
           }
           w.innerHTML = elHtml;
           
           document.getElementById("ResultsDiv").appendChild(w);
       }
   }
}
function HideDiv()
{
 
    new YAHOO.widget.Effects.Fold(document.getElementById("divWeather"));
    //document.getElementById("divWeather").style.display = 'none';
    
}
function ShowDiv(Result)
{

CloseAllPopups();
document.getElementById("divWeather").innerHTML = "";


var strReplaceAll = Result;
var intIndexOfMatch = strReplaceAll.indexOf("|");
    while (intIndexOfMatch != -1){
    strReplaceAll = strReplaceAll.replace( "|", "'" )
    intIndexOfMatch = strReplaceAll.indexOf( "|" );
}
    Result =  strReplaceAll;
    var oLucene = eval("("+Result+")");
    var o;
    if(document.all)
    {
        o = document.createElement('<div class ="WeatherPopup"></div>');
    }
    else
    {
        o = document.createElement('div');
        o.setAttribute('class','WeatherPopup');
    }
    var odiv = document.createElement('div');
    odiv.innerHTML ="<h1>Weather<img src='images/close.gif' style='margin-left:185px;cursor:pointer;' onclick='HideDiv()' title='Close' alt='Close' /></h1>"
    document.getElementById("divWeather").appendChild(odiv);
    sRes = "<img src='images/big/"+oLucene.icon+".gif'/><br/>";
    sRes +="<strong>Station Name: </strong>"+oLucene.StationName;
    sRes +="<br/><strong>Min: </strong>"+oLucene.Min;
    sRes +="<br/><strong>Max: </strong>"+oLucene.Max;
    sRes +="<br/><strong>Sun Rise: </strong>"+oLucene.SunRise;
    sRes +="<br/><strong>Sun Set: </strong>"+oLucene.SunSet;
    sRes +="<br/><strong>Moon Rise: </strong>"+oLucene.MoonRise;
    sRes +="<br/><strong>Moon Set: </strong>"+oLucene.MoonSet;
    sRes +="<br/><strong>Moon Phase: </strong>"+oLucene.MoonPhaze;
    sRes +="<br/><strong>Weather: </strong>"+oLucene.WeatherDesc;
    sRes +="<br/><strong>Wind: </strong>"+oLucene.WindDesc
    o.innerHTML = sRes;
    document.getElementById("divWeather").appendChild(o);
    Rounded("div#divWeather","all","#f6f6f6","#2667aa","smooth border #000000");
    document.getElementById("divWeather").style.display = '';
    new YAHOO.widget.Effects.UnFold(document.getElementById("divWeather"));
        
}
