/******
* FUNÇÃO QUE RETONA O EVENT DO TECLADO DE ACORDO COM O BROWSER
******/
function wKey(evt) {
	var key;
	
	try { // Browser's Internet Explorer
		key = event.keyCode;
	}
	catch (err) { // Mozilla Firefox, Opera, Google Chrome
		key = evt.keyCode;
	}
	
	return key;
}

var ajax = $ajax();

function $ajax() {
	var xml_http;
	
	// trabalha com todos os browsers menos as versões antigas ao IE7
	try {
		xml_http = new XMLHttpRequest();
	}
	catch (e) {
		var ie_versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.5.0", 
							  "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", 
							  "MSXML2.XMLHttp.2.0", "Microsoft.XMLHttp"];
			
			// escolhe a melhor versão para o IE
			for (var i=0; i < ie_vesions.length; i++) {
				try {
					xml_http = new ActiveXObject(ie_versions[i]);
				}
				catch (e) {}
			}
	}
		
	// retorna o objeto ou um erro
	if (!xml_http) {
		alert('Não foi possivel criar o objeto XMLHttpRequest!!!');
	}
	else {
		return xml_http;
	}
}


/******
* 'Class' para criar aliases para o document.getElement*
******/
var $g = {
	id: function(obj) {
		return document.getElementById(obj);
	},
	
	tagName: function(obj, tag) {
		return obj.getElementsByTagName(tag);	
	}
}

/******
* funções para trabalhar com as abas
******/
function set_aba(aba, tab) {
	this.aba = aba;
	this.tab = tab;
}

function troca_aba(aba, tab, arr_abas) {
	for (i=0; i < arr_abas.length; i++) {
		a = $g.id(arr_abas[i].aba);
		a.className = 'aba_unselected';
		
		t = $g.id(arr_abas[i].tab);
		t.style.display = 'none';
	}
	
	a = $g.id(aba);
	a.className = 'aba_selected';
	
	t = $g.id(tab);
	t.style.display = '';
}

/******
* função para simular um slide show de imagens
******/
function slide(arr_images) {
	var index = 0;
	
	var i = window.setInterval(function() {
									// caso o index seja maior que o número de images a trocar, zera o index
									if (index > arr_images.length - 1) { index = 0; }
									
									// executa a troca de imagem
									$g.id('box_image').src = arr_images[index];
									// fa um efeito de transição
									fade_in('box_image', 0.1);
									
									// passa para a próxima imagem
									index++;
							   }, 5000);
	
	
}

function fade_out(id, time) {
	target = document.getElementById(id);
	alpha = 100;
	timer = (time*1000)/50;
	var i = setInterval(
			function() {
				if (alpha <= 0)
					clearInterval(i);
				setAlpha(target, alpha);
				alpha -= 2;
			}, timer);
}

function fade_in(id, time) {
	target = document.getElementById(id);
	alpha = 0;
	timer = (time*1000)/50;
	var i = setInterval(
			function() {
				if (alpha >= 100)
					clearInterval(i);
				setAlpha(target, alpha);
				alpha += 2;
			}, timer);
}

function setAlpha(target, alpha) {
	target.style.filter = "alpha(opacity="+ alpha +")";
	target.style.opacity = alpha/100;
}


function abre_cadastro() {
	// captura o tamanho da tela
	var screen_x = window.screen.width;
	var screen_y = document.body.scrollHeight;

	var div = document.createElement('div');
	div.id = "alpha";
	div.style.height = screen_y + 'px';
	document.body.appendChild(div);
	
	var div2 = document.createElement('div');
	div2.className = "cont";

	div.appendChild(div2);
}

function write_combo(obj, cmp, label, valor) {
	$g.id(obj).innerHTML = '<p>' + label + '</p>';
	$g.id(cmp).value = valor;
}

/******
* 'Classe' para trabalhar com cookies
******/
var $cookie = {
	set: function (cookie, value) {
		// só sontinua se a string de nome não for vázia
		if (cookie != '') {
			document.cookie = cookie + '=' + value;
		}
	},
	
	get: function (cookie) {
		// só sontinua se a string de nome não for vázia
		if (cookie == '')
			return('');
		
		cookie_index = document.cookie.indexOf(cookie + '=');
		
		if (cookie_index == -1)
			return('');
			
		cookie_value = document.cookie.substr(cookie_index + cookie.length + 1, document.cookie.length);
		
		end_cookie = cookie_value.indexOf(';');
		if (end_cookie != -1)
			cookie_value = cookie_value.substr(0, end_cookie);
			
		space = cookie_value.indexOf('+');
		while (space != -1) {
			cookie_value = cookie_value.substr(0, space) + ' ' + cookie_value.substr(space + 1, cookie_value.length);
			
			space = cookie_value.indexOf('+');
		}
		
		// retorna o valor do cookie
		return(cookie_value);
	},
	
	clear: function () {
		cookies = document.cookie;
		cookie = cookies;
		expiracao = new Date();
		expiracao.setYear(expiracao.getYear() -1);
		
		while (cookie.lenght > 0) {
			cookie  = cookies.substr(0, cookies.indexOf(';'));
			cookies = cookies.substr(cookies.indexOf(';') +1, cookies.length);
			
			if (cookie != '')
				document.cookie = cookie + '; expires=' + expiracao;
			else
				document.cookie = cookies + '; expires=' + expiracao;
		}
	}
}
