if(document.getElementById){
	var f1 = new domFunction(infoservico,{'infoservico':'id'});
	var f2 = new domFunction(initforms,{'form':'tag'});
	window.onload = setFooter;
	window.onresize = setFooter;
}

// flash
function initSwf(page){
	if($("banner")){
		var f = new SWFObject("http://www.iocert.com.br/swf/iocert.swf", "myflash", "770", "175", "7", "#ffffff");
		f.addVariable("path", document.getElementsByTagName("base")[0].href);
		f.addVariable("initShowing", page);
		f.addParam("wmode", "transparent");
		f.write("banner");
	}
}

// setfooter
function getWindowHeight(){
	var windowHeight = 0;
	if(typeof(window.innerHeight)=='number'){ windowHeight=window.innerHeight; }
	else {
		if(document.documentElement&&document.documentElement.clientHeight){ windowHeight = document.documentElement.clientHeight; }
		else { if(document.body&&document.body.clientHeight) { windowHeight=document.body.clientHeight; } }
	}
	return windowHeight;
}

function setFooter(){
	var windowHeight = getWindowHeight();
	if (windowHeight > 0) {
		var el = $("maincolbody");
		el.style.height = "auto";
		var elHeight = el.offsetHeight;
		var elParent = el.parentNode;

		var a = elParent.offsetTop;
		var b = 90; // altura footer - vide css
		var h = windowHeight - a - b;
		var minHeight = 235;
		var newHeight = (h > minHeight && h > elHeight)
			? h
			: ((elHeight > minHeight) ? elHeight : minHeight );

		var sidecol = $("sidecol");
		if(sidecol){
			sidecolHeight = sidecol.offsetTop + sidecol.offsetHeight - a - b;
			newHeight = (newHeight < sidecolHeight) ? sidecolHeight : newHeight;
		}

		if(newHeight != elHeight){ el.style.height = newHeight + "px"; }
	}
}

// infoservico
function infoservico(){
	var i = $("infoservico");
	var s = $("showinfoservico");
	if(!i || !s){ return; }
	i.fx = new fx.Combo(i, {duration: 250});
	i.fx.hide();
	s.onclick = function(){
		i.fx.toggle();
		setTimeout(setFooter,250);
		return false;
	}
}

// forms
function show(el){
	var s = $(el);
	s.style.visibility = "visible";
	s.fx = new fx.Opacity(el, {duration: 500});
	s.fx.setOpacity(0);
	s.fx.custom(0,1);
}

function hide(el){
	var h = $(el);
	h.fx = new fx.Opacity(el, {duration: 500});
	h.fx.custom(1,0);
	h.fx.setOpacity(0);
	h.style.display = "none";
}

function enable(button){ if(button.disabled == true){ button.disabled = false; } }

function disable(button){ if(button.disabled == false){ button.disabled = true; } }

function validar(email){
	regexs1 = [
		/^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/, // characters allowed on name: 0-9a-Z-._ on host: 0-9a-Z-. on between: @
		/([0-9a-zA-Z_]{1})\@./, // name must end with alpha or num
		/.\@([0-9a-zA-Z_]{1})/, // must start or end with alpha or num
		/\.([a-zA-Z]{2,5})$/ // host must end with '.' plus 2-5 alpha for TopLevelDomain
	];
	regexs2 = [
		/^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/, // pair .- or -. or -- or .. not allowed
		/.\.\-.|.\-\..|.\.\..|.\-\-./, // host must start with alpha or num
		/.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./ // pair ._ or -_ or _. or _- or __ not allowed
	];
	for (var x=0, r1; r1=regexs1[x]; x++){ if(!r1.exec(email)) return false; }
	for (var y=0, re; r2=regexs2[y]; y++){ if(r2.exec(email)) return false; }
	return true;
}

function checkfields(){
	var vazios = 0;
	var invalidos = 0;
	var b = document.getElementsByClassName("button")[0];
	var campos = document.getElementsByClassName("required");
	campos.each(function(el, i){
		if(el.value.length == 0){
			vazios++;
		} else if(Element.hasClassName(el, "email") && !validar(el.value)){
			invalidos++;
		}
	});
	// se houver algum vazio ou email invalido
	if(vazios!=0 || invalidos!=0){ disable(b) }
	else { enable(b) }
}

