// Atribuição de máscaras
(function($){ $(function(){ $('input:text').setMask(); }); 	})(jQuery);

function mudarFoto(img){
	var caminhoFotoMedia 	= img;
	var caminhoFoto361		= img.replace("245/", "361/");
	var caminhoFotoAmpliada = img.replace("245/", "980/");
	
	$('#img').css({'display':'none'});
	
	$('#img').html('<img id="imgGrande" onmouseover="amplia()" src="'+caminhoFotoMedia+'" alt="khelf" jqimg="'+caminhoFotoAmpliada+'" style="cursor:crosshair;" />');	
	
	$('#img').fadeIn(2000);
	$('#spanimagem2').html('<img src="'+caminhoFoto361+'" />');
}

function Popup(cat, cod){
	window.open('phpAjax/verEstoque.php?cod='+cod+'&categoria='+cat, "Estoque", "height=400px, width=700px");
}

//##########################################################################################################

//Abrir div 

// JavaScript Document

function getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY){
			xScroll = document.body.scrollWidth;
			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) {
			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 = windowWidth;
		else pageWidth = xScroll;
		arrayPageSize = {pageWidth:pageWidth,pageHeight:pageHeight,windowWidth:windowWidth,windowHeight:windowHeight}
		return arrayPageSize;
	}

function getPageScroll(){
		var yScroll;
		if (self.pageYOffset) yScroll = self.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop) yScroll = document.documentElement.scrollTop;
		else if (document.body) yScroll = document.body.scrollTop;
		arrayPageScroll = {yScroll:yScroll};
		return arrayPageScroll;
	}

function abrir(x){
		var divFoto = document.getElementById("foto"+x);
		var Divimagem	 = document.getElementById("imagem"+x);
		var divSombra = document.getElementById("sombra"+x);
		var imagem = document.getElementById("imagem"+x);
		var xScroll = document.body.scrollWidth;
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			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;
		}
		
		divFoto.style.display = "block";
		Divimagem.style.display = "block";
		divSombra.style.display = "block";
		
		var largura = imagem.style.width;
		var altura = imagem.style.height;
	
		var pageSize = getPageSize();
		var pageScroll = getPageScroll();
		if(x == 3 || x == 9){
			
			$('#nome').val('');
			$('#email').val('');
			$('#nome_amigo').val('');
			$('#email_amigo').val('');
			$('#codProduto').val('');
			$('#mensagem').html('');
			
			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (+20)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-30)) / 2);
			if (boxTop > 15){
				divFoto.style.top = boxTop - 60 + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";
			
		}else if(x == 11){
		
			$('#login').val('');
			$('#senha').val('');
			
			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (+20)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-30)) / 2);
			if (boxTop > 15){
				divFoto.style.top = boxTop - 60 + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";
			
		}else if(x == 10){
			$('#nome1').val('');
			$('#nome2').val('');
			$('#nome3').val('');
			$('#email').val('');
			$('#emai2').val('');
			$('#emai3').val('');
			$('#msg').val('');
			$('#mensagem').html('');

			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (+20)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-30)) / 2);
			if (boxTop > 15){
				divFoto.style.top = boxTop - 60 + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";
			
		}else if(x == 22){
			
			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (+20)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-30)) / 2);
			if (boxTop > 15){
				divFoto.style.top = boxTop + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";

		}else if(x == 5 || x == 2){
			
			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (0)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (0)) / 2);
			if (boxTop > 15){
				divFoto.style.top = boxTop + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";	
			
		}else if(x == 12){
			
			document.getElementById("mensagem_news").innerHTML = "&nbsp;";
			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (0)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (300)) / 2);
			
			if (boxTop > 15){
				divFoto.style.top = (boxTop - 170) + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";	
			
		}else if(x == 6 || x == 7){
			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (+20)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-30)) / 2);
			if (boxTop > 15){
				divFoto.style.top = boxTop - 60 + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";
			
		}else if(x == 8){

			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (-90)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-100)) / 2);
			
			if (boxTop > 15){
				divFoto.style.top = boxTop + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";
			
		}else if(x == 16){

			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"")) - 160 / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-40)) / 2);
			
			divFoto.style.top = "35px";
			divFoto.style.left = boxLeft + "px";
			
		}else{
			
			var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura.replace(/px/,"") - (-90)) / 2);
			var boxLeft = ((pageSize.windowWidth - largura.replace(/px/,"") - (-350)) / 2);
			if (boxTop > 15){
				divFoto.style.top = boxTop + "px";
			} else {
				divFoto.style.top = "15px";
			}
			divFoto.style.left = boxLeft + "px";
		
		}	
		divSombra.style.height = (pageSize.pageHeight + 'px');
		divSombra.style.width = (xScroll + 'px');
	}

function fechar(x){
		document.getElementById("foto"+x).style.display = "none";
		document.getElementById("imagem"+x).style.display = "none";
		document.getElementById("sombra"+x).style.display = "none";
	}
	
function envia_amigo(){
	enviando();
	var url   = "envia-amigo.php";

	$.post(url, {
		   			nome: $('#anome').val(),
		   			email: $('#aemail').val(),
		   			nome_amigo: $('#anome_amigo').val(),
		   			email_amigo: $('#aemail_amigo').val(),
		   			msg: $('#msg').val(),
		   			produto: $('#acodProduto').val()
				}, 

				function (response){ 
					$('#anome').val('');
					$('#aemail').val('');
					$('#anome_amigo').val('');
					$('#aemail_amigo').val('');
					$('#msg').val('');
					$('#mensagem').html(response);
				} 
			);
	return false;
}

function enviando(){
	$('#mensagem').html('Enviando...');
}

function envia_login(){
	$.post("login-ajax.php", {
		   			email: $('#login').val(),
		   			senha: $('#senha').val()
				}, 

				function (response){ 
					$('#mensagem_login').html(response);
					//alert(response);
					if(response == "Logon realizado."){
						location.href=('?');
					}
				} 
			);
	return false;
}

function busca(){
	$('#fbusca').submit();	
}

function cadastra_news(){
	url = "cadastra-news.php";

	$.post(url, {
			email_news: $('#email_news').val(),
			nome_news: $('#nome_news').val()
		}, 

		function (response){ 
			$('#mensagem_news').html('<br>'+response);
			$('#email_news').val('');
			$('#nome_news').val('');
			return false;
		} 
	);

	
}

function cleanAcents(str){
	str = str.toUpperCase();
	str = str.replace("Á", "A");
	str = str.replace("É", "E");
	str = str.replace("Í", "I");
	str = str.replace("Ó", "O");
	str = str.replace("Ú", "U");
	str = str.replace("Â", "A");
	str = str.replace("Ê", "E");
	str = str.replace("Ô", "O");
	str = str.replace("Ã", "A");
	str = str.replace("Õ", "O");
	str = str.replace("Ç", "C");
	return str;
}


function envia_news(){
	document.getElementById("news").submit();	
}

function show_correios(){
	var width  = 500;
	var height = 500;
	window.open('http://www.buscacep.correios.com.br/','null','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',top='+((screen.height-height)/2)+',left='+((screen.width-width)/2));
}

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}



