var constructed = false;

$(function() {
	bannerInstance = new bannerControls();
	bannerInstance.construct();

	$('.banner-navigation a').live('click', function(e) {
		e.preventDefault();
		if(!bannerInstance.animating) {
			bannerInstance.animate($(this).index());
		}
	});
	$('.banner-prev-next a').live('click', function(e) {
		e.preventDefault();
		if(!bannerInstance.animating) {
			if($(this).hasClass('prev')) {
				bannerInstance.goPrev();
			} else {
				bannerInstance.animate();
			}
		}
	})
});
bannerControls = function() {

	this.imgCronometro = baseUrl + '/default/imagens/89.gif';
	this.tempoCronometro = 4500;
	this.activeItem = 0;
	this.animating = false;

	this.construct = function() {
		classSelf = this;
		Img = new Image();
		$(Img).load(function() {
			if(!constructed) {
				classSelf.startLoop();
				constructed = true;
			}
		}).attr({
			src : this.imgCronometro
		}).error(function() { 
			alert('ok')
		});
	}

	this.startLoop = function() {
		this.totalBanners = $('.banner-list').children().length;
		if(this.totalBanners > 0) {
			$('.banner-list').children().each(function() {
				linkClass = '';
				if($(this).index() == 0) {
					linkClass = $(this).index() == 0 ? ' class="active" ' : '';
					$('.legenda-banner #texto').html($(this).attr('title'));
				}
				$('.banner-navigation').append('<a href="#" ' + linkClass + '></a>');
			});
			$('.banner-list').children(':eq(0)').addClass('active');
			this.addCronometro();
			if(this.totalBanners > 1) {
				this.setInterval();
			}
		}
	}
	this.addCronometro = function() {
		imgElement = $('<img/>', {
			src : this.imgCronometro
		});
		$('.banner-timer').append(imgElement);
	}
	this.getNext = function() {
		nextItem = (this.activeItem + 1) <= this.totalBanners - 1 ? this.activeItem + 1 : 0;
		this.activeItem = nextItem;
		return parseInt(nextItem);
	}
	this.setInterval = function() {
		this.interval = setInterval("bannerInstance.animate()", this.tempoCronometro);
	}

	this.goPrev = function() {
		prevItem = (this.activeItem - 1) < 0 ? this.totalBanners - 1 : this.activeItem - 1;
		this.activeItem = prevItem;
		this.animate(prevItem);
	}

	this.animate = function(idx) {
		/*if(this.animating == true) {
		 while(this.animating == true) {
		 continue;
		 }
		 }*/
		this.animating = true;
		clearInterval(this.interval);
		idx = typeof (idx) == 'undefined' ? this.getNext() : idx;
		//window.console.debug(idx);
		$('.banner-timer img').remove();
		$('.banner-list .active').fadeOut('slow', function() {
			$('.banner-navigation .active').removeClass('active');
			$('.banner-list .active').removeClass('active');
			nextElement = $('.banner-list div:eq(' + idx + ')');
			nextLink = $('.banner-navigation a:eq(' + idx + ')');

			nextLink.addClass('active');
			$('.legenda-banner #texto').html(nextElement.attr('title'));
			nextElement.fadeIn('slow', function() {
				nextElement.addClass('active');
				bannerInstance.addCronometro();
				if(bannerInstance.totalBanners > 1) {
					bannerInstance.setInterval();
				}
				bannerInstance.animating = false;
			});
		});
	}
}
