var $qube = jQuery.noConflict();

$qube(document).ready(function(){
	if(((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/Android/))||(navigator.userAgent.match(/webOS/))) && ($qube('#mobileMenuTabWrapper').is(':visible'))){
		iPhoneMenu();
		mobileImageResize();
	} else {
		mainmenu();
		iPhoneDesktopAdjustments();
	}
	if( navigator.userAgent.match(/iPad/i) ) {
		iPadAdjustments();
	}
	
	extraContent();
	pageTopHeight();
	fixedFooter();
});

function mobileViewport(){
	if(((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/Android/))||(navigator.userAgent.match(/webOS/))) && ($qube('#mobileMenuTabWrapper').is(':visible'))){
		$qube('head').append('<meta name = "viewport" content = "width=device-width; initial-scale=1.0; maximum-scale=1.0;" />');
	}
}
$qube(function(){ mobileViewport(); });

/* ExtraContent r1.3 02-23-09 12:33 */

function extraContent(){
	var i=0;
	while (i<=10) {
		$qube('#myExtraContent'+i+' script').remove();
		$qube('#myExtraContent'+i).appendTo('#extraContainer'+i);
		i++;
	}
}


function mainmenu(){

	var menuConfig = {
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 50, // number = milliseconds for onMouseOver polling interval    
		over: revealChildren, // function = onMouseOver callback (REQUIRED)    
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: hideChildren
		};
	
	function revealChildren(){
		$qube(this).find("ul:first").css({visibility: "visible",display: "none"}).slideDown(300);
	}
	
	function hideChildren(){
		$qube(this).find("ul:first").fadeOut(150);
	}
	
	
	$qube("#menu ul ul").parent().addClass("ddarrow");
	$qube("#menu ul ul").parent().append("<span></span>");
	$qube("#menu ul ul").css({display: "none"}); // Opera Fix
	$qube("#menu li").hoverIntent(menuConfig);

}

function pageTopHeight(){
	if( $qube.browser.msie && (parseInt($qube.browser.version) == '9' )){
		if (($qube("#pageTopWrapper").css("position") == "fixed")){		
			$qube("#container").css("padding-top", '49px');
		}
	} else {
		var ptHeight = $qube("#pageTopWrapper").height();
		if (($qube("#pageTopWrapper").css("position") == "fixed")){
			$qube("#container").css("padding-top", ptHeight);
		}
	}
}

function fixedFooter(){
	var footerHeight = $qube("#footerWrapper").height();
	if (($qube("#footerWrapper").css("position") == "fixed")){
		$qube("#footerSpacer").css("height", footerHeight);
	}
}

function iPhoneMenu(){
	$qube('#mobileMenuTab').click(function () {
	$qube('#menuWrapper').slideToggle('medium');
	});
	var subMenuBg = $qube('#menu ul ul').css('background-color');
	var subMenuText = $qube('#menu ul ul a').css('color');
	$qube('#menu ul').css({backgroundColor: subMenuBg});
	$qube('#menu ul a').css({color: subMenuText});
}

function mobileImageResize() {
	$qube(window).load(function() {
		var contentWidth = $qube('#contentWrapper #content').width();
		$qube('.imageStyle').each(function(){
			if($qube(this).width() > contentWidth){
				$qube(this).width(contentWidth);
			}
		});
		
		if($qube('#logo img').width() > contentWidth){
			$qube('#logo img').width(contentWidth);
		}
	});
}

function iPadAdjustments() {
	if ( $qube('#mainWrapper').width() > '700' ) {
		$qube('#mainWrapper, #breadcrumbWrapper, #extraContainer1, #extraContainer7').css({'width': '92%'});
		$qube('#footerWrapper').css({'position': 'relative', 'bottom': 'auto'});
		$qube('#content').css({'overflow': 'hidden'});
	}
	var contentWidth = $qube('#contentWrapper #content').width();
	$qube('#content .imageStyle').each(function(){
		if(($qube(this).width()) > contentWidth){
			$qube(this).width(contentWidth);
		}
	});
	var sidebarWidth = $qube('#sidebarWrapper .sidebar').width();
	$qube('#sidebarWrapper .sidebar .imageStyle').each(function(){
		if(($qube(this).width()) > sidebarWidth){
			$qube(this).width(sidebarWidth);
		}
	});
}

function iPhoneDesktopAdjustments() { 
	if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/Android/))||(navigator.userAgent.match(/webOS/))){ iPadAdjustments(); }
}

(function($){
	/* hoverIntent by Brian Cherne */
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
	
})(jQuery);
