/**
 * Javascript site Telbras
 *
 * @author Luis Dalmolin <luis@escape.ppg.br> | Escape Criativação
 */


var browser = $.browser.name;

$(function() {
    $('#telefone').setMask('phone');
	
	$('.adicionar-orcamento[title]').colorTip({ color:'black' });
	
	/** 
	 * IE
	 */
	if( browser == 'msie' ) {
		$('#btEnviaBusca').css('top', '10px');
		$('.formulario').css('padding', '3px 5px 0px 8px');
		
		if($.browser.version == '7.0') {
		    $('#espaco-box-esquerda-2, #espaco-box-esquerda-1').html('<p style="height:125px; margin:0px">&nbsp;</p>');
			$('#box-esquerda img').css('position', 'absolute').css('marginBottom', '0px');			
			
			$('#banner table td.fisrt').css('padding', '0px 0px 0px 13px');
			$('#btEnviaBusca').css('top', '3px');
			$('#banner table td.last').css('paddingTop', '52px').css('paddingLeft', '8px');
		}
	
		if($.browser.version == '8.0') {
			$('#container #content #conteudo #box-esquerda img').css('marginBottom', '5px');
		}
	}
	
	/**
	 * Firefox
	 */
	if( browser == 'firefox' ) {
		$('#box-esquerda img').css('marginBottom', '5px');
		$('#container #content #conteudo #box-esquerda img').css('position', 'absolute');
		$('#espaco-box-esquerda-2, #espaco-box-esquerda-1').html('<p style="height:113px">&nbsp;</p>');
		$('#banner table td.last').css('paddingLeft', '4px');
	}
	
	
	/**
	 * Chrome
	 */
	if( browser == 'chrome' )
	    $('.botao-troca img').css('top', '1px').css('marginBottom', '7px');
	
	
	/**
	 * Busca
	 */
	$('#btEnviaBusca').click(function(e) {
        var busca = $('#busca').val();
		if( busca.length < 2 ) {
		    alert('Minímo de 2 caracteres para a busca');
			return false;
		}
    });
	
	
	/**
	 * Adicionando um produto ao orçamento
	 */
	$('.produto-orcamento').click(function(e) {
        var id    = $(this).attr('id').split('-');
		var quant = $('#quantidade').val();
		
		var result = $.ajax({ 
            type  : 'POST', 
			data  : ({ acao : 'inserir' , idProduto : id[1] , quant : quant }), 
			url   :  config.ajax + '_adicionar-item-orcamento.php', 
			async : false, 
			success : function(data) {
				if( data == 'true' ) {
					alert('Produto adicionado ao orçamento com sucesso');
					aumentarTotalOrcamento();
				} else {
					alert('Este produto já está em seu orçamento');
				}		
			}, error : function() {
				alert('Página não encontrada');
			}
		});
    });
	
	/* topo */
	$('#btEpiTroca').hover(
		function() {
			if( $(this).attr('class') != 'atual' )
			    $(this).animate({ top: "-10px" }, 150);
		},		
		function() {
			if( $(this).attr('class') != 'atual' )
    			$(this).animate({ top: "-20px" }, 150);
		}
	);
	
	/* topo */
	$('#btSinalizacaoTroca').hover(
		function() {
			if( $(this).attr('class') != 'atual' )
			    $(this).animate({ top: "-10px" }, 150);
		},		
		function() {
			if( $(this).attr('class') != 'atual' )
    			$(this).animate({ top: "-20px" }, 150);
		}
	);
	
		
	$('.thumb').click(function() {
		$('.galeria').hide();
		var id = $(this).attr('id').split('-');
		id = id[1];
		$('#img-' + id).fadeIn(550);
	});
	
});

function trocaBG(tipo, id) {	
	$('body').css('backgroundImage', 'url('+config.url+'_img/bg-' + tipo + '.png)');
	$('#container').css('backgroundImage', 'url('+config.url+'_img/bg-container-' + tipo + '.png)');
	$('#banner').css('backgroundImage', 'url('+config.url+'_img/banner-' + tipo + '.png)');
	
	$('#btSinalizacaoTroca').attr('src', ''+config.url+'_img/bt-sinalizacao-' + tipo + '.png');
	$('#btEpiTroca').attr('src', ''+config.url+'_img/bt-epi-' + tipo + '.png');
	
	if(id == 'btSinalizacaoTroca') {
		$('#btSinalizacaoTroca').addClass('atual');
		$('#btEpiTroca').removeClass('atual');
		$('#btEpiTroca').css('top', '-20px');
	} else {
		$('#btEpiTroca').addClass('atual');	
		$('#btSinalizacaoTroca').removeClass('atual');
		$('#btSinalizacaoTroca').css('top', '-20px');
	}
	
	
	$.post(config.ajax + '_troca-categoria.php', { acao: 'trocar-categoria', categoria: tipo });
	
	window.setTimeout(function() {
		window.location = config.url + 'home/';
	}, 1000);
	
}

function isInt(x) { 
	var y = parseInt(x); 
	if (isNaN(y))
		return false; 
	return x==y && x.toString()==y.toString(); 
}

function trocaPagina(pag) {
    window.setTimeout(window.location=pag, 5500);	
}

function aumentarTotalOrcamento() {
    var total = parseInt($('.totalItensOrcamento').html());
	total++;
	$('.totalItensOrcamento').html(total);	
}
function diminuirTotalOrcamento() {
    var total = parseInt($('.totalItensOrcamento').html());
	total--;
	$('.totalItensOrcamento').html(total);	
}
function confirmar(msg) {
    if( !confirm(msg) )
	   return false;
}


/* TRAZENDO AS IMAGENS */
function MM_preloadImages() {
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
