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

			var s = this.serial = $.superfish.o.length;
			var o = $.extend({},$.superfish.defaults,op);
			
			if (o.autoWidth){
				//BR: start autoextend...
				var $menu = $(this);

				var $li = $("> li", $menu);
				var $lastLi = $($li[$li.length-1]);
				$lastLi.addClass("last");
				var liWidth = 0;
				$li.each(function(i){ liWidth += $(this).width(); });
				var extraWidth = parseInt(($menu.width() - liWidth) / $li.length);
				liWidth = 0;

				if(typeof($.fn.bgIframe) == "function") {$("ul", $li).bgiframe();}

				$li.each(function(i){
					var w = this.offsetWidth + extraWidth;
					$(this).css("width",w +'px');
					liWidth += w;
	 				var parentLi = this;
	 				parentLi.longest = w;
	 				$(this).find("ul:first li a").each(function(){
	 					if ( this.offsetWidth > parentLi.longest ) {parentLi.longest = this.offsetWidth;}
	 				})
	 				.end()
	 				.find("ul:first")
	 				.css({width: parentLi.longest+'px', top: $menu[0].offsetHeight + 'px'})
				});

				// fix the last nav item to fill in the blanks
				var lastLiWidth = $lastLi.width() + ($menu.width() - liWidth);
				var $lastUl = $lastLi.find("ul:first");
				$lastLi.css("width", lastLiWidth + 'px' );
				if (lastLiWidth > $lastUl.width()) $lastUl.css('width',lastLiWidth+'px');

				//BR: end autoextend
			}
			
			o.$path = $('li.'+o.pathClass,this).each(function(){
				$(this).addClass(o.hoverClass+' '+bcClass)
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			$.superfish.o[s] = $.superfish.op = o;
			
			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out)
			.not('.'+bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			
			o.onInit.call(this);
			

		}).addClass('superfish');
		
	};
	
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = $.superfish.op,
				$ul = $('li.'+o.hoverClass,this).add(this).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = $.superfish.op,
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			o.onBeforeShow.call($ul);

			if ($ul[0]){
				if(!$ul[0].initialized){
					//add drop shadows, etc...
					$ul.append('<span class="shadow shB"></span><span class="shadow shBL"></span><span class="shadow shBR"></span><span class="shadow shT"></span><span class="shadow shR"></span><span class="shadow shL"></span>');
					$ul[0].initialized = true;
				}
			}

			if (o.animate){
				$ul.animate(o.animation,o.speed,function(){ o.onShow.call(this); });				
			} else {
				$ul.show();
				o.onShow.call(this);
			}
			
			return this;
		}
	});
	
	$(window).unload(function(){
		$('ul.superfish').each(function(){
			$('li',this).unbind('mouseover','mouseout','mouseenter','mouseleave');
		});
	});
})(jQuery);


/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-11 23:14:51 -0500 (Wed, 11 Jul 2007) $
 * $Rev: 2323 $
 *
 * Version 2.1
 */

(function($){

/*
 * @name bgiframe
 * @type jQuery
 * @cat Plugins/bgiframe
 * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 */
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};

})(jQuery);
