var tempX,tempY;
function Ajax(){
	this.objAjax = null;
	this.Init();	
};

Ajax.prototype.Init = function(){
	try{
		this.objAjax = new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
		try{
			this.objAjax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(ex){
			try{
				this.objAjax = new XMLHttpRequest();
			}catch(exc){
				this.objAjax = false;
			}
		}
	}
}

Ajax.prototype.Request = function(url,config){
	metodo = config.method;
	parametros = config.params;
	sucesso = config.onSucess;
	erro = config.onError;
	serialize = config.serialize;
	sync = config.sync;
	
	if(this.objAjax){
		
		sync = (typeof sync == "undefined" || sync == 1) ? sync = true : sync = false;
		
		if(metodo == "GET" && typeof(parametros) != "undefined"){
			url = url + "?" + parametros;
		}
		
		if(metodo == "POST" && typeof(parametros) == "undefined" && typeof("serialize") != "undefined"){
			parametros = Form.serialize(serialize);
		}
		
		ajax = this.objAjax;
		ajax.open(metodo, url, sync);
		
		if(metodo == "POST"){
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		
		ajax.onreadystatechange = function(){
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					params = {
						texto: ajax.responseText
					}
					if (typeof(sucesso) != "undefined") 
						sucesso(params);
				}
				else {
					if (typeof(erro) != "undefined") 
						erro();
				}
			}
		}
		dados = (metodo == "GET") ? null : parametros;
		ajax.send(dados);
	}
}


var Form = function(){}
Form.serialize = function(form){
	var F = document.forms[form];
	if(typeof(F) != "undefined"){
		var elem = F.elements, dados = "";
		for(i = 0;i < elem.length;i ++){
			dados += (elem[i].name != "") ? elem[i].name +"="+ encodeURIComponent(elem[i].value) +"&" : "";
		}
		return dados.replace(/&$/i,'');
	}else return "";
}

Cookie = function(){
	this.date = new Date();
}
/**
 * Cria um Cookie
 * @param {String} nome
 * @param {String} valor
 * @param {Integer} dias
 */
Cookie.set = function(nome,valor,dias){
	this.date = new Date();
	if(dias){
		this.date.setTime(this.date.getTime()+dias*1000*60*60*24);
		expires = ";expires="+this.date.toGMTString();
	}else{
		expires = "";	
	}
	document.cookie = nome +"="+ escape(valor) + expires +";path=/";
}
/**
 * Retorna um valor de um determinado Cookie
 * @param {String} nome
 */
Cookie.get = function(nome){
	if(document.cookie.indexOf(nome + "=") != -1){
		cookies = document.cookie.split("; ");
		for(i = 0;i < cookies.length;i ++){
			if(cookies[i].indexOf(nome + "=") != -1){
				return unescape(cookies[i].substr(nome.length + 1));
			}
		}
	}else return "";	
}
/**
 * Exclui um determinado Cookie
 * @param {String} nome
 */
Cookie.erase = function(nome){
	this.set(nome,"",-1);	
}
/**
*
* @name Mouse
* @desc Funções para mouse
*
**/
var Mouse = function(){};
/**
*
* @name Mouse.posicao
* @desc Obtém posição do mouse no documento
*
**/
Mouse.posicao = function(e){
	IE = document.all ? true : false;
	if (!IE) document.onmousemove;
	if(IE){
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}else{
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	if(tempX < 0) tempX = 0;
	if(tempY < 0) tempY = 0;
}

/**
*
* @name Size
* @desc Funcoes para retornar medidas
*
**/
var Size = function(){}
/**
*
* @name getPageSize
* @desc Pega as dimensões da pagina que está sendo exibida
*		ao usuário
*
**/
Size.getPageSize = function(){
	var xScroll, yScroll,
		windowWidth, windowHeight;
		
	if(window.innerHeight && window.scrollMaxY){
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}else if(document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}else{
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	if(self.innerHeight){
		windowWidth  = self.innerWidth;
		windowHeight = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){
		windowWidth  = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}else if(document.body){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	if(yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;
	
	if(xScroll < windowWidth) pageWidth = windowWidth;
	else pageWidth = xScroll;
	
	arrayPageSize = {pageWidth:pageWidth,pageHeight:pageHeight,windowWidth:windowWidth,windowHeight:windowHeight}
	return arrayPageSize;
}

/**
*
* @name getPageScroll
* @desc Retorna a posicao X e Y da barra de rolagem
*
**/
Size.getPageScroll = function(){
	var yScroll;
	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;
	arrayPageScroll = {yScroll:yScroll};
	return arrayPageScroll;
}
