$ = function (a) { return document.getElementById(a); };

$_ = getFlashObject = getFlash = function(n) {
	if (navigator.appName.indexOf("Microsoft")!=-1) return window[n]; else return document[n];
};

/*
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
		var df;   // : DocumentFragment
		var r = this.ownerDocument.createRange();

		switch (String(sWhere).toLowerCase()) {  // convert to string and unify case
		case "beforebegin":
			r.setStartBefore(this);
			df = r.createContextualFragment(sHTML);
			this.parentNode.insertBefore(df, this);
			break;

		case "afterbegin":
			r.selectNodeContents(this);
			r.collapse(true);
			df = r.createContextualFragment(sHTML);
			this.insertBefore(df, this.firstChild);
			break;

		case "beforeend":
			r.selectNodeContents(this);
			r.collapse(false);
			df = r.createContextualFragment(sHTML);
			this.appendChild(df);
			break;

		case "afterend":
			r.setStartAfter(this);
			df = r.createContextualFragment(sHTML);
			this.parentNode.insertBefore(df, this.nextSibling);
			break;
		}
	};
}
*/

String.prototype.format = function(casas) {
	var ret = new String('');
	while (ret.length < (casas-this.toString().length)) ret += '0';
	return ret+this;
};

String.prototype.isWhite = function() {
	if (!isVoid(this)) {
		var value = this.replace(/^\s+/m,'').replace(/\s+$/m,'');
		return (value == '');
	}
	return true;
}

/*utilizado no calendario*/
Date.prototype.getString = function() {
	return this.getDate().toString().format(2)+'/'+(this.getMonth()+1).toString().format(2)+'/'+this.getFullYear();
};
/******************************************/


randRange = function(min, max) {
	return Math.floor(Math.random()*(max-min+1))+min;
}



/*ELEMENTOS HTML*/
function hideSelect() {
	var elems = document.getElementsByTagName("select");
	for (var x=0; x<elems.length; x++) elems[x].style.display = "none";
}
function showSelect() {
	var elems = document.body.getElementsByTagName("select");
	for (var x=0; x<elems.length; x++) elems[x].style.display = "inline";
}
/******************************************/



/*EVENTOS*/
function addEvent(obj, evType, fn){ 
	if (obj!=window)obj = $(obj);
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}
function cancelCascataEvento(e) {
	if (e != null) {
		if (is_ie) {
			e.cancelBubble = true;
		} else {
			e.stopPropagation();
		}
	}
};
/******************************************/



/*DIMENSÕES*/
unitsWindow = function() {
	// Variáveis utilizadas no objeto
	var yScroll = new Number(0);		//posição Y do scroll
	var xScroll = new Number(0);		//posição X do scroll
	var wndWidthVis = new Number(0);	//width janela, visível
	var wndHeightVis = new Number(0);	//height janela, visível
	var wndWidthHid = new Number(0);	//width janela, tamanho total visível + invisível
	var wndHeightHid = new Number(0);	//height janela, tamanho total visível + invisível
	
	this.widthVisible = function() {
		return wndWidthVis;
	}

	this.heightVisible = function() {
		return wndHeightVis;
	}

	this.widthTotal = function() {
		return wndWidthHid;
	}

	this.heightTotal = function() {
		return wndHeightHid;
	}

	this.posScrollY = function() {
		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;

		return yScroll;
	}

	this.posScrollX = function() {
		if (self.pageXOffset) xScroll = self.pageXOffset;
		else if (document.documentElement && document.documentElement.scrollLeft) xScroll = document.documentElement.scrollLeft; 
		else if (document.body) xScroll = document.body.scrollLeft; 

		return xScroll;
	}

	this.scrollToTop = function() {
		if (self.pageYOffset) self.pageYOffset = 0;
		else if (document.documentElement && document.documentElement.scrollTop) document.documentElement.scrollTop = 0; 
		else if (document.body) document.body.scrollTop = 0;

		return yScroll;
	}

	this.load = function() {
		if (window.innerWidth) wndWidthVis = window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth) wndWidthVis = document.documentElement.clientWidth;
		else if (document.body) wndWidthVis = document.body.clientWidth;

		if (window.innerHeight) wndHeightVis=window.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight) wndHeightVis=document.documentElement.clientHeight;
		else if (document.body) wndHeightVis=document.body.clientHeight;
	
		wndWidthHid = document.body.clientWidth;

		wndHeightHid = document.body.clientHeight;	
	}
}
/******************************************/



/*FORMULÁRIOS*/
function verifica_email(str_email){
	if (str_email.search(/^[a-zA-Z0-9]+[_a-zA-Z0-9-]*(\.[_a-z0-9-]+)*@[a-z?G0-9]+(-[a-z?G0-9]+)*(\.[a-z?G0-9-]+)*(\.[a-z]{2,4})$/) != -1) return true;
	else return false;
}

//verifica se o campo contém o valor informado e apaga o valor no onfocus e volta o valor informado no onblur caso o campo esteja vazio
//v = valor
//a = acao (1=focus / 0=blur)
//o = campo (this)
function verValor(v,a,o){
	if(a==1)if(o.value==v)o.value='';//focus
	if(a==0)if(o.value=='')o.value=v;//blur
}
/******************************************/



