﻿ /// <reference path="../scripts/VeJavaScriptIntellisenseHelper.js" />
    var map = null; var control; var shCo; var mySt = null; var myCLA = null; var myCLO = null; var myMapDiv=null;
    var myTideStationsSource = "";var mySession=null;
//    function pageLoad(sender, arg) {
//        if (map == null) PageMethods.PageSession("TUS_Ses", OnPSCComplete, OnPSCCallError);
//    }

//    function OnPSCComplete(result, userContext, methodName) {        
//        mySession = eval("(" + result + ")");
//        if (mySession != null) {            
//            var stateName = mySession.StateParam;
//            myTideStationsSource = "../../rss/tidestations.aspx?state=" + stateName;
//            map = new VEMap('myMap');
//            map.LoadMap(new VELatLong(mySession.aState.CenterLatitude, mySession.aState.CenterLongitude), 5);            
//            var ua = window.navigator.userAgent;
//            shCo = !(ua.indexOf("Safari") > 0);   
//            AddTideStationLayerLayer();    
//        }
//    }
//    function OnPSCCallError(error, userContext, methodName) {
//        if (error !== null) {alert(error.get_message());}
//        mySession = null;
//    }

    function pageLoad(sender, arg) {        
	    var ua = window.navigator.userAgent;        
        shCo = !(ua.indexOf("Safari") > 0);        
        mySt = document.getElementById('TS_STATE').innerText;
        myCLA = document.getElementById('TS_CLA').innerText;
	    myCLO = document.getElementById('TS_CLO').innerText;
	    if ((mySt != null) && (myCLA != null) && (myCLO != null)) 
	    {
            myTideStationsSource = "../../rss/tsv.aspx?state=" + mySt;
            map = new VEMap('myMap');
            map.LoadMap(new VELatLong(parseFloat(myCLA), parseFloat(myCLO)), 5);        
            var ua = window.navigator.userAgent;
            shCo = !(ua.indexOf("Safari") > 0);   
            AddTideStationLayerLayer();    
        }
    }

    function AddTideStationLayerLayer() {
        var l = new VEShapeLayer();
        var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, myTideStationsSource, l);
        if (veLayerSpec != undefined)
        {
            if (shCo) AddMyControl();
            map.ImportShapeLayerData(veLayerSpec, onFeedLoad, true);
        }
    }
    function onFeedLoad(feed) {
        var stIcon = "<img src='../../images/icons/misc/transmit_blue.png'/>";
        var stRefIcon = "<img src='../../images/icons/misc/transmit.png'/>";
        for (var i = 0; i < feed.GetShapeCount(); i++) 
        { var pin = feed.GetShapeByIndex(i);
          if (pin.Notes.indexOf("#*R*#") == -1) {pin.SetCustomIcon(stIcon);} else {pin.SetCustomIcon(stRefIcon);}            
        }
        if (shCo) RemoveMyControl();
    }
    function AddMyControl() {
        if (control == null) {
            control = document.createElement("div");control.id = "myControl";
            control.style.top = "140px";control.style.left = "140px";control.style.width = "140px";control.style.height = "40px";
            control.style.padding = "20px 30px";control.style.border = "1px solid black";control.style.background = "White";
            control.innerHTML = "<span style='font-family:Arial; font-size:14px;color:Black; background-color:White;margin-bottom:30px;'>Loading Tide Stations<br/></span><img src='../../images/anim/slider01.gif'/>";
            map.AddControl(control);
            addShim(control);
        }
    }

    function addShim(el) {
        var shim = document.createElement("iframe");
        shim.id = "myShim"; shim.frameBorder = "0";shim.style.position = "absolute";shim.style.zIndex = "1";
        shim.style.top = el.offsetTop;shim.style.left = el.offsetLeft;shim.width = el.offsetWidth;shim.height = el.offsetHeight;
        el.shimElement = shim; el.parentNode.insertBefore(shim, el);
    }
    function RemoveMyControl() { if (control != null) { map.DeleteControl(control); control = null; } }
    if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
