/*
 * jQuery MD plugin 1.0
 * Released: June 7, 2011
 * 
 * Copyright (c) 2011 Steve Koehler
 * Email: steve@tiny-threads.com
 * 
 * Original Design: Michael Leigeber
 * http://www.leigeber.com/2011/04/custom-javascript-dialog-boxes/
 *
 * Updated Design: Steve Koehler
 * http://www.tiny-threads.com/blog/2011/06/07/jquery-modal-dialog-plugin/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 *  Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * @license http://www.opensource.org/licenses/mit-license.php
 * @project jquery.md
 */
 
(function( $ ){
	$.md = function(msg,options){
		if(navigator.appName=='Microsoft Internet Explorer')
			this.ie=true;
		else
			this.ie=false;
		
        this.defaults = {
            timeout: 0
            , showClose: true
            , width: 525
            , buttons: {}
            , type: 'err'
            , title: ''
			, cssDir: 'css'
        };
        this.DialogTypes = new Array("error", "warning", "success", "prompt");
        this.DialogTitles = {
            "err": "Error"
            , "warning": "Warning!"
            , "success": "Success"
            , "prompt": "Please Choose"
        };

 
        // Creates and shows the modal dialog
        this.showDialog= function  (msg, options) {
				
            // Merge default title (per type), default settings, and user defined settings
            var settings = $.extend( this.defaults, options);
            settings.type=settings.type.toLowerCase();
             
            //if an invalid dialog type, default to error
            if(!$.inArray(settings.type, this.DialogTypes) || settings.type=='error') {
                settings.type='err';
            }
            //if no title is entered, default to selected dialog type
            if(settings.title=='' || typeof settings.title == 'undefined'){
                settings.title=this.DialogTitles[settings.type];
            }
            // If there's no timeout, make sure the close button is show (or the dialog can't close)
            settings.timeout = (typeof(settings.timeout) == "undefined") ? 0 : settings.timeout;
            settings.showClose = ((typeof(settings.showClose) == "undefined") | !settings.timeout) ? true : !!settings.showClose;
 
            // Check if the dialog elements exist and create them if not
            if (!document.getElementById('md')) {
				$('head').append('<link type="text/css" rel="stylesheet" href="'+settings.cssDir+'/jquery.md.css" />');
				if(this.ie)
					$('head').append('<link type="text/css" rel="stylesheet" href="'+settings.cssDir+'/jquery.md-ie.css" />');
                $('body').prepend(
                    "<div id='md-mask'>&nbsp;</div>"+
                    "<div id='md'>"+
                    "<div id='md-header'>" +
                        "<div id='md-title'></div>" +
                        "<div id='md-close'></div>" +
                    "</div>" +
                    "<div id='md-content'>" +
                        "<div id='md-content-inner' />" +
                        "<div id='md-button-container'>" +
                            "<input type='button' class='md-button' value='Close'>" +
                        "</div>" +
                    "</div>"+
                    "</div>"
                    );
                                 
                $('#md').hide();
                $('#md-mask').hide();
                 
 
                // Set the click event for the "x" and modal            
                $("#md-close").click($.md.hide);
                //$(".md-button").click($.md.hide);
                $('#md-mask').click($.md.hide);
            }
 
            var dl = $('#md');
             
            //set title and content
            $('#md-title').html(settings.title);
            $('#md-content-inner').html(msg);
 
            // Center the dialog in the window but make sure it's at least 25 pixels from the top
            // Without that check, dialogs that are taller than the visible window risk
            // having the close buttons off-screen, rendering the dialog unclosable 
            dl.css('width', settings.width);
            var dialogTop = Math.abs($(window).height() - dl.height()) / 2;
			if(this.ie){
				dl.css('position','absolute');
			}
			dl.css('left', ($(window).width() - dl.width()) / 2);
			dl.css('top', (dialogTop >= 25) ? dialogTop : 25);
			 
             
            // erase the old buttons            
            $('#md-button-container').html('');
            var c = 0;
            var btn = settings.buttons;
             
            // add the new buttons
            for(x in btn){
                $('#md-button-container').append("<input type='button' class='md-button' id='md-button-"+x+"' value='"+x+"'>");
                $('#md-button-'+x).click(btn[x]);
            }
             
            //switch close and buttons on and off
            if (!settings.showClose) {
                $('#md-close').hide();
                $('#md-button-container').hide();
            } else {
                $('#md-close').show();
                $('#md-button-container').show();
            }
             
            // remove the dialog type classes and replace them with the new type
            for(i=0;i<this.DialogTypes.length;i++){
                $('#md-header').removeClass(this.DialogTypes[i]+ 'header');
                $('#md-content').removeClass(this.DialogTypes[i]);
                $('.md-button').removeClass(this.DialogTypes[i]+ 'button');
            }
            $('#md-header').addClass(settings.type + "header");
            $('#md-content').addClass(settings.type);
            $('.md-button').addClass(settings.type + "button");
 
            // set timeout
            if (settings.timeout) {
                window.setTimeout("$('#md').fadeOut('slow', 0); $('#md-mask').fadeOut('normal', 0);", (settings.timeout * 1000));
            }
             
            // get the mask height. Use document height unless it's smaller than the window
            if($(document).height() > $(window).height())
                $('#md-mask').height($(document).height());
            else
                $('#md-mask').height($(window).height());
                 
            // fade the dialog in
            dl.fadeIn("slow");
            $('#md-mask').fadeIn("normal");
             
            return true;
        };
         
        // display the whole thing
        this.showDialog(msg,options);
   
	};	
     
    //hide the dialog window
    $.md.hide=function () {
            $('#md').fadeOut("slow", function () { $(this).hide(0); });
            $('#md-mask').fadeOut("normal", function () { $(this).hide(0); });
            return true;
    };
})( jQuery );
