﻿/* Acts as an interface for 3rd party notification-plugins used by dit
 * Author: NHA 
 *
 *  Depends on:
 *	jquery.jnotify.js
 *  jquery-1.4.2.js
 */

(function ($) {

    $.extend({

        notify_clear: function () {
            /// <summary>Removes all visible notifications</summary>	       
            /// <returns type="jQuery" />
            $.pnotify_remove_all();
        },

        notify_history: function (enable) {
            /// <summary>Enable/disable the notification history bar.</summary>
            /// <param name="enable" type="var">Boolean value indicating wether or not to display the history bar.
            ///  </param>
            /// <returns type="jQuery" />
            //$.pnotify({ pnotify_history: true });
            $(".ui-pnotify-history-container").css('display', (enable) ? 'block' : 'none');
        },
      
        notify: function (options) {
            /// <summary>Displays a notification</summary>
            /// <param name="options" type="Options">A set of key/value pairs that configure the the notification. 
            /// (examples: {title:'My dirty title', text:'frækt', autohide:false, delay:8000, enable_history:false, width:180, type:'error', clear:true; loader:true } )
            ///  </param>
            /// <returns type="jQuery" />


            // Build main options.
            var opts;
            if (typeof options != "object") {
                opts = $.extend({}, $.notify.defaults);
                opts.text = options;
            } else {
                opts = $.extend({}, $.notify.defaults, options);
            }

            // add load-indicator below the notification text 
            if (opts.loader) { opts.text += "<div class='loader' />"; }

            // remove existing notices
            if (opts.clear) { $.pnotify_remove_all(); }

            // merge options to 3rd party plugin:
            var pnotify_options = {
                pnotify_title: opts.title
				, pnotify_text: opts.text
				, pnotify_history: opts.enable_history
				, pnotify_close: opts.close
				, pnotify_hide: opts.autohide
				, pnotify_delay: opts.delay
				, pnotify_width: opts.width
                , pnotify_insert_brs: opts.insert_brs

				, pnotify_shadow: false
				, pnotify_animation: "fade"
				, pnotify_animate_speed: "fast"
            };

            switch (opts.type) {
                case 'error':

                    pnotify_options.pnotify_error_icon = (!pnotify_options.pnotify_title) ? '' : 'ui-icon ui-icon-alert';
                    pnotify_options.pnotify_type = "error";
                    pnotify_options.pnotify_animate_speed = "fast";
                    pnotify_options.pnotify_hide = false;
                    break;

                case 'success':
                    pnotify_options.pnotify_notice_icon = 'ui-icon ui-icon-check';
                    break;

                case 'shoutout':
                    pnotify_options.pnotify_notice_icon = 'ui-icon ui-icon-signal-diag';
                    break;

                case 'question':
                    pnotify_options.pnotify_notice_icon = 'ui-icon ui-icon-info';
                    pnotify_options.pnotify_animation = 'slide'
                    break; 

                default:
                    pnotify_options.pnotify_error_icon = (!pnotify_options.pnotify_title) ? '' : 'ui-icon ui-icon-info';
                    pnotify_options.pnotify_notice_icon = '';
                    break;
            }

            // ...and finally, do the magic:
            return $.pnotify(pnotify_options);
        }
    });


    $.notify.defaults = {
        // The notice's title - if false, title isn't displayed
        title: false
        // The notice's text. if false, text isn't displayed
		, text: false
        // Display a pull down menu to redisplay previous notices, and place the notice in the history.
		, enable_history: true
        // Type of the notice. "notice", "success", "error", "shoutout" (måske?) - defaults to notice.
		, type: "notice"
        // Provide a button for the user to manually close the notice.
		, close: true
        // After a delay, remove the notice.
		, autohide: true
        // Delay in milliseconds before the notice is removed.
		, delay: 3000
        // Add a ajax load indicator below the message text.
		, loader: false
        // Remove all current notifiations before the new notification is displayed.
		, clear: false
        // Width of the notice.
		, width: "230px"
        // insert <br> tags
        ,insert_brs : true

    };
})(jQuery);

var _alert;
$(document).ready(function () {    
	consume_alert();
});

function consume_alert() {
	if (_alert) return;
	_alert = window.alert;
	window.alert = function (message) {
		$.pnotify({
			pnotify_title: 'Alert',
			pnotify_text: message
		});
	};
}

function release_alert() {
	if (!_alert) return;
	window.alert = _alert;
	_alert = null;
}

