function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}

function numberFormat(field,intDecimal,separadorDecimal,separadorMilhar){
	/*
	*@description formata um número
	*/
	var strlen = field.length;
	var num = "";
	for(x=0;x<strlen;x++){
		if(isNaN(field.substring(x,x+1))){
			num += field.substring(x,x+1);	
		}
	}
	nnumber = field.replace(num,separadorDecimal);
	var decs = nnumber.indexOf(separadorDecimal,0);
	var ndecs = nnumber.substring(decs+1,nnumber.length);
	ndesclen = ndecs.length;
	
	while(ndesclen<intDecimal){
		nnumber += 0;
		ndesclen++;
	}
									

	var nMiles = nnumber.substring(0,decs);
	var toNum = 3;
	if(separadorMilhar!=null){
		while(nMiles.length >= toNum){
			var upMiles = nnumber.substring(0,decs-toNum);
			var miles = nnumber.substring(decs-toNum,decs);
			var rest =  nnumber.substring(decs,nnumber.length);
			nnumber = upMiles+separadorMilhar+miles+rest;
			toNum = toNum + 3;
		}
	}
	return(String(nnumber));
}


function trim(string) {
	/*
	*@description retira espaços no início e no fim de uma string
	*/
	var len = string.length;
	 while(string.substring(0,1)==" "){
		string = string.substring(1,len);
		var len = string.length; 
	}
	
	var num = string.length;
	
	while(string.substring((num-1),num)== " "){
		string = string.substring(0,(num-1));
		var num = string.length;
	}
	
	return string;
	}


function explode(separator,string){
	/*
	*@description divide uma string em um array
	*/
	if(separator.length > 0 && string.length > 0){
		
		_str = string;
		
		var array = new Array();
		
		while(_str.indexOf(separator) > -1){
			_pos = _str.indexOf(separator);
			_push = _str.substring(0,_pos);
			array.push(_push);
			_str = _str.substring(_pos+1);
		}
			array.push(_str);
		
		return array;
		
	}else{
		return false;	
	}				
}

function implode(stringGlue,arrayPieces){
	/*
	*@description transforma array em uma string
	*/
	if(stringGlue.length > 0 && arrayPieces.length > 0){
		
		_pieces = arrayPieces;
		
		_string = new String();
		
		for(x=0;x<_pieces.length;x++){
			if(x+1 != _pieces.length){
				_string += _pieces[x] + stringGlue;	
			}else{
				_string += _pieces[x];	
			}
		}
		
		return _string;
		
	}else{
		return false;	
	}
}

function getElementsByTagNameAndAttributeWithTheValue(tag,attribute,value){
	var _tag = document.getElementsByTagName(tag);
	
	var elements = new Array();
	
	for( x = 0 ; x < _tag.length ; x++){
		if(eval('_tag[x].' + attribute) == value){
			elements.push(_tag[x]);	
		}
	}
	return elements;	
}

function getav(tag,attribute,value){
	return	getElementsByTagNameAndAttributeWithTheValue(tag,attribute,value);
}


function ge(id){
	return document.getElementById(id);
}

function get(tag){
	return document.getElementsByTagName(tag);
}

function nE(tag){
	return document.createElement(tag);
}

function getPageSize(){
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
}

function OpensCommentsWindow(){
	var _links = get('a');
	
	for(x=0;x<_links.length;x++){
		if(_links[x].rel == '_comentarios'){
			_links[x].onclick = function(e){								
				if(navigator.appName == 'Microsoft Internet Explorer'){
					try{
						if(ge('filterNews')){
							ge('filterNews').style.display = 'none';
						}
						if(ge('filterFlog')){
							ge('filterFlog').style.display = 'none';
						}
						if(ge('filterJogos')){
							ge('filterJogos').style.display = 'none';
						}
						if(ge('filterSeasons')){
							ge('filterSeasons').style.display = 'none';
						}
					}catch(e){}
				}
				_size = getPageSize();
	
				posY = get('html')[0].scrollTop + (get('html')[0].clientHeight/2) - 255;
				
				posX = (get('html')[0].clientWidth/2) - 300;	
								
				ajax = openAjax();
				ajax.open('GET', this.href + '&ajax=true' ,true);
				ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ajax.onreadystatechange = function (){
					if(ajax.readyState == 4){						
						response = ajax.responseText;
						_p1 = response.indexOf('<!-- BEGINNING BLOCK TO FILTER RESULTS -->');
						_p1 = response.substring(_p1);
						_p2 = _p1.indexOf('<!-- END BLOCK TO FILTER RESULTS -->');
						_p2 = _p1.substring(42,_p2);
						ge('corpo').innerHTML = _p2 + ge('corpo').innerHTML;
						ge('CommentsWindow').style.left = posX + 'px';
						ge('CommentsWindow').style.top = posY + 'px';						
						carrega();
						try{
							alert(ge('errors').firstChild.nodeValue);
							delete error;							
						}catch(e){}
					}
				};
				ajax.send(null);
				return false;
			};	
		}
	}
}

