/*Front Slide banner variables
duration: duration of one image staying time (ms)
duration2: easing duration (ms)
*/

(function($) {
	$.fn.fadeBanners = function(duration, duration2) {
	
		if(!duration) duration = 5000;
		if(!duration2) duration2 = 800;
		
		var banners = this.find('.images');
		var buttons = this.find('.buttons');
		var loader = this.find('.loader');
		
		var current = 0;
		
		var frontSlideTimer;
		var imgs = new Array();
		var total = banners.children('a').length;
	
		buttons.find('a').click( function() {
			return false;
		});
		
			
		//Slide preparation
		banners.children('a').each( function(index) {
			var img = $(this).children('img');
			if(index != 0) $(this).hide();
			
			img.load( function() {
				imgs.push(index);
				
				if(imgs.length == total) {
					
					loader.hide();
					frontSlideTimer = setInterval( function() {
						current = changeImage(banners, total, current, duration2);
					}, duration);
				}
			});
			
			img.attr('src', $(this).attr('name'));
			$(this).css({'position':'absolute', 'top':'0px', 'left':'0px'});
		
		});

		//Slide buttons preparation
		buttons.find('a').click( function(e) {
			var nextID = $(this).attr('id').substr(1);
			clearInterval(frontSlideTimer);
			
			current = changeImage(banners, total, current, duration2, nextID);
					
			frontSlideTimer = setInterval( function() {
				current = changeImage(banners, total, current, duration2);
			}, duration);
			
			cancelEvent(e);
		});
		
		return this;
	};
})(jQuery);


function changeImage(banners, total, current, duration2, nextID) {
	
	if(nextID) {
		next = nextID;
		//console.log('next:'+next+'current:'+current);
	}else{
		//console.log(current);
		next = Number(current) + 1;
		if(next == total) next = 0;
		
		//console.log('next:'+next+'current:'+current);
	}
	banners.children('a').eq(current).stop(true, true).fadeOut(duration2, function() { banners.children('a').eq(next).stop(true, true).fadeIn(duration2)});
	
	return next;			
}


function cancelEvent(e)
{
	  e = e ? e : window.event;
	  if(e.stopPropagation)
		e.stopPropagation();
	  if(e.preventDefault)
		e.preventDefault();
	  e.cancelBubble = true;
	  e.cancel = true;
	  e.returnValue = false;
	  return false;
}

/*** Easing plugin ***/
//http://gsgd.co.uk/sandbox/jquery/easing/
