/*
	Author: S919
	Date: 07/10/2009
*/
var currentBanner = 0;
var bannerRotateOptions;
var rotationId;
var rotationsCount = 0;
(function($){
	$.fn.bannerRotate = function(options)
	{
		log('options = ' + options);
		
		var defaults = 
		{
			images: [],
			pauseSeconds: 8,
			fadeSpeed: 0.3,
			cssClass: '',
			rotations: options.images.length
		};
		
		var options = $.extend(defaults, options);
		bannerRotateOptions = options;
		var target = this;
		var index = 1;
		if(options.images.length == 0){return; }
		
		var init = function()
		{
			$(target)[0].src = options.images[0].src;
			$(target).parent()[0].href = options.images[0].href
			
			linkOn = ''
			linksHTML = '';
			for(bri = 0; bri < options.images.length; bri++)
			{
				if(bri == 0){linkOn = 'on'; }else{linkOn = ''}
				linksHTML += '<li id="bannerLink' + bri + '" class="' + linkOn + '" ><a href="javascript:changeBanner(' + bri + ')" >' + (bri + 1) + '</a></li>';					
			}		
			$('#bannerLinks').html(linksHTML);			
		}
		
		var rotate = function()
		{
			if(rotationsCount < options.rotations)
			{
				$('#bannerLinks li.on').removeClass('on');
				$('#bannerLink' + index).addClass('on');
				currentBanner = index;
				
				$(target).fadeTo( (options.fadeSpeed * 1000), 0.0, function()
				{
					$(this).parent()[0].href = options.images[index].href
					$(this)[0].src = options.images[index].src;
					$(this).fadeTo((options.fadeSpeed * 1000), 1.0);					
					if(index < options.images.length - 1) {index++; } else {index = 0;}
				});
				
				rotationsCount++;				
			}
			else
			{
				clearInterval(rotationId);
			}
		}
		
		init();		
		rotationId = setInterval(rotate, options.pauseSeconds*1000);
		
	};
})(jQuery);
var xxx;
function changeBanner(index)
{
	log(' = changeBanner. index = ' + index);
	
	clearInterval(rotationId);
	
	if(index != currentBanner)
	{
		$('#bannerLinks li.on').removeClass('on');
		$('#bannerLink' + index).addClass('on');
		currentBanner = index;
		
		$('#bannerImage').fadeTo( (bannerRotateOptions.fadeSpeed * 1000), 0.0, function()
		{ 
			$(this).parent()[0].href = bannerRotateOptions.images[index].href;
			$(this)[0].src = bannerRotateOptions.images[index].src;
			$(this).fadeTo((bannerRotateOptions.fadeSpeed * 1000), 1.0);
		});
	}
}


