var $$ = $.fn;
var $$ativo = 1;
var $$last = 1;
var $$total = 3; // total de banners


$$.extend({
	Slideshow : {
		Ready : function(){ 
			$$total = $('#banner ul li').length;
			
			$('#banner ul li a').click(function(){
					$$ativo = $(this).html();
					
					if(!$(this).hasClass('ativo')){
						$('#banner ul li a').removeClass('ativo');
						// IE
						if ( $.browser.msie && $.browser.version < 9 )
							$('#banner .banner').fadeOut(0);
						else
							$('#banner .banner').fadeOut(500);
					}
					
					$$.Slideshow.Interrupted = true;
					$(this).addClass('ativo');
					// IE
					if ( $.browser.msie && $.browser.version < 9 ) {
						$('#banner #banner-' + $$ativo).fadeIn(0, function(){
							$('#banner ul li a').stop();
							$('#banner .banner').stop();
						});
					}
					else {
						$('#banner #banner-' + $$ativo).fadeIn(500, function(){
							$('#banner ul li a').stop();
							$('#banner .banner').stop();
						});
					}
					$$.Slideshow.Interrupted = false;
			});
			
			this.Transition();
		},

    	Transition : function(){
			if(!this.Interrupted){
				// IE
				if ( $.browser.msie && $.browser.version < 9 ) {
					$('#banner #banner-' + $$last).fadeOut(0, function() {
					$('#banner ul li a.banner-' + $$last).removeClass('ativo');
						$('#banner ul li a.banner-' + $$ativo).addClass('ativo');
						$('#banner #banner-' + $$ativo).fadeIn(0, function(){
							$('#banner ul li a').stop();
							$('#banner .banner').stop();
						});
						
						if($$ativo > $$total)
							$$last = $$total;
						else
							$$last = $$ativo;
						
						$$ativo++;
						
						if($$ativo > $$total)
							$$ativo = 1;
					
						if($$total > 1)					
							setTimeout('$$.Slideshow.Transition();', 5000);
					});
				}
				else {
					$('#banner #banner-' + $$last).fadeOut(500, function() {
					$('#banner ul li a.banner-' + $$last).removeClass('ativo');
						$('#banner ul li a.banner-' + $$ativo).addClass('ativo');
						$('#banner #banner-' + $$ativo).fadeIn(500, function(){
							$('#banner ul li a').stop();
							$('#banner .banner').stop();
						});
						
						if($$ativo > $$total)
							$$last = $$total;
						else
							$$last = $$ativo;
						
						$$ativo++;
						
						if($$ativo > $$total)
							$$ativo = 1;
					
						if($$total > 1)					
							setTimeout('$$.Slideshow.Transition();', 5000);
					});
				}
			}
			else
				if($$total > 1)
					setTimeout('$$.Slideshow.Transition();', 5000);
			
		}
	}
});

$(document).ready(
	function() {
		$$.Slideshow.Ready();
	}
);
