﻿
var intervalo = null;

$(document).ready(function() {

	var div = "";
	var largura = 642 / 6;
	var altura = 225 / 3;

	var tabela = "<table cellpadding='0' cellspacing='0'>";
	for(var posY = 0; posY < 3; posY++) {
		tabela += "<tr>";
		for(var posX = 0; posX < 6; posX++) {
			var divName = "img_" +((posY * 6) + posX);
			tabela += "<td id='"+divName+"'>"+divName+"</td>";
		}
		tabela += "</tr>";
	}
	tabela += '</table>';
	tabela += '<a href="'+$('#referencia_banner li img:first').attr('alt')+'" target="_blank" id="link_banner_home">&nbsp</a>';

	$('#banner_home').html(tabela);
	

	for(posY = 0; posY < 3; posY++) {
		for(posX = 0; posX < 6; posX++) {
			divName = "img_" +((posY * 6) + posX);
			$('#'+divName).html("<div class='" + divName + "'></div>");
			$('.'+divName).css( {
				'float': 'left',
				'width': largura,
				'height': altura,
				'z-index': ((posY * 6) + posX)
			});
			$('.'+divName).css('background', 'url("'+ $('#referencia_banner li img:first').attr('src') +'") ' + (- posX * largura) + "px " + (- posY * altura)+ "px");
		}
	}

	$('.arrowRight').click(function() {
		getValorSeta(1);
		alteraPapel();
	});

	$('.arrowLeft').click(function() {
		getValorSeta(-1);
		alteraPapel();
	});

	$('#banners_list li').live('click',function() {
		atual = $(this).attr('title') - 1;
		//console.log(atual);
		alteraPapel();
	});
	
	carregaImagens();
	intervalo = setInterval(random, 10000);
});
var imagens = Array();
var linkImg = Array();
var atual = 0;
var total;

function random() {
	getValorSeta(1);
	alteraPapel();
}


function carregaImagens() {
	$('#banners_list').html('');
	var i = 1;
	$('#referencia_banner li img').each(function() {
		imagens.push($(this).attr('src'));
		linkImg.push($(this).attr('alt'));
		$('#banners_list').append('<li title="'+ i + '">' + (i++) + '</li>');
	});
	$('#banners_list li:first').attr('class', 'active');
	total = imagens.length;
}

function getValorSeta(incremento) {
	atual = atual + incremento;
	if(atual < 0) {
		atual = total-1;
	}
	else if(atual >= total) {
		atual = 0;
	}
}

function fisherYates ( myArray ) {
	var i = myArray.length;
	if ( i == 0 ) return false;
	while ( --i ) {
		var j = Math.floor( Math.random() * ( i + 1 ) );
		var tempi = myArray[i];
		var tempj = myArray[j];
		myArray[i] = tempj;
		myArray[j] = tempi;
	}
}

function alteraPapel() {

	clearInterval(intervalo);

	$('#banners_list .active').attr('class', '');
	$('#banners_list li[title='+(atual+1)+']').attr("class", 'active');
	var listaItens = [];
	for(var posY = 0; posY < 3; posY++) {
		for(var posX = 0; posX < 6; posX++) {
			var valorDiv = ((posY * 6) + posX);
			listaItens.push(valorDiv);
		}
	}
	if(linkImg[atual]) {
		$('#link_banner_home').css('display','block');
		$('#link_banner_home').attr('href', linkImg[atual]);
	}
	else {
		$('#link_banner_home').css('display','none');
	}
	fisherYates(listaItens);
	var delay = 0;
	for(divName in listaItens) {
		delay = 40 * divName;
		$('.img_'+listaItens[divName]).delay(delay).fadeOut(500, function() {
			$(this).css('background-image', "url('" + imagens[atual]+ "')");
			$(this).fadeIn(500);
		});
	}
	intervalo = setInterval(random, 5000);
}
