function change_tab(id){
	
	if(id == 4 || id == 5) {
		if (!$.cookie('sid')) {
			$('#login-modal').modal('show');
		} else {
			loadTab(id);
		}
	} else {
		loadTab(id);
	}
}

function loadTab(id) {
	$('.tab_block').attr('class' , 'tab_hide') ;
	$('#0'+id).addClass('tab_block');
	
	$('.logado').show();
	$('.naoLogado').hide();
}

function toggle_tab(id){
	$('#0'+id).toggle();
}


function validaAlerta(ret){
	alert ( ret.msg );
}

function validaPrancheta(is_alerta){
	if(is_alerta){
		if(($("#AlertaReducao").val() == "" || $("#AlertaReducao").val() == 0 ) && $("#AlertaValor").val() == ""){
			
			alert ( 'Por favor, antes de prosseguir, informe o valor correspondente ao alerta ou o percentual de redução!' );
			return false ;
		}
		
		var dados = $("#AlertaAddForm").serialize();	

	} else { 

		var dados = $("#PranchetaAddForm").serialize();
		
	}
	
	jQuery.ajax({
		url:'/alerta/verificaAlerta',
		dataType:'json',
		data:dados,
		type:'POST',
		success:function(result) {
			
			if ( ! result.is_logged ) {

				if ( result.erro_valor ) {
					alert ( result.msg ) ; 
				} else {
					tb_show('Welcome','/Cadastro/cadastroNovo/"+result.alerta+"?iframe=true&width=310&height=320', null);
				}
			} else {
				
				if ( result.saved ) {
			
					load_top();
					alert ( result.msg ) ; 
					
					document.location.reload(true) ; 
				} else {
					alert ( result.msg ) ; 
				} 
			}
		},
		error:function(r) {
		}
	});		

}

function preencheValor (  ) {
	
	valor = $("#AlertaValorExibe").val() ;
	$("#alerta_valor").val( valor );
	
}

function exibeValorPerc ( ) {
	percentual = $("#alerta_reducao").val();
	ckd = $("input[type=radio][name=data\\[Alerta\\]\\[tipo_alerta\\]]:checked").val() ;
	
	if ( percentual > 0 && ckd == 1 ) {
		
		var dados = jQuery("#formAdicionarAlerta").serialize();	
		jQuery.ajax({
			url:'/alerta/getPrecoLoja/',
			dataType:'json',
			data:dados,
			type:'POST',
			success:function(result) {
				$('#alerta_valor').val(result.preco_loja);
				$("#AlertaValorShow").html( result.preco_loja );
			},
			error:function(r) {
			}
		});
	}
}

$(document).ready(function(){
	
//	Controla a exibição de destaque das abas da tela de cotação
	
	//$("#precos").css('background' , 'url("http://images.jacotei.com.br/imgs/layout_2012/itens_lojas2.jpg") no-repeat -0px -75px');
	
				/*
			 *  Simple image gallery. Uses default settings
			 */
	
	$(".link_item_loja").click(function(){
		parent_id = $(this).parent().attr('id');
		position = $(this).parent().position();
		//$(".tabs_cotacao").css('background' , '') ;
		$(".tabs_cotacao").removeClass("menu-active-style");
		//$("#"+parent_id).css('background' , 'url("/imgs/layout_2012/itens_lojas2.jpg") no-repeat -'+Math.round(position.left)+'px '+' -75px');
		$("#"+parent_id).addClass("menu-active-style");
	});
	
	if ( ordenacao != 'undefined' ){
		$("#"+ordenacao).ready(function(){
			$(".link_ordenacao").removeClass('destaque_ordenacao');
			$("#"+ordenacao).attr('class' , 'destaque_ordenacao');
		});
	} else {
		$("#1").attr('class' , 'destaque_ordenacao');
	}
	
	$('.link_ordenacao').click(function () {
		link_ordenar = link_ordenar.replace(/pag=[0-9]*[0-9]&/, "");
		id = $(this).attr("id");
		location.href = link_ordenar + id + '#cotacao';
	});
	
	$("input[type=radio][name=data\\[Alerta\\]\\[tipo_alerta\\]]").click(function() {
		var checked = $("input[type=radio][name=data\\[Alerta\\]\\[tipo_alerta\\]]:checked").val();

		if (checked==1) { 

			$("#campo_valor").hide();
			$("#campo_percentual").show();
			exibeValorPerc() ;
			$("#AlertaReducao").focus();
			
		} else if (checked==2) { 
		
			$("#campo_percentual").hide();
			$("#campo_valor").show();
			$("#AlertaValorExibe").focus();
		}
	});

	exibeValorPerc();

	$('#AlertaValorExibe').mask('#.##0,00', {reverse: true, maxlength: false});

	/* verifica se a url passada é vinda do link reviews com a anchor #review,
     * Se sim exibe a aba com os reviews
     */
    var anchorOne = window.location.hash.substring(1);
    var anchorTwo = window.location.hash.substring(17);
    
    if(anchorOne == 'review'){
    	change_tab(6);
		$('.tabs_cotacao').removeClass("menu-active-style");
		$('#reviews').addClass("menu-active-style");    
    } else if(anchorTwo == 'review'){
		change_tab(6);
		$('.tabs_cotacao').removeClass("menu-active-style");
		$('#reviews').addClass("menu-active-style");
    }
});