var timeout_id = Number();
var active, current;
var flag = true;

jQuery(function(){
	$('#banner IMG').css({
		'visibility' : 'visible',
		'display' : 'none'
	});
	$('#banner IMG:first').css('display', 'block');

	$('#arrow-right').click(function() {
		if (flag) {
			if (active != undefined) 
				active.removeClass('active last-active');
			
			clearInterval(timeout_id);
			slideSwitch('right');
			timeout_id = setInterval(function(){
				slideSwitch("right");
			}, 5000);
		}
	});
	$('#arrow-left').click(function() {
		if (flag) {
			if (active != undefined) 
				active.removeClass('active last-active');
			
			clearInterval(timeout_id);
			slideSwitch('left');
			timeout_id = setInterval(function(){
				slideSwitch("right");
			}, 5000);
		}
	});
		
});

function slideSwitch(action) {
	flag = false;
	active = jQuery('#banner IMG.active');
    if ( active.length == 0 ) active = jQuery('#banner IMG:last');
        
    var next =  active.next().length ? active.next() : jQuery('#banner IMG:first');
    var prev =  active.prev().length ? active.prev() : jQuery('#banner IMG:last');
    active.addClass('last-active');
        
    if(action=='left')
    {
        current=prev;
    }
    else{
        current=next;
    }
    
    current.css({
        'display' : 'block',
        'opacity' : '0.0'
    }).addClass('active')
        /*.stop()*/
        .animate({opacity: 1.0}, 1000, function() {
		   flag = true;
		   active.css({
            'display': 'none'
           });
           active.removeClass('active last-active');
        });
        
} // termina

window.onload = function(){
	timeout_id = setInterval(function(){ 
	   slideSwitch("right"); 
	}, 8000 );
}

