// JavaScript Document
/*
 * This is a simple jQuery notification bar inspired by the stackoverflow.com notification bar and the Hello Bar.
 * 
 * Version 0.1.4
 * March 18, 2011
 *
 * Copyright (c) 2010 Leo Silva (http://about.me/leosilva)
 * Dual licensed under the MIT and GPL licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-license.php
 * 
 * USAGE AND OPTONS:
 *
 *	$('#myDiv').howdyDo({
 *		effect		: 'fade',
 *		duration	: 250,
 *		delay		: 2000,
 *		hideAfter	: 10000,
 *		action		: 'hover',
 *		autoStart	: true,
 *		barClass	: 'myclass',
 *		easing		: 'easeOutBounce',
 *		openAnchor	: 'show',
 *		closeAnchor	: 'hide',
 *		callback	: function(){}
 *	});
 *
 * TESTED WITH:
 *	FF 3.6, Opera 11, IE 9, Chrome 8, Safari 5.0.3
 *	jQuery v.1.4.4
 *	jQueryUI v.1.8.7
 */
( function($) {
	$.fn.howdyDo = function( options ){
		var defaults = {
			effect		: 'slide',
			duration	: 500,
			delay		: 500,
			hideAfter	: 0,
			action		: 'hover',
			autoStart	: true,
			barClass	: 'howdydo-style',
			easing		: 'easeOutBounce',
			openAnchor	: 'show',
			closeAnchor	: 'hide',
			callback	: function(){}
		};
		var options = $.extend( defaults, options );
		
		var obj = $(this);
		obj.detach().prependTo( 'body' ).wrap( '<div id="howdydo-wrapper">' );

		var openBar = '<div id="howdydo-open" class="' + options.barClass + '"><a style="margin:0; padding:0;" href="javascript:;">' + options.openAnchor + '</a><div class="label">Meet the Family</div></div>'; // creates opening anchor
		var closeBar = '<div id="howdydo-close"><a href="javascript:;">' + options.closeAnchor + '</a></div><div style="clear:both;"></div>' // creates closing anchor  

		obj.addClass( options.barClass + ' howdydo-box' ).html( obj.html() + closeBar ).after( openBar );

		var objWrapper = $( '#howdydo-wrapper' );
		var objOpen = $( '#howdydo-open' );
		var objClose = $( '#howdydo-close' );
		
		objWrapper.after("<div style='clear:both;'></div>");

		switch( options.action ){ // action class options
			case 'scroll'	: objWrapper.addClass( 'scroll' ); break;
			case 'push'		: objWrapper.addClass( 'push' ); break;
			default			: objWrapper.addClass( 'hover' );
		}

		switch( options.effect ){ // effect options, per effect type
			case 'blind': effectOptions = { direction: 'vertical', easing: options.easing }; break;
			case 'drop'	: effectOptions = { direction: 'up', easing: options.easing }; break;
			case 'fade'	: effectOptions = {}; break;
			default		: options.effect = 'slide'; effectOptions = { direction: 'up', easing: options.easing };
		}

		objClose.click( function(){ howdydoHide(); } ); // hide/close on click
		objOpen.click( function(){ howdydoShow(); } ); // show/open on click

		$( document ).keyup( function( e ) { // close on Esc
		 	if( e.keyCode == 27 && obj.is( ':visible' ) ){ howdydoHide(); }
		});
		
		if( options.autoStart === true ){ howdydoShow( options.delay ); } // autoStart

		function howdydoShow( delay ){
			if( !delay || delay < 0 ) { delay = 0; }
			setTimeout( function(){
				if( options.action == 'push' ) {
					objOpen.toggle( options.effect, effectOptions, options.duration, function() {
						objWrapper.animate( { height: obj.outerHeight() }, 100,  function() {
							obj.toggle( options.effect, effectOptions, options.duration, options.callback );
						});
					});
				} else {
					obj.toggle( options.effect, effectOptions, options.duration, options.callback );
					objOpen.toggle( options.effect, effectOptions, options.duration );
				}
			}, delay );
			if( options.hideAfter > 0 && options.autoStart == true ) { barAnim = setTimeout( function(){ howdydoHide(); }, ( options.hideAfter + options.duration ) ); options.hideAfter = 0; }
		}

		function howdydoHide(){
			if( typeof barAnim != 'undefined' ) { clearTimeout( barAnim ); }
			if( options.action == 'push' ) {
				obj.toggle( options.effect, effectOptions, options.duration, function() {
					objWrapper.animate( { height: 0 }, 100, function() {
						objOpen.toggle( options.effect, effectOptions, options.duration, options.callback );
					});
				});
			} else {
				obj.toggle( options.effect, effectOptions, options.duration, options.callback );
				objOpen.toggle( options.effect, effectOptions, options.duration );
			}
		}
	}
})( jQuery );