function CloseCommentsWindow(){
	var _btClose = ge('closeWindow');
	_btClose.onmouseup = function(){
		if(ge('CommentsWindow')){
			ge('corpo').removeChild(ge('CommentsWindow'));
			if(navigator.appName == 'Microsoft Internet Explorer'){
				try{
					if(ge('filterNews')){
						ge('filterNews').style.display = '';
					}
					if(ge('filterFlog')){
						ge('filterFlog').style.display = '';
					}
					if(ge('filterJogos')){
						ge('filterJogos').style.display = '';
					}
					if(ge('filterSeasons')){
						ge('filterSeasons').style.display = '';
					}
				}catch(e){}
			}
		}
		return false;
	};
}

function ManageCommentsWindow(){
	
	var _CommentsWindow = ge('CommentsWindow');
	
	var _links = _CommentsWindow.getElementsByTagName('a');
	
	for(x=0;x<_links.length;x++){
		if(_links[x].target != '_blank'){
			_links[x].onclick = function(){
				
				_left = ge('CommentsWindow').style.left;
				_top = ge('CommentsWindow').style.top;
				
				ajax = openAjax();
				ajax.open('GET', this.href ,true);
				ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ajax.onreadystatechange = function (){
					if(ajax.readyState == 4){
						response = ajax.responseText;	
						if(ge('CommentsWindow')){
							ge('corpo').removeChild(ge('CommentsWindow'));
						}
						ge('corpo').innerHTML = response + ge('corpo').innerHTML;
						
						ge('CommentsWindow').style.left = _left;			
						ge('CommentsWindow').style.top =  _top;			
						carrega();
					}
				};
				ajax.send(null);
				return false;
			};
		}
	}
	
	var _sD = ge('scrollDown');

	_sD.onclick = function(){
		try{
			ge('CommentsBox').scrollTop = ge('CommentsBox').scrollHeight;
		}catch(e){}
		
		try{
			ge('CommentsWindow').scrollTop = ge('CommentsWindow').scrollHeight;	
		}catch(ee){}
		return false;
	};
	
	var _sU = ge('scrollUp');
	
	_sU.onclick = function(){
		try{
			ge('CommentsBox').scrollTop = 0;
		}catch(e){}
		try{
			ge('CommentsWindow').scrollTop = 0;	
		}catch(ee){}
		return false;
	};

	
	var _form = ge('CommentForm');
	
	_form.onsubmit = function(){
		_left = ge('CommentsWindow').style.left;
		_top = ge('CommentsWindow').style.top;
		_comments = ge('comments').value;
		ajax = openAjax();
		ajax.open('POST', this.action ,true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function (){
			if(ajax.readyState == 4){
				response = ajax.responseText;	
				
				if(ge('CommentsWindow')){
					ge('corpo').removeChild(ge('CommentsWindow'));
				}
				ge('corpo').innerHTML = response + ge('corpo').innerHTML;
				
				ge('CommentsWindow').style.left = _left;			
				ge('CommentsWindow').style.top =  _top;
				
				carrega();
				
				try{
					if(ge('errors')){
						ge('comments').value = _comments;
						ge('CommentsBox').scrollTop = ge('CommentsBox').scrollHeight;
						alert(ge('errors').firstChild.nodeValue);
						delete error;
					}else{						
						var _link = getav('a','rev','_numComentarios');						
						for(x=0;x<_link.length;x++){
							_a = explode('/',_link[x].href);
							_b = _a.length-1;
							if(_a[_b] == 'CommentsWindow.php?' + ge('hiddenField').name +'=' + ge('hiddenField').value){								
								_link[x].firstChild.nodeValue = (parseInt(_link[x].firstChild.nodeValue) + 1) + ' Comentários »';
							}	
						}
					}
				}catch(e){}
			}
		};
		var send = '&comments=' + ge('comments').value;
		if(ge('user')){	
			send += '&user=' + ge('user').value;			
		}
		send += '&'+ ge('hiddenField').name +'=' + ge('hiddenField').value;
		send += '&sbt=true';
		ajax.send(send)
		return false;
	};
	

	
}

function MoveCloseCommentsWindow(){
	document.onmousedown = function(selected){
		try{
			
			newEvent = selected; 
			
			obj = newEvent.target;
						
		}catch(IE){
		
			newEvent = 'event'; 
		
			obj = eval(newEvent).srcElement;  			
		
		}
		
		if(obj.className == 'moveBar'){			
														
			mover = true;
			
			objLeft = ge('CommentsWindow').style.left.substring(0,ge('CommentsWindow').style.left.length - 2);
			
			objTop = ge('CommentsWindow').style.top.substring(0,ge('CommentsWindow').style.top.length - 2);
			
			posX = parseInt(objLeft*1+0); //posição x atual
				
			posY = parseInt(objTop*1+0); //posição y atual
				
			mouseX = eval(newEvent).clientX; // posição x do mouse
				
			mouseY = eval(newEvent).clientY; // posição y do mouse
			
			document.onmousemove = function(newEvent){
						
				try{
					if(mover){						
						ge('CommentsWindow').style.margin = '0px 0px 0px 0px';
						
						ge('CommentsWindow').style.left = (posX + eval(newEvent).clientX - mouseX ) + 'px';
						
						ge('CommentsWindow').style.top = (posY + eval(newEvent).clientY - mouseY) + 'px';
						
						return false;
					}							
				
				}catch(IE){
				
					if(mover){						
											
						ge('CommentsWindow').style.left = (posX + event.clientX - mouseX ) + 'px';
						
						ge('CommentsWindow').style.top = (posY + event.clientY - mouseY ) + 'px';
																	
						return false;
					}	
				}						
				
			}; 
				
			return false;	
		
		}
	};
	
	document.onmouseup = function(){
		
		mover = false;
		
		
		
	}
}

function createCookie(name,value,mins) {
	if (mins) {
		var date = new Date();
		date.setTime(date.getTime()+(1000*10));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function SetCookie(){
	if(readCookie('views')){
		createCookie('views',true,1);
	}	
}

function getContent (_url , _send){		
	ajax = openAjax();
	ajax.open('POST', _url ,true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.onreadystatechange = function (){
		if(ajax.readyState == 4){
			response = ajax.responseText;	
			
			_p1 = response.indexOf('<!-- BEGINNING BLOCK TO FILTER RESULTS -->');
			_p1 = response.substring(_p1);
			_p2 = _p1.indexOf('<!-- END BLOCK TO FILTER RESULTS -->');
			_p2 = _p1.substring(42,_p2);
			
			ge('conteudo').innerHTML = _p2;
			
			carrega();
		}
	};
	var send = '&ajax=true&';		
		send += _send;							
	ajax.send(send)
	return false;
};

function ManageSelectField(){
	_field = new Array();	
	_url = new Array();
	
	if(_field[0] = ge('filterNews')){
		_url[0] = '../home/noticias.php';
	}else if( (_field[0] = ge('filterAgenda')) && (_field[1] = ge('filterAnoAgenda')) ){
		_url[0] = '../home/agenda.php';
		_url[1] = '../home/agenda.php';
	}else if(_field[0] = ge('filterJogos')){
		_url[0] = '../home/jogos.php';
	}else if(_field[0] = ge('filterSeasons')){
		_url[0] = '../home/temporada.php';
	}else if(_field[0] = ge('filterFlog')){
		_url[0] = '../home/flog.php';
	}
	
	for(x=0;x<_field.length;x++){
		__url = _url[x];
		_field[x].onchange = function(){			
			_send = new String();
			for(a=0;a<_field.length;a++){
				_send += '&' + _field[a].name + '=' + _field[a].value + '&';
			}			
			getContent(__url , _send);
		}
	}
		
	_links = get('a'); 
	
	for(x=0;x<_links.length;x++){
		if(_links[x].rel == '_paginacao'){
			_links[x].onclick = function(){
				_send = new String();
				for(a=0;a<_field.length;a++){
					_send += '&' + _field[a].name + '=' + _field[a].value + '&';
				}
				getContent(this.href , _send);
				return false;
			}	
		}
	}		
}

function AbreJogosTemporada(){	
	var _linhas = get('tr');
	for(x=0;x<_linhas.length;x++){
		if(_linhas[x].className == 'linhaJogos'){
			_linhas[x].onmouseover = function(){
				this.style.background = '#555555';	
			};
			
			_linhas[x].onmouseout = function(){
				this.style.background = '';				
			};
		}
	}
}

function validaFormContato(){	
	

	var obj = new filtering('formContato','AJAX');
	
	obj.filter('nome','filterName','2','', new Array('Campo "nome" possui caracteres inválidos','Tamanho mínimo não atingido. O campo "nome" deverá conter, no mínimo, 2 caracteres','Tamanho máximo excedido. O campo "nome" deverá conter, no máximo, 255 caracteres','O campo "nome" não pode ser nulo') ,false);
	obj.filter('email','filterEmail','','', new Array('O campo "email" não pode ser nulo','Email inválido') ,false);
	obj.filter('assunto','filterSizeField','2','', new Array('Tamanho mínimo não atingido. O campo "assunto" deverá conter, no mínimo, 2 caracteres','Tamanho máximo excedido. O campo "assunto" deverá conter, no máximo, 255 caracteres','O campo "assunto" não pode ser nulo') ,false);
	obj.filter('mensagem','filterSizeField','2,10240','', new Array('Tamanho mínimo não atingido. O campo "mensagem" deverá conter, no mínimo, 2 caracteres','Tamanho máximo excedido. O campo "mensagem" deverá conter, no máximo, 10240 caracteres','O campo "mensagem" não pode ser nulo') ,false);
	
}


function aumenta(id){
	img = ge(id);
	
	if(parseInt(img.style.top+0) > 22){
		img.style.top = parseInt(img.style.top+0) - 30 + 'px';
	}else if(parseInt(img.style.top+0) < 22){
		img.style.top = '22px';
	}
	
	if(parseInt(img.style.left+0) > 1){
		img.style.left = parseInt(img.style.left+0) - 30 + 'px';
	}else if(parseInt(img.style.left+0) < 1){
		img.style.left = '0px';
	}
	
	if(parseInt(img.style.top+0) <= 22 && parseInt(img.style.left+0) <= 1){
		if(parseInt(img.style.width+0) < 570){
			img.style.width = (img.width + 46) + 'px';
		}else if(parseInt(img.style.width+0) >= 570){
			img.style.width = '570px';
			clearInterval(int);
		}
	}
}

function MudaImagem(){
	
	var _img = get('img');
	
	
	for(x=0;x<_img.length;x++){
		if(_img[x].className == 'imagemEventoDestaque3'){
			_img[x].onclick = function(){
				try{
					clearInterval(int);
					this.id = '';
					this.style.position = '';
					this.style.top = '';
					this.style.left = '';
					this.style.width = '80px';
					delete int;
				}catch(e){
					this.style.position = 'absolute';
					this.style.top = this.offsetTop + 'px';
					this.style.left = this.offsetLeft + 'px';
					this.id = 'id';					
					int = setInterval("aumenta('id')",1)
				}
			}			
		}
	}
	
}

function carrega(){
	try{
		OpensCommentsWindow();		
	}catch(e){}
	try{
		CloseCommentsWindow();
	}catch(e){}
	try{
		MoveCloseCommentsWindow();
	}catch(e){}
	try{
		ManageCommentsWindow();
	}catch(e){}
	try{
		ManageSelectField();
	}catch(e){}	
	try{
		AbreJogosTemporada();
	}catch(e){}	
	try{
		validaFormContato();
	}catch(e){}	
	try{
		MudaImagem();
	}catch(e){}	
	SetCookie();
	setInterval("SetCookie()",1000);
	
}

window.addEvent('domready',function(){
									carrega()
									});



