/**
 * @autor Alex - Salve
 */


var img_path = 'util/img/prot_ideal/prod/';

var produtos = produtos || {};
produtos.pesquisa = {};

produtos.pesquisa.PrdutoIdeal = function (){
	var boxPassos = $('.boxProtetorIdeal div');
	var objPublicoAlvo=null;
	  var fatorPele;
	var navegacao = {}
	
	boxPassos.not(':first').hide();
	
	$('.boxProtetorIdeal .tomDePele a.bt').click(function(e){
		e.preventDefault();
		boxPassos.hide();
		show_loading('.suporte_loading');
		navegacao.fatorPele = this.rel;
		boxPassos.eq(1).show();
		hide_loading();
	})

	$('a.bt_voltar').click(function(e){
		e.preventDefault();
		if(!$(this).is('.bt_prim')) {
			boxPassos.hide();
			show_loading('.suporte_loading');
			boxPassos.filter(objPublicoAlvo.getBoxAnterior(navegacao)).show();
			hide_loading();
		} else {
			boxPassos.hide();
			show_loading('.suporte_loading');
			boxPassos.filter(':first').show();
			hide_loading();
		}
	})


	$('a.bt_recomecar').click(function(e){
		e.preventDefault();
		navegacao = {};
		boxPassos.hide();
		show_loading('.suporte_loading');
		boxPassos.filter(':first').show();
		hide_loading();
	})



	$('.boxProtetorIdeal .publicoAlvo a.bt').click(function(e){
		e.preventDefault();
		var publico = parseInt(this.rel);
		
		switch(publico) {
			case 1:
			    objPublicoAlvo = new produtos.pesquisa.Crianca(navegacao.fatorPele);
				break;
			case 2: // ADULTO
			case 3: // FAM�LIA: A MESMA COISA DE ADULTO
			    objPublicoAlvo = new produtos.pesquisa.Adulto(navegacao.fatorPele);
				break;
		}
		navegacao.publico = publico;
		boxPassos.hide();
		show_loading('.suporte_loading');
		boxPassos.filter(objPublicoAlvo.getProximoBox(navegacao)).show();
		hide_loading();
	})
	
	
	$('.boxProtetorIdeal .oleosidade a.bt').click(function(e){
		e.preventDefault();
		navegacao.oleosidade = this.rel;
		boxPassos.hide();
		show_loading('.suporte_loading');
		boxPassos.filter(objPublicoAlvo.getProximoBox(navegacao)).show();
		hide_loading();
	})
	
	$('.boxProtetorIdeal .bronzeamento a.bt').click(function(e){
		e.preventDefault();
		navegacao.bronzeamento = this.rel;
		boxPassos.hide()
		show_loading('.suporte_loading');
		boxPassos.filter(objPublicoAlvo.getProximoBox(navegacao)).show();
		hide_loading();
	})
	
	$('.boxProtetorIdeal .esporte a.bt').click(function(e){
		e.preventDefault();
		navegacao.esporte = this.rel;
		boxPassos.hide()
		show_loading('.suporte_loading');
		boxPassos.filter(objPublicoAlvo.getProximoBox(navegacao)).show();
		hide_loading();
	})
	
}

  
  
  //CLASSE CRIAN�A
  produtos.pesquisa.Crianca = function(fatorPele){
	  var arrPassos = new Array('.publicoAlvo');
	  var boxAtual = 0;
	  var totalPassos = arrPassos.length;
	  
	  this.getProximoBox = function(navegacao)
						  {
							  boxAtual++;
							  if(boxAtual < totalPassos){
								  
								  return arrPassos[boxAtual];
							  }else{
															  getProdutoIdeal(navegacao);
															  return '.resultado';
							  }
						  }	
						  
	  this.getBoxAnterior = function(navegacao)
						  {
							  boxAtual--;
							  return arrPassos[boxAtual];
						  }
  }
  
  //CLASSE ADULTO
  produtos.pesquisa.Adulto = function(fatorPele){
	  var boxAtual = 0;	
	  
	  if(fatorPele > 4)
		  var arrPassos = new Array('.publicoAlvo', '.oleosidade','.bronzeamento','.esporte');
	  else
		  var arrPassos = new Array('.publicoAlvo', '.oleosidade','.esporte');
		  
	  var totalPassos = arrPassos.length;
	  
	  this.getProximoBox = function(navegacao)
						  {
							  boxAtual++;
							  if(boxAtual < totalPassos){
								  return arrPassos[boxAtual];
							  }else{
																  getProdutoIdeal(navegacao);
																  return '.resultado';
							  }
						  }
  
  
	  this.getBoxAnterior = function(navegacao)
						  {
							  boxAtual--;
							  return arrPassos[boxAtual];
						  }
  }
  
  
  //BUSCA PRODUTO NO BANCO
  function getProdutoIdeal(navegacao){
	  navegacao.metodo ='getProdutoIdeal';
	  $.ajax({
			  type:"GET", datatype:'json', url:endereco_padrao_ajax, data:navegacao,
			  success:function(retorno)
			  {
					  //alert(dump(retorno));
					  exibe_prot_ideais(retorno);
			  }
	  });
  }
  
  function exibe_prot_ideais(retorno) {//	alert('teste');
	  /*
	  if(!dados) {
		  alert('dados vazios: ' + dump(retorno));
		  return;
	  }
	  */
  
	  //$('.resultado .prods a').hide();
	  var dados = eval(retorno);
	  var qtd = dados.length; // alert(qtd);
	  for (var i=0; i<qtd; i++) {
		  k = i+1;
		  produto = dados[i];
		  nome = produto[0];
		  imagem = img_path + produto[1];
		  pagina = produto[2];
		  
		  // atribui aos links
		  $('.resultado .prods .prod' + k).attr('href', pagina);
		  $('.resultado .prods .prod' + k).attr('title', nome);
		  $('.resultado .prods .prod' + k + ' img').attr('src', imagem);
		  
	  }
	  
	  if(qtd==1) {
		  $('.resultado .prods .prod1').css('left', '50px');
	  } else {
		  $('.resultado .prods .prod1').css('left', '0px');
	  }
	  $('.resultado .prods').show();
  }
  
  
  $('.resultado .prods .prod1 img').load(function() {
	  $('.resultado .prods .prod1').show();
  });
  
  $('.resultado .prods .prod2 img').load(function() {
	  $('.resultado .prods .prod2').show();
  });
  
  
  
  
  produtos.Carrossel = function()
  {
	  
	  var currentPic 	= 0;
	  var totalPics 	= 0;
	  
	  this.parent		= '#gallery';
	  this.picWidth;
	  this.numberPicShow = 1;
	  this.classMask  = '.mask';
	  this.classRow = '.row';
	  this.classItem = 'div.item';
	  this.btnPrevius = '.prev';
	  this.btnNext = '.next';
	  this.opacity = false;
	  
	  
	  this.apply = function()
				  {
					  var step	 	= this.picWidth * this.numberPicShow;
					  var btnPrevius 	= this.btnPrevius;
					  var btnNext 	= this.btnNext;
					  var classRow 	= this.classRow;
					  var parent	= this.parent;
					  var classItem	= this.classItem;
					  
					  totalPics 	= jQuery(this.classRow + ' '+this.classItem,parent).length;
					  rowWidth 	= jQuery(this.classRow,parent).width() * totalPics;
										  var opacity         = this.opacity;
  
					  opacity ? jQuery(classItem,parent).hide().filter(':first').show() : '';
										  
					  jQuery(this.classRow,parent).width(rowWidth);
					  
					  //LISTENER BTN ANTERIOR
					  jQuery(btnPrevius,parent).click(function(){
						  currentPic--;
						  if(currentPic >= 0)
						  {
							  jQuery(classRow,parent).animate({left: '+='+step}, 1000) ;
														  if(opacity)
														  {
															  jQuery(classItem,parent).eq(currentPic + 1).fadeOut(1000);
															  jQuery(classItem,parent).eq(currentPic).fadeIn(100);
														  }
						  }else{
							  currentPic = 0;
						  } 	
						  if (currentPic <= 0) {
							  jQuery(this).addClass('disabled');
						  }
						  jQuery(btnNext,parent).removeClass('disabled');	
						  return false;
					  });
					  
					  //LISTENER BTN PROXIMO
					  jQuery(btnNext,parent).click(function(){
						  currentPic++;
						  if(currentPic < totalPics)
						  {
							  jQuery(classRow,parent).animate({left: '-='+step},1000) ;
														  if(opacity)
														  {
															  jQuery(classItem,parent).eq(currentPic -1).fadeOut(1000)
															  jQuery(classItem,parent).eq(currentPic).fadeIn(1000)
														  }
  
												  }
						  else
						  {
							  currentPic = totalPics -1;
						  }
						  if (currentPic >= totalPics -1) 
							  jQuery(this).addClass('disabled')
						  jQuery(btnPrevius,parent).removeClass('disabled');
						  
						  return false;
					  });
					  
				  }
  }
  
  
		  var util = util || {};
			  util.LiteBox = function()
							  {
  
  
								  this.mask       = '#maskLiteBox';
								  this.boxWindow  = '.boxWindow';
								  this.btnClose   = ".close";
  
								  var boxWin;
								  var masks;
  
								  this.showPopUP = function()
								  {
									 boxWin      = this.boxWindow;
									 masks       = this.mask;
									 
									 scaleMask();
									 scaleWindow();
									 $(masks).fadeIn(1000);
									 $(masks).fadeTo("slow",0.5);
									 $(boxWin).fadeIn(2000);
								  }
  
  
								 var scaleWindow = function()
								 {
									  //armazena a largura e a altura da tela
									  var maskHeight  = $(document).height();
									  var maskWidth   = $(window).width();
  
									  //Define largura e altura do div#mask iguais ás dimensçoes da tela
									  $(masks).css({'width':maskWidth,'height':maskHeight});
								 }
  
								 var scaleMask = function()
								 {
									 //armazena a largura e a altura da janela
									  var winH = $(window).height();
									  var winW = $(window).width();
  
									  //centraliza na tela a janela popup
									  var top = winH/2-$(boxWin).height()/2;
  
									  if(top > 0 ){
										  $(boxWin).css('top',  top);
									  }else{
										  $(boxWin).css('top',  0);
									  }
  
									  $(boxWin).css('left', winW/2-$(boxWin).width()/2);
								 }
  
							  //FECHAR LITEBOX
							  $(this.mask).click(function(){$(boxWin).fadeOut(1000);$(masks).fadeOut(1000);});
							  $(this.btnClose).click(function(e){e.preventDefault();$(boxWin).fadeOut(1000);$(masks).fadeOut(1000);});
  
							  //REDIMENSIONA JANELA
							  $(window).resize(function(){
								  scaleMask();
								  scaleWindow();
							  })
							  
						  }
						  //FIM CLASSE LITEBOX
  

