 jQuery(document).ready(function() {
	
	// If no active items then we set the first item as active
	if($('#mainnav > li.active').length < 1) {
		$('#mainnav > li:first').addClass('active');
	} 
	
	// Removes the search text on click
	$('#searchbox').click(function(){
		var doneOnce = false;
		return function(){
			if(!doneOnce){
				this.value = '';
				$(this).css('color', 'black');
				doneOnce = true;
			}
		}
	}());
	
	/*
		This is really just for IE6 and maybe IE7,
		but it causes no Ill effects on any browser
		but Safari. Basically it takes the subnav's
		active LI and tries to absolutely position
		a SPAN to the bottom center of the LI.
		
		We used it for the Active Down Arrow.
	*/
	if(!jQuery.browser.safari) {
		var active = $('#mainnav li ul li.active');
		var span = active.children('span');
		var aWidth = active.outerWidth();
		var sWidth = span.outerWidth();
		span.css('left', (aWidth-sWidth)/2);
	}

	var firstActiveItem = $('#mainnav > li.active');
	firstActiveItem.next('li').addClass('rightOfActiveItem');
	// We want the LI previous to the LI.Active to also know whats going on.
	firstActiveItem.prev('li').addClass('leftOfActiveItem');
	firstActiveItem.find('ul > li.active').next('li').addClass('rightOfActiveItem');
	// This is just incase who ever is creating our menu forgets to add the last class
	// to the last LI in both the mainmenu and the submenu.
	$('#mainnav li:last-child').addClass('last');
	// Fix for social icons technically being last
	$('#mainnav li.social-icons').prev().addClass('last');
	var mainMenuItems = $('#mainnav > li');
	var mainMenuItemsA = mainMenuItems.children('a');
	
	mainMenuItemsA.hover(
		function(){
			var parentLI = $(this).parent('li');
			// We remove the active and other classes when hoverering over the A
			// link mostly since our LI encompasses the whole menu bar and can be
			// triggered accidently. The A however only coveres the link itself.
			firstActiveItem.removeClass('active');
			firstActiveItem.next('li').removeClass('rightOfActiveItem');
			firstActiveItem.prev('li').removeClass('leftOfActiveItem');
			parentLI.addClass('active');
			parentLI.next('li').addClass('rightOfActiveItem');
			parentLI.prev('li').addClass('leftOfActiveItem');
		},function(){}
	);
	mainMenuItems.hover(
		function(){},
		function(){
			/*
				Instead of having this code be handled by hovering out of the A
				tag we put it so it triggers when you leave the total area of the LI
				tag. This works nicely as you can stay within the confines of our menu
				and it won't flip back to the orginal active item until you leave the menu
				compleletly.
			*/
			$(this).removeClass('active');
			$(this).next('li').removeClass('rightOfActiveItem');
			$(this).prev('li').removeClass('leftOfActiveItem');
			firstActiveItem.addClass('active');
			firstActiveItem.next('li').addClass('rightOfActiveItem');
			firstActiveItem.prev('li').addClass('leftOfActiveItem');
		}
	);
	/*
		Adds shadows to all containers in our content div.
		NOTE: this gets applied and reshadowed in other areas
		of the code. This gives us a good starting point. Containers
		that haven't had their content loaded yet (AJAX), end up 
		getting the wrong size shadow and need it reapplied after load.
	*/
	$('#content div.container').dropShadow({
		blur: 2,
		left: 2,
		top: 2
	});
	
	
	
	$('#sidebar ul li a, #sidebar h3 a').click(function(e){
		// Preform AJAX Call for DOM load
		var url = e.target.href;
		/*
			This is a hardcoded fix for our online customer
			support system where we need to always link to the
			static page. When support.cfm is clicked and this script
			executes instead of doing an ajax load it allows the page
			to keep on loading.
		*/
		
		if(!$(e.target).hasClass('reload')) {
		//if(url.indexOf("support.cfm") == -1 && url.indexOf("PressKit.zip") == -1){
			e.preventDefault();
			
			
			var params = url.indexOf("?") == -1 ? "?" : "&";
			params += 'ct=1&r='+Math.random()*99999;
			
			var myurl = url  + params;
					
			$('#content').empty().load(myurl);
			
			
			try {
			// Google Analytics 
			pageTracker._trackPageview(myurl);
			} catch(err) {}
			
			$('#title').html($(e.target).attr('title'));
			var target = $(e.target);
			target.parent().parent().parent().find('li.selected').removeClass('selected');
			target.parent().addClass('selected');
		} else {
			window.location.href = url;
		}
	});
	
	/*
		To much text for the poor menu to handle.
		We are going to try to half the line-height.
		That is the best we can do. Plus we already
		have overflow: hidden setup.
	*/
	var fixSidebarLinks = function(){
		var ele = $(this);
		if(ele.height() > ele.parent().height()) {
			ele.css('line-height', (parseInt(ele.css('line-height'))/2)+'px');
			ele.parent().css('overflow', 'hidden');
		}
	};
	$('#sidebar h3 a, #sidebar ul li a').each(fixSidebarLinks);
	
	$('#sidebar').accordion({
		autoheight: false,
		header: 'h3',
		event: 'click',
		active: '.selected'
	});

	/*
		When a combo item is selected the user should
		be jumped from the current page to the one they
		selected.
	*/
	$('#searchcombo').change(function(e){
		var selection = e.target;
		var index = selection.selectedIndex;
		var href = $(selection.options[index]).attr('href');
		if(href != "") window.location.href = href;
	});
	
	
});	// end onReader



