function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
 
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
 
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

jQuery(document).ready(function() {
    jQuery('#mycarousel_').jcarousel({
        auto: 5,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
	$("#abrenewscad").fancybox({
		'width'				: 400,
		'height'			: 250,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	atualizajs();
});


function atualizajs() {
	$("input[type=text][name*=cep]").mask("99999-999");
	$("input[type=text][name*=data]").mask("99/99/9999");
	$("input[type=text][name*=nascimento]").mask("99/99/9999");
	$("input[type=text][name*=_fone_]").mask("(99) 9999-9999");
	
}

function getAjax(url,parametro,localdiv) {
	$.get(url,parametro, function(r){ $("#"+localdiv).html(r); });
}

function marcados(reg) {
	for (count = 0; count < reg.length; count++) {
		if (listapro[reg[count]] == '1') {
			$('#pro_'+reg[count]).attr('checked',true);
		}
	}

}

function enderecoEntrega() {
	if ($('#end_igual').attr('checked')) {
		$('#usu_entcep').val($('#usu_cep').val());
		$('#usu_entendereco').val($('#usu_endereco').val());
		$('#usu_entnumero').val($('#usu_numero').val());
		$('#usu_entcomplemento').val($('#usu_complemento').val());
		$('#usu_entbairro').val($('#usu_bairro').val());
		$('#usu_entcidade').val($('#usu_cidade').val());
		$('#usu_entestado').val($('#usu_estado').val());
	}
}


function checaprod(situacao,valor) {
	var olax = '';
	var tot = 0;
	for (count = 0; count < listapro.length; count++) {
		if (listapro[count] == '1') {
			tot++;
		}
	}
	if (tot>=3 && situacao)	{
		$('#pro_'+valor).attr('checked',false);
		alert("Selecione apenas 3 produtos");
		return true;
	}
	if (situacao==true) {
		listapro[valor] = '1';
	} else {
		delete(listapro[valor]);
	}
	for (count = 0; count < listapro.length; count++) {
		if (listapro[count] == '1') {
			olax = olax+'-'+count;
		}
	}
	$('#usu_desejo').val(olax);
}

function apenasNumero(objCampo,e) {
	var scan	= getKey(e);
	var tecla	= String.fromCharCode(scan);
	var mask	= "0123456789"
	if (scan == 13 || scan == 8 || scan == 0) return true;
	if (mask.indexOf(tecla) == -1) {
		return false;
	}
	return true;
}

function apenasValor(objCampo,e) {
	var scan	= getKey(e);
	var tecla	= String.fromCharCode(scan);
	var mask	= "0123456789."
	if (scan == 13 || scan == 8 || scan == 0) return true;
	if (mask.indexOf(tecla) == -1) {
		return false;
	}
	return true;
}
function getKey(e)
{
	return (window.event) ? event.keyCode : e.which
}

function verificacupom() {
	if ($('#cupom').val().length==18) {
		return true;
	} else {
		alert('Preencha o código do Cupom de Desconto corretamente');
		return false;
	}
}

function enviapedido() {
	if ($('#npedido').val().length<2) {
		alert('Digite o Número do Pedido corretamente');
		$('#npedido').focus();
		return false;
	}
	return true;
}

function enviacep() {
	if ($('#usu_email').val()=='') {
		alert('Digite seu EMAIL corretamente');
		$('#usu_email').focus();
		return false;
	}
	if ($('#cep').val().length!=9) {
		alert('Digite o CEP corretamente');
		$('#cep').focus();
		return false;
	}
	return true;
}

function cadastropessoa(tipo) {
	if (tipo=="fisica")	{
		$("#tdnome").html("Nome Completo");
		$("#tdcpf").html("CPF");
		$("#tdrg").html("RG");
		$("#trdata").show();
		$("#trsexo").show();
	}
	if (tipo=="juridica")	{
		$("#tdnome").html("Raz&atilde;o Social");
		$("#tdcpf").html("CNPJ");
		$("#tdrg").html("Inscrição Estadual");
		$("#trdata").hide();
		$("#trsexo").hide();
	}

}

function trocapadraocad (tipo) {
	if (tipo=='fisica') {
		$('#cadastro').attr('class','cad_fisica');
		$('#titendereco').html('Meu Endere&ccedil;o');
	} else {
		$('#cadastro').attr('class','cad_juridica');
		$('#titendereco').html('Endere&ccedil;o Comercial');
	}
	getAjax('_meiocadastro.php','tipo='+tipo,'meiocadastro');
}

function checaopcproduto() {
	var ar = $('caixaopcao').getElementsByTagName('input');
	var tem = false;
	for(var i=0; i < ar.length; i++) {
		if(ar[i].checked){ 
			tem = true;
			$('opcid').value = ar[i].value;
		}
	}
	if ($('quant').value<1)	{
		alert("A quantidade deve ser maior que zero"); 
		return false; 
	}
	if(tem == false) { 
		alert("Selecione uma opção do produto!"); 
		return false; 
	} 
	return true;
}

function verificacep() {
	if ($('#cep').val().length==9) {
		return true;
	} else {
		alert('Preencha o CEP corretamente');
		return false;
	}
}

function checaopcpagamento() {
	var boolValid = false;
	var sele;
	$('.opcPagamento').each(function() {
		if ( $(this).is(':checked') ) {
			boolValid = true;
			sele = $(this).val();
		}
	});
	if(boolValid == false) { 
		alert("Selecione uma opção pagamento!"); 
		return false; 
	} 
	if ($('#interface_'+sele).val()=='pgto_visa') {
		$('#formPgto').attr('target','mpg_popup');
		mpg_popup = window.open("", "mpg_popup","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,screenX=0,screenY=0,left=0,top=0,width=765,height=540");
		window.location="https://www.musitechinstrumentos.com.br/redirecionacbp.php";
	}
	if ($('#interface_'+sele).val()=='pgto_mastercielo') {
		$('#formPgto').attr('target','mpg_popup');
		mpg_popup = window.open("", "mpg_popup","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,screenX=0,screenY=0,left=0,top=0,width=765,height=540");
		window.location="https://www.musitechinstrumentos.com.br/redirecionacbp.php";
	}
	if ($('#interface_'+sele).val()=='pgto_mastercard') {
		$('#formPgto').attr('target','mpg_popup');
		mpg_popup = window.open("", "mpg_popup","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,screenX=0,screenY=0,left=0,top=0,width=780,height=540");
		window.location="/aguardando_pagamento";
	}
	if ($('#interface_'+sele).val()=='pgto_diners') {
		$('#formPgto').attr('target','mpg_popup');
		mpg_popup = window.open("", "mpg_popup","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,screenX=0,screenY=0,left=0,top=0,width=780,height=540");
		window.location="/aguardando_pagamento";
	}
	if ($('#interface_'+sele).val()=='pgto_itaushop') {
		$('#formPgto').attr('target','mpg_popup');
		mpg_popup = window.open('', 'mpg_popup', 'toolbar=yes,menubar=yes,resizable=yes,status=no,scrollbars=yes,width=675,height=485');	
		window.location="/aguardando_pagamento";
	}
	if ($('#interface_'+sele).val()=='pgto_caixadeb') {
		$('#formPgto').attr('target','mpg_popup');
		mpg_popup = window.open('', 'mpg_popup', 'toolbar=yes,menubar=yes,resizable=yes,status=no,scrollbars=yes,width=675,height=485');	
		window.location="/aguardando_pagamento";
	}

	return true;
}

function popWin(URL,NAME,WIDTH,HEIGHT,RESIZE,SCROLL) {
   winId = window.open(URL,NAME,'width='+WIDTH+',height='+HEIGHT+',resizable='+RESIZE+',scrollbars='+SCROLL+',menubar=no,toolbar=no,location=no,directories=no,status=no');
	var clientWidth = screen.availWidth;
	var clientHeight = screen.availHeight;
	var xPos = (clientWidth - WIDTH)/2;
	var yPos = (clientHeight - HEIGHT)/2;
	winId.moveTo(xPos,yPos);
}

var arrPageSizes = new Array();
var arrPageScroll =  new Array();

function ___getPageSize(){var xScroll,yScroll;if(window.innerHeight&&window.scrollMaxY){xScroll=window.innerWidth+window.scrollMaxX;yScroll=window.innerHeight+window.scrollMaxY;}else if(document.body.scrollHeight>document.body.offsetHeight){xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight;}else{xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight;}
var windowWidth,windowHeight;if(self.innerHeight){if(document.documentElement.clientWidth){windowWidth=document.documentElement.clientWidth;}else{windowWidth=self.innerWidth;}
windowHeight=self.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){windowWidth=document.documentElement.clientWidth;windowHeight=document.documentElement.clientHeight;}else if(document.body){windowWidth=document.body.clientWidth;windowHeight=document.body.clientHeight;}
if(yScroll<windowHeight){pageHeight=windowHeight;}else{pageHeight=yScroll;}
if(xScroll<windowWidth){pageWidth=xScroll;}else{pageWidth=windowWidth;}
arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;}
function ___getPageScroll(){var xScroll,yScroll;if(self.pageYOffset){yScroll=self.pageYOffset;xScroll=self.pageXOffset;}else if(document.documentElement&&document.documentElement.scrollTop){yScroll=document.documentElement.scrollTop;xScroll=document.documentElement.scrollLeft;}else if(document.body){yScroll=document.body.scrollTop;xScroll=document.body.scrollLeft;}
arrayPageScroll=new Array(xScroll,yScroll);return arrayPageScroll;}

function fechaJAN() {
	$('#jquery-conteudo').html('');
	$('#jquery-overlay,#jquery-lightbox,#jquery-conteudo').fadeOut(function(){$('#jquery-overlay,#jquery-lightbox').remove();});$('embed, object, select').css({'visibility':'visible'});
}

function abreJANx() {
	$('embed, object, select').css({'visibility':'hidden'});
	$('body').append('<div id="jquery-overlay"></div>');
	arrPageSizes = ___getPageSize();
	arrPageScroll = ___getPageScroll();
	$('#jquery-overlay').css({backgroundColor:'#000',opacity:0.8,width:arrPageSizes[0],height:arrPageSizes[1]}).fadeIn();

	$('body').append('<div id="jquery-lightbox"></div><div id="jquery-conteudo"></div>');
	$('#jquery-overlay').click(function(){ fechaJAN(); });
	$('#jquery-lightbox').css({width:'620px',height:'600px',top:arrPageScroll[1]+(arrPageSizes[3]/10),left:(arrPageSizes[2]/2)-3100}).show();
	$('#jquery-conteudo').css({width:'600px',height:'500px',top:(arrPageScroll[1]+(arrPageSizes[3]/10))+20,left:(arrPageSizes[2]/2)-300}).show();
	$.get("/revenda_cad.php", function(r){ $("#jquery-conteudo").html(r); });	
}


function enviaIndisponivel() {
	if ($('#ind_nome').val()=='') {
		alert("Preencha seu nome");
		$('#ind_nome').focus();
		return false;
	}
	if ($('#ind_email').val()=='') {
		alert("Preencha seu E-mail");
		$('#ind_email').focus();
		return false;
	}

	var dados = $('#formDisponivel').serialize();
	getAjax('/_sendavisoproduto.php',dados,'resultadoindisponivel');
	return false;
}

function enviaConsultenos() {
	if ($('#con_nome').val()=='') {
		alert("Preencha seu nome");
		$('#con_nome').focus();
		return false;
	}
	if ($('#con_email').val()=='') {
		alert("Preencha seu E-mail");
		$('#con_email').focus();
		return false;
	}

	var dados = $('#formConsulte').serialize();
	getAjax('/_sendavisoproduto.php',dados,'resultadoconsulte');
	return false;
}

function enviaAmigo() {
	if ($('#env_amigonome').val()=='') {
		alert("Preencha o Nome do amigo");
		$('#env_amigonome').focus();
		return false;
	}
	if ($('#env_amigoemail').val()=='') {
		alert("Preencha o E-mail do amigo");
		$('#env_amigoemail').focus();
		return false;
	}
	if ($('#env_meunone').val()=='') {
		alert("Preencha seu Nome");
		$('#env_meunone').focus();
		return false;
	}

	var dados = $('#formAmigo').serialize();
	getAjax('/_sendavisoproduto.php',dados,'caixaamigo');
	return false;
}

function ComprarJunto() {
	$("form#formCompreJunto").submit();
	return true;
}

function VerificarCompreJunto() {
	var itensVendaCasada = $(".chkVendaCasada:checked");
	if (itensVendaCasada.length > 0) {
		$("#spaCompreJunto").html("<a id=\"btnCompreJunto\" href=\"javascript:ComprarJunto();\" title=\"Comprar Junto\" ><img src=\"/img/btn_comprar_junto.gif\" /></a>");
	} else {
		$("#spaCompreJunto").html("<a id=\"btnCompreJunto\" disabled=\"disabled\" href=\"javascript:void(0)\"><img src=\"/img/btn_comprar_junto_off.gif\" /></a>");
	}
}


/*******************
* Método: scrollTo
* Finalidade: acessa uma âncora, rolando a página dinamicamente
*********************/
$.getPos = function (e)
{
    var l = 0;
    var t  = 0;
    var w = $.intval($.css(e,'width'));
    var h = $.intval($.css(e,'height'));
    var wb = e.offsetWidth;
    var hb = e.offsetHeight;
    while (e.offsetParent){
        l += e.offsetLeft + (e.currentStyle?$.intval(e.currentStyle.borderLeftWidth):0);
        t += e.offsetTop  + (e.currentStyle?$.intval(e.currentStyle.borderTopWidth):0);
        e = e.offsetParent;
    }
    l += e.offsetLeft + (e.currentStyle?$.intval(e.currentStyle.borderLeftWidth):0);
    t  += e.offsetTop  + (e.currentStyle?$.intval(e.currentStyle.borderTopWidth):0);
    return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};

$.getClient = function(e)
{
    if (e) {
        w = e.clientWidth;
        h = e.clientHeight;
    } else {
        w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
        h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
    }
    return {w:w,h:h};
};

$.getScroll = function (e) 
{
    if (e) {
        t = e.scrollTop;
        l = e.scrollLeft;
        w = e.scrollWidth;
        h = e.scrollHeight;
    } else  {
        if (document.documentElement && document.documentElement.scrollTop) {
            t = document.documentElement.scrollTop;
            l = document.documentElement.scrollLeft;
            w = document.documentElement.scrollWidth;
            h = document.documentElement.scrollHeight;
        } else if (document.body) {
            t = document.body.scrollTop;
            l = document.body.scrollLeft;
            w = document.body.scrollWidth;
            h = document.body.scrollHeight;
        }
    }
    return { t: t, l: l, w: w, h: h };
};

$.intval = function (v)
{
    v = parseInt(v);
    return isNaN(v) ? 0 : v;
};

$.fn.ScrollTo = function(s) {
    o = $.speed(s);
    return this.each(function(){
        new $.fx.ScrollTo(this, o);
    });
};

$.fx.ScrollTo = function (e, o)
{
    var z = this;
    z.o = o;
    z.e = e;
    z.p = $.getPos(e);
    z.s = $.getScroll();
    z.clear = function(){clearInterval(z.timer);z.timer=null};
    z.t=(new Date).getTime();
    z.step = function(){
        var t = (new Date).getTime();
        var p = (t - z.t) / z.o.duration;
        if (t >= z.o.duration+z.t) {
            z.clear();
            setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
        } else {
            st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
            sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
            z.scroll(st, sl);
        }
    };
    z.scroll = function (t, l){window.scrollTo(l, t)};
    z.timer=setInterval(function(){z.step();},13);
};




function retornava() {
	var boolValid = false;
	var radioValue;
	$('.opccomenta').each(function() {
		if ( $(this).is(':checked') ) {
			boolValid = true;
			radioValue = $(this).val();
		}
	});
	if(boolValid == false) { 
		alert("Selecione a Avaliação");
		return false; 
	} 

	if ($('#ava_texto').val()=='') {
		alert("Preencha seu Comentário");
		$('#ava_texto').focus();
		return false;
	}
	if ($('#ava_nome').val()=='') {
		alert("Preencha seu Nome");
		$('#ava_nome').focus();
		return false;
	}
	if ($('#ava_email').val()=='') {
		alert("Preencha seu Email");
		$('#ava_email').focus();
		return false;
	}
	if ($('#ava_cidade').val()=='') {
		alert("Preencha sua Cidade");
		$('#ava_cidade').focus();
		return false;
	}
	if ($('#ava_estado').val()=='') {
		alert("Preencha seu Estado");
		$('#ava_estado').focus();
		return false;
	}
	getAjax('/_comenta.php','proid='+$('#proid').val()+'&actc=insert&ava_valor='+radioValue+'&ava_texto='+$('#ava_texto').val()+'&ava_nome='+$('#ava_nome').val()+'&ava_email='+$('#ava_email').val()+'&ava_cidade='+$('#ava_cidade').val()+'&ava_estado='+$('#ava_estado').val(),'comentalist');
}	
