// JavaScript Document
var XHR_Login = XMLHTTPRequest();
var XHR_Opcoes = XMLHTTPRequest();
var XHR_Enquete = XMLHTTPRequest();
adEvento(window, 'load', initLogin);
var timerMeuMenu;
var destVisivel;
var arrLinkDestaques;
var timeToDest = 3000;
var resTimer;
var textH2;

function initLogin()
{
	adEvento(gE('formLogin'), 'submit', login);
	adEvento(gE('strEmail'), 'focus', function(){ if(gE('strEmail').value == "seu e-mail") gE('strEmail').value = ""; } );
	adEvento(gE('strSenha'), 'focus', function(){ gE('strSenha').value = ""; } );
	adEvento(gE('enqVoto'), 'click', votaEnquete);
	adEvento(gE('colunasSemanais'), 'change', mudaSecao);
	
	clock();
	
	arrLinkOpcoes = gE('materias').getElementsByTagName('ul')[0].getElementsByTagName('a');
	for(i=0; i<arrLinkOpcoes.length; i++)
	{
		adEvento(arrLinkOpcoes[i], 'click', exibeDestaquesPorOpcao);
	}
	
	if(gE('MeuMenu'))
	{
		adEvento(gE('MeuMenu'),'mouseover', mostraMeuMenu);
		adEvento(gE('MeuMenu'),'mouseout', function() { timerMeuMenu = setTimeout("escondeMeuMenu()",1500); });
		adEvento(document,'click', escondeMeuMenu);
		arrA = gE('MeuMenu').getElementsByTagName('a');
		for(i=0; i<arrA.length; i++)
		{
			if(arrA[i].nodeName == "A")
				adEvento(arrA[i],'click', escondeMeuMenu);
		}
	}
	
}

function mudaSecao(e)
{
		window.location = "http://www.imasters.com.br/secao/"+gE('colunasSemanais').value;
}

function votaEnquete(e)
{
	var tmp = 0;
	
	postString = getForm(gE('formEnquete'),0);
	XHR_Enquete.open('post', '/ajax.php?r=15&rand='+Math.floor(Math.random()*100), true);
	XHR_Enquete.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XHR_Enquete.onreadystatechange=responseEnquete;
	XHR_Enquete.send(postString);
}

function responseEnquete()
{
	if(XHR_Enquete.readyState == 4)
	{
		if (XHR_Enquete.status == 200) 
		{
			eval(unescape(XHR_Enquete.responseText.replace(/\+/g," ")));
			
			if(arrEnquete.length > 0)
			{
				while(gE('divEnquete').hasChildNodes()){ gE('divEnquete').removeChild(gE('divEnquete').firstChild); }
				gE('enqVoto').parentNode.removeChild(gE('enqVoto'));
		
				gE('divEnquete').appendChild(newUl = cE('ul',''));
				
				for(i=0; i<arrEnquete[1].length; i++)
				{
					newUl.appendChild(newLi = cE('li',''));
					newLi.appendChild(cE('strong',arrEnquete[1][i][2]+"% "));
					newLi.appendChild(cTN(' '+arrEnquete[1][i][0]));
					newLi.appendChild(cE('br'));
				}
			}
		}
	}
}

function timerDest()
{
	if(timeToDest != 0) resTimer = setTimeout("nextDest(); timerDest();", timeToDest);
}

function mudaDestTopo(e)
{
	if(typeof(e)=='undefined')var e=window.event;
    source=e.target?e.target:e.srcElement;
    //Correção para o bug do Konqueror/Safari
    if(source.nodeType==3)source=source.parentNode;
	
	if(resTimer) clearTimeout(resTimer);
	timeToDest = 0;
	showDest(source.firstChild.nodeValue);
}

function prevDest(e)
{
	nextVisivel = "0"+(destVisivel-1);
	if(nextVisivel == "00") nextVisivel = "05";
	showDest(nextVisivel); 
	if(e) return poin(e);	
}

function nextDest(e)
{
	nextVisivel = "0"+(parseInt(destVisivel)+1);
	if(nextVisivel == "06") nextVisivel = "01";
	showDest(nextVisivel); 
	if(e) return poin(e);	
}

