/*
Called by Main-Carousel Book-Carousel and Cd-Carousel
Sets up autoscrolling and pausing while hovering.
*/
var resumeTime = 9000;
function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
	   var auto = carousel.options.auto;
		carousel.startAuto(0);
		setTimeout(function(){
			if(!carousel.auto){
				carousel.startAuto(auto);
			}
		}, resumeTime);
	});
	
	carousel.buttonPrev.bind('click', function() {
		var auto = carousel.options.auto;
		carousel.startAuto(0);
		setTimeout(function(){
			if(!carousel.auto){
				carousel.startAuto(auto);
			}
		}, resumeTime);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.auto = carousel.auto || carousel.options.auto;
		carousel.startAuto(0);
	}, function() {
		carousel.startAuto(carousel.auto);
	});
}

jQuery(document).ready(function() {
	$('#carousel').show();
	// If no active items then we set the first item as active
	if($('#mainnav > li.active').length < 1) {
		$('#mainnav > li:first').addClass('active');
		
	} 
	if($('#mainnav > li.active > ul > li.active').length < 1) {
		$('#mainnav > li.active > ul > li:first').addClass('active');
	}
	
 	$('#loginbox, #passwordbox').focus(function(){
		$(this).css('background-image', 'none');
	});
	
	$('#loginbox, #passwordbox').each(function(){
		if(this.value != "") $(this).css('background-image', 'none');
	});
	
	$('#quickLinks').change(function(e){
		var selection = e.target;
		var index = selection.selectedIndex;
		var href = $(selection.options[index]).attr('value');
		if(href != "") window.location.href = href;
	}); 
	
	// Really just an IE6 Fix
	// Doesn't cause any issues on any browsers except safari for somereason.
	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');
	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');
		}
	);

	/*
		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;
	});
	

	
	jQuery('#footerCarousel').jcarousel({
			auto: 9,
			wrap: 'both',
			visible: 3,
			scroll: 3,
			initCallback: mycarousel_initCallback
		});

	

	/*
	Called by Main-Carousel Book-Carousel and Cd-Carousel
	Sets up autoscrolling and pausing while hovering.
	*/
	var resumeTime = 9000;
	function mycarousel_initCallback(carousel) {
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
		   var auto = carousel.options.auto;
			carousel.startAuto(0);
			setTimeout(function(){
				if(!carousel.auto){
					carousel.startAuto(auto);
				}
			}, resumeTime);
		});
		
		carousel.buttonPrev.bind('click', function() {
			var auto = carousel.options.auto;
			carousel.startAuto(0);
			setTimeout(function(){
				if(!carousel.auto){
					carousel.startAuto(auto);
				}
			}, resumeTime);
		});

		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.auto = carousel.auto || carousel.options.auto;
			carousel.startAuto(0);
		}, function() {
			carousel.startAuto(carousel.auto);
		});
	}

});
