/*
 * jQuery Growl plugin
 * Version 1.0.0-b3 (09/04/2008)
 * @requires jQuery v1.2.3 or later
 *
 * Examples at: http://fragmentedcode.com/jquery-growl
 * Copyright (c) 2008 David Higgins
 * 
 * Special thanks to Daniel Mota for inspiration:
 * http://icebeat.bitacoras.com/mootools/growl/
 */
(function($) {

$.growl = function(title,message,image,priority) { notify(title,message,image,priority); }
$.growl.version = "1.0.0-b2";

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(title,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, /%title%/, (title?title:''));
	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);
	if ($.growl.settings.displayTimeout > 0) {
		setTimeout(function(){
			jQuery.growl.settings.noticeRemove(notice, function(){
				notice.remove();
			});
		}, jQuery.growl.settings.displayTimeout);
	}
};

  
// default settings

theposition = 'fixed';
thetop = '10px';
if ( ($.browser.msie) && ($.browser.version<7) ) {
	theposition = 'absolute';
	thetop = '90px';
}

$.growl.settings = {
	dockTemplate: '<div></div>',
	dockCss: {
		position: theposition,
		top: thetop,
		right: '10px',
		width: '250px',
		backgroundColor: 'transparent',
		zIndex: '100000001'
	},
	noticeTemplate: 
	'<div style="margin-top: 15px; float:right;">' +
	'<div class="t" style="background-color:#FFFFFF;">' +
	'	<div class="b">' +
	'		<div class="l">' +
	'			<div class="r">' +
	'				<div class="bl">' +
	'					<div class="br">' +
	'						<div class="tl">' +
	'							<div class="tr">' +
		'<div style="padding: 4px; width:240px;">' + 
		' <h2>%title%</h2>' +
		' <p style="margin-left:10px; margin-bottom:10px; margin-right:10px;">%message%</p>' +
		'</div>' + 
	'							</div>' +
	'						</div>' +
	'					</div>' +
	'				</div>' +
	'			</div>' +
	'		</div>' +
	'	</div>' +
	'</div>' +
	'</div>',
	noticeCss: {
	},
	noticeDisplay: function(notice) {
		notice.css({'opacity':'0'}).fadeIn(jQuery.growl.settings.noticeFadeTimeout);
	},
	noticeRemove: function(notice, callback) {
		notice.animate({opacity: '0', height: '0px'}, {duration:jQuery.growl.settings.noticeFadeTimeout, complete: callback});
	},
	noticeFadeTimeout: 'slow',
	displayTimeout: 4500,
	defaultImage: 'sym_quest.png',
	defaultStylesheet: null,
	noticeElement: function(el) {
		$.growl.settings.noticeTemplate = $(el);
	}
};
})(jQuery);
