$j = jQuery;
	
	$j(function(){

		$j('.scroll-pane').jScrollPane({
			scrollbarWidth:14,
			scrollbarMargin:0,
			arrowSize:14,
			showArrows:true
		});
	});
	
	/**
	 * @function InputTextToggle
	 * @description Factory for toggling the apearence of a string in an input[text] such as a search form.
	 * @param s1 The id of the element to toggle.
	 * @param s2 The text that should be toggled.
	 * @return Function Assign it to events such as element.onblur, element.onfocus, window.onload.
	 */
	function InputTextToggle(s1, s2){
		var call = 0;
		return function(){
			var txt = document.getElementById(s1);
			var defaultText = s2;
			if (txt.value.length == 0 || call == 0){
				txt.value = defaultText;
			}
			else if (txt.value == defaultText){
				txt.value = '';
			}
			call++;
		}
	}
	
	/**
	 * @function setupTextToggle
	 * @description Utility function for setting up one or more input[type=text];
	 * @param ... an object with with key/value pairs for id and text.
	 */
	function setupTextToggle(){
		for (var i=0; i<arguments.length; i++){
			
			var arg = arguments[i];
			var el = document.getElementById(arg.id);
			var func = InputTextToggle(arg.id, arg.text);
			el.onblur = func;
			el.onclick = func;
			func();
				
		}
	}
	
	$j(function(){
		setupTextToggle(
			{'id': 'siteSearchTxt', 'text': 'Search'},
			{'id': 'siteNewsletterEmail', 'text': 'Email Address'}
		);
		
		$j('#siteCategoriesInner>ul>li').bind('mouseenter', function(){
			$j(this).find('.subCats').fadeIn();
		}).bind('mouseleave', function(){
			$j(this).find('.subCats').fadeOut();
		})
	});
	
	var ieTimer;
	
	function centerTabMenu(){
		var BASE_MARGIN_LEFT_RIGHT = 3;
	
		$j('#siteCategoriesInner>ul').css('paddingLeft', 0);
	
		$j('#siteCategoriesInner>ul>li').css({
			marginLeft: BASE_MARGIN_LEFT_RIGHT,
			marginRight: BASE_MARGIN_LEFT_RIGHT
		});
		
		$j('#siteCategoriesInner>ul').css('float', 'left');
		
		var cw = $j('#siteCategoriesInner').width();
		var tw = $j('#siteCategoriesInner>ul').width();
		
		$j('#siteCategoriesInner>ul').css('float', 'none');
		
		var ttw = (tw < 900)? 900 : tw;
		
		var leftMargin = Math.floor((cw - ttw) / 2);
		
		$j('#siteCategoriesInner>ul').css('paddingLeft', leftMargin);
		
		var lis = $j('#siteCategoriesInner>ul>li').size();
		
		var extraMargin = Math.floor(((ttw - tw) / lis) / 2);
		
		$j('#siteCategoriesInner>ul>li').css({
			marginLeft: BASE_MARGIN_LEFT_RIGHT + extraMargin,
			marginRight: BASE_MARGIN_LEFT_RIGHT + extraMargin
		});
		
		if ($j.browser.msie){
			if (ieTimer){
				ieTimer = setTimeout(function(){
					$j('#siteCategoriesInner>ul').hide().show();
					clearTimeout(ieTimer);
					alert('timeout');
				}, 1000);
			}
		}
	}
	
	$j(function(){
		centerTabMenu();
		$(window).bind('resize', centerTabMenu);
})(jQuery);
