﻿// JScript File
function AtlasWeather(enabledCallback, disabledCallback) {
    this.weatherEnabled;
    this.dc = disabledCallback;
    this.ec = enabledCallback;
    this.WeatherIsEnabled = function () {
        if (this.weatherEnabled)
            return true;
        else
            return false;
    }
    this.ToggleWeather = function () {
        var stormLogo = $get('norkartmap1_stormLogo')
        var we = $('weathercontent');
        if (bVEMap == false) {
            if (we) {
                if (this.weatherEnabled) {
                    Event.stopObserving('map', 'map:moved', this.weatherEnabled);
                    Event.stopObserving('map', 'map:zoomedOut', atlasman.zoomedOut);
                    we.hide();
                    this.weatherEnabled = null;
//                    if (stormLogo != null) {
//                        stormLogo.style.display = "none";
//                    }
                    if (this.dc)
                        this.dc();
                }
                else {
                    this.weatherEnabled = this.updateWeatherInfo.bindAsEventListener(this);
                    Event.observe('map', 'map:moved', this.weatherEnabled);
                    Event.observe('map', 'map:zoomedOut', atlasman.zoomedOut);
//                    if (stormLogo != null) {
//                        stormLogo.style.display = "block";
//                    }
                    this.updateWeatherInfo();
                    we.show();
                    if (this.ec)
                        this.ec();
                }
            }
        }
        else 
        { 
            if (we) {
                if (this.weatherEnabled) {
                    this.disableWeather();
                    return;
                    vemap.DetachEvent("onendpan", this.weatherEnabled);
                    vemap.DetachEvent("onendzoom", atlasman.zoomedOut);
                    we.hide();
                    this.weatherEnabled = null;
//                    if (stormLogo != null) {
//                        stormLogo.style.display = "none";
//                    }
                    if (this.dc)
                        this.dc();
                }
                else {
                    this.weatherEnabled = this.updateWeatherInfo.bindAsEventListener(this);
                   vemap.AttachEvent("onendpan", this.weatherEnabled);
                   vemap.AttachEvent("onendzoom", atlasman.zoomedOut)
//                   if (stormLogo != null) {
//                       stormLogo.style.display = "block";
//                   }
                    this.updateWeatherInfo();
                    we.show();
                    if (this.ec)
                        this.ec();
                }
            }
        }
    }
    this.disableWeather = function () {
        if (this.weatherEnabled) {
            var we = $('weathercontent');

            if (bVEMap == false) {

                Event.stopObserving('map', 'map:moved', this.weatherEnabled);
                Event.stopObserving('map', 'map:zoomedOut', atlasman.zoomedOut);
            }
            else {
                vemap.DetachEvent("onendpan", this.weatherEnabled);
                vemap.DetachEvent("onendzoom", atlasman.zoomedOut);
            }
            
            we.hide();
            this.weatherEnabled = null;
            if (this.dc)
                this.dc();
        }
    }
    this.updateWeatherInfo = function () {
        var winW = 630, winH = 460;
        if (parseInt(navigator.appVersion) > 3) {
            if (navigator.appName == "Netscape") {
                winW = window.innerWidth;
                winH = window.innerHeight;
            }
            if (navigator.appName.indexOf("Microsoft") != -1) {
                winW = document.body.offsetWidth;
                winH = document.body.offsetHeight;
            }
        }
        var x = winW / 2;
        var y = winH / 2;
        //        x = 420 + 80;
        //        y = 300 + 80;
        var $weatherBox = $('weatherwrapper');
        var pos = ($weatherBox).cumulativeOffset();
        x = pos[0] + 290;
        y = pos[1] + 40;
        XX = 0.0;
        YY = 0.0;
        var compareHighPriorityLocationsOnly = false;
        if (bVEMap == false) {
            var coord = map.getPixelLatLon(new Coordinate(x, y));
            XX = coord.x;
            YY = coord.y;
            var nkZoomLvl = map.getZoomLevel();
            //alert(nkZoomLvl);
            if (nkZoomLvl < 10) {
                compareHighPriorityLocationsOnly = true;
            }
        }
        else {
            pixel = new VEPixel(x, y);
            var latLongVE = vemap.PixelToLatLong(pixel);
            YY = latLongVE.Latitude;
            XX = latLongVE.Longitude;
            var veZoomLvl = vemap.GetZoomLevel();

        }
        // filter on location when zoom level is above a certain treshold.
        var enableYrLocationFilter = false;
        var preload = $('weatherPreLoad');
        preload.show();
        var $location = $('weatherLocation');
        if ($location) {
            $location.innerHTML = '';
        }
        //        var se = $('stormError');
        //        se.hide();
        weatherjs.GetAtlasNowAnd3DayWeather(XX, YY, compareHighPriorityLocationsOnly, this.OnAtlasWeather3DayFetched.bind(this));
    }
    // test
    this.CreateSymbolPath = function (code) {
        if (code) {
            return "images/weather2/" + code + ".png";
        } else {
            return "images/weather2/blank.gif";
        }
    }
    this.OnAtlasWeather3DayFetched = function (arg) {
        var $location = $('weatherLocation');
        var $nowTemp = $('weatherNowTemp');
        var $weatherNowSymbol = $('weatherNowSymbol');
        var $dayNm1Short = $('weather1DayName');
        var $weather1Symbol = $('weather1DaySymbol');
        var $temp1day = $('weather1Temp');
        var $dayNm2Short = $('weather2DayName');
        var $weather2Symbol = $('weather2DaySymbol');
        var $temp2day = $('weather2Temp');
        var $dayNm3Short = $('weather3DayName');
        var $weather3Symbol = $('weather3DaySymbol');
        var $temp3day = $('weather3Temp');

        $location.innerHTML = "Mangler værdata";
        $nowTemp.innerHTML = "";
        $weatherNowSymbol.src = "images/blank.gif";
        $dayNm1Short.innerHTML = '-';
        $dayNm2Short.innerHTML = '-';
        $dayNm3Short.innerHTML = '-';
        $weather1Symbol.src = 'images/blank.gif';
        $weather2Symbol.src = 'images/blank.gif';
        $weather3Symbol.src = 'images/blank.gif';
        $temp1day.innerHTML = '';
        $temp2day.innerHTML = '';
        $temp3day.innerHTML = '';

        if (arg) {
            if (arg.lat && arg.lon) {
                if ($location) {
                    $location.innerHTML = arg.lat + '<br />' + arg.lon;
                }
            }
            if (arg.WeatherNow) {
                if (arg.WeatherNow.Temperature) {
                    var deg = parseFloat(arg.WeatherNow.Temperature);
                    if ($nowTemp) {
                        $nowTemp.innerHTML = deg.toFixed(0) + "&deg;";
                    }
                }
                if ($weatherNowSymbol) {
                    $weatherNowSymbol.src = this.CreateSymbolPath(arg.WeatherNow.WeatherSymbolCode);
                }
            }
            if (arg.Weather1Day) {
                if (arg.Weather1Day.DayNmShort) {
                    if ($dayNm1Short) {
                        $dayNm1Short.innerHTML = arg.Weather1Day.DayNmShort;
                    }
                }
                if ($weather1Symbol) {
                    $weather1Symbol.src = this.CreateSymbolPath(arg.Weather1Day.WeatherSymbolCode);
                }
                if (arg.Weather1Day.Temperature) {
                    var deg = parseFloat(arg.Weather1Day.Temperature);
                    if ($temp1day) {
                        $temp1day.innerHTML = deg.toFixed(0) + "&deg;";
                    }
                }
            }
            if (arg.Weather2Day) {
                if (arg.Weather2Day.DayNmShort) {
                    if ($dayNm2Short) {
                        $dayNm2Short.innerHTML = arg.Weather2Day.DayNmShort;
                    }
                }
                if ($weather2Symbol) {
                    $weather2Symbol.src = this.CreateSymbolPath(arg.Weather2Day.WeatherSymbolCode);
                }
                if (arg.Weather2Day.Temperature) {
                    var $temp2day = $('weather2Temp');
                    var deg = parseFloat(arg.Weather2Day.Temperature);
                    if ($temp2day) {
                        $temp2day.innerHTML = deg.toFixed(0) + "&deg;";
                    }
                }
            }
            if (arg.Weather3Day) {
                if (arg.Weather3Day.DayNmShort) {
                    if ($dayNm3Short) {
                        $dayNm3Short.innerHTML = arg.Weather3Day.DayNmShort;
                    }
                }
                if ($weather3Symbol) {
                    $weather3Symbol.src = this.CreateSymbolPath(arg.Weather3Day.WeatherSymbolCode);
                }
                if (arg.Weather3Day.Temperature) {
                    var deg = parseFloat(arg.Weather3Day.Temperature);
                    if ($temp3day) {
                        $temp3day.innerHTML = deg.toFixed(0) + "&deg;";
                    }
                }
            }
        }
        var preload = $('weatherPreLoad');
        if (preload)
            preload.hide();
        //alert("municipal=" + arg.locationNm + ", temp: " + arg.WeatherNow.temperature + ", timeFrom: " + arg.WeatherNow.timeFrom + "\nwinFilename: " + arg.windDirFilename + ", dist: " + arg.distance);
    }
    this.OnWeatherNowFetched = function (arg) {
        if (arg) {
            var esymbol = $('stormSymbol');
            if (esymbol)
                esymbol.innerHTML = "symbol: " + arg.symbol;
            var elat = $('stormLat');
            if (elat)
                elat.innerHTML = arg.lat;
            var elon = $('stormLon');
            if (elon)
                elon.innerHTML = arg.lon;
            var weatherWindDir = $('weatherWindDir');
            if (weatherWindDir) {
                if (arg.windDirFilename)
                    weatherWindDir.src = "images/weather/" + arg.windDirFilename;
                else
                    weatherWindDir.src = "images/weather/blank.gif";
            }
            var weatherBox = $('weatherBox');
            if (weatherBox)
                weatherBox.style.backgroundImage = "url('images/weather/" + arg.symbolFilename + "')";
            var ewindspeed = $('weatherWindSpeed');
            if (ewindspeed)
                ewindspeed.innerHTML = arg.windspeed;
            var etemperature = $('weatherTemp');
            if (etemperature)
                etemperature.innerHTML = arg.temperature;
            var ecloudcover = $('stormCloudCover');
            if (ecloudcover)
                ecloudcover.innerHTML = arg.cloudcover;
            var preload = $('#weatherPreLoad');
            if (preload)
                preload.hide();
        }
    }
    this.OnWeatherNowFetchError = function (error) {
        var errMsg;
        errMsg = "Kunne ikke hente været\nFeilmelding: ";
        if (error)
            errMsg += error.get_message();
        else
            errMsg += "Ingen feilmelding mottatt.";
//        var se = $('stormError');
//        se.innerHTML = errMsg;
//        $('stormTemperature').innerHTML = 'Error';
        var preload = $('weatherPreLoad');
        preload.hide();
    }
}
