$(function(){

	/**
	* Management Sidebar Functionality
	*/		
	$management_sidebar = $('table#management_sidebar tr td');
	$management_sidebar.hover(function(){
		$('img.gray', $(this)).hide();
		$('img.black', $(this)).show();
	},function(){
		$('img.black', $(this)).hide();
		$('img.gray', $(this)).show();	
	});

	/**
	* Homepage Navigation Functionality
	*/

	var sidebarShow = function(obj, type, page, current_element) {
		var sidebar_open_class = 'sidebar_open'+ (current_element ? '_'+ current_element : '');
		if(type == 'show') {
			obj.removeClass('sidebar_closed');
			obj.addClass(sidebar_open_class);
			if(page == 'home') {
				$('span.arrow_open_'+current_element, obj).css({'display':'inline'});
			}
			else {
				$('span.arrow_open', obj).css({'display':'inline'});
			}
			$('span.arrow_closed', obj).css({'display':'none'});
			
			$('ul.sidebar_tier2', obj).slideDown(100);		
		}
		else {
			obj.removeClass(sidebar_open_class);
			obj.addClass('sidebar_closed');
			if(page == 'home') {
				$('span.arrow_open_1', obj).css({'display':'none'});
			}
			else {
				$('span.arrow_open', obj).css({'display':'none'});
			}
			$('span.arrow_closed', obj).css({'display':'inline'});
			$('ul.sidebar_tier2', obj).slideUp(100);		
		}
	}
	
	$sidebar_home = $('div#right_sidebar_home ul.sidebar_tier1 li.sidebar_tier1_element');
	$sidebar_tier1_home = $('ul.sidebar_tier1 li#home_menu_8');
	sidebarShow($sidebar_tier1_home, 'show', 'home', 8);
	
	$sidebar_home.click(function(){
		if( !$('ul.sidebar_tier2', $(this)).is(':visible') ) {
			var current_sidebar_element = $(this).attr('id').replace(/^home_menu_/, '');
			sidebarShow($sidebar_home, 'hide', 'home', 1);
			sidebarShow($sidebar_home, 'hide', 'home', 2);
			sidebarShow($sidebar_home, 'hide', 'home', 3);
			sidebarShow($sidebar_home, 'hide', 'home', 4);
			sidebarShow($sidebar_home, 'hide', 'home', 5);
			sidebarShow($sidebar_home, 'hide', 'home', 6);
			sidebarShow($sidebar_home, 'hide', 'home', 7);
			sidebarShow($sidebar_home, 'hide', 'home', 8);
			sidebarShow($(this), 'show', 'home', current_sidebar_element);
		}
		else {
			var current_sidebar_element = $(this).attr('id').replace(/^home_menu_/, '');
			sidebarShow($(this), 'hide', 'home', current_sidebar_element);
		}
	});		
	
	$sidebar = $('div#right_sidebar ul.sidebar_tier1 li.sidebar_tier1_element');
	$sidebar.click(function(){
		if( !$('ul.sidebar_tier2', $(this)).is(':visible') ) {
			sidebarShow($sidebar, 'hide', 'main');
			sidebarShow($(this), 'show', 'main');
		}
		else {
			sidebarShow($(this), 'hide', 'main');
		}
	});		
	
	$search = $('input#search');
	$search.click(function(){
		if( $(this).val() == 'Search' ) {
			$(this).val('');
		}
	});
	
});

$(window).load(function(){
	var o = {};
	o.delay = 4500;
	o.transSpeed = 400;
	o.autorun = false;
	o.buttons = false;
	
	var currentSlideIndex = 0;
	var $slideShowContainer = $('div#intro_slideshow');
	var $bottomSlideShowBox = $('td.bottom_section');

	$slideShowContainer.slideShow(o).startSlides();

	$slideShowContainer.bind('slideChanged', function(){
		var currentSlideIndex = $slideShowContainer.currentSlideIndex();
		var image_id = 'img#bottom_image_'+ (currentSlideIndex + 1);
		var text_id = 'div#bottom_text_'+ (currentSlideIndex + 1);
		var past_image_id = 'img#bottom_image_'+ ((currentSlideIndex == 0 ? 3 : currentSlideIndex));	
		$('div.intro_text').hide();
		$(image_id).hide();
		$(text_id).show();
		$(past_image_id).show();
	});

	$slideShowContainer.hover(function(){
		currentSlideIndex = $slideShowContainer.currentSlideIndex();
		$slideShowContainer.stopSlides();
	}, function(){
		$slideShowContainer.startSlides();
	});
	
	$bottomSlideShowBox.hover(function(){
		var hover_index = $(this).attr('id').replace(/bottom_section_/, '');
		$slideShowContainer.changeSlide((hover_index-1));
		$slideShowContainer.stopSlides();
	},
	function(){
		var hover_index = $(this).attr('id').replace(/bottom_section_/, '');
		var past_image_id = 'img#bottom_image_'+ hover_index;
		var text_id = 'div#bottom_text_'+ hover_index;
		$(past_image_id).show();
		$(text_id).hide();
		$slideShowContainer.changeSlide((hover_index-1));	
		$slideShowContainer.startSlides();
	});
	
});
