/*******************************************
* Notifications manager                    *
* UbiCast 2009 - 2012, all rights reserved *
* Author: Stephane Diemer                  *
*******************************************/

function NotificationsManager() {
    // params
    this.url = "";
    this.label = "This location is broadcasting live";
    this.icon = "";
    this.display_time = 10000;
    this.check_period = 30000;
    // vars
    this.displayed = false;
    this.active_lives = new Array();
    this.data_hash = "";
    this.hidden_sessions = {};
    
    this.init();
}

NotificationsManager.prototype.init = function () {
    var stored_sessions = get_cookie("notifications_sessions");
    var splitted = stored_sessions.split("|");
    for (var i = 0; i < splitted.length; i ++) {
        this.hidden_sessions[splitted[i]] = true;
    }
    
    var obj = this;
    $(window).ready(function() {
        obj.check_lives();
        if (navigator.platform == "iPad" || navigator.platform == "iPhone" || navigator.platform == "iPod") {
            $("#overlay_notifications").addClass("no-fixed");
        }
    });
}

NotificationsManager.prototype.show = function () {
    if ($("#notifications_button").hasClass("hidden")) {
        $("#notifications_button").removeClass("hidden");
        $("#notifications_place").show("slow");
    }
}
NotificationsManager.prototype.hide = function () {
    if (!$("#notifications_button").hasClass("hidden")) {
        $("#notifications_button").addClass("hidden");
        $("#notifications_place").hide("slow");
    }
}
NotificationsManager.prototype.toggle = function () {
    if ($("#notifications_button").hasClass("hidden"))
        this.show();
    else
        this.hide();
}

NotificationsManager.prototype.check_lives = function () {
    var obj = this;
    $.ajax({
        url: this.url,
        dataType: "json",
        cache: false,
        success: function (json_data) {
            var data_hash = null;
            try { data_hash = json_data.data_hash; }
            catch (e) { }
            if (data_hash != null && data_hash != obj.data_hash) {
                obj.data_hash = json_data.data_hash;
                obj.active_lives = json_data.lives;
                obj.update_display();
            }
        }
    });
    setTimeout(function() {
        obj.check_lives();
    }, obj.check_period);
}
NotificationsManager.prototype.update_display = function () {
    var html = "";
    if (this.active_lives.length > 0) {
        var lives_to_display = 0;
        html += "<div class=\"live-stream-list\">";
        if (this.active_lives.length == 1) {
            var live = this.active_lives[0];
            html += "<p><a href=\""+live.url+"\">";
            html += "<img src=\""+this.icon+"\"/> <span>"+this.label+"</span>";
            html += "</a></p>";
            if (!this.hidden_sessions[live.id+"-"+live.session_id]) lives_to_display += 1;
        }
        else {
            html += "<table><tr>";
            html += "<td><img src=\""+this.icon+"\"/></td> <td><p>"+this.label+"</p><ul>";
            for (var i = 0; i < this.active_lives.length; i++) {
                var live = this.active_lives[i];
                html += "<li><a href=\""+live.url+"\">"+live.title+"</a></li>";
                if (!this.hidden_sessions[live.id+"-"+live.session_id]) lives_to_display += 1;
            }
            html += "</ul></td></tr></table>";
        }
        html += "</div>";
        $("#notifications_place").html(html);
        if (!this.displayed) {
            $("#overlay_notifications").slideDown();
            this.displayed = true;
        }
        if (lives_to_display > 0) {
            this.show();
            var obj = this;
            setTimeout(function() {
                obj.hide_and_save();
            }, obj.display_time);
        }
    }
    else {
        $("#notifications_place").html("");
        if (this.displayed) {
            $("#overlay_notifications").slideUp();
            this.displayed = false;
        }
    }
}
NotificationsManager.prototype.hide_and_save = function () {
    this.hide();
    var sessions = "";
    for (var i = 0; i < this.active_lives.length; i++) {
        if (sessions != "") sessions += "|"
        sessions += this.active_lives[i].id+"-"+this.active_lives[i].session_id;
        this.hidden_sessions[this.active_lives[i].id+"-"+this.active_lives[i].session_id] = true;
    }
    set_cookie("notifications_sessions", sessions);
}




