

(function ($) {

    $.fn.notificationBar = function () {
        $this = $(this);
        $this.css({ "visibility": 'hidden' });
        $this.css({ "margin-top": $("#NotificationBarContent").height() + 'px' });
        //alert("Blah");
    }

    //----------------------------------------------------------------------

    var timeout;
    $.fn.notificationBar.showMessage = function (params) {
        if (!document.getElementById("NotificationBar")) {
            $.fn.notificationBar.createNotificationBar();
        }
		
        var msgHTML = new String();

        // Create the title HTML
        var titleIcon = new String();

        if (params.TitleIcon) {
            titleIcon = params.TitleIcon;
        } else {
            titleIcon = $.fn.notificationBar.defaults.titleIcon;
        }

        // Create the title HTML
        var MsgTitle = new String();

        if (params.MsgTitle) {
            MsgTitle = params.MsgTitle;
        } else {
            MsgTitle = $.fn.notificationBar.defaults.msgTitle;
        }



        var titleHTML = '<h5 class="Icon EmbeddedLeft CentreVertically Title ' + titleIcon + '">' + MsgTitle + '</h5>';
        msgHTML += titleHTML;

        // Create the message HTML
        for (var i = 0; i < params.MsgList.length; i++) {
            var thisIcon = new String();
            var thisMsg = new String();

            if (params.MsgList[i].Icon) {
                thisIcon = params.MsgList[i].Icon
            } else if (params.DefaultIcon) {
                thisIcon = params.DefaultIcon;
            } else {
                thisIcon = "";
            }

            if (params.MsgList[i].Msg) {
                thisMsg = params.MsgList[i].Msg
            }
            msgHTML += '<p class="Icon EmbeddedLeft ' + thisIcon + '">' + thisMsg + '</p>';
        }


        $("#NotificationBarContent").html(msgHTML);
        var barHeight = $("#NotificationBarContent").height();


        if ($this.css('visibility') == 'hidden') {
            $this.css({ "margin-top": -barHeight });
            $this.css({ "visibility": 'visible' });

            // Set the length of time the message will be displayed for
            var displayTime;

            if (params.Time) {
                displayTime = params.Time;
            } else {
                displayTime = $.fn.notificationBar.defaults.time;
            }

            $this.animate({ marginTop: "0px" }, 500, function () {
                timeout = setTimeout('$.fn.notificationBar.hideMessage()', displayTime);
            });
        } else {
            $this.animate({ marginTop: -barHeight }, 500, function () {
                $this.css({ "visibility": 'hidden' });
            });
        }
    };

    //----------------------------------------------------------------------

    $.fn.notificationBar.createNotificationBar = function () {
        var barHTML = new String();

        barHTML += '<div id="NotificationBar">';
        barHTML += '<div class="CenteredContainer">';
        barHTML += '<div id="NotificationBarContent"></div>';
        barHTML += '</div></div>';

        $('body').prepend(barHTML);
        $("#NotificationBar").notificationBar({});

    };
    //----------------------------------------------------------------------

    $.fn.notificationBar.hideMessage = function () {
        clearTimeout(timeout);
        var barHeight = $("#NotificationBarContent").height();
        $this.animate({ marginTop: -barHeight }, 500, function () {
            $this.css({ "visibility": 'hidden' });
        });
    };

    //----------------------------------------------------------------------

    $.fn.notificationBar.defaults = {
        time: 5000,
        titleIcon: 'AddNew',
        msgTitle: 'Default message title'
    };

})(jQuery);
