(function (lmig, $, undefined) {
    lmig.notice = lmig.notice || {};

    lmig.notice.config = function (conf) {
        // If called without parameter, returns current configuration
        if (!conf) return window["_notification_conf_"] || {};
        window["_notification_conf_"] = conf;
    }

    $(window).load(function () {
        var conf = lmig.notice.config();
        window["_callback_"] = function (data) {
            if (!!data.length) {

                var notice = data[0];

                // Check if the notice is activated
                if (!notice || !notice.IsActivated) return;

                // Hide private notice
                if (!notice.IsPublic && !conf.userHash) return;

                // Parse date
                var today = new Date(),
			        start_ = eval('new' + notice.StartDate.replace(/\//g, ' ')),
			        end_ = eval('new' + notice.EndDate.replace(/\//g, ' '));

                // Check if the notice should display today 
                if (start_.getTime() < today.getTime() && end_.getTime() > today.getTime()) {
                    var notification_tmpl = $.template(null, "<div class='close'></div><p class='notification-title'>${Title}</p><p class='notification-content'>${Message}</p>");

                    $.tmpl(notification_tmpl, notice).appendTo(conf.container || '#notification-panel');

                    var center_y = (window.innerHeight >> 1) - 150;
                    var center_x = (window.innerWidth >> 1) - 200;
                    if (!$.browser.msie) {
                        $('.notification').css({
                            'left': center_x,
                            'display': 'block'
                        });

                        $('.notification').animate(
                            { top: center_y + 50 },
                            { duration: 1000, easing: 'jswing' }
                        ).animate(
                            { top: center_y },
                            { duration: 1000, easing: 'easeOutBounce' }
                        );
                    } else {
                        $('.notification').css({

                            'top': '30%',
                            'left': '30%',
                            'display': 'block'
                        }).fadeIn(500);
                    }

                    $('.close').click(function () {
                        $('.notification').fadeOut(500);
                        $.cookie('_notice_displayed', true);
                    });
                }
            }
        }

        if (!$.cookie('_notice_displayed')) {
            $.ajax({
                dataType: 'jsonp',
                jsonp: false,
                jsonpCallback: 'callback',
                url: 'http://localhost/LMIPortal/services/NoticeService.svc/GetNoticeByProject?pid=' + conf.projectId + '&hash=' + conf.userHash + '&callback=_callback_',
                type: 'GET'
            });
        }
    });

} (window.lmig = window.lmig || {}, jQuery));