function checkemail(el){
	checkfields();
	msgid = "msg"+el.id;
	msg = $(msgid);
	// se nao houver msg na tela ainda
	if(!msg){
		createmsg(el);
		msg = $(msgid);
	}
	// se msg ja existe
	if(!validar(el.value)){
		var txt = "Email invalido";
		msg.className = "invalido";
	} else {
		var txt = "Email valido";
		msg.className = "valido";
	}
	msg.innerHTML = txt;
	msg.fx.custom(0, 1);
	if(msg.className == "invalido"){
		b = document.getElementsByClassName("button")[0];
		disable(b);
	}	
}

function createmsg(el){
	msg = document.createElement("span");
	msg.id = "msg"+el.id;
	msg.className = "invalido";
	msg.innerHTML = "Email invalido";
	el.parentNode.appendChild(msg);
	msg.fx = new fx.Opacity(msgid, {duration: 500});
	msg.fx.setOpacity(0);
	msg.fx.custom(0, 1);
}

function createupdate(el){
	update = document.createElement("div");
	update.id = "update";
	el.parentNode.insertBefore(update,el);
}

function initforms(){
	var pages = ["fale", "trabalhe"];
	var ajax = false;
	pages.each(function(p,i){
		if(Element.hasClassName(document.body, p)) ajax = true;
	});
	if(ajax == true){
		// desativa botao
		var b = document.getElementsByClassName("button")[0];
		disable(b);
		checkfields();
		// ajax forms
		f = document.forms[0];
		f.id = "form";
		f.onsubmit = function(){
			// desabilita botao
			b = document.getElementsByClassName("button")[0];
			disable(b);
			// cria espaco para update
			createupdate(f);
			// envia dados por ajax
			var myajax = new XHConn(); if(!myajax) return;
			var tipoform = $("tipoform").value;
			var vars = "ajax=true&tipoform=" + tipoform +
						"&nome=" + $("nome").value +
						"&email=" + $("email").value +
						"&prefixo=" + $("prefixo").value +
						"&fone=" + $("fone").value +
						"&ramal=" + $("ramal").value;
			switch(tipoform){
				case "contato":
					vars = vars + "&mensagem=" + $("mensagem").value;
					break;
				case "curriculo":
					vars = vars + "&curriculo=" + $("curriculo").value;
					break;
			}
			var fnWhenDone = function(oXML){
				var update = $("update");
				update.innerHTML = oXML.responseText;
			};
			myajax.connect("enviar.php", "POST", vars, fnWhenDone);
			return false;
		}
		// ativa botoes qdo todos os campos necessarios estiverem preenchidos
		var campos = document.getElementsByClassName("required");
		campos.each(function(el, i){
			el.onkeyup = function(){
				checkfields();
			};
		});
		// valida emails
		var campos = document.getElementsByClassName("email");
		campos.each(function(el, i){
			el.onkeyup = function(){ checkemail(this) }
		});
	}
}

