﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery */

//  
//  Author: Kevin Burkitt
//  Date:   2010-06-16
//
cc.alerts = function() {

    return {
        timerData: null,
        timerDataInterval: 3000,
        timerDisplay: null,
        timerDisplayInterval: 3000,
        alertCount: 4,
        list: [],
        offset: -1,
        started: false,
        hidden: true,

        // 
        onAlertEvents: [],

        init: function(container) {

            // Only active for logged in users:
            if (typeof cc.activeUsername === 'undefined' || cc.activeUsername === '') {
                return;
            }

            // Init Timer for alert updates
            //cc.alerts.loadAlerts();
        },

        loadAlerts: function() {
            cc.ajax.getJSON(
                '/alerts/' + cc.alerts.getLastUpdated(),
                {},
                function(data, textStatus, XMLHttpRequest) {
                    if (typeof data === 'undefined' || data.length <= 0) {
                        return;
                    }

                    cc.alerts.list = data;
                },
                function() {
                    if (cc.alerts.started !== true) {
                        cc.alerts.updateDisplay();
                        cc.alerts.started = true;
                    }

                    // Init Timer for alert updates
                    clearTimeout(cc.alerts.timerData);
                    cc.alerts.timerData = setTimeout(function() {
                        cc.alerts.loadAlerts();
                    }, cc.alerts.timerDataInterval);
                });
        },

        getLastUpdated: function() {
            if (cc.alerts.listOk() !== true) {
                return cc.alertsLastUpdate;
            }

            for (var item in cc.alerts.list) {
                if (item.createdAtTicks > cc.alertsLastUpdate) {
                    cc.alertsLastUpdate = item.createdAtTicks;
                }
            }

            return cc.alertsLastUpdate;
        },

        updateDisplay: function() {
            if (jQuery('#cc-top-notify-alert').size() <= 0) {
                var html = "<div id=\"cc-top-notify-alert\">";
                html += "<div class=\"tna-bg clearfix\"><div class=\"tna-bg-center\"></div></div>";

                html += "<div class=\"tna-content\"><div class=\"tna-content-center\">";
                html += "<div class=\"tna-left\" id=\"cc-alerts-content\"></div>";
                html += "<div class=\"tna-right\">";
                html += "<a href='#' class=\"tna-arrow\" id='cc-alerts-header-prev'>&lt;</a>";
                html += "<a href='#' class=\"tna-arrow\" id='cc-alerts-header-next'>&gt;</a>";
                html += "<a href='#' class=\"tna-arrow\" id='cc-alerts-header-close'>X</a>";
                html += "</div></div></div>";

                html += "</div>";

                jQuery(html).hide().prependTo("body");

                jQuery('#cc-alerts-header-close').click(function() {
                    clearTimeout(cc.alerts.timerData);
                    clearTimeout(cc.alerts.timerDisplay);
                    jQuery('#cc-top-notify-alert').slideUp(500);
                    return false;
                });

                jQuery('#cc-alerts-header-next').click(function() {
                    clearTimeout(cc.alerts.timerData);
                    clearTimeout(cc.alerts.timerDisplay);
                    cc.alerts.showNext();
                    return false;
                });

                jQuery('#cc-alerts-header-prev').click(function() {
                    clearTimeout(cc.alerts.timerData);
                    clearTimeout(cc.alerts.timerDisplay);
                    cc.alerts.showPrev();
                    return false;
                });
            }

            cc.alerts.showNext();

            clearTimeout(cc.alerts.timerDisplay);
            cc.alerts.timerDisplay = setTimeout(function() {
                cc.alerts.updateDisplay();
            }, cc.alerts.timerDisplayInterval);
        },

        showNext: function(c_name, value, expiredays) {
            if (cc.alerts.listOk() !== true) {
                return;
            }

            cc.alerts.offset += 1;
            if (cc.alerts.offset >= cc.alerts.list.length) {
                cc.alerts.offset = 0;
            }

            cc.alerts.show();
        },

        showPrev: function(c_name, value, expiredays) {
            if (cc.alerts.listOk() !== true) {
                return;
            }

            cc.alerts.offset -= 1;
            if (cc.alerts.offset < 0) {
                cc.alerts.offset = cc.alerts.list.length - 1;
            }

            cc.alerts.show();
        },

        show: function() {
            if (cc.alerts.listOk() !== true) {
                return;
            }

            if (cc.alerts.hidden) {
                jQuery('#cc-top-notify-alert').slideDown(500);
                cc.alerts.hidden = false;
            }

            var alert = cc.alerts.list[cc.alerts.offset];
            if (typeof alert !== 'undefined' && typeof alert.message !== 'undefined' && alert.message !== '') {
                jQuery('#cc-alerts-content').slideDown(500, function() {
                    if (cc.alerts.list.length > 1) {
                        jQuery('#cc-alerts-content').hide();
                    }
                    jQuery('#cc-alerts-content').html(alert.message).fadeIn(500);

                    if (cc.alerts.list.length > 1) {
                        jQuery('#cc-alerts-header-prev').show();
                        jQuery('#cc-alerts-header-next').show();
                    } else {
                        jQuery('#cc-alerts-header-prev').hide();
                        jQuery('#cc-alerts-header-next').hide();
                    }
                });
            }
        },

        listOk: function() {
            if (typeof cc.alerts.list === 'undefined' || cc.alerts.list.length <= 0) {
                if (cc.alerts.hidden === false) {
                    jQuery('#cc-top-notify-alert').slideUp(500);
                    cc.alerts.hidden = true;
                }
                return false;
            }
            return true;
        }

    };

} ();


