/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
(function($) {
	$.fn.promoShow = function(settings) {
		settings = $.extend({
			arrImagens: new Array(),
			arrLinks: new Array(),
			arrIntro: new Array(),
			classButtons: "promoShowButtons",
                        bgPosOn: "0 100%",
                        bgPosOff: "0 0",
                        width: '',
                        height: '',
                        intervaloTempo: 5000,
                        transicaoTempo: 500
		}, settings);


		return this.each(function() {
			var divPromoShow = $(this);
                        divPromoShow.css('width', settings.width);
                        divPromoShow.css('height', settings.height);

                        var arrImages = new Array();
                        var arrLinks = new Array();
                        var arrIntro = new Array();
                        var itemAtual = 0;
                        var timerId = null;

                        for(i=0;i<=settings.arrImagens.length-1;i++){
                            arrImages[i] = new Image();
                            arrImages[i].src = settings.arrImagens[i];
                            arrImages[i].style.border = 'none';
                            if(settings.arrLinks[i]){
                                arrLinks[i] = document.createElement('a');
                                arrLinks[i].href = settings.arrLinks[i];
                                arrLinks[i].style.display = 'none';
                                $(arrLinks[i]).prepend(arrImages[i]);
                            }
                            arrIntro[i] = settings.arrIntro[i];
                        }


                        function trocarPara(itemNumero){
                          
                            if(itemNumero >= 0 && itemNumero < arrLinks.length){
                                $('#intro').fadeOut(settings.transicaoTempo,function(){
                                    divPromoShow.children("a").fadeOut(settings.transicaoTempo,function(){
                                        divPromoShow.empty()
                                        divPromoShow.prepend(arrLinks[itemNumero]);
                                        divPromoShow.children("a").fadeIn(settings.transicaoTempo,function(){
                                            $('#intro').html(arrIntro[itemNumero]);
                                            $('#intro').fadeIn(settings.transicaoTempo);
                                        });
                                    });
                                });
                                $('.'+settings.classButtons).each(function (index, domEle) {
                                    if(index == itemNumero){
                                        $(this).css('background-position',settings.bgPosOn);
                                    }else{
                                        $(this).css('background-position',settings.bgPosOff);
                                    }
                                });
                                
                            }
                        }

                        function iniciar(){
                            $('.'+settings.classButtons).each(function (index, domEle) {
                                  $(this).css('background',settings.bgColorOff);
                                  $(this).click(function(){
                                      trocarPara(index);
                                      window.clearInterval(timerId);
                                  });
                            });

                            divPromoShow.prepend(document.createElement('a'));
                            divPromoShow.one("slideTo",trocarPara);
                            trocarPara(0);
                            
                            timerId = window.setInterval(function(){
                                itemAtual++;
                                if(itemAtual == arrLinks.length) itemAtual = 0;
                                trocarPara(itemAtual);
                             },settings.intervaloTempo);
                        } 
                        
                         iniciar();

                         // divPromoShow.trigger('slideTo',[1]);
                        
		});
	}

})(jQuery);


