/*
 * Superfish v1.4.1 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

(function($j){
	$j.superfish = {};
	$j.superfish.o = [];
	$j.superfish.op = {};
	$j.superfish.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		delay		: 300,
		animation	: {opacity:'show'},
		speed		: 'normal',
		oldJquery	: false, /* set to true if using jQuery version below 1.2 */
		disableHI	: false, /* set to true to disable hoverIntent usage */
		// callback functions:
		onInit		: function(){},
		onBeforeShow: function(){},
		onShow		: function(){}, /* note this name changed ('onshow' to 'onShow') from version 1.4 onward */
		onHide		: function(){}
	};
	$j.fn.superfish = function(op){	
		var bcClass = 'sfbreadcrumb',
			over = function(){
				var $j$j = $j(this), menu = getMenu($j$j);
				getOpts(menu,true);
				clearTimeout(menu.sfTimer);
				$j$j.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $j$j = $j(this), menu = getMenu($j$j);
				var o = getOpts(menu,true);
				
				clearTimeout(menu.sfTimer);
				if ( !$j$j.is('.'+bcClass) ) {
					menu.sfTimer=setTimeout(function(){
						$j$j.hideSuperfishUl();
						if (o.$jpath.length){over.call(o.$jpath);}
					},o.delay);
				}		
			},
			getMenu = function($jel){ return $jel.parents('ul.superfish:first')[0]; },
			getOpts = function(el,menuFound){ el = menuFound ? el : getMenu(el); return $j.superfish.op = $j.superfish.o[el.serial]; },
			hasUl = function(){ return $j.superfish.op.oldJquery ? 'li[ul]' : 'li:has(ul)'; };

		return this.each(function() {
			var s = this.serial = $j.superfish.o.length;
			var o = $j.extend({},$j.superfish.defaults,op);
			
			o.$jpath = $j('li.'+o.pathClass,this).each(function(){
				$j(this).addClass(o.hoverClass+' '+bcClass)
					.filter(hasUl()).removeClass(o.pathClass);
			});
			$j.superfish.o[s] = $j.superfish.op = o;
			
			$j(hasUl(),this)[($j.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out)
			.not('.'+bcClass)
				.hideSuperfishUl();
			
			var $ja = $j('a',this);
			$ja.each(function(i){
				var $jli = $ja.eq(i).parents('li');
				$ja.eq(i).focus(function(){over.call($jli);}).blur(function(){out.call($jli);});
			});
			
			o.onInit.call(this);
			
		}).addClass('superfish');
	};
	
	$j.fn.extend({
		hideSuperfishUl : function(){
			var o = $j.superfish.op,
				$jul = $j('li.'+o.hoverClass,this).add(this).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($jul);
			return this;
		},
		showSuperfishUl : function(){
			var o = $j.superfish.op,
				$jul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			o.onBeforeShow.call($jul);
			$jul.show(); //XXX
			return this;
		}
	});
	
	$j(window).unload(function(){
		$j('ul.superfish').each(function(){
			$j('li',this).unbind('mouseover','mouseout','mouseenter','mouseleave');
		});
	});
})(jQuery);
