/* jQuery ui.toaster.js - 0.1rc1
 *
 * (c) Maxime Haineault <haineault@gmail.com>
 * http://haineault.com 
 * 
 * MIT License (http://www.opensource.org/licenses/mit-license.php)
 *
 * Inspired by experimental ui.toaster.js by Miksago (miksago.wordpress.com)
 * Thanks a lot.
 *
 * */
 var toaster;
 
jQuery.widget('ui.toaster', {
	init: function(){
		toaster	= this;
		
		var wrapper = '#ui-toaster-'+ toaster.options.position;

		if (!jQuery(wrapper).get(0)) {
			jQuery('<div />').attr('id', 'ui-toaster-'+ toaster.options.position).appendTo('body');
		}

		toaster.toaster = jQuery('<div id="ui-toaster-div" style="display:none;" class="ui-toaster" />')
			.append(jQuery('<span class="ui-toaster-border-tr" /><span class="ui-toaster-border-tl" /><span class="ui-toaster-border-tc" />'))
			.append(jQuery('<span id="ui-toaster-body" class="ui-toaster-body" />').html(jQuery('<div id="window_body" />').append($(toaster.element).html())))
			.append(jQuery('<span class="ui-toaster-border-br" /><span class="ui-toaster-border-bl" /><span class="ui-toaster-border-bc" />'))
			.width(toaster.options.width)
            .hover(function(){ toaster.pause.apply(toaster)}, function(){ toaster.resume.apply(toaster)})
			[(toaster.options.position.match(/bl|br/)) ? 'prependTo': 'appendTo'](wrapper);

		// Closable
		if (toaster.options.closable) {
			toaster.toaster.addClass('ui-toaster-closable');
			if ($(toaster.toaster).find('.ui-toaster-close').length > 0) {
				jQuery('.ui-toaster-close', jQuery(toaster.toaster)).click(function(){ toaster.hide.apply(toaster); });
			}
			else {
				jQuery(toaster.toaster).click(function(){ toaster.hide.apply(toaster); });
			}
		}

		// Sticky
		if (toaster.options.sticky) {
			jQuery(toaster.toaster).addClass('ui-toaster-sticky');
		}
		else {
			toaster.resume();
		}
		
		// Delay
		if (!!toaster.options.delay) {
		   setTimeout(function(){
				toaster.open.apply(toaster);
			}, toaster.options.delay * 1000);
		}
		else {
			toaster.open.apply(toaster);
		}
	},

	open: function() {
		this.options.show.apply(this.toaster);
	},

	hide: function(){
		if (this.options.onHide) this.options.onHide.apply(this.toaster);
		this.close(this.options.hide);
	},

	close: function(effect) {
		var toaster   = this;
		var effect = effect || toaster.options.close;
		if (toaster.options.onClose) {
			effect.apply(toaster.toaster);
		}
		effect.apply(toaster.toaster, [toaster.options.speed, function(){
			if (toaster.options.onClosed) toaster.options.onClosed.apply(toaster.toaster);
			jQuery(toaster.toaster).remove();
		}]);
	},

	resume: function() {
		var toaster = this;
		toaster.timer = setTimeout(function(){
			toaster.close.apply(toaster);
		}, toaster.options.timeout * 1000 + toaster.options.delay * 1000);
	},

	pause: function() { clearTimeout(this.timer); }
});

$.ui.toaster.defaults = {
	delay:    0,      // delay before showing (seconds)
	timeout:  20,      // time before hiding (seconds)
	width:    280,    // toast width in pixel
	position: 'tr',   // tl, tr, bl, br
	speed:    'slow', // animations speed
	closable: false,   // allow user to close it
	sticky:   false,  // show until user close it
	onClose:  false,  // callback before closing
	onClosed: false,  // callback after closing
	onOpen:   false,  // callback before opening
	onOpened: false,  // callback after opening
	onHide:   false,  // callback when closed by user
	show:	  $.fn.slideDown, // showing effect
	hide:	  $.fn.fadeOut,   // closing effect (by user)
	close:    $.fn.slideUp    // hiding effect (timeout)
};













 