function isVoid(obj) {
	if (obj != null && typeof obj != 'undefined' && obj != "") return false;
	return true;
}

var key = 0;
function soNumeroPress(evento){
	if (key == 0)	{	
		key = evento.keyCode;	
	}	
	if ((key == 8 || key == 13 || key == 9 || key == 71 || key == 46 || key  == 37  || key  == 39) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)) {
		VerifiqueTAB=true; 
		return true;
	} else{
		return false;
	}
}

function soNumeroDown(evento){
	key = evento.keyCode;
	if ((key == 8 || key == 13 || key == 9 || key == 46) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)){
		VerifiqueTAB=true; 
		return true;
	} else {
		return false;
	}
}

VerifiqueTAB=true;
function Mostra(quem, tammax) {
	if ( (quem.value.length == tammax) && (VerifiqueTAB) ) {
		var i=0,j=0, indice=-1;
		for (i=0; i<document.forms.length; i++) {
			for (j=0; j<document.forms[i].elements.length; j++) {
				if (document.forms[i].elements[j].name == quem.name) {
					indice=i;
					break;
				}
			}
			if (indice != -1)
		         break;
		}
		for (i=0; i<=document.forms[indice].elements.length; i++) {
			if (document.forms[indice].elements[i].name == quem.name) {
				while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
						(i < document.forms[indice].elements.length) ) {
							i++;
				}
				document.forms[indice].elements[(i+1)].focus();
				VerifiqueTAB=false;
				break;
			}
		}
	}
}



function openFileIFrame(form, url)
{
	/*
	simula ajax
	abre uma página em iframe e esta página controla o js
	a pagina que utilizar esta funcao deve ter um form multipart com metodo post
	*/
	var anticache = "?anticache=" + new Date().getMilliseconds() + Math.random();
	var ts = new Date().getTime();
	var frame_name = 'frmupload'+ts;

	if (document.all && !window.opera) {
		var html = '<iframe name="'+frame_name+'" src="/media/img/blank.gif" style="width:0px;height:0px;visibility:hidden;"></iframe>';
		//var html = '<iframe name="'+frame_name+'" src="/media/img/blank.gif" style="width:1000px;height:500px;"></iframe>';
		document.body.insertAdjacentHTML('BeforeEnd', html);
	} else {
		var frame = document.createElement('IFRAME');
		frame.name = frame_name;
		frame.width = 10;
		frame.height = 10;
		frame.style.visibility = 'hidden';
		document.body.appendChild(frame);
	}

	var frm = $(form);
	//form.setAttribute('enctype', 'multipart/form-data');
	frm.target = frame_name;
	frm.action = url + anticache;
	frm.submit();
}



var galeriapub = {
	'opened': false,
	'img': '',

	'show': function(foto){
		this.opened = true;
		mensure.load();

		var DS = {x:mensure.widthVisible(), y:mensure.heightTotal()};
		$('blockbg').style.height = DS.y + 'px';
		if (!is_ie) {
			$('blockbg').style.width = (DS.x - 17) + 'px';
		} else {
			$('blockbg').style.width = DS.x + 'px';
		}
		$('blockbg').style.display = 'block';
		
		$('fotoGrande').style.display = "block";
		$('contentfotogrande').style.display = "none";

		this.alinha();
		this.loadImg(foto)
	},

	'alinha' : function(){
		if(!this.opened) return;
		var SC = {x:mensure.posScrollX(), y:mensure.posScrollY()};
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:$('fotoGrande').offsetWidth, y:$('fotoGrande').offsetHeight};
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-SZ.y)/2;
		$('fotoGrande').style.top = (newY+SC.y)+"px";
		$('fotoGrande').style.left = (newX+SC.x)+"px";
		$('fotoGrande').onclick=$('blockbg').onclick=function(){
			galeriapub.hide();
		}
	},

	'hide': function(){
		this.opened = false;
		$('fotoGrande').style.top = "-1000px";
		$('fotoGrande').style.lef = "-1000px";
		$('fotoGrande').style.display = "none";
		$('blockbg').style.display = 'none';
		$('blockbg').onclick=null;
	},

	/******************/

	'tImg':null,
	'loadedImg':false,
	'imgLoad':null,
	'checkLoadImg': function(){
		clearTimeout(this.tImg);
		if(this.loadedImg){
			$('contentfotogrande').src = this.imgLoad.src;
			$('contentfotogrande').style.display = 'block';
			$('contentfotograndeLoading').style.display = 'none';
			this.loadedImg = false;
		}else{
			this.tImg = setTimeout("galeriapub.checkLoadImg()",400);
		}
	},
	'loadImg': function(foto){
		$('contentfotogrande').style.display = 'none';
		$('contentfotograndeLoading').style.display = 'block';

		this.imgLoad = new Image(600,435);
		this.imgLoad.src = foto;
		if(this.imgLoad.complete){
			galeriapub.loadedImg = true;
			this.checkLoadImg();
			return;
		}
		this.imgLoad.onload = function(){
			galeriapub.loadedImg = true;
		}
		this.checkLoadImg();
	}

}