// xhconn | bfults@gmail.com
function XHConn(){
	var xmlhttp, bComplete = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e){ try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e){ try { xmlhttp = new XMLHttpRequest(); }
	catch (e){ xmlhttp = false; }}}
	if(!xmlhttp) return null;
	this.connect = function(sURL, sMethod, sVars, fnDone){
		if(!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		try {
			if(sMethod == "GET"){
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			} else {
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function(){
				var enviando = $("update");
				if(xmlhttp.readyState < 4){
					enviando.innerHTML = "Enviando mensagem...";
					if($("form").style.display != "none"){
						enviando.style.height = document.forms[0].offsetHeight + "px";
						hide("form");
					}
				} else if(xmlhttp.readyState == 4 && !bComplete){
					setTimeout(function(){
						enviando.innerHTML = "";
						bComplete = true;
						fnDone(xmlhttp);
					}, 1000);
				}};
			xmlhttp.send(sVars);
		}
		catch(z){ return false; }
		return true;
	};
	return this;
}

// domFunction by brothercake | http://www.brothercake.com/
function domFunction(f, a){
	var n = 0;
	var t = setInterval(function(){
		var c = true;
		n++;
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)){
			c = false;
			if(typeof a == 'object'){
				for(var i in a){
					if((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)){ 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		if(n >= 60){
			clearInterval(t);
		}
	}, 250);
};

// prototype | (c) 2005 sam stephenson | http://prototype.conio.net/

var Class = {
	create: function(){
		return function(){
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function(){
		return __method.apply(object, arguments);
	}
}

Function.prototype.bindAsEventListener = function(object) {
var __method = this;
	return function(event) {
		__method.call(object, event || window.event);
	}
}

function $() {
	if(arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if(typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}

if(!window.Element) var Element = new Object();

Object.extend(Element, {
	remove: function(element) {
		element = $(element);
		element.parentNode.removeChild(element);
	},

	hasClassName: function(element, className) {
		element = $(element);
		if(!element) return;
		var hasClass = false;
		element.className.split(' ').each(function(cn){
			if(cn == className) hasClass = true;
		});
		return hasClass;
	},

	addClassName: function(element, className) {
		element = $(element);
		Element.removeClassName(element, className);
		element.className += ' ' + className;
	},
  
	removeClassName: function(element, className) {
		element = $(element);
		if(!element) return;
		var newClassName = '';
		element.className.split(' ').each(function(cn, i){
			if(cn != className){
				if(i > 0) newClassName += ' ';
				newClassName += cn;
			}
		});
		element.className = newClassName;
	},

	cleanWhitespace: function(element) {
		element = $(element);
		$c(element.childNodes).each(function(node){
			if(node.nodeType == 3 && !/\S/.test(node.nodeValue)) Element.remove(node);
		});
	},

	find: function(element, what) {
		element = $(element)[what];
		while (element.nodeType != 1) element = element[what];
		return element;
	}
});

var Position = {
	cumulativeOffset: function(element) {
		var valueT = 0, valueL = 0;
		do {
			valueT += element.offsetTop  || 0;
			valueL += element.offsetLeft || 0;
			element = element.offsetParent;
		} while (element);
		return [valueL, valueT];
	}
};

document.getElementsByClassName = function(className) {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = [];
	$c(children).each(function(child){
		if(Element.hasClassName(child, className)) elements.push(child);
	});  
	return elements;
}

// useful array functions
Array.prototype.each = function(func){
	for(var i=0;ob=this[i];i++) func(ob, i);
}

function $c(array){
	var nArray = [];
	for (i=0;el=array[i];i++) nArray.push(el);
	return nArray;
}

// moo.fx | valerio proietti | http://moofx.mad4milk.net

var fx = new Object();
// base
fx.Base = function(){};
fx.Base.prototype = {
	setOptions: function(options) {
	this.options = {
		duration: 500,
		onComplete: '',
		transition: fx.sinoidal
	}
	Object.extend(this.options, options || {});
	},

	go: function(){
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	step: function(){
		var time  = (new Date).getTime();
		if(time >= this.options.duration+this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
			if(this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = this.options.transition(Tpos) * (this.to-this.from) + this.from;
		}
		this.increase();
	},

	custom: function(from, to) {
		if(this.timer != null) return;
		this.from = from;
		this.to = to;
		this.go();
	},

	hide: function(){
		this.now = 0;
		this.increase();
	},

	clearTimer: function(){
		clearInterval(this.timer);
		this.timer = null;
	}
}

// stretchers
fx.Layout = Class.create();
fx.Layout.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.el.style.overflow = "hidden";
		this.el.iniWidth = this.el.offsetWidth;
		this.el.iniHeight = this.el.offsetHeight;
		this.setOptions(options);
	}
});

fx.Height = Class.create();
Object.extend(Object.extend(fx.Height.prototype, fx.Layout.prototype), {	
	increase: function(){
		this.el.style.height = this.now + "px";
	},

	toggle: function(){
		if(this.el.offsetHeight > 0) this.custom(this.el.offsetHeight, 0);
		else this.custom(0, this.el.scrollHeight);
	}
});

fx.Width = Class.create();
Object.extend(Object.extend(fx.Width.prototype, fx.Layout.prototype), {	
	increase: function(){
		this.el.style.width = this.now + "px";
	},

	toggle: function(){
		if(this.el.offsetWidth > 0) this.custom(this.el.offsetWidth, 0);
		else this.custom(0, this.el.iniWidth);
	}
});

// fader
fx.Opacity = Class.create();
fx.Opacity.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.now = 1;
		this.increase();
		this.setOptions(options);
	},

	increase: function(){
		if(this.now == 1 && (/Firefox/.test(navigator.userAgent))) this.now = 0.9999;
		this.setOpacity(this.now);
	},
	
	setOpacity: function(opacity) {
		if(opacity == 0) this.el.style.visibility = "hidden";
		else this.el.style.visibility = "visible";
		if(window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + opacity*100 + ")";
		this.el.style.opacity = opacity;
	},

	toggle: function(){
		if(this.now > 0) this.custom(1, 0);
		else this.custom(0, 1);
	}
});

// transitions
fx.sinoidal = function(pos){
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
	//this transition is from script.aculo.us
}

// composition effect: widht/height/opacity
fx.Combo = Class.create();
fx.Combo.prototype = {
	setOptions: function(options) {
		this.options = {
			opacity: true,
			height: true,
			width: false
		}
		Object.extend(this.options, options || {});
	},

	initialize: function(el, options) {
		this.el = $(el);
		this.setOptions(options);
		if(this.options.opacity) {
			this.el.o = new fx.Opacity(el, options);
			options.onComplete = null;
		}
		if(this.options.height) {
			this.el.h = new fx.Height(el, options);
			options.onComplete = null;	
		}
		if(this.options.width) this.el.w = new fx.Width(el, options);
	},
	
	toggle: function(){ this.checkExec('toggle'); },

	hide: function(){ this.checkExec('hide'); },
	
	clearTimer: function(){ this.checkExec('clearTimer'); },
	
	checkExec: function(func){
		if(this.el.o) this.el.o[func]();
		if(this.el.h) this.el.h[func]();
		if(this.el.w) this.el.w[func]();
	},
	
	// only ifwidth+height
	resizeTo: function(hto, wto) {
		if(this.el.h && this.el.w) {
			this.el.h.custom(this.el.offsetHeight, this.el.offsetHeight + hto);
			this.el.w.custom(this.el.offsetWidth, this.el.offsetWidth + wto);
		}
	},

	customSize: function(hto, wto) {
		if(this.el.h && this.el.w) {
			this.el.h.custom(this.el.offsetHeight, hto);
			this.el.w.custom(this.el.offsetWidth, wto);
		}
	}
}

fx.Accordion = Class.create();
fx.Accordion.prototype = {
	setOptions: function(options) {
		this.options = {
			delay: 100,
			opacity: false
		}
		Object.extend(this.options, options || {});
	},

	initialize: function(togglers, elements, options) {
		this.elements = elements;
		this.setOptions(options);
		var options = options || '';
		elements.each(function(el, i){
			options.onComplete = function(){
				if(el.offsetHeight > 0) el.style.height = '1%';
			}
			el.fx = new fx.Combo(el, options);
			el.fx.hide();
		});

		togglers.each(function(tog, i){
			tog.onclick = function(){
				(Element.hasClassName(tog, "menos")) ? Element.removeClassName(tog, "menos") : Element.addClassName(tog, "menos");
				this.showThisHideOpen(elements[i]);
				return false;
			}.bind(this);
		}.bind(this));
	},

	showThisHideOpen: function(toShow){
		if(toShow.offsetHeight == 0) setTimeout(function(){this.clearAndToggle(toShow);}.bind(this), this.options.delay);
		else { this.clearAndToggle(toShow) }
	},

	clearAndToggle: function(el){
		el.fx.clearTimer();
		el.fx.toggle();
	}
}