﻿(function($) {

    $.fn.fadeMessage = function(msg, options) {
        //alert(msg.length);
        var imagepath = "/assets/images/admin/";
        var opts = $.extend({}, $.fn.fadeMessage.defaults, options);
        var msgHtml = "<div class='fade_message'></div><div id='fade_message' title='click to close'><img id='fade_close' src='" + imagepath + "closebutton.png' alt='close' /><div id='fade_message_text'></div></div>";
        var o = $.meta ? $.extend({}, opts, $(this).data()) : opts;

        $(this).prepend(msgHtml);

        $msg = $(this).find("#fade_message");
        $container = $(this).find(".fade_message");


        if (o.bg != 'true')
            $container.hide();

        //maps to ModalPopupControl.MessageType 
        switch (o.type) {
            case '0':
                $msg.css("background-image", "url(" + imagepath + "info.gif)");
                break;
            case '1':
                $msg.css("background-image", "url(" + imagepath + "question.gif)");
                break;
            case '2':
                $msg.css("background-image", "url(" + imagepath + "warning.gif)");
                break;
            case '3':
                $msg.css("background-image", "url(" + imagepath + "error.gif)");
                break;
            default:
                $msg.css("background-image", "url(" + imagepath + "info.gif)");
        }

        $msg.css("background-repeat", "no-repeat");

        if (o.backgroundcolor.length > 0)
            $msg.css("background-color", o.backgroundcolor);

        if (o.bordercolor.length > 0)
            $msg.css("border-color", o.bordercolor);

        if (msg.length > 300)
            $msg.width("600px");
            
        $msg.html(msg);
        $msg.show();
        $msg.center();

        if (o.allowClose == 'true') {
            $msg.click(function() {
                $msg.hide();
                $container.hide();
                if (o.redirect && o.redirect.length > 0)
                    document.location = o.redirect;
            });
        }

        if (o.fadeOut != 'false') {
            $container.hide();
            $msg.animate({ opacity: 1.0 }, o.timeout);
            $msg.fadeOut(o.speed);
        }
        else {
            //$.scrollTo(0);    // requires jquery.scrollTo-1.4.2-min.js
            $container.height($(document).height());
        }
    }

    $.fn.fadeMessage.defaults = {
        type: '0', //information icon 
        fadeOut: 'false',
        bg: 'false', //opaque bg 1 = show
        redirect: '',
        timeout: 2000,
        speed: 1000,
        backgroundcolor: '',
        bordercolor: '',
        allowClose: 'true'
    };

})(jQuery);

function displayFadeMessage(msg, type, fadeOut, modalBg, redirect, bck, brdr, allowClose) {

    $(document).ready(function() {

        var options = {
            type: type,
            fadeOut: fadeOut,
            bg: modalBg,
            redirect: redirect,
            backgroundcolor: bck,
            bordercolor: brdr,
            allowClose: allowClose
        }
        //alert(msg);
        try
        { $("body").fadeMessage(msg, options); }
        catch (e)
        { alert(msg); }



    });

}




