//Document JavaScritp

var busca = {

	/*
	*	@name: init
	*	@version: 1.0
	*	@author: Danilo Santos
	*	@description: Inicia as configurações e funções responsaveis pela área de busca de lançamentos do site
	*/
	
	init : function()
	{
		$("div.scroll").each(function(){
			busca.configScroll($(this));
		});
		
		$("ul li").each(function(){
			//$("p", this).corner();
		});
		
		busca.configSearch();
		busca.validateSearch();
	},
	
	/*
	*	@name: configScroll
	*	@version: 1.0
	*	@param: object HTMLObject
	*	@author: Danilo Santos
	*	@description: Configura a rolagem horizontal na página de filtro de busca
	*/
	
	configScroll : function(object)
	{
		var counter = 0;
		var liWidth = 0;
		var ulWidth = 0;
		var atual = 0;
		var mover = 0;
		
		$("ul li", object).each(function(){
			liWidth = $(this).css("width");
			
			counter ++;
		});
		
		liWidth = parseInt(liWidth.substr(0, liWidth.length - 2)) + 45;
		
		ulWidth = liWidth * counter;
		
		$("ul", object).css({
			width : ulWidth
		});
		
		$(".seta-esq a", object).css({
			opacity : 0.2,
			filter 	: "alpha(opacity = 20)"
		});
		
		//funções para rolagem do conteudo para a esquerda
		$(".seta-esq a", object).click(function(e){
			e.preventDefault();
			atual = $("div.contentScroll ul", object).css("left");
			
			if(atual == "auto"){
				atual = "0px";
			}
			
			atual = parseInt(atual.substr(0,atual.length - 2));
			
			mover = atual + 207;
			
			if(atual < 0){
				$("div.contentScroll ul", object).animate({
					left: mover
				}, 500, function(){
					if(mover == 0){
						$(".seta-esq a", object).animate({
							opacity : 0.2,
							filter 	: "alpha(opacity = 20)"
						}, 500);
					}
				});
				
				$(".seta-dir a", object).animate({
					opacity : 1.0,
					filter 	: "alpha(opacity = 100)"
				}, 500);
			} 
		});
		
		//funções para rolagem do conteudo para a direita
		$(".seta-dir a", object).click(function(e){
		
			e.preventDefault();
			atual = $("div.contentScroll ul", object).css("left");
			
			if(atual == "auto"){
				atual = "0px";
			}
			
			atual = parseInt(atual.substr(0,atual.length - 2));
			mover = atual - 207;

			if(mover > -(ulWidth - 787)){

				$("div.contentScroll ul", object).animate({
					left: mover
				}, 500, function(){ 
					if((mover - 207) < -(ulWidth - 787)){
						$(".seta-dir a", object).animate({
							opacity : 0.2,
							filter 	: "alpha(opacity = 20)"
						}, 500);
					}
				});
				
				$(".seta-esq a", object).animate({
					opacity : 1.0,
					filter 	: "alpha(opacity = 100)"
				}, 500);
			} 	
		});
	},
	
	configSearch : function()
	{
		$("div.perfil .contentScroll ul li").each(function(){

			$(this).click(function(){
				$("li.selected", $(this).parent()).removeAttr("class");
				
				$(this).attr("class", "selected");
				
				$("#perfil").val($(this).attr("perfil"));
			});

		});
		
		$("div.preco .contentScroll ul li").each(function(){
		

			$(this).click(function(){
				$("li.selected", $(this).parent()).removeAttr("class");
				
				$(this).attr("class", "selected");
				
				$("#preco").val($(this).attr("preco"));
			});

		});
		
		$("div.regiao .contentScroll ul li").each(function(){

			$(this).click(function(){
				$("li.selected", $(this).parent()).removeAttr("class");
				
				$(this).attr("class", "selected");
				
				$("#regiao").val($(this).attr("regiao"));
			});

		});
	},
	
	validateSearch : function()
	{
		$("#search").submit(function(){
			var error = false;
		
			if($("#perfil").val() == ""){
				// mensagem erro
				$("._perfil").css("display", "block");
				error = true;
			}
			
			if($("#preco").val() == ""){
				// mensagem erro
				$("._preco").css("display", "block");
				error = true;
			}
			
			if($("#regiao").val() == ""){
				// mensagem erro
				$("._regiao").css("display", "block");
				error = true;
			}
			
			if(!error){
				return true;
			} else {
				return false;
			}
		});
	}
	
}

$(function(){
	busca.init();
});

