/*
 * jQuery Growl plugin
 * Version 1.0.2 (8/1/2009)
 * @requires jQuery v1.3.2 or later
 *
 * Examples at: http://fragmentedcode.com/jquery-growl
 * Copyright (c) 2008-2009 David Higgins
 * 
 * Special thanks to Daniel Mota for inspiration:
 * http://icebeat.bitacoras.com/mootools/growl/
 */


(function ($) {

    $.growl = function(message, image, priority) { notify( message, image, priority); };
    $.growl.version = "1.0.2";

    function create(rebuild) {
        var instance = document.getElementById('growlDock');
        if (!instance || rebuild) {
            instance = $(jQuery.growl.settings.dockTemplate).attr('id', 'growlDock').addClass('growl');
            if (jQuery.growl.settings.defaultStylesheet) {
                $('head').append('<link rel="stylesheet" type="text/css" href="' + jQuery.growl.settings.defaultStylesheet + '" />');
            }

        } else {
            instance = $(instance);
        }
        $('body').append(instance.css(jQuery.growl.settings.dockCss));
        return instance;
    };

    function r(text, expr, val) {
        while (expr.test(text)) {
            text = text.replace(expr, val);
        }
        return text;
    };

    function notify(message, image, priority) {
        var instance = create();
        var html = jQuery.growl.settings.noticeTemplate;
        if (typeof (html) == 'object') html = $(html).html();
        html = r(html, /%message%/, (message ? message : ''));
        html = r(html, /%image%/, (image ? image : jQuery.growl.settings.defaultImage));
        html = r(html, /%priority%/, (priority ? priority : 'normal'));

        var notice = $(html)
		.hide()
		.css(jQuery.growl.settings.noticeCss)
		.fadeIn(jQuery.growl.settings.notice); ;

        $.growl.settings.noticeDisplay(notice);
        instance.append(notice);
        $('a[rel="close"]', notice).click(function () {
            notice.remove();
        });
        if ($.growl.settings.displayTimeout > 0) {
            setTimeout(function () {
                jQuery.growl.settings.noticeRemove(notice, function () {
                    notice.remove();
                });
            }, jQuery.growl.settings.displayTimeout);
        }
    };


    // default settings
    $.growl.settings = {
        dockTemplate: '<div></div>',
        dockCss: {
            position: 'fixed',
            bottom: '10px',
            right: '10px',
            width: '225px',
            zIndex: 50000
        },
        noticeTemplate:
		'<div class="notice">' +
		' <p>%message%</p>' +
		'</div>',
        noticeCss: {
        },
        noticeDisplay: function (notice) {
            notice.css({ 'opacity': '0' }).fadeIn(jQuery.growl.settings.noticeFadeTimeout);
            window.clearInterval(window.intval);
        },
        noticeRemove: function (notice, callback) {
            notice.fadeOut(jQuery.growl.settings.noticeFadeTimeout);
        },
        noticeFadeTimeout: 'slow',
        displayTimeout: 15000,
        defaultImage: 'growl.jpg',
        defaultStylesheet: null,
        noticeElement: function (el) {
            $.growl.settings.noticeTemplate = $(el);
        }
    };
})(jQuery);
