﻿var proxy;
var tickerIndex;
// JScript File
var WeatherRealtime = {
    timer: 10240, // second
    display: 25, // how many items displayed
    interval: null,
    animation: 1500, //milisecond

    init: function() {
        if (!WeatherRealtime.interval) {
            tickerIndex = 0;
            // stop scrolling when mouseover the message
            // start scroll again when mouseout
            $("#ticker_content .wrapper").mouseover(function() {
                WeatherRealtime.stopScroll();
            }).mouseout(function() {
                WeatherRealtime.startScroll();
            })

            // setInterval("WeatherRealtime.RatingUpdate()", this.timer * 1000);            
            WeatherRealtime.startScroll();
        }
    },

    RatingUpdate: function() {
        var i;
        // add class active when mouseover
        // remove class active when mouse out
        var cpostItem = $(".wrapper .headline");
        $(cpostItem).mouseover(function() {
            $(this).addClass("active");
        }).mouseout(function() {
            $(this).removeClass("active");
        });

        if (map == null) return;
        var bounds = getBoundText(map.getBounds());
        if (proxy == null) proxy = new Sys.Data.DataService(SERVICE_URL);
        proxy.query("WeatherInBounds?onlyuser=true&bounds=" + bounds, ticker_querySuccess, ticker_queryFailure);
    },
    startScroll: function() {
        if (!this.interval) {
            this.interval = setInterval("WeatherRealtime.RatingUpdate()", this.timer * 1000);
        }
    },
    stopScroll: function() {
        if (this.interval) {
            clearInterval(this.interval);
            this.interval = false;
        }
    }
};

function _StringFormatInline() {
    var txt = this;
    for (var i = 0; i < arguments.length; i++) {
        var exp = new RegExp('\\{' + (i) + '\\}', 'gm');
        txt = txt.replace(exp, arguments[i]);
    }
    return txt;
}

function _StringFormatStatic() {
    for (var i = 1; i < arguments.length; i++) {
        var exp = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        arguments[0] = arguments[0].replace(exp, arguments[i]);
    }
    return arguments[0];
}

if (!String.prototype.format) {
    String.prototype.format = _StringFormatInline;
}

if (!String.format) {
    String.format = _StringFormatStatic;
}


function getBoundText(bounds) {
    var formatText = "(({0},%20{1}),%20({2},%20{3}))"
    var southWest = bounds.getSouthWest();  
    var northEast = bounds.getNorthEast();
    return String.format(formatText, round(southWest.lat()), round(southWest.lng()), round(northEast.lat()), round(northEast.lng()));
}

function round(value){
    return Math.round(value * 20) / 20;
}


function ticker_initDefault() {
    if (map == null) return;
    var bounds = map.getBounds();
    if (proxy == null) proxy = new Sys.Data.DataService(SERVICE_URL);
    // proxy.query("WeatherInBounds?onlyuser=true&bounds=" + bounds, ticker_queryDefaultSuccess, ticker_queryFailure);
}


function ticker_queryDefaultSuccess(result, context, operation) {
    if (result && result.length > 0) {
        for (i = 0; i < result.length; i++) {
            setDetails(result[i]);
        }
    }
}
function ticker_querySuccess(result, context, operation) {

    if (result && result.length > 0 && tickerIndex > -1) {
        if (tickerIndex > result.length) tickerIndex = result.length - 1;
        setDetails(result[tickerIndex]);
        
        var postItem = $(".wrapper .headline");        
        // fade out and remove the last post
        $(postItem[WeatherRealtime.display]).animate({ opacity: .50 }, WeatherRealtime.animation, function() {
            $(postItem[WeatherRealtime.display]).remove();
        });
        // add class active when mouse over
        // remove class active when mouseout
        $(postItem).mouseover(function() {
            $(this).addClass("active");
        }).mouseout(function() {
            $(this).removeClass("active");
        });

        tickerIndex--;
        if (tickerIndex < 0) tickerIndex = result.length;
    }
}

function ticker_queryFailure(error, context, opeartion) {
    // alert(error);
}

function setDetails(details) {
    if (details == null) return;
    var localTime = new Date(details.EnteredDateTime);    
    var location = details.CityName;
//    if (details.PostalCode != null && details.PostalCode != "")
//        location = details.PostalCode + ", " + location;
//    var html = "<span class='nopadding'><b><a href='station.aspx?id=" + details.LocationId + "'>" +
//            location + "</a></b>" +
//            localTime.format("HH:mm") + " - " +
//            "<span style='width:50px;'>Temperatuur: </span>" + details.Temperature + "° C<br />" +
//            "<span style='width:50px;'></span><img src='wicons/" + details.WeatherFeelingIcon + ".gif' /><br /></span>";
    var html = "<table class='ticker_table'><tr><td colspan='2'>" +
            "<b><a href='station.aspx?id=" + details.LocationId + "'>" +
            location + "</a></b></td></tr><tr>" +
            "<td>" + localTime.format("HH:mm") + "</td><td style='text-align:right;'> " + details.Temperature + "° C</td></tr><tr>" +
            "<td colspan='2' style='text-align:center;'><img src='/wicons/" + details.WeatherFeelingIcon + ".gif' /></td></tr></table>";
            
    $(" <tr />")
             .attr("id", "wrapper-header")
             .addClass("headline")
             .html("<td>" + html + "</td>")
             .prependTo($("#ticker_list"));
}

// initial once ducument finish loaded
$(WeatherRealtime.init);
