$(function(){
	//VERIFICA A COR DO TEXTO NO CSS
	var corSegmento = $('h3#titulo001').css("color");
	//REPASSA EM HEXADECIMAL AO FIREFOX
	if ( ($.browser.mozilla)){
		var r = /(\d+), (\d+), (\d+)/;
		var cores = r.exec(corSegmento);
		var red = tratarHexa(new Number(cores[1]).toString(16));
		var green = tratarHexa(new Number(cores[2]).toString(16));
		var blue = tratarHexa(new Number(cores[3]).toString(16));
		corSegmento = red+green+blue;
	}
	else
	{
		corSegmento = corSegmento.substr(1);
	};
	//CRIA TITULOS
	var titulo01 = $('h3#titulo001');
	var titulo02 = $('h3#titulo002');
	var titulo03 = $('h3#titulo003');
	var titulo1 = new Titulo( titulo01[0], titulo01.html(), corSegmento, "18", "false", "none", 200, 23, '' );
	var titulo2 = new Titulo( titulo02[0], titulo02.html(), corSegmento, "18", "true", "none", 300, 23, '' );
	var titulo3 = new Titulo( titulo03[0], titulo03.html(), 'b7b8b7', "18", "false", "none", 300, 23, '' );
	
	function tratarHexa( num )
	{
		return (num.length==2)?(num):('0'+num);
	}
	
	//Atualiza item
	$.calculaItemTotal();

	//CAPTURA EVENTO NAS SETAS
	/*$('div#selecionaAnterior').click( function(){ $.selecionaAnterior(); return false;});
	$('div#selecionaProximo').click( function(){ $.selecionaProximo(); return false;});
	*/
	
	// jCarouselLite
	$("#carrossel").jCarouselLite({
		btnNext: "div#selecionaProximo",
		btnPrev: "div#selecionaAnterior",
		circular: false
	});
	
	//itemInicial & itemFinal
	qtdeItemTotal < 4 ? $('p#contadorItens').hide() : $('p#contadorItens').show();

	$('div#selecionaAnterior').click( function(){
		if ( itemInicial <= 1 ) { return false; }
		$('span#itemInicial').html( itemInicial-1 );
		$('span#itemFinal').html( itemFinal-1 );
		itemInicial = parseInt( $('span#itemInicial').html() );
		itemFinal = parseInt( $('span#itemFinal').html() );
	});

	$('div#selecionaProximo').click( function(){
		if ( itemInicial >= qtdeItemTotal - qtdeItens ) { return false; }
		$('span#itemInicial').html( itemInicial+1 );
		$('span#itemFinal').html( itemFinal+1 );
		itemInicial = parseInt( $('span#itemInicial').html() );
		itemFinal = parseInt( $('span#itemFinal').html() );
	});
});

$(window).bind('load', function(){
// NICHOS
	var objFormDestinos = $('form#frmDestinos');
	objFormDestinos.children('input#query').val("Todos");
	var destinos = new $.formDestino(objFormDestinos, objFormDestinos.find('select.selNichos'), objFormDestinos.find('input.btnNichoSubmit'), objFormDestinos.find('select#selDestinos'), objFormDestinos.find('a.btnDestinoSubmit'));
	destinos.loadDestinos();
	destinos.submitNichos();
});

$.calculaItemTotal = function(){
	window.qtdeItemTotal = 0;
	window.qtdeItens = 2;
	window.itemInicial = 1;
	window.itemFinal = itemInicial + qtdeItens;
	$('div#papeisParede ul li').each(function(){
		qtdeItemTotal += 1;
	});
	$('span#itemInicial').html( itemInicial );
	$('span#itemFinal').html( itemFinal );
	$('span#itemTotal').html( qtdeItemTotal );
}

$(function(){
   //VERIFICA RESOLUÇÂO ATUAL DA TELA
   var tela = screen.width + " x " + screen.height;
   $('#resolucaoAgora span').html(tela);
   
});

/* ////////////////// */
// Tratamento de submit para form de Nichos e Destinos
$.formDestino = function(formDestinos, selNicho, btnSubmitNicho, selDestinos, btnSubmitDestino){
	this.formDestinos = formDestinos;
	this.selNicho = selNicho;
	this.btnSubmitNicho = btnSubmitNicho;
	this.selDestinos = selDestinos;
	this.btnSubmitDestino = btnSubmitDestino;
	this.semaforoIndex = 4;
}

$.formDestino.prototype.semaforo = function(param){
	this.semaforoIndex--;
	switch(this.semaforoIndex){
		case 3:
				this.loadDestinos(param);
			break;
		case 2:
				this.showDestinos(param);
			break;
		case 1:
				this.submitDestinos();
			break;
	}
}

$.formDestino.prototype.submitNichos = function(){
	var ref = this;

	/*this.btnSubmitNicho.click(function(){
		ref.semaforoIndex = 4;

		ref.semaforo(ref.selNicho.val());

		return false;
	});*/
	this.selNicho.change(function(){
		ref.semaforoIndex = 4;
		this.selOpt = this.options[this.selectedIndex];

		if(this.selOpt.value == ""){

			ref.selDestinos.html("<option>"+this.selOpt.innerHTML+"</option>")

		} else {

			$("input#query").val(this.selOpt.value);

			ref.semaforo(ref.selNicho.val());
		}
		return false;
	});

}

$.formDestino.prototype.loadDestinos = function(nicho){
	var ref = this;
	var ajaxURL = this.formDestinos.attr('action');
	var ajaxType = this.formDestinos.attr('method');
	var ajaxData = "query=" + this.formDestinos.children('input#query').val() + "&field=" + this.formDestinos.children('input#field').val() + "&diretorio=" + this.formDestinos.children('input#diretorio').val() + "&index=" + this.formDestinos.children('input#index').val();

	var ajaxOptions = {
		type: ajaxType,
		url: ajaxURL,
		dataType: 'xml',
		data: ajaxData,
		complete: function(xhr){
			ref.semaforo(xhr);
		}
	}

	$.ajax(ajaxOptions);
}

$.formDestino.prototype.showDestinos = function(xhr){
	var ref = this;

	this.selDestinos.children('option').remove();

	$(xhr.responseXML).find('nicho').children('destino').each(function(i){
		var $this = $(this);

		ref.selDestinos.append('<option value="' + $this.attr('url') + '">' + $this.text() + '</option>');
	});

	ref.semaforo();
}

$.formDestino.prototype.submitDestinos = function(){
	var ref = this;

	this.selDestinos.dblclick(function(){
		window.location.href = ref.selDestinos.val();

		return false;
	});

	this.btnSubmitDestino.click(function(){
		window.location.href = ref.selDestinos.val();

		return false;
	});
}
