(function($){

	var backZ = 5;
	var middleZ = 6;
	var frontZ = 7;
	
	var to = slideshowPauseTime; /* Change the speed of the Slide Show */
	var textTo = textDelay; /* Change the speed of the Text */
	var speed = transitionSpeed; /* Change the speed of the Image */
	
	var  nextSlide;
	var prevVisibleSlide;
	
	function showText()
	{
		$(".quote", nextSlide).fadeIn(speed);
	}
	
	function slideshow()
	{
		nextSlide = $(".visible-slide").index() != $(".slide-show li").size()-1 ? $(".visible-slide").next() : $("ul.slide-show li").eq(0);
		prevVisibleSlide = $(".visible-slide");
		
		prevVisibleSlide.css("z-index", middleZ);
		nextSlide.css("z-index", frontZ);
		$(".quote", nextSlide).hide();
		$(".image", nextSlide).hide();
			
		$(".image", nextSlide).fadeIn(speed, function(){
			$(".visible-slide").removeClass("visible-slide").css("z-index", backZ);
			nextSlide.addClass("visible-slide").css("z-index", frontZ);
			setTimeout(showText, textTo);
			$(".quote", prevVisibleSlide).show();
			$(".image", prevVisibleSlide).show();
			prevVisibleSlide.show();
			prevVisibleSlide.css("z-index", backZ);
			setTimeout(slideshow, to);
		});
		
	}

	$(document).ready(function(){
		
		$("ul.slide-show li").css("z-index", backZ);
		$("ul.slide-show li").eq(0).css("z-index", frontZ).addClass("visible-slide");
		
		
		setTimeout(slideshow, to);
		
		$('.navigation ul li').hover(function(){ 
			$(this).find('ul.sub-menu:eq(0)').show();
			if ( $(this).find('> span.heading').length != 0 ) {
				$(this).find('> span.heading').addClass('hover');
			} else {
				$(this).find('> a:eq(0)').addClass('hover');
			};
		 },
		 function(){  
		 	$(this).find('ul.sub-menu').hide();
		 	if ( $(this).find('> span.heading').length != 0 ) {
				$(this).find('> span.heading').removeClass('hover');
			} else {
				$(this).find('> a:eq(0)').removeClass('hover');
			};
	 	});
		
	});
	
	$(window).load(function() {
		
		$('.quote h3').show();
		
		// EXTEND CONTENT-B HEIGHT TO MATCH SIDEBAR HEIGHT
		if ( $.browser.msie ) {
			setTimeout(calculate_heights, 1000);
		} else {
			calculate_heights();
		};
	});
	
	function calculate_heights () {
		var content_height = $('.content-b').height();
		var cont_full_height = $('.content-b').innerHeight();
		var sidebar_height = $('.sidebar-b').height();
		var content_height_diff = cont_full_height - content_height;
		var height = parseInt(sidebar_height) - content_height_diff;
		if ( cont_full_height < sidebar_height ) {
			$('.content-b').height(height);
		} else if ( cont_full_height > sidebar_height ) {
			$('.sidebar-b').height(cont_full_height);
		}
	}
})(jQuery)
