/*
 * Superfish v1.3 - jQuery menu widget
 *
 * Copyright (c) 2007 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
 *
 * YOU MAY DELETE THIS CHANGELOG:
 * v1.2.1 altered: 2nd July 07. added hide() before animate to make work for jQuery 1.1.3. See comment in 'over' function.
 * v1.2.2 altered: 2nd August 07. changed over function .find('ul') to .find('>ul') for smoother animations
 * 				   Also deleted the iframe removal lines - not necessary it turns out
 * v1.2.3 altered: jquery 1.1.3.1 broke keyboard access - had to change quite a few things and set display:none on the
 *				   .superfish rule in CSS instead of top:-999em
 * v1.3	: 		   Pretty much a complete overhaul to make all original features work in 1.1.3.1 and above.
 *				   .superfish rule reverted back to top:-999em (which is better)
 */

(function(J){
	J.fn.superfish = function(o){
		var Jsf = this,
			defaults = {
			hoverClass	: 'sfHover',
			pathClass	: 'overideThisToUse',
			delay		: 400,
			animation	: {opacity:'show'},
			speed		: 'normal'
		},
			over = function(){
				clearTimeout(this.sfTimer);
				clearTimeout(Jsf[0].sfTimer);
				J(this)
				.showSuperfishUl()
				.siblings()
				.hideSuperfishUl();
			},
			out = function(){
				var JJ = J(this);
				if ( !JJ.is('.'+o.bcClass) ) {
					this.sfTimer=setTimeout(function(){
						JJ.hideSuperfishUl();
						if (!J('.'+o.hoverClass,Jsf).length) { 
							over.call(Jcurrents.hideSuperfishUl());
						}
					},o.delay);
				}		
			};
		J.fn.extend({
			hideSuperfishUl : function(){
				return this
					.removeClass(o.hoverClass)
					.find('ul:visible')
						.hide()
					.end();
			},
			showSuperfishUl : function(){
				return this
					.addClass(o.hoverClass)
					.find('>ul:hidden')
						.animate(o.animation,o.speed,function(){
							J(this).removeAttr('style');
						})
					.end();
			},
			applySuperfishHovers : function(){
				return this[(J.fn.hoverIntent) ? 'hoverIntent' : 'hover'](over,out);
			}
		});
		o = J.extend({bcClass:'sfbreadcrumb'},defaults,o || {});
		var Jcurrents = J('.'+o.pathClass,this).filter('li[ul]');
		if (Jcurrents.length) {
			Jcurrents.each(function(){
				J(this).removeClass(o.pathClass).addClass(o.hoverClass+' '+o.bcClass);
			});
		}
		var JsfHovAr=J('li[ul]',this).applySuperfishHovers(over,out)
			.find('a').each(function(){
				var Ja = J(this), Jli = Ja.parents('li');
				Ja.focus(function(){
					over.call(Jli);
					return false;
				}).blur(function(){
					Jli.removeClass(o.hoverClass);
				});
			})
			.end()
			.not('.'+o.bcClass)
				.hideSuperfishUl()
			.end();
		J(window).unload(function(){
			JsfHovAr.unbind('mouseover').unbind('mouseout');
		});
		return this.addClass('superfish').blur(function(){
			out.call(this);
		});
	};
})(jQuery);