var urlBase = location.protocol + '//' + location.host + '/';
if(location.host == 'www'){
	var pos = location.pathname.indexOf('/', 1);
	urlBase += location.pathname.substr(1, pos);
} else {
	//urlBase += 'novo/';
}



function abrir(url,width,height) {
//	width +=18;
	//height +=22;
	var left = ((screen.width)/2)-(width/2);
	var top = ((screen.height)/2)-(height/2);

	window.open(urlBase + "popup.php?foto="+url,'janela', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

function abrirGrande(url,width,height,titulo) {
//	width +=18;
	//height +=22;
	var left = ((screen.width)/2)-(width/2);
	var top = ((screen.height)/2)-(height/2);

	window.open(urlBase + "popup_foto.php?foto=" + url + '&titulo=' + titulo,'janela', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

jQuery(document).ready(externalLinks);

String.prototype.trim = function(c, t){
    return c = "[" + (c == undefined ? " " : c.replace(/([\^\]\\-])/g, "\\\$1")) + "]+",
    this.replace(new RegExp((t != 2 ? "^" : "") + c + (t != 1 ? "|" + c + "$" : ""), "g"), "");
};

isDate = function(y, m, d){
		if(!m){
			m = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
		}
		if(!d){
			d = {d: 3, m: 2, y: 1};
		}
        if(typeof y == "string" && m instanceof RegExp && d){
            if(!m.test(y)) return 1;
            y = RegExp["$" + d.y], m = RegExp["$" + d.m], d = RegExp["$" + d.d];
        }
        d = Math.abs(d) || 0, m = Math.abs(m) || 0, y = Math.abs(y) || 0;
        return d < 1 || d > 31 ? 2 : m < 1 || m > 12 ? 3 : /4|6|9|11/.test(m) && d == 31 ? 4
        : m == 2 && (d > ((y = !(y % 4) && (y % 1e2) || !(y % 4e2)) ? 29 : 28)) ? 5 + !!y : 0;
};

String.prototype.isCPF = function(){
    var c = this;
    if((c = c.replace(/[^\d]/g,"").split("")).length != 11) return false;
    if(new RegExp("^" + c[0] + "{11}$").test(c.join(""))) return false;
    for(var s = 10, n = 0, i = 0; s >= 2; n += c[i++] * s--);
    if(c[9] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    for(var s = 11, n = 0, i = 0; s >= 2; n += c[i++] * s--);
    if(c[10] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    return true;
};

String.prototype.isCNPJ = function(){
    var b = [6,5,4,3,2,9,8,7,6,5,4,3,2], c = this;
    if((c = c.replace(/[^\d]/g,"").split("")).length != 14) return false;
    for(var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
    if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    for(var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
    if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
    return true;
};

String.prototype.isEmail = function(){
	return /^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i.test(this);
}

String.prototype.pad = function(l, s, t){
    return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
        + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
        + this + s.substr(0, l - t) : this;
};

function externalLinks() {
	if (!document.getElementsByTagName) 
		return;

	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "externo")
			anchor.target = "_blank";
	}

	jQuery("a").each(function(){
		if(this.rel.toLowerCase() == 'lightbox'){
			$(this).click(function(){		
				largura = this.href.substring(this.href.indexOf("largura=")+8,this.href.indexOf("&altura"));
				altura = this.href.substring(this.href.indexOf("altura=")+7,this.href.length);					
				abrir(this.href, largura, altura);
				return false;
			});
		};

		if(this.rel.toLowerCase().match('ecard')){
			$(this).click(function(){
				abrir(this.href,500,400)
				return false;
			});
		}

		if(this.rel.toLowerCase().match('lightbox-large')){
			$(this).click(function(){
				abrirGrande(this.href, 550, 400, $(this).attr('title'))
				return false;
			});
		}
	});
}

// FUNÇÃO PARA MOSTRAR DIV
var varAtivado = false
			
function mostraConteudo(id){
	if (document.getElementById(id).style.display=='') {
		document.getElementById(id).style.display='none';
		varAtivado = false;
	} else {
		document.getElementById(id).style.display='';
		varAtivado ? document.getElementById(varAtivado).style.display='none' : null;
		varAtivado = id;
	}
}

Number.prototype.toMoney = function(c, d, t){
	var n = this;
	var negative = parseFloat(n) < 0;
    var m = (
		c = Math.abs(c) + 1 ? 
			c 
		: 
			2, 
		d = d || ",", 
		t = t || ".",
        /(\d+)(?:(\.\d+)|)/.exec(n + "")
	), 
	x = m[1].length > 3 ? 
		m[1].length % 3 
	: 
		0;
    return (
		negative ?
			'-'
		:
			'') 
		+ (x ? 
			m[1].substr(0, x) + t 
		: 
			"") 
		+ m[1].substr(x).replace(/(\d{3})(?=\d)/g, "$1" + t) 
		+ (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
};