// onload0
// TODO: COMENT
(function() {
    var arrADS = new Array(),
	TIMING = '20000',
	interval;
	
	window.arrADS = arrADS;

    var rotator = window.rotator = {
        start: function(t) {
            if (typeof t != 'undefined')
                TIMING = t;

            this.engine();
            this.drawControls();
        },

        engine: function() {
            var items = jQuery('div.adRotator div.container');
			var j = 0;
            for (var k = 0; k < items.length; k++)
                arrADS[k] = items.filter(':eq(' + k + ')').hide();

            arrADS[0].show();

            interval = setTimeout(this.switcher, arrADS[items.length -1].attr('tempo'));
        },

        stop: function() {
            clearInterval(interval);
        },

        switcher: function(i, user) {
            if (!user)
                for (var k = 0; k < arrADS.length; k++) {
					if (arrADS[k].is(':visible')) {
						arrADS[k].hide();
	
						if (typeof arrADS[k + 1] != 'undefined') {
							arrADS[k + 1].show();
							arrADS[k + 1].html(arrADS[k + 1].html());
							i = k + 1;
						} else {
							arrADS[0].show();
							arrADS[0].html(arrADS[0].html());
							i = 0;
						}
						rotator.stop();
						interval = setTimeout(rotator.switcher, arrADS[i].attr('tempo'));
						k = arrADS.length;
					}
				}
            else
                for (var c = 0; c < arrADS.length; c++) {
					arrADS[c].hide();
	
					arrADS[i].show();
					arrADS[i].html(arrADS[i].html());
					rotator.stop();
					interval = setTimeout(rotator.switcher, arrADS[i].attr('tempo'));
				}
			
            jQuery('ul.controls a').removeClass('ativo').filter(':eq(' + i + ')').addClass('ativo');
        },

		next: function()
		{
			var controls = jQuery('ul.controls a'),
				index = controls.index(controls.filter('.ativo')) + 1;
				
			if ( typeof arrADS[ index ] == 'undefined' )
				index = 0;
			
			this.stop();
			this.switcher( index, true );
		},
		
		prev: function()
		{
			var controls = jQuery('ul.controls a'),
				index = controls.index(controls.filter('.ativo')) - 1;
				
			if ( typeof arrADS[ index ] == 'undefined' )
				index = arrADS.length - 1;

			this.stop();
			this.switcher( index, true );
		},		

		drawControls: function()
		{
			var controle = jQuery( 'div.adRotator ul.controls' ),
				containers = jQuery( 'div.adRotator div.container' ),
				template = '<li class="##classe##"><a href="#" class="##classe##">##label##</a></li>',
				html;

			containers.each(function( k ){
				html = template.replace( new RegExp( '##classe##', 'gi' ), k == 0 ? 'ativo primeiro' : '' ).replace( new RegExp( '##label##', 'gi' ), jQuery( this ).attr( 'title' ) );
				controle.append( html );
			});
			


			this.centralizar();
			this.controls();
		},

        controls: function() {

            var controls = jQuery('ul.controls a'), 
				voltar = jQuery('div.adRotator a.voltar'),
				avancar = jQuery('div.adRotator a.avancar'),
				$this = this;

            controls.click(function() {
                $this.stop();
                $this.switcher(controls.index(this), true);
                return false;
            });

			
			voltar.click(function(){
				$this.prev();	
				return false;
			});

			avancar.click(function(){
				$this.next();
				return false;
			});
        },
		
		centralizar: function()
		{
			var controls = jQuery( 'ul.controls' ),
				items = controls.find( 'li' ),
				itemsWidth = 0,
				controlWidth = controls.width();
				


			items.each(function( i ){
				itemsWidth += jQuery( this ).width();
			});

//			items.filter( ':first' ).css( 'padding-left', ( ( controlWidth - itemsWidth ) / 2 ) - ( itemsWidth / 2 ) );
		}
    }
})();


jQuery(function() {
    rotator.start();

});