function showDest(id)
{
	
	for(i=0; i<arrLinkDestaques.length; i++)
	{
		if(arrLinkDestaques[i].parentNode.className != "setas")
		{
			if(i+1 == 0+id)
				arrLinkDestaques[i].parentNode.className = "show";
			else
				arrLinkDestaques[i].parentNode.className = "";
		}
	}
	
	
	if(gE('d_'+destVisivel)) gE('d_'+destVisivel).className = "";
	if(gE('d_'+id))
	{
		destVisivel = id;
		gE('d_'+id).className = "show";
		/*
		h2Topo = gE('destaques').getElementsByTagName('h2')[0];
		while(h2Topo.hasChildNodes()) h2Topo.removeChild(h2Topo.firstChild);
		
		if(textH2[id] == "")
			textH2[id] = gE('d_'+id).getElementsByTagName('h2')[0].firstChild.cloneNode();
		
		h2Topo.appendChild(textH2[id]);*/
	}
}

function clock()
{
	dataNow = new Date;
	
	spanClock = gE('noticias').getElementsByTagName('h2')[0].getElementsByTagName('span')[0].firstChild;
	if(dataNow.getHours()<10) hora = "0"+dataNow.getHours();
	else hora = dataNow.getHours();
	
	if(dataNow.getMinutes()<10) minutos = "0"+dataNow.getMinutes();
	else minutos = dataNow.getMinutes();
	
	if(dataNow.getSeconds()<10) segundos = "0"+dataNow.getSeconds();
	else segundos = dataNow.getSeconds();
	
	spanClock.nodeValue = hora+":"+minutos+":"+segundos;
	setTimeout('clock();',1000);
}

function exibeDestaquesPorOpcao(e)
{
	postString = "strUrl="+getSrc(e).href.replace("http://"+document.domain,"");
	XHR_Opcoes.open('post', '/ajax.php?r=9&rand='+Math.floor(Math.random()*100), true);
	XHR_Opcoes.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XHR_Opcoes.onreadystatechange=responseOpcoes;
	XHR_Opcoes.send(postString);
	return poin(e);
}

function responseOpcoes()
{
	if(XHR_Opcoes.readyState == 4)
	{
		if (XHR_Opcoes.status == 200) 
		{
			eval(unescape(XHR_Opcoes.responseText.replace(/\+/g," ")));
			arrUlMaterias = gE('materias').getElementsByTagName('ul');
			
			if(vetConteudo.length == 8)
			{
				while(arrUlMaterias[1].hasChildNodes()) arrUlMaterias[1].removeChild(arrUlMaterias[1].firstChild);
				
				for(i=0; i<4 && vetConteudo[i][0] != ""; i++)
				{
					if((i % 2) == 1) newLi = cE('li', ""); else newLi = cE('li');
					newImg = cE('img', Array("src=http://static.imasters.com.br/img/icone/"+vetConteudo[i][0]+".jpg", "alt="), "");
					newSpan = cE('span', " "+vetConteudo[i][1]);
					newSpan2 = cE('span', Array("className=data"), " ("+vetConteudo[i][5]+")");
					newH3 = cE('h3');
					newA = cE('a', Array("href="+vetConteudo[i][2]), vetConteudo[i][3]);
					if(i < 2) newP = cE('p', vetConteudo[i][4]); else newP = cE('p', vetConteudo[i][4]);
					
					newH3.appendChild(newA);
					newLi.appendChild(newImg);
					newLi.appendChild(newSpan);
					newLi.appendChild(newSpan2);
					newLi.appendChild(newH3);
					newLi.appendChild(newP);
					arrUlMaterias[1].appendChild(newLi);
				}
				
				while(arrUlMaterias[3].hasChildNodes()) arrUlMaterias[3].removeChild(arrUlMaterias[3].firstChild);
				
				
				for(i=4; i<vetConteudo.length; i++)
				{
					if((i % 2) == 1) newLi = cE('li', Array("className=alt"), ""); else newLi = cE('li');
					newImg = cE('img', Array("src=http://static.imasters.com.br/img/icone/"+vetConteudo[i][0]+".jpg", "alt="), "");
					newSpan = cE('span', " "+vetConteudo[i][1]);
					newSpan2 = cE('span', Array("className=data"), " ("+vetConteudo[i][5]+")");
					newH3 = cE('h3');
					newA = cE('a', Array("href="+vetConteudo[i][2]), vetConteudo[i][3]);
					if(i < 10) newP = cE('p', Array("className=linha"), vetConteudo[i][4]); else newP = cE('p', vetConteudo[i][4]);
					
					
					newH3.appendChild(newA);
					newLi.appendChild(newImg);
					newLi.appendChild(newSpan);
					newLi.appendChild(newSpan2);
					newLi.appendChild(newH3);
					newLi.appendChild(newP);
					arrUlMaterias[3].appendChild(newLi);
				}
			}
		}
	}
}

