﻿jQuery(document).ready(function(){

	// Get Max Images
	var maxImages = jQuery('#bannerPanel').attr('maxImages');
	var curImage = 1;
	var timerId = 0;
	
	// Assign Click Events
	jQuery('#leftButton,#rightButton').bind('click', function(){
	
		switch(jQuery(this).attr('id'))
		{
			case 'leftButton':
				newImage = curImage - 1;
			break;
			default:
				newImage = curImage + 1;
			break;
		}

		RestartTimer();
		SwitchImage(curImage, newImage);
	
	});
	
	jQuery('img[link]').bind('click', function(){
	
		var thisLink = jQuery(this).attr('link');
		window.location.href = thisLink;
	
	});
	
	function StartTimer(){
	
		timerId = setInterval(MoveAheadOne, 8000);
	
	}
	
	function RestartTimer(){
		
		// Stop Timer.
		clearTimeout (timerId);
		
		// Start Timer Again.
		StartTimer();

	}
	
	function MoveAheadOne(){
	
		newImage = curImage + 1;
	
		SwitchImage(curImage, newImage);
	
	}
	
	function SwitchImage(currentImage, nextImage){
	
		// Correct Ids if need be
		if (nextImage > maxImages){ nextImage = 1; }
		if (nextImage < 1 ){ nextImage = maxImages; }
		
		
		// Fade out image.
		jQuery('#hBanner_'+currentImage).animate({ opacity: 0 }, 1000);
		
		// Change Class
		jQuery('#hBanner_'+currentImage).attr('class', 'hideBanner');
		
		// Set Start opacity
		jQuery('#hBanner_'+nextImage).css('opacity', '0');
		
				// Change Class
		jQuery('#hBanner_'+nextImage).attr('class', 'showBanner');
		
		// Fade In image
		jQuery('#hBanner_'+nextImage).animate({ opacity: 1 }, 1000);
		
		// Change Position
		curImage = nextImage;
	}
	
	StartTimer();

});