// TODO: COMENT
(function() {
    var arrADS = new Array(),
	TIMING = '20000',
	interval;

    var rotator = window.rotator = {
        start: function(t) {
            if (typeof t != 'undefined')
                TIMING = t;

            this.engine();
            this.controls();
        },

        engine: function() {
            var items = $('div.adRotator div.container');
            var j = 0;
            for (var k = items.length - 1; k >= 0; k--)

                arrADS[j++] = items.filter(':eq(' + k + ')').hide();

            //arrADS[items.length - 1].show();
            items.eq(0).show();
            interval = setTimeout(this.switcher, items.eq(0).attr('tempo'));
        },

        stop: function() {
            clearInterval(interval);
        },
        switcher: function(i, user) {
            if (!user) {
                var container = $('div.adRotator div.container'),
                	visible = container.filter(":visible"),
                	next = visible.next();
                temp = 0;

                if (visible.length) {
                    visible.hide();

                    if (next.length) {
                        next.show();

                        swfobject.embedSWF(next.attr("swfURL"), next.attr("swfID"), next.attr("swfLargura"), next.attr("swfAltura"), next.attr("swfVersao"), null, { clickurl: next.attr("swfClickURL") }, { wmode: 'transparent', allowScriptAccess: 'always' });

                        i = container.index(next);
                    }
                    else {
                        var first = container.eq(0);

                        first
                        	.show()
                        	.html(first.html())
                        	.innerHTML = '';

                        swfobject.embedSWF(
                        	first.attr("swfURL"),
                        	first.attr("swfID"),
                        	first.attr("swfLargura"),
                        	first.attr("swfAltura"),
                        	first.attr("swfVersao"),
                        	null,
                        	{ clickurl: first.attr("swfClickURL") },
                        	{ wmode: 'transparent', allowScriptAccess: 'always' }
                    	);

                        i = 0;
                    }

                    rotator.stop();
                    interval = setTimeout(rotator.switcher, container.eq(i).attr('tempo'));
                }
            }
            /*
            if (!user)
            for (var k = 0; k < arrADS.length; k++) {
            if (arrADS[k].is(':visible')) {
            arrADS[k].hide();

                    if (typeof arrADS[k + 1] != 'undefined') {

                        //alert( "" + i);
            arrADS[k + 1].show();
            arrADS[k + 1].html(arrADS[k + 1].html());
            arrADS[k + 1].innerHTML = '';
            swfobject.embedSWF(arrADS[k + 1].attr("swfURL"), arrADS[k + 1].attr("swfID"), arrADS[k + 1].attr("swfLargura"), arrADS[k + 1].attr("swfAltura"), arrADS[k + 1].attr("swfVersao"), null, { clickurl: arrADS[k + 1].attr("swfClickURL") }, { wmode: 'transparent', allowScriptAccess: 'always' });
            i = k + 1;
            } else {

                        arrADS[0].show();
            arrADS[0].html(arrADS[0].html());
            arrADS[0].innerHTML = '';
            swfobject.embedSWF(arrADS[0].attr("swfURL"), arrADS[0].attr("swfID"), arrADS[0].attr("swfLargura"), arrADS[0].attr("swfAltura"), arrADS[0].attr("swfVersao"), null, { clickurl: arrADS[0].attr("swfClickURL") }, { wmode: 'transparent', allowScriptAccess: 'always' });
            i = 0;
            }
            rotator.stop();
            alert("" + i);
            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());
                arrADS[i].innerHTML = '';
                swfobject.embedSWF(arrADS[i].attr("swfURL"), arrADS[i].attr("swfID"), arrADS[i].attr("swfLargura"), arrADS[i].attr("swfAltura"), arrADS[i].attr("swfVersao"), null, { clickurl: arrADS[i].attr("swfClickURL") }, { wmode: 'transparent', allowScriptAccess: 'always' });

                rotator.stop();
                interval = setTimeout(rotator.switcher, arrADS[i].attr('tempo'));
            }
            
//            if (user) {
//                if (i == 0)
//                    temp = 0;
//                if (i == 1)
//                    temp = 1;
//                if (i == 2)
//                    temp = 2;
//            }
//            else {
//                if (i == 0)
//                    {temp = 2; alert(""+temp)}                    
//                if (i == 1)
//                    {temp = 1; alert(""+temp)}
//                if (i == 2)
//                    {temp = 0; alert(""+temp)}
//            }
            if (user) {
                temp = i;
            }
            else {
                    {temp = arrADS.length -(i+1) ;}
            }

            $('ul.controls a').removeClass('ativo').filter(':eq(' + temp + ')').addClass('ativo');
        },

        controls: function() {
            var controls = $('ul.controls a'),
			$this = this;

            controls.click(function() {
                $this.stop();
                $this.switcher(controls.index(this), true);

                return false;
            });
        }
    }
})();

$(function() {
    rotator.start();
});