function escondeMeuMenu(e)
{
	ul = gE('MeuMenu').getElementsByTagName("ul")[0];
	if(ul.style.display == 'block')
		ul.style.display = 'none';
	
}

function mostraMeuMenu(e)
{
	if(timerMeuMenu){ clearTimeout(timerMeuMenu); }
	Meumenu = gE('MeuMenu');
	if(Meumenu)
	{
		ul = Meumenu.getElementsByTagName("ul")[0];
		ul.style.display = 'block';
	}
}

function login(e)
{
	
	postString = getForm(gE('formLogin'));
	XHR_Login.open('post', '/meu/index.php?rand='+Math.floor(Math.random()*100), true);
	XHR_Login.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	XHR_Login.onreadystatechange=responseLogin;
	XHR_Login.send(postString);

	return poin(e);
}

function responseLogin()
{
	if(XHR_Login.readyState == 4)
	{
		if (XHR_Login.status == 200) 
		{
			strMsg = "";
			eval(unescape(XHR_Login.responseText.replace(/\+/g," ")));
			
			if(strMsg != "") { alert(strMsg); }
			else
			{
				remDiv = gE('login').getElementsByTagName("div")[0];
				while(remDiv.hasChildNodes())
				{
					remDiv.removeChild(remDiv.firstChild);
				}
				
				a_1 = cE('a',Array("href=http://meu.imasters.com.br"), "Meu iMasters");
				span_1 = cE("span");
				span_1.appendChild(a_1);
				
				gE('login').getElementsByTagName("div")[0].appendChild(span_1);
				
				textNode_1 = cTN("Olá ");
				insertAfter(gE('login').getElementsByTagName("div")[0].lastChild, textNode_1);
				
				strong = cE("strong", strUserNome);
				insertAfter(gE('login').getElementsByTagName("div")[0].lastChild, strong);
				
				textNode_2 = cTN(". Acesse suas opçoes personalizadas ao lado.");
				insertAfter(gE('login').getElementsByTagName("div")[0].lastChild, textNode_2);
				
				Meumenu = cE("div", Array("id=MeuMenu"), "");
				objUl = cE("ul","");
				for(i=0; i<arrMeuMenu.length; i++)
				{
					objLi = cE("li","");
					objA = cE("a",Array("href="+arrMeuMenu[i][1]), arrMeuMenu[i][0]);
					objLi.appendChild(objA);
					objUl.appendChild(objLi);
				}
				Meumenu.appendChild(objUl);
				
				insertAfter(gE('login').lastChild, Meumenu);
				adEvento(gE('MeuMenu'),'mouseover', mostraMeuMenu);
				adEvento(gE('MeuMenu'),'mouseout', function() { timerMeuMenu = setTimeout("escondeMeuMenu()",1500); });
				adEvento(document,'click', escondeMeuMenu);
				arrA = gE('MeuMenu').getElementsByTagName('a');
				for(i=0; i<arrA.length; i++)
				{
					if(arrA[i].nodeName == "A")
						adEvento(arrA[i],'click', escondeMeuMenu);
				}
					}
			
		}
	}
}



function hide2() {
	gE('banner').style.display = 'none';
}


