var objhttp;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	objhttp = new XMLHttpRequest();
    if (objhttp.overrideMimeType) 
    	objhttp.overrideMimeType('text/xml');
} 
else if (window.ActiveXObject) 
{ // IE
	try {
    	objhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (e) 
	{
	    try {
    	    objhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
		catch (e) {}
    }
 }	

var jobId = 1;//current jobId
var interval;//interval to get the articles
var loadingInterval;//interval for loading
var boxColors = new Array("#e5ffbd","#cae893","#cbf581","#b9eb63","#9bdb50","#78b916","#649f0b");//colors of box articles
var sections = new Array("HOME","HITECH","MARKET","WALLS","WORLD");//possible section to bring the articles from
var arcColors = new Array(18);//background colors of articles boxes
var mouseOverBoxColor = 'White';//color for mouseover box	
var lastSect = -1;//current tab
var updateDelay = 30000;//delay for getting the articles 
var loadingDelay = 2000;//delay for loading
	
/*changing section when clicking on tab*/
function chooseSection(sect)
{
	if (sect != lastSect)
	{
		document.getElementById("rightTab"+sect).className = 'rightTabOn';
		document.getElementById("backTab"+sect).className = 'backTabOn';
		document.getElementById("leftTab"+sect).className = 'leftTabOn';
		
		if (lastSect != -1){
			document.getElementById("rightTab"+lastSect).className = 'rightTabOff';
			document.getElementById("backTab"+lastSect).className = 'backTabOff';
			document.getElementById("leftTab"+lastSect).className = 'leftTabOff';
		}
		
		lastSect = sect;
		//changing the jobId to 1 for getting all the articles of the section
		jobId = 1;		
		stopInterval();
		startInterval();		
	}
}		

/*stop getting articles*/
function stopInterval()
{
	window.clearInterval (interval);
	interval = "";
}

/*trim string function to delete spaces at a begginnig and the end of the string*/
function trimString(str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function setDateAndTime(tm)
{
	if (tm){
		var ar=tm.split(" ");			
		document.getElementById("lastUpdDate").innerHTML = ar[0];//update date
		document.getElementById("lastUpdTime").innerHTML = "&nbsp;"+ar[1]+"&nbsp;";//update time
	}
}

function buildBoxes()
{
	var divArcs="";
	for (var cnt = 0; cnt < 18; cnt++)									
		divArcs+= '<div align=right id=arc'+cnt+' class="arcBox txtArc'+cnt+'" onmouseover="this.style.backgroundColor = mouseOverBoxColor;" onmouseout=OnMouseOutBox('+cnt+');></div>';
	divArcs+= '<div id="newsMapLoading" class="newsMapLoading"></div>';
	var elm = document.getElementById("arcDivs");
	if (elm)
		elm.innerHTML = divArcs;		
}

function getArticles()
{	
	var arcElm,counter,slug,headline,color;//article details
	if ((objhttp.readyState==4) && (objhttp.status == 200))
	{			
		var respText = objhttp.responseText;//get response text	
		if(respText != "")//if there is response text
		{			
			respText=trimString(respText);
			var ar=respText.split("@@@");			
			var len=ar.length;
			if (len != 3){				
				
				for (var i = len-2; i > 2; i--)
				{						
					if (trimString(ar[i]) == "")
						continue;
					tmp=ar[i].split("@@");				
					arcLen=tmp.length;

							
					counter = tmp[0];
					if (counter > 17)//to much articles
						continue;						

					arcElm = document.getElementById("arc"+counter);
					if (arcElm){	
						if (arcLen == 2)
							color = tmp[1];														
						else{
							slug = tmp[1];						
							headline = tmp[2];
							color = tmp[3];					
							arcElm.innerHTML = "<a class=arcHeadline target=_blank href='/tmc/article.jhtml?ElementId="+slug+"&log=newsmap'>"+headline+"</a>";						
						}					
						arcElm.style.backgroundColor = boxColors[color];
						arcColors[counter] = boxColors[color];										
					}
				}	
				var totalArc = ar[2];
				for (var i = 17; i >= totalArc; i--){ 	
					arcElm = document.getElementById("arc"+i);
					if (arcElm){
						arcElm.innerHTML = "";
						arcElm.style.backgroundColor = "White";
					}
					arcColors[i] = "White";
				}				
			}
			//setDateAndTime(ar[1]);
			jobId = ar[0];//update job id			
		}				
	}
}

function stopLoadingInterval()
{	
	var elm = document.getElementById("newsMapLoading");
	if (elm)
		elm.style.display = 'none';	
	window.clearInterval(loadingInterval);
	loadingInterval = "";	
}

function loadingArticles()
{	
	stopLoadingInterval();	
	var elm = document.getElementById("newsMapLoading");
	if (elm)
		elm.style.display = 'block';
	loadingInterval = setInterval('stopLoadingInterval()', loadingDelay); 	
}

function getChangedArc(){	
	try{	
		var parameters = "jobId="+jobId+"&section="+sections[lastSect];
		loadingArticles();		
		objhttp.open ("POST", "/web2/newsmap/getChangedArticles.jhtml", true);
		objhttp.onreadystatechange=getArticles;				
		objhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;");				
		objhttp.send(parameters);			
	}catch(e){}					
}

/*start getting the articles*/
function startInterval()
{
	getChangedArc();
	interval = setInterval('getChangedArc()', updateDelay); 	
}	

function OnMouseOutBox(elm)
{
	document.getElementById("arc"+elm).style.backgroundColor = arcColors[elm];
}

function showColors()
{
	for(var i=0; i < boxColors.length;i++)
		document.write('<img src=/tmc/i/0.gif style="background-color : '+boxColors[i]+'; height:10; width:30;">&nbsp;');
}