// FS Framework - http://www.fazedordesite.com
// ALIAS
function $id() {
  var elements = new Array();
  var totArgs = arguments.length;
  for (var i = 0; i < totArgs; i++) {
    if (typeof arguments[i] == 'string') arguments[i] = document.getElementById(arguments[i]);
    if (arguments.length == 1) return arguments[i];
    elements.push(arguments[i]);
  }
  return elements;
}
function fsClass(){
    return function() {
      this.initialize.apply(this, arguments);
	}
}
var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}
function $tag(t){
	return document.getElementsByTagName(t);
}
// EVENT
function addEvent(object, eventType, doIt){
	if(object.addEventListener){ // ALL BROWSERS
		object.addEventListener(eventType, doIt, false); 
		return true;
	} else if (object.attachEvent){ // IE	
		var r = object.attachEvent('on'+eventType, doIt);
		return r;
	} else 	return false;
}
// ================
var $obj = {
	create: function(type, id, parentObj,formType){
		this.newObj = document.createElement(type);
		if(id) this.newObj.setAttribute('id',id);
		if(formType) this.newObj.type=formType;
		if(type=="form" || type=="iframe" || formType) if(id) this.newObj.setAttribute('name',id);
		
		parentObj.appendChild(this.newObj);
	},
	clone: function(orObj,cloneObj,cloneType){
		this.dimension = Array();
		this.position = Array();
		this.dimension = this.getDimension(orObj);
		this.position = this.getPos(orObj);
		$obj.create(cloneType,cloneObj,$tag("body").item(0));
		$id(cloneObj).style.position='absolute';
		$id(cloneObj).style.left=this.position[1]+"px";
		$id(cloneObj).style.top=this.position[0]+"px";
		$id(cloneObj).style.width=this.dimension[1]+"px";
		$id(cloneObj).style.height=this.dimension[0]+"px";
		$id(cloneObj).style.zIndex=999;
	},
	opacity: function(obj,value){
		var bsv = value/100; 
		$id(obj).style.opacity=bsv;
		$id(obj).style.MozOpacity=bsv;
		$id(obj).style.filter="alpha(opacity="+value+")";
	},
	getPos: function(obj){
		if($id(obj)){
			obj = $id(obj);
			var top=obj.offsetTop;
			objY=obj;
			while((objY=objY.offsetParent) != null) top+=objY.offsetTop;
			var left=obj.offsetLeft;
			objX=obj;
			while((objX=objX.offsetParent)!=null) left+=objX.offsetLeft;
			var position=Array(top,left);
			return position;
		} else return Array("ERROR:","Object '"+obj+"' not found");
	},
	getDimension: function(obj){
		if($id(obj)){
			obj = $id(obj);
			var height=obj.offsetHeight;
			var width=obj.offsetWidth;
			var dimension=Array(height,width);
			return dimension;
		} else return Array("ERROR:","Object '"+obj+"' not found");
	}
}
// ==========================================
// AJAX
function ajax(url, div, info, method, formID, script, func){
	this.url = url; // content url
	this.method = (method) ? method : 'GET'; // method GET or POST, by standard  "GET"
	this.div = div; // content destination
	var w;
	if (div=='div_central')
		w = 929;
	else w = 717;
	this.info = (info) ? "<div class='carregando'><table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0'><tr><td align='center' valign='middle'><table border='0' cellpadding='20' cellspacing='0'><tr><td ALIGN='center'><img src='images/icon_carrega.gif'  align='absmiddle' /><br>"+info+"</td></tr></table></td></tr></table></div>" : 'loading'; // while loading
	this.formID = formID; // form ID is required for post requests only
	this.script = (script==0 || script==1) ? script : 1;
	this.func = func;
}
ajax.prototype = {
    connect: function(){
		if(!this.url) return;
        this.xmlHttp = null;
		if( window.XMLHttpRequest ) this.xmlHttp = new XMLHttpRequest(); // All browsers
		else if( window.ActiveXObject){ // IE
			try{
				this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e){
				try{
					this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if(this.xmlHttp != null && this.xmlHttp != undefined ){
			var object = this;
			this.xmlHttp.onreadystatechange = function(){ object.getState.call(object); }
			
			if(this.method=="GET") this.executeGET(); // get request
			else this.executePOST() // post request		
		}
	},
	getState: function(){
		if(this.xmlHttp.readyState == 4 ){
			this.result(this.xmlHttp.responseText);
			if(this.script) this.executeScripts(this.xmlHttp.responseText);
			if(this.func!=null) eval(this.func);
		} else this.loading();
	},
	executeGET: function(){
		this.xmlHttp.open(this.method,this.url, true);
		this.xmlHttp.send(null);
		
	},
	executePOST: function(){
		var fields = "";
		if(!this.formID) alert("Erro: falta indicar FORM");
		for(i=0;i<$id(this.formID).length;i++){
			fields+=$id(this.formID)[i].name+"="+$id(this.formID)[i].value+"&";
		}
		this.xmlHttp.open(this.method,this.url, true);
		this.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.xmlHttp.send(fields);		
	},	
	executeScripts: function(text){
		var ini = 0;
		while (ini!=-1){
			ini = text.indexOf('<script', ini);
			if (ini >=0){
				ini = text.indexOf('>', ini) + 1;
				var fim = text.indexOf('</script>', ini);
				text = text.substring(ini,fim);
				eval(text);
			}
		}
	},
    loading: function(){
		this.result(this.info)
	},           
	result: function(r){ $id(this.div).innerHTML= r; }
}
function fsAjax(url, div, info, method, formID, script,func){
//	alert(div);
	var requestContent = new ajax(url, div, info, method, formID, script,func);
	requestContent.connect();
}

/* END OF AJAX */ 

// =======================================================

// BELLOW HERE WILL BE IN SEPARETED FILES IN FUTURE

// FORM VALIDATION
var validation = {
	Email: function(mail){
		var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
		if(typeof(mail) == "string"){
			if(er.test(mail)) return true;
		}else if(typeof(mail) == "object"){
			if(er.test(mail.value)){
				return true;
			}
		}else return false;
	}
}
// EFFECTS

var effects = {
	colorMask: function(color, obj, objOpacity){
		if(!$id("fsMask")) $obj.clone("imagem","fsMask","div");
		$id("fsMask").style.background=color;
		$obj.opacity("fsMask",objOpacity);		
	},
	fade: function(color, obj, from, to, func, nowOpacity){
		from = (!from)?0:from;
		to = (!to)?0:to;
		color = (!color)?null:color=="null"?null:color;
		
		nowOpacity = (!nowOpacity)?from:nowOpacity;
		
		if(!color) $obj.opacity(obj,nowOpacity);
		else this.colorMask(color,obj,nowOpacity);
		if(from>=to){			
			if(nowOpacity>to){
				nowOpacity-=10;
				setTimeout("effects.fade('"+color+"','"+obj+"',"+from+","+to+",'"+func+"','"+nowOpacity+"')",5);
			} else if(func) eval(func);
		} else {
			if(nowOpacity<to){
				nowOpacity+=10;
				setTimeout("effects.fade('"+color+"','"+obj+"',"+from+","+to+",'"+func+"',"+nowOpacity+")",5);
			}else if(func) eval(func);
		}
	}
}

function formataTelefone(campo) {
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

	if ( tam > 2 && tam < 5 )
		campo.value = vr.substr( 0, tam - 2  ) + '-' + vr.substr( tam - 2, tam );
	if ( tam >= 5 && tam <= 10 )
		campo.value = vr.substr( 0, 2 ) + '-' + vr.substr( 2, 4 ) + '-' + vr.substr( 6, 4 ); 
}

function formataCEP(campo){
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

	if ( tam <= 3 )
		campo.value = vr;
	if ( tam > 3 ) 
		campo.value = vr.substr(0, tam-3 ) + '-' + vr.substr(tam-3, tam);
}

function filtraCampo(campo){
	var s = "";
	var cp = "";
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) {  
		if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != " " && vr.substring(i,i + 1) != "."  && vr.substring(i,i + 1) != "," && vr.substring(i,i + 1) != ":" ){
		 	s = s + vr.substring(i,i + 1);}
	}
	campo.value = s;
	return cp = campo.value
}
function formataData(campo){
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

	if ( tam > 2 && tam < 5 )
		campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
	if ( tam >= 5 && tam <= 10 )
		campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 

}

function formataDataHora(campo){
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;
	if ( tam > 2 && tam < 5 )
		campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
	if ( tam >= 5 && tam < 9 )
		campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	if ( tam >= 9 && tam < 11 )
		campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ) + ' ' + vr.substr( 8, 3 ); 
	if ( tam >= 11 && tam <= 12 )
		campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ) + ' ' + vr.substr( 8, 2 ) + ':' + vr.substr( 10, 2 );  

}

function formataCPF(campo){
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length ;
	if ( tam <= 2 ){
 		campo.value = vr ;}
	if ( tam > 2 && tam <= 5 ){
		campo.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam );}
	if ( tam >= 6 && tam <= 8 ){
		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr(tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam );}
	if ( tam >= 9 && tam <= 11 ){
		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr(tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam );}

}

function mostraArea(tr) {
	if (document.getElementById(tr).style.display == 'inline') {
		document.getElementById(tr).style.display = 'none';
	} else {
		document.getElementById(tr).style.display = 'inline';
	}  		
}

/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/ 

var w=1
var h=1

if (document.getElementById || document.all)
document.write('<div id="trailimageid" style="position:absolute;visibility:hidden;left:0px;top:-1000px;width:1px;height:1px;border:1px solid #888888;background:#DDDDDD;"><img id="ttimg" name="LargeImg" src="img/s.gif" /></div>')

function gettrailobj()
{
	if (document.getElementById) return document.getElementById("trailimageid").style
	else if (document.all) return document.all.trailimagid.style
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidetrail()
{
	document.onmousemove=""
	document.getElementById('ttimg').src='/img/s.gif'
	gettrailobj().visibility="hidden"
	gettrailobj().left=-1000
	gettrailobj().top=0
}


function showtrail(width,height,file)
{
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1)
	{
//		w=width
//		h=height
		
		// followmouse()
	
		document.getElementById('ttimg').src=file
		document.onmousemove=followmouse
		var w = document.images['LargeImg'].width
		var h = document.images['LargeImg'].height
		gettrailobj().visibility="visible"
		gettrailobj().width=w+"px"
		gettrailobj().height=h+"px"


	}
}


function followmouse(e)
{

	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1)
	{

		var xcoord=20
		var ycoord=20

		if (typeof e != "undefined")
		{
			xcoord+=e.pageX
			ycoord+=e.pageY
		}
		else if (typeof window.event !="undefined")
		{
			xcoord+=truebody().scrollLeft+event.clientX
			ycoord+=truebody().scrollTop+event.clientY
		}

		var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
		var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

		if (xcoord+w+3>docwidth)
		xcoord=xcoord-w-(20*2)

		if (ycoord-truebody().scrollTop+h>truebody().clientHeight)
		ycoord=ycoord-h-20;

		gettrailobj().left=xcoord+"px"
		gettrailobj().top=ycoord+"px"

	}

}

function mOvrB(src,clrOver) {
	if (!src.contains(event.fromElement)) {
		src.style.cursor = 'hand';
		src.bgColor = clrOver;
	}
}
function mOutB(src,clrIn) {
	if (!src.contains(event.toElement)) {
		src.style.cursor = 'default';
		src.bgColor = clrIn;
	}
}

function mOvrA(src,over) {
		src.style.cursor = 'hand';
		src.style.backgroundImage = 'url(images/bt_'+over+'.gif)';
//		src.bgColor = clrOver;
}
function mOutA(src,out) {
		src.style.cursor = 'pointer';
		src.style.backgroundImage = 'url(images/bt_'+out+'.gif)';
//		src.bgColor = clrIn;
}

function pegaValorDeLista(fr,id,cp,dv,pg) {
	document.getElementById(dv).style.visibility  = 'visible';
	document.form.id.value=id;
	document.form.cp.value=cp;
	document.form.fr.value=fr;
	document.form.div.value=dv;
//	ajaxHTML(dv,pg);
	fsAjax(pg, dv, 'carrega', 'POST', 'form', '', '');
}
function buscaNomeDeLista(fr,id,cp,div,pg) {
	fsAjax(pg, div, 'carrega...', 'POST', fr, '', '');
}
function passaValorDaLista(div,id,nm) {
	document.getElementById(div).style.visibility  = 'hidden';
	document.getElementById(document.form.id.value).value = id;
	document.getElementById(document.form.cp.value).value = nm;
}

function incrementaDownload(id) {
	$.ajax({
		type: "POST",
		url: "incrementaDownload.php",
		data: "idItem="+id
	});
}

function cadastraUsuarioMural(id,acao) {
	var dt = new Date();
	document.form.Cod.value = id;
	document.form.acao.value = acao;
	fsAjax('mural/mural_cadastro.php?ncod='+encodeURI(Math.random()+"_"+dt.getTime()), 'div_mural', 'Gravando...', 'POST', 'form', '','');
}

function loginMural(id,acao) {
	document.form.Cod.value = id;
	document.form.acao.value = acao;
	fsAjax('mural/mural_login.php', 'div_mural', 'Logando...', 'POST', 'form', '','');
}

function trocaSenhaMural(id,acao) {
	document.form.Cod.value = id;
	document.form.acao.value = acao;
	fsAjax('mural/mural_trocaSenha.php', 'div_mural', 'Atualizando...', 'POST', 'form', '','');
}

function esqueciSenhaMural(id,acao) {
	document.form.Cod.value = id;
	document.form.acao.value = acao;
	fsAjax('mural/mural_esqueciSenha.php', 'div_mural', 'Enviando...', 'POST', 'form', '','');
}

function novoRecadoMural(id,acao) {
	document.form.Cod.value = id;
	document.form.acao.value = acao;
	fsAjax('mural/mural_recado.php', 'div_mural', 'Enviando...', 'POST', 'form', '','');
}

function executarAcao(id,acao,msg,pg,div,form) {
	if (msg=='') msg = 'Carregando...';
	document.form.Cod.value = id;
	document.form.acao.value = acao;
	fsAjax(''+pg+'', ''+div+'', ''+msg+'', 'POST', ''+form+'', '','');
}

function palavra(texto,lim){
	var y = true;
	var qt=0;
	var par="";
	var tam=texto.value.split(" ");
	if (tam.length>0){
		for (var nn=0;nn<tam.length;nn++){
			if (tam[nn].length>lim){
				qt++;
				par=tam[nn];
				break;
			}
		}
	}else{
		if (texto.length>lim){
			qt++;
				par=texto;
		}
	}
	if(qt>0){
		alert("Palavra muito longa no texto: \n" + tam[nn] +
			"\n\nSepare-a para prosseguir com o envio do recado.");
		texto.focus();
		y = false;
	}
	return y;

}

function jhTip() {
	$(".leiaMais a").jHelperTip({
		trigger: "hover", 
		source: "attribute", 
		attrName: "name", 
		opacity: 0.9, 
		autoClose:true
	});
};

function emoticonsOver() {
	$('ul.emoticons li').ahover({moveSpeed: 100, hoverEffect: function() {
		$(this)
			.css({opacity: 0.99})
			.animate({opacity: 0.5}, 750)
			.animate({opacity: 0.99}, 750)
			.dequeue();
		$(this).queue(arguments.callee);
	}});
};
function escolheEmo(id) {
	$('#emo').replaceWith('<div id="emo"><img src="mural/emoticons/'+id+'.png" /></div>');
	$(':input#imagem').val(id);
}
function textarea () {
	var maximos = new Array ();
	$("textarea").attr("maxlength", function (i) {
		var ancho = 421;
		$(this).after("<div id='progresso"+i+"' style='color:#FFF;padding-left:3px;background-color:#CC0000;width:"+ancho+"px;height:21px;background:url(mural/images/textarea.png) no-repeat; border: 1px solid #cccccc;  margin-top:5px; background-position:-"+ancho+"px 0px'></div>");
		if (maximos[i] = this.getAttribute('maxlength')) {
			$('#progresso'+i).html("["+this.value.length+" / "+maximos[i]+"]");
			$(this).keypress(function(event) {
				var pos = ancho-parseInt((ancho*parseInt(this.value.length))/maximos[i]);
				if (this.value.length<=maximos[i]) {
					$('#progresso'+i).css('background-position','-'+pos+'px 0px');
					$('#progresso'+i).html("["+this.value.length+" / "+maximos[i]+"]");
				} else {
					$('#progresso'+i).css('background','');
					$('#progresso'+i).css('background-color','#CC0000');
				}
				return ((event.which == 9) || (this.value.length <= maximos[i]));
			});
		}
	});
}
function arqTipos(){
	$("#arqTipos img, #arqTipos2 img").jHelperTip({
		trigger: "hover", 
		source: "attribute", 
		attrName: "name", 
		opacity: 0.9, 
		autoClose:true
	});
	$(".arq3 img,$arqTipos img").jHelperTip({
		trigger: "hover", 
		source: "attribute", 
		attrName: "name", 
		opacity: 0.9, 
		autoClose:true
	});
};

function ahoverMPgs() {
    $('ul.menuPaginas li').ahover({moveSpeed: 100, hoverEffect: function() {
        $(this)
            .css({opacity: 0.85})
            .animate({opacity: 0.5}, 500)
            .animate({opacity: 0.85}, 500)
            .dequeue();
        $(this).queue(arguments.callee);
    }});
}

function verAnexo(tp,id) {
	$('#tampaTudo').css('display','inline');
	$('#verAnexo').css('display','inline');
	fsAjax('verAnexo.php?id='+id+'&tipo='+tp, 'verAnexo', 'Carregando...', 'GET', '', '','');
	$('#tampaTudo').click(function() {
		escondeAnexo();
	});
$(window).keydown(function(event){
	switch (event.keyCode) {
		case 27: escondeAnexo();
	}
});

}
function escondeAnexo() {
	$('#tampaTudo').css('display','none');
	$('#verAnexo').css('display','none');
	return false;
}
