// JavaScript New Scroller
//News Public Variables for News
var newsShowIntv=null;
var newsHideIntv=null;
var newsGlowIntv=null;
var newsId=-1;
var newsTxt="";
var nwleft;
var hex ="FFFFFF";
var cRet=false;
var cCCount=0;
var	startHex="FFFFFF";//"7FA5E1";
var	endHex="FBD94F";

//runs newsRead function after the page loads


//This function run once to show the news for the first time
function newsRead(){
	document.getElementById("NewsPad").style.left="770px";
	showNews();
}

function showNews(){
	
	var newsTxt = getNews();
	document.getElementById("NewsPad").innerHTML=newsTxt;
	document.getElementById("NewsPad").style.color="#"+startHex;

	newsShowIntv=window.setInterval(scrollNews,30);
	
}

function scrollNews(){
	nwLeft = parseInt(document.getElementById("NewsPad").style.left);
	//alert(nwLeft);
	if(nwLeft<=13){
		window.clearInterval(newsShowIntv);
		newsShowIntv=null;
		newsShowIntv=window.setTimeout(pauseNews,5000);
	}
	else{
		nwLeft -=(nwLeft/5)+3;
		if(newsGlowIntv==null && nwLeft<=20){
			hex=startHex;
			newsGlowIntv=window.setInterval(glowNews,30);
		}
		
		document.getElementById("NewsPad").style.left=nwLeft+"px";
	}
	
}

function pauseNews(){
		window.clearTimeout(newsShowIntv);
		newsShowIntv=null;
		newsHideIntv=window.setInterval(hideNews,30);
	
}
function hideNews(){
	nwLeft = parseInt(document.getElementById("NewsPad").style.left);
	if(nwLeft<=-600){
		window.clearInterval(newsHideIntv);
		newsHideIntv=null;
		document.getElementById("NewsPad").style.left="600px";
		showNews();
	}
	else{
		nwLeft-=(Math.abs(nwLeft)/5)+3;
		document.getElementById("NewsPad").style.left=nwLeft+"px";
	}


}

function glowNews(){
	//alert(cCCount);
	var r,g,b;
	var cCycle=30;
	
	var cc;
	var c=hex2rgb(hex);

	if(cCCount>cCycle){
		//alert(cCCount+":"+hex);
		cRet=true;
	}
		
	if(!cRet){
		cCCount++;
		cc=hex2rgb(endHex);
	}
	else{
		cCCount--;
		cc=hex2rgb(startHex);
			
	}

	hex=rgb2hex({r:(c.r+Math.round((cc.r-c.r)/cCycle)),g:(c.g+Math.round((cc.g-c.g)/cCycle)),b:(c.b+Math.round((cc.b-c.b)/cCycle))});
	
	document.getElementById("NewsPad").style.color="#"+hex;

	if(cCCount<=0) {
		cRet=false;
		window.clearInterval(newsGlowIntv);
		newsGlowIntv=null;
		document.getElementById("NewsPad").style.color="#"+startHex;
	}


}

///get NEWS  function :
function getNews(){
	var news=new Array();
	// Add News text here as html. can use links and class newsLink. 
	//MAKE SURE NOT TO PUT A FULL STOP AFTER ANY NEWS ITEM
	news.push("<a href='http://www.cdis.com.br' target='_self' class='newsLink'>Portal da Estrat&eacute;gia &ndash; Enfim, a gest&atilde;o sem complica&ccedil;&otilde;es!</a>");
	//news.push("<a href='produtos.html' target='pages' class='newsLink'>Com o Portal da Estrat&eacute;gia isso &eacute; coisa do passado!</a>");	
	news.push("<a href='http://www.cdis.com.br' target='_self' class='newsLink'>Clique aqui para saber mais.</a>");	
	newsId++;	
	return news[newsId%news.length];
}



function hex2dec(h){return parseInt(h,16);}
function dec2hex(d) {return d.toString(16);}
function rgb2hex(c){
	var  r,g,b;
	r=dec2hex(c.r);
	if(r.length>2) r=r.substr(0,2);
	if(r.length<2) r="0"+r;
	g=dec2hex(c.g);
	if(g.length>2) g=g.substr(0,2);
	if(g.length<2) g="0"+g;
	b=dec2hex(c.b);
	if(b.length>2) b=b.substr(0,2);
	if(b.length<2) b="0"+b;
	//c= {r:200,g:150,b:200}
	//return string
	return (r+g+b).toString().substr(0,6);
	
}
function hex2rgb(c){
	var r=g=b=0;
	var strR,strG,strB;
	strR = c.substr(0,2);
	strG = c.substr(2,2);
	strB = c.substr(4,2);
	if(strR)  r=hex2dec(strR);
	if(strG)  g=hex2dec(strG);
	if(strB)  b=hex2dec(strB);
	return {r:r,g:g,b:b}
}


