// jQuery Alert Dialogs Plugin
//
// Version 1.1
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 14 May 2009
//
// Visit http://abeautifulsite.net/notebook/87 for more information
//
// Usage:
//		jAlert( message, [title, callback] )
//		jConfirm( message, [title, callback] )
//		jPrompt( message, [value, title, callback] )
// 
// History:
//
//		1.00 - Released (29 December 2008)
//
//		1.01 - Fixed bug where unbinding would destroy all resize events
//
// License:
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC. 
//
(function($) {

    $.getScript('/properties/alertContents.js');

    $.alerts = function(){
        var constants = {
            verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
            horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
            repositionOnResize: true,           // re-centers the dialog on window resize
            overlayOpacity: 0.65,                // transparency level of overlay
            overlayColor: '#111',//'#FFF',               // base color of overlay
            draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
            ok: '/images/confirm_yes.png',         // text for the OK button
            cancel: '/images/confirm_no.png', // text for the Cancel button
            closeButton: 'Close',
            dialogClass: null                  // if specified, this class will be applied to all dialogs
        }
        // Public methods

        this.alert = function(message, title, callback) {
            if( title == null ) title = 'Alert';
            _show(title, message, null, 'alert', function(result) {
                    if( callback ) callback(result);
            });
        }

        this.confirm = function(message, title, callback) {
                if( title == null ) title = 'Confirm';
                _show(title, message, null, 'confirm', callback);
        }

        this.prompt = function(message, value, title, callback) {
                if( title == null ) title = 'Prompt';
                _show(title, message, value, 'prompt', callback);
        }

        // Private methods

        var _show = function(title, msg, value, type, callback) {

            _hide();
            _overlay('show');

            $("BODY").append(
              '<div id="popup_container">' +
                '<h3 id="popup_title"></h3>' +
                '<div id="popup_content">' +
                  '<div id="popup_message"></div>' +
                    '</div>' +
              '</div>');

            if( constants.dialogClass ) $("#popup_container").addClass(constants.dialogClass);

            // IE6 Fix
            var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';

            $("#popup_container").css({
                    position: pos,
                    zIndex: 99999,
                    padding: 0,
                    margin: 0
            });

            $("#popup_title").text(title);
            $("#popup_content").addClass(type);
            $("#popup_message").text(msg);
            $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );

            $("#popup_container").css({
                    minWidth: $("#popup_container").outerWidth(),
                    maxWidth: $("#popup_container").outerWidth()
            });

            _reposition();
            _maintainPosition(true);

            switch( type ) {
                case 'alert':
                    $("#popup_message").after('<div id="popup_panel">'+ _createBtn(constants.closeButton) + '</div>');
                    $("#popup_"+constants.closeButton).click( function() {
                        _hide();
                        callback(true);
                    });
                    $("#popup_"+constants.closeButton).focus().keypress( function(e) {
                        alert(e.keycode);
                        if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_"+constants.closeButton).trigger('click');
                    });
                break;
                case 'confirm':
                   var btnHtml = "";
                   jQuery.each(callback, function(i, callb) {
                       btnHtml +=  _createBtn(callb[0]);

                   });
                   $("#popup_message").after('<div id="popup_panel">' + btnHtml + '</div>');

                   jQuery.each(callback, function(i, callb) {
                       $("#popup_"+callb[0]).click( function() {
                            _hide();
                            if( callb[1] ) eval(callb[1]).call();
                        });
                   });
                break;
                case 'prompt':
                    var pbtnHtml = "";
                    jQuery.each(callback, function(i, callb) {
                       pbtnHtml += _createBtn(callb[0]);
                    });
                    $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel">'+pbtnHtml+'</div>');
                    $("#popup_prompt").width( $("#popup_message").width() );

                    jQuery.each(callback, function(i, callb) {
                       $("#popup_"+callb[0]).click( function() {
                           var val = $("#popup_prompt").val();
                           _hide();
                           if( callb[1] ) eval(callb[1]).call(this, [val]); //two args this and the array of values
                        });
                   });
                    if( value ) $("#popup_prompt").val(value);
                    $("#popup_prompt").focus().select();
                break;
            }

            //IE 7 fix--Mani
             var disp = ($.browser.msie && parseInt($.browser.version) <= 7) ? "inline" : "inline-block";
             $(".popup_btn").css("display", disp);

            // Make draggable
            /*if( constants.draggable ) {
                try {
                    $("#popup_container").draggable({handle: $("#popup_title")});
                    $("#popup_title").css({cursor: 'move'});
                } catch(e) { /* requires jQuery UI draggables */ //}
            //}
        }

        var _createBtn = function(btn) {
            return '<div class="popup_btn" '+ 'id="popup_'+ btn +'" >' +
                        '<div class="popup_btn_l"></div>' +
                        '<div class="popup_btn_m"><span>'+ btn +'</span></div>' +
                        '<div class="popup_btn_r"></div>' +
                   '</div>';
        }

        var _hide = function() {
            $("#popup_container").remove();
            _overlay('hide');
            _maintainPosition(false);
        }

        var _overlay = function(status) {
            switch( status ) {
                case 'show':
                    _overlay('hide');
                    $("BODY").append('<div id="popup_overlay"></div>');
                    $("#popup_overlay").css({
                        position: 'absolute',
                        zIndex: 99998,
                        top: '0px',
                        left: '0px',
                        width: '100%',
                        height: $(document).height(),
                        background: constants.overlayColor,
                        opacity: constants.overlayOpacity
                    });
                break;
                case 'hide':
                        $("#popup_overlay").remove();
                break;
            }
        }

        var _reposition = function() {
            var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + constants.verticalOffset;
            var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + constants.horizontalOffset;
            if( top < 0 ) top = 0;
            if( left < 0 ) left = 0;

            // IE6 fix
            if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
            
            $("#popup_container").css({
                top: top + 'px',
                left: left + 'px'
            });
            $("#popup_overlay").height( $(document).height() );
        }

        var _maintainPosition = function(status) {
            if( constants.repositionOnResize ) {
                switch(status) {
                    case true:
                            $(window).bind('resize', _reposition);
                    break;
                    case false:
                            $(window).unbind('resize', _reposition);
                    break;
                }
            }
        }
    }

    // Shortuct functions
    $.alerts.prototype.jAlert = function(message, title, callback) {
            this.alert(message, title, callback);
    }

    $.alerts.prototype.jConfirm = function(message, title, callback) {
            this.confirm(message, title, callback);
    };

    $.alerts.prototype.jPrompt = function(message, value, title, callback) {
            this.prompt(message, value, title, callback);
    };

    $.alerts.prototype.createAlert = function(alertKey, type, callback) {
        var msg = ALERT_BOX.getMessage(alertKey);
        var title = ALERT_BOX.getTitle(alertKey);
        switch(type){
            case 'default':
                this.alert(msg, title, callback);
            break;
            case 'custom':
                this.confirm(msg, title, callback);
            break;
            case 'prompt':
                this.prompt(msg, "", title, callback);
            break;
        }
    }

})(jQuery);
