$(document).ready(function(){
	
	// START Slideshow
	$('#slideshow_control').append('<span class="nav"></span>');
	var counter = 1;
	$('#slideshow > div.div-content').each(function(){
		var addClass = '';
		
		if($(this).hasClass('active'))
		{
			addClass = ' class="active"';
		}
		
		$('#slideshow_control > span.nav').append('<a href="#"'+addClass+' rel="slide_'+counter+'"></a>');
		$(this).attr('id', 'slide_'+counter);
		counter++;
		//alert($(this).attr('title'));
	});
	
	$('#slideshow_control').append('<span class="title">'+$('#slideshow div.active').attr('title')+'</span>');
	
	
	$('#slideshow_control > span.nav > a').click(function(e){
		e.preventDefault();
		clearInterval(slideInterval);
		slideSwitch($(this).attr('rel'));
	});
	
	
	var slideInterval = setInterval("slideSwitch(null)", 4000);
	// END Slideshow
	
	
	// START Main Menu Animation
	$('#main_menu ul li a:not(.active)').mouseover(function(){
		$(this).stop().animate({'height': '113px'}, 100)
	});
	$('#main_menu ul li a:not(.active)').mouseout(function(){
		$(this).stop().animate({'height': '88px'}, 100)
	});
	$('#main_menu ul').removeClass('nojs');
	// END Main Menu Animation
	
	
	// Prevent default action on # link
	$('a[href="#"]').click(function(e){
		e.preventDefault();
		return false;
	});
});


function slideSwitch(slideIdToShow) {
	
	
	var $active = $('#slideshow div.active');
	
	if ( $active.length == 0 ) $active = $('#slideshow div:last');
	
	if(slideIdToShow == null)
	{
		// use this to pull the divs in the order they appear in the markup
		var $next =  $active.next().length ? $active.next()	: $('#slideshow div.div-content').first();
	}
	else
	{
		var $next = $('#slideshow div#'+slideIdToShow);
	}
	
	if($next.hasClass('active'))
	{
		return false;
	}
	
	$("#slideshow_control span.title").html("");
	$('#slideshow_control > span.nav > a').removeClass('active');
	
	$active.addClass('last-active');
	
	$("#slideshow_control span.title").html($next.attr("title"));
	$('#slideshow_control > span.nav > a[rel='+$next.attr('id')+']').addClass('active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 500, function() {
			$active.removeClass('active last-active');
		});
	
}
