﻿/* ------------------------------------------------------------------------
	Nortech Banner Rotator
------------------------------------------------------------------------- */

(function($){  

    $.fn.bannerRotator = function(params) {
 
    	var showBanner = function(bRotator, banner) {
			//Set Variables
			var imgAlt = $('img', banner).attr("alt"); //Get Alt Tag of Image
			var imgLink = $('a',banner).attr("href"); //Get NavigateTo URL
			var imgUrl = $('a img',banner).attr("rel"); //Get Main Image URL
			var imgDesc = $('.block', banner).html(); 	//Get HTML of block
			var imgDescHeight = $(".main_image", bRotator).find('.block').height();	//Calculate height of block	

			if ($(banner).is(".active")) {  //If it's already active, then...
				return false; // Don't click through
			} else {
				//Animate the Teaser	
				$(".main_image .block", bRotator).animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
					$(".main_image .block", bRotator).html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 250 );
					$(".main_image a", bRotator).attr({ href: imgLink });
					$(".main_image img", bRotator).animate( {opacity: "0"}, 250, function() {
						$(".main_image img", bRotator).attr({ src: imgUrl, alt: imgAlt}).ready(function() { 
							$(".main_image img", bRotator).animate( {opacity: "1.0"}, 250)
						});
					});	
				});
			}
			
			$(".image_thumb ul li", bRotator).removeClass('active'); //Remove class of 'active' on all lists
			$(banner).addClass('active');  //add class of 'active' on this list only
			
			if ( ($(banner).offset().top - $(bRotator).offset().top) < 0 ) 
				$('#bannerThumbs .fixed', bRotator).css({marginTop: "0px"});
				

			if ( ($(banner).offset().top + $(banner).outerHeight() - $('.fixed', bRotator).offset().top) > $(bRotator).outerHeight() ) {
				var adjustMarginTop = $(banner).offset().top + $(banner).outerHeight() - $('.fixed', bRotator).offset().top - $(bRotator).outerHeight();
				$('#bannerThumbs .fixed', bRotator).css({marginTop: "-" + adjustMarginTop + "px"});
}
			return false;
    	};
    	
    	var startAutoRotateTimer = function(bRotator, banners) {

    		bRotator.stopTime("autoRotateTimer");  		

			bRotator.everyTime(6000, "autoRotateTimer", function(i) { 
				active_index = $(banners).index($(".image_thumb ul li.active"));
				active_index++;
				if (active_index == banners.length) active_index = 0;
				showBanner(bRotator, banners[active_index])
			}, 0);    		
    	}
    	
    	var stopAutoRotateTimer = function(bRotator) {
    		bRotator.stopTime("autoRotateTimer");  		
    	}
    
    	return this.each(function() { 
	    	var bRotator = $(this);
	    	var banners = $(".image_thumb ul li", bRotator);
	    	
			// Show 1st banner
			showBanner(bRotator, $(".image_thumb ul li:first", bRotator));
		
			$(".image_thumb ul li", bRotator)
				.click(
					function(){
						showBanner(bRotator, this);
					})
				.hover(
					function(){
						$(this).addClass('hover');
					},
					function() {
						$(this).removeClass('hover');
					});
					

			//Toggle Teaser
			$("a.collapse", bRotator).click(function(){
				$(".main_image .block", bRotator).slideToggle();
				$("a.collapse", bRotator).toggleClass("show");
			});
			
			$(bRotator).fadeIn("slow");
			
			//Initialize mouse over navigation
			MouseoverNav.init('bannerThumbs', 250, $('#bannerThumbs').outerHeight(), 0.15);

			// Pause auto rotation on hover
			bRotator.hover( function(){stopAutoRotateTimer(bRotator);}, function() {startAutoRotateTimer(bRotator, banners);} );
			
			//Start auto rotation timer
			startAutoRotateTimer(bRotator, banners);
					
		}); 	
    };  

})(jQuery);  

