var permStop = false;
var clickStop = false;


function slideSwitchFunctionality(){
		
		var $active = $('#active div.current');
		$('#active > div').fadeOut('fast');
		
		if ( $active.length == 0 ) $active = $('#active div:last');
		
		var $next =  $active.next().length ? $active.next() : $('#active div:first');
	
		$active.addClass('last-current');
		
		
		$next.css({opacity: 0.0})
			.fadeIn('fast')
			.addClass('current')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('current last-current');
				
			});	

}

function activeSlideSwitch(imgSlide){

	$("#vertBan").trigger("play", true);
		switch(imgSlide){
		case 'activeSlide1':
			$("#vertBan").trigger("slideTo", '.activeSlide2');
			$("#vertBan").trigger("pause", true);

			break;
		case 'activeSlide2':
			
			$("#vertBan").trigger("slideTo", '.activeSlide3');
			$("#vertBan").trigger("pause", true);
			break;
		case 'activeSlide3':
			$("#vertBan").trigger("slideTo", '.activeSlide4');
			$("#vertBan").trigger("pause", true);

			break;	
		case 'activeSlide4':
			$("#vertBan").trigger("slideTo", '.activeSlide5');
			$("#vertBan").trigger("pause", true);

			break;	
		case 'activeSlide5':
			$("#vertBan").trigger("slideTo", '.activeSlide1');
			$("#vertBan").trigger("pause", true);

			break;	
		};
	
}




function slideSwitchLock(imgSlide){

	clickStop = true;

	var $bigChoiceSelected = $('.'+imgSlide +'div.current');

	var $active = $('#active div.current');
	$('#active > div').fadeOut('fast');
	$active.removeClass('current last-current active');
	$("#"+imgSlide).addClass('current').fadeIn('slow').animate({opacity: 1.0}, 1000);
}



$(document).ready(function() {
	
	$('div.container').css('display','block');
	
	$('div.bottomItem').each(function() {
	
		var differential = ($(this).children('div.subNav').outerWidth() - $(this).outerWidth()) / 2
	
		$(this).children('div').css({
			'left' : -differential + 'px',
			'display' : 'none'
		});
	
	});

	
	$('div.bottomItem').hover(
    	function() {
    		$(this).children('div').css('z-index','9999');
    		/* changed stop(true,true) to stop(false,true) */
    		$(this).children('div').stop(false,true).slideDown('fast');
    	},
    	function() {
    		/* changed stop(true,true) to stop(false,true) */
    		$(this).children('div').stop(false,true).hide();
    	}
    	
    	);
    	
    	/*  for stopping the fadeIn/fadeOut of the main image */
	$('.wrap').hover(
		function(){
			permStop = true;
		}, 
		function(){
			permStop = false;
		}
	);
	
	$('#active').hover(
		function(){
			permStop = true;
			$('#vertBan').trigger("pause");
		}, 
		function(){
			permStop = false;
			$('#vertBan').trigger("play");
		}
	);
	
	$('#vertBan > li').click(function(){
		var activeSlideClass = $(this).attr('class');
		activeSlideSwitch(activeSlideClass);

	});
	$('#vertBan').carouFredSel({
                    direction: 'up',
		    items : {
			visible: 'variable'
		    },
                    auto : {
                        duration: '800'
			
                    },
                    scroll : {
			items : 1,
			duration : '800',
			pauseOnHover : 'true',
			onBefore : function(){
				if(permStop == false && clickStop == false){
					slideSwitchFunctionality();
				};	
				
				
			}
			
			
                    }  
         });



});

$(window).load(function() {
	
	if($('body').attr('id') != 'home') {
		
		var content = $('div.contentContainer').html();
		
		$('div.contentContainer').html('<div class="topLeft"></div><div class="topMiddle"></div><div class="topRight"></div><div class="middleMiddle"><div class="textContainer">' + content + '</div></div><div class="middleRight"></div><div class="bottomContainer"><div class="bottomLeft"></div><div class="bottomMiddle"></div><div class="bottomRight"></div></div>');
		
		if($('div.middleMiddle').innerHeight() < '435') {
			$('div.middleMiddle').css('background-position','left bottom');
		}
		
		$('div.middleRight').css('height',$('div.middleMiddle').outerHeight() + 'px');
		
		if($('div.contentMain').innerHeight() <= $('div.contentSubMenu').innerHeight()) {
			$('div.contentMain').css('height',$('div.contentSubMenu').innerHeight() + 'px');
		}
		
		else if($('div.contentMain').innerHeight() > $('div.contentSubMenu').innerHeight()) {
			$('div.contentSubMenu').css('height',$('div.contentMain').innerHeight() + 'px');
		}
	}
});
