﻿/*!
* CodeForce LLC
* jQuery customalert plugin
* Version 2.33 (29-MAR-2010)
* @requires jQuery v1.2.3 or later
*
* Copyright (c) 2007-2008 M. Alsup
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* This plugin will display an alert dialog with a left side image icon and message on the right. It is used in conjunction
* with the jquery-ui.js and jquery-ui.css files
* 
*   OPTIONS available for overriding
*    imgUrl: this image to show in the alert dialog
*    txtClass: the class name used to style the dialog text,
*    title: this of the header of the dialog,
*    containerId: this is the name of the div container for the alert. used mainly to do other minipulation once teh dialog is shown,
*    imageAlign: where the image will show up only options is left or right,
*    maxHeight: max hieght the dialog can be resixed to,
*    maxWidth: max width the dialog can be resized to,
*    modal: if the alert is modal: default is true,
*    zIndex: the zindex of the alert dialog
*
*
* Example Usage
<script type="text/javascript">
$(document).ready(function () {
    $.alertShow('custom error message', { zIndex: 4000, title: 'New title' } );            
});

</script>
OR 

<asp:Button ID="Button1" runat="server" CssClass="testbtn" OnClientClick=" $.alertShow('custom error message'); return false;" Text="Button" />
*
*
*/

(function ($) {

    if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
        alert('customalert requires jQuery v1.2.3 or later!  You are using v' + $.fn.jquery);
        return;
    }
    if (jQuery.ui == undefined) {
        alert('customalert requires jQuery UI v1.7.* or later!');
        return;
    }

    $.alertShow = function (msg, opts) { createAlert(window, opts, msg); };
    $.alertHide = function () { destroyAlert(); };

    var AlertObjects = new Array();
    function AlertObject(id, html) {
        this.id = id;
        this.html = html;
    }

    // plugin defaults - added as a property on our plugin function
    $.fn.defaults = {
        imgUrl: '/shared/images/global/alert.png',
        txtClass: 'dialogtext',
        title: 'Alert!',
        containerId: 'divAlert',
        imageAlign: 'left',
        maxHeight: 700,
        maxWidth: 900,
        modal: true,
        zIndex: 1000
    };

    function _htmlExists(htmlArray, id) {
        for (var i = 0; i < htmlArray.length; i++) {
            if (htmlArray[i].id == id) {
                return htmlArray[i];
            }
        }
        return null;
    }

    function _createHtml(options) {
        //create alert and append to obj
        var alertHtml = '<div id="' + options.containerId + '" title="' + options.title + '" class="jqrycustomalert" style="display:none;"><table><tr>';
        if (options.imageAlign == "left") {
            alertHtml = alertHtml + '<td valign="top"><img src="' + options.imgUrl + '" alt="alert" /></td>';
            alertHtml = alertHtml + '<td valign="top" style="padding-left: 10px;"><div id="divAlertMessage" class="' + options.txtClass + '"></div></td>';
        } else {
            alertHtml = alertHtml + '<td valign="top" style="padding-right: 10px;"><div id="divAlertMessage" class="' + options.txtClass + '"></div></td>';
            alertHtml = alertHtml + '<td valign="top"><img src="' + options.imgUrl + '" alt="alert" /></td>';
        }
        alertHtml = alertHtml + '</tr></table></div>';

        return alertHtml;
    }


    function createAlert(obj, options, msg) {
        options = $.extend({}, $.fn.defaults, options || {});
        var full = (obj == window);

        var alertHtmlObject = _htmlExists(AlertObjects, options.id);
        var html = null;
        if (alertHtmlObject == null) {
            html = _createHtml(options);
            //add to tracking collection
            AlertObjects.push(new AlertObject(options.id, html));
        } else {
            html = alertHtmlObject.html;
        }

        if (full) {
            $(html).appendTo($('body'));
        } else {
            $(html).appendTo($(obj));
        }
        if (msg == null || msg == undefined) {
            msg = "An unknown error occured.";
        }
        $("#divAlertMessage").html(msg);

        $("#" + options.containerId).dialog({
            autoOpen: true,
            modal: options.modal,
            maxHeight: options.maxHeight,
            maxWidth: options.maxWidth,
            autoResize: true,
            zIndex: options.zIndex,
            buttons: { "Ok": function () { $(this).dialog("close"); } },
            close: function (event, ui) { destroyAlert(obj, options); }

        });

    };

    function destroyAlert() {
        $(".jqrycustomalert").remove();
    };

})(jQuery);
