(function($) { 
	//Slideshow
	function slideSwitch() {
		
    var $active = $('#slideshow IMG.active');
		var $txActive = $('.introHome li.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');
		
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2000, function() {
            $active.removeClass('active last-active');
        });
				
		//$(".introHome ul").animate( {marginTop:-220} , 400, "easeInQuad" );
	}
	

 
 //---------------------------------------------------------------------------------------------------------------------
 //JQUERY 
 //---------------------------------------------------------------------------------------------------------------------

$(document).ready(function(){
	var imgCounter = 0;											
	// JavaScript Document
 function initialize(){
		homeImageResize();
		carouselResize();
		carouselControl();
		
		$(window).resize(function(){
  		homeImageResize();
			carouselResize();
		});
	}
	
	
	
	
	
	//---------------------------------------------------------------------------------------------------------------------
 //Home image resizer
 //---------------------------------------------------------------------------------------------------------------------
 	//$(".introHome ul li:not(.active)").hide();
 
	function homeImageResize(){
		window.w = $(window).width();
 		window.h = $(window).height();
		
		$("#slideshow").height(h-140);
		window.Sw = $("#slideshow").width();
 		window.Sh = $("#slideshow").height();
		//$("homeNav").height(h*.4);
		//to center the images of the banner 
		$("#slideshow img").each(function (i) {
		
				var wr = Sw / $(this).width();
				var hr = Sh / $(this).height(); 
				var enlargement = Math.max(wr, hr);
				var imgw = Math.round($(this).width() * enlargement);
				var imgh = Math.round($(this).height() * enlargement); 
				$(this).width(imgw)
				$(this).height(imgh)
				var imgWidth = $(this).width()	
				var imgHeight = $(this).height()	
				
				if(imgHeight>Sh){
					var halfImgHeight = imgHeight/2;
					var centerImage =  $("#slideshow").height()/2 - halfImgHeight + 180;
					$(this).css({'top' : centerImage});
				}
		})
	}
	
	
	//---------------------------------------------------------------------------------------------------------------------
 //Carousel stuff
 //---------------------------------------------------------------------------------------------------------------------
	function carouselResize(){
		var imagesW = 0;
		$("#carousel").css("overflow","hidden");
		$("#carousel").height(h-140);
		var imageWidthCarousel = (w/4);	
		$("#carousel ul li").each(function (i) {
																				
			imagesW += imageWidthCarousel;
			hTxBox = $(".carouselTextbox",this).height();
			hCarousel = $("#carousel").height();
			
			
			$(this).width(imageWidthCarousel);
			var imgW	= $("img",this).width();
			var imgH	= $("img",this).height();
			var liW = $(this).width();
			var liH = $(this).height();
			
			if(imgW<liW){
				var enlargAmount =  liW/imgW 
				$("img",this).width(imgW*enlargAmount)
				$("img",this).height(imgH*enlargAmount)
			}
			//$("#logo").css({ opacity: .5 })
		})
		$("#carousel ul").width(imagesW);
	}
	
	
	function carouselControl(){
		$("#carousel .prev").click(function () { 
			var ulCurrentMargin = $(this).parent().find("ul").offset()
			var margin_left = ulCurrentMargin.left + $(this).parent().find("ul li:eq("+imgCounter+")").width();	
			if(margin_left < 1){
				$(this).parent().find("ul").animate( {marginLeft:margin_left} , 600, "easeInOutCubic" );
				imgCounter--;
			}else{
				$(this).parent().find("ul").animate( {marginLeft:"0"} , 400, "easeInQuad" );
			}
			
			
			
			//$(this).parent().find("ul").animate( {marginLeft:"-300px"}, 400, "easeInQuad" );
    });
		$("#carousel .next").click(function () { 			
			var carouselw = $("#carousel ul").width();
			var ulCurrentMargin = $(this).parent().find("ul").offset();
			var margin_left = ulCurrentMargin.left - $(this).parent().find("ul li:eq("+imgCounter+")").width();
			
			if(margin_left > w - carouselw){
      	$(this).parent().find("ul").animate( {marginLeft:margin_left} , 600, "easeInOutCubic" );
				imgCounter++
			}else{
				var margin_left = w - carouselw
				$(this).parent().find("ul").animate( {marginLeft:margin_left} , 400, "easeInQuad" );
			}
			
			
    });

	}
    
	
	//---------------------------------------------------------------------------------------------------------------------
 //Main Navigation FX
 //---------------------------------------------------------------------------------------------------------------------
	$("#mainNav li a:not(.current, .section)").hover(function () {
		$(this).stop().animate( {backgroundPosition:"(0 -50px)", paddingTop:"10px", height:"38px"} , 300, "easeOutQuad" );
	},function () {
		$(this).stop().animate( {backgroundPosition:"(0 -100px)", paddingTop:"12px", height:"36px"} , 200, "easeInQuad" );
	}); 
	
	
	
	//---------------------------------------------------------------------------------------------------------------------
 //bottom navigation FX
 //---------------------------------------------------------------------------------------------------------------------
	$(".subNav").hide();
	$(".navVisible").hover(function () {
		//clearInterval(slideShowAnimator);
		$(this).parent().find(".homeItemBg").stop().fadeIn({ 
				duration: 200, 
				easing: "easeOutQuad", 
				complete: function(){$(this).parent().find(".subNav").slideDown(400, "easeOutQuad")}
		});		 
	},function () {
		$(this).parent().stop().find(".homeItemBg").fadeOut(100, "easeInQuad");
		$(this).parent().stop().find(".subNav").slideUp(200, "easeOutQuad");
		
	}); 
	
	$(".subNav li a").hover(function () {
		$(this).stop().animate( { backgroundColor:"#FFFFFF", paddingLeft:"5px"} , 100, "easeOutQuad" );
	},function () {
		$(this).stop().animate( { backgroundColor:"transparent", paddingLeft:"0px"} , 100, "easeOutQuad" );
	}); 
	
	//---------------------------------------------------------------------------------------------------------------------
 //Sub Nav FX
 //---------------------------------------------------------------------------------------------------------------------
	$("#subNav ul li a:not(.current)").hover(function () {
		$(this).stop().animate( { backgroundColor:"#FFFFFF", paddingLeft:"10px"} , 100, "easeOutQuad" );
	},function () {
		$(this).stop().animate( { backgroundColor:"transparent", paddingLeft:"0px"} , 100, "easeOutQuad" );
	});
	initialize();
	//var slideShowAnimator = setInterval( slideSwitch, 5000 );

});
		
})(jQuery);	