var http_request = false;
var width, img, imgSrc, articleNum, prev, next;
var topArray = new Array;
var bottomArray = new Array;
var b=1;
var num=100;
var prevIndex;
function makeRequest() {
	url = "news_app_en.xml";
	http_request = false;
	if (window.XMLHttpRequest) 
	{	// Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{	http_request.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) 
	{	// IE
		try 
		{	http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{	try 
			{	http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}

	if (!http_request) 
	{	alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = getContents;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function getContents() 
{	if (http_request.readyState == 4) 
	{	if (http_request.status == 200) 
		{	var xmldoc = http_request.responseXML;
			//xmldoc.IgnoreWhitespace = true;
			var xmldocNodes = xmldoc.getElementsByTagName("article");
			var xmldocPics = xmldoc.getElementsByTagName("image_large");
			//alert("xmldocNodes.length = " + xmldocNodes.length);
			for (i=0; i<xmldocNodes.length; i++) 
			{	if(xmldocNodes[i].nodeName == "article") 
				{	var article_image = xmldoc.getElementsByTagName("image_large").item(i).firstChild.data;
					imgSrc = "img" + i;
					imgSrc = new Image();
					imgSrc.src = article_image;
					var photo_credit = xmldoc.getElementsByTagName("image_credit").item(i).firstChild.data;
					if(photo_credit == "-") 
					{	var photo_credit = "";
					}
					var article_headline = xmldoc.getElementsByTagName("headline").item(i).firstChild.data;
					if(article_headline == "-") 
					{	var article_headline = "";
					}
					var article_abstract = xmldoc.getElementsByTagName("abstract").item(i).firstChild.data;
					var article_link= xmldoc.getElementsByTagName("link").item(i).firstChild.data;
					if(article_link == "-") 
					{	var article_link = "nolink";
					}
					if((article_link.indexOf("/") == 0) || (article_link == "nolink")) 
					{	var article_type = "internal";
					} 
					else if (article_link.indexOf("javascript") == 0) 
					{	var article_type = "media";
					} 
					else 
					{	var article_type = "external";
					}
					var top = "";
					top+='<table width="398" cellpadding="0" cellspacing="0" border="0">';
					if(article_link == "nolink") 
					{	top+='<tr><td align="center" height="250" valign="top" colspan="3"><img src="'+imgSrc.src+'" width="500" height="250" border="0" alt="'+article_headline+'"></td></tr>';
					} 
					else if(article_type == "external") 
					{	top+='<tr><td align="center" height="250" valign="top" colspan="3"><a href="javascript:openExternal(\''+article_link+'\',true);"><img src="'+imgSrc.src+'" width="500" height="250" border="0" alt="'+article_headline+'"></a></td></tr>';
					} 
					else 
					{	top+='<tr><td align="center" height="250" valign="top" colspan="3"><a href="'+article_link+'"><img src="'+imgSrc.src+'" width="500" height="250" border="0" alt="'+article_headline+'"></a></td></tr>';
					}
					top+=' <tr><td height="17" class="bgPstextW" align="right" colspan="3"><font size="-2" color="#333333"><I>'+photo_credit+'&nbsp;&nbsp;</I></font></td></tr>';
					top+=' <tr>';
					top+='	<td width="1" valign="top" class="bg3"><spacer type="block" width="1" height="1"></td>';
					top+=' <td valign="top">';
					top+=' <table height="90" cellpadding="0" cellspacing="0" border="0">';
					top+=' <tr>';
					top+='	 <td width="10" valign="top" rowspan="6"><spacer type="block" width="1" height="1"></td>';
					top+='		 <td width="480" height="7"><spacer type="block" width="1" height="1"></td>';
					top+='	 <td width="5" valign="top" rowspan="6"><spacer type="block" width="1" height="1"></td>';
					top+=' </tr>';
					top+=' <tr>';
					if(article_headline == "") 
					{	top+=' <td width="480" valign="top" class="newsHead"></td>';
					} 
					else 
					{	if(article_link == "nolink") 
						{	top+=' <td width="480" valign="top" class="newsHead"><span class="ajaxTitleNoLink">'+article_headline+'</span></td>';
						}
						else if(article_type == "external") 
						{	top+=' <td width="480" valign="top"><a href="javascript:openExternal(\''+article_link+'\',true);" class="ajaxTitle">'+article_headline+'</a></td>';
						} 
						else 
						{	top+=' <td width="480" valign="top"><a href="'+article_link+'" class="ajaxTitle">'+article_headline+'</a></td>';
						}
					}
					top+=' </tr>';
					top+=' <tr><td height="7"><spacer type="block" width="1" height="1"></td></tr>';
					if(article_link == "nolink") 
					{	top+=' <tr><td width="480" valign="top" class="newsHead"><span class="ajaxBodyNoLink">'+article_abstract+'</span></td></tr>';
					} 
					else if(article_type == "external") 
					{	top+='	 <tr><td width="480" valign="top"><a href="javascript:openExternal(\''+article_link+'\',true);" class="ajaxBody">'+article_abstract+'</a></td></tr>';
					} 
					else 
					{	top+=' <tr><td width="480" valign="top"><a href="'+article_link+'" class="ajaxBody">'+article_abstract+'</a></td></tr>';
					}
					top+=' <tr><td height="15"><spacer type="block" width="1" height="1"></td></tr>';
					top+=' </table>';
					top+=' </td>';
					top+='	<td width="1" valign="top" class="bg3"><spacer type="block" width="1" height="1"></td>';
					top+=' </tr>';
					top+='</table>';
					topArray[i+1] = top;
					var bottomnav = "";
					bottomnav+=' <td><a href="javascript:changeImg('+(i+1)+');setContents('+(i+1)+');" onclick="tabContentMeasure(\'en_GB\',' +(i+1)+')"><img src="/sichuan/images/wn'+(i+1)+'.gif" name="w'+(i+1)+'" width="20" height="17" border="0" alt="'+(i+1)+'"></a></td>';
					bottomArray[i+1] = bottomnav;
				}
			}
			var bottom = "";
			bottom+=' <table width="398" cellpadding="0" cellspacing="0" border="0">';
			bottom+=' <tr>';
			bottom+='	 <td width="1" valign="top" class="bg3" rowspan="2"><spacer type="block" width="1" height="1"></td>';
			bottom+='	 <td width="10" valign="top" rowspan="6" rowspan="2"><spacer type="block" width="1" height="1"></td>';
			bottom+=' <td width="480" align="right" valign="top">';
			bottom+=' <table cellpadding="0" cellspacing="0" border="0">';
			bottom+=' <tr>';
			for(i=1; i<bottomArray.length; i++) 
			{	bottom+= bottomArray[i];
			}
			bottom+=' <td><a href="javascript:Previous();" onMouseover="changeCtrlImg(11);" onMouseout="changeCtrlImg(11);" onclick="callMeasurement(\'prevButton\')"><img src="/sichuan/images/wn11.gif" name="w11" width="20" height="17" border="0" alt=""></a></td>';
			bottom+=' <td><div id="pauseDiv"><a href="javascript:changePauseImg(12);Pause();" onMouseover="changeCtrlImg(12);" onMouseout="changeCtrlImg(12);"><img src="/sichuan/images/wn12.gif" name="w12" width="13" height="17" border="0" alt=""></a></div></td>';
			bottom+=' <td><a href="javascript:Next();" onMouseover="changeCtrlImg(14);" onMouseout="changeCtrlImg(14);" onclick="callMeasurement(\'nextButton\')"><img src="/sichuan/images/wn14.gif" name="w14" width="20" height="17" border="0" alt=""></a></td>';
			bottom+=' </tr>';
			bottom+=' </table>';
			bottom+=' </td>';
			bottom+=' <td width="7" rowspan="2"><spacer type="block" width="1" height="1"></td>';
			bottom+='	 <td width="1" valign="top" class="bg3" rowspan="2"><spacer type="block" width="1" height="1"></td>';
			bottom+=' </tr>';
			bottom+=' <tr><td height="5"><spacer type="block" width="1" height="1"></td></tr>';
			bottom+=' <tr><td height="1" colspan="5" class="bg3"><spacer type="block" width="1" height="1"></td></tr>';
			bottom+=' </table>';
			document.getElementById("ajaxDivBottom").innerHTML = bottom;
			setContents();
		}
		else 
		{	alert("There was a problem with the request.");
		}
	} 
}

function setControls() 
{	if(articleNum == 1) 
	{	prev = topArray.length-1; 
	} 
	else 
	{	prev = articleNum-1;
	}
	if((articleNum+1) == topArray.length) 
	{	next = 1;
	} 
	else 
	{	next = articleNum+1;
	} 
	var controls = "";
}

function Previous() 
{	setContents(prev); 
}

function Pause() 
{	setContents(articleNum); 
}

function Next () 
{	setContents(next); 
}

function changeImg(imgNum) 
{	for(k=1; k<topArray.length; k++) 
	{	tabname = "w0";
		name = "w" + k;
		if (k != imgNum) 
		{	 document[name].src = "/sichuan/images/wn" + k + ".gif";
		}
		if (k == imgNum) 
		{	document[name].src = "/sichuan/images/wr" + k + ".gif";
			//if (k == 1) {	 
			//document[tabname].src = "/images/fr0.gif";
			//} else {
			//document[tabname].src = "/images/fn0.gif";
			//}
		}
	}
}

function changeCtrlImg(ctrlNum) 
{	ctrlName = "w" + ctrlNum;
	ctrlSrc = document[ctrlName].src;
	if (ctrlSrc.indexOf("wr") == -1) 
	{	document[ctrlName].src = "/sichuan/images/wr" + ctrlNum + ".gif";
	} 
	else 
	{	document[ctrlName].src = "/sichuan/images/wn" + ctrlNum + ".gif";
	}
}

function changePauseImg(pauseNum) 
{	pauseName = "w" + pauseNum;
	pauseSrc = document[pauseName].src;
	wn12Src = "<a href='javascript:changePauseImg(12);Pause();' onMouseover='changeCtrlImg(12);' onMouseout='changeCtrlImg(12);'><img src='/sichuan/images/wn12.gif' name='w12' width='13' height='17' border='0' alt=''></a>";
	wn13Src = "<a href='javascript:changePauseImg(13);Pause();' onMouseover='changeCtrlImg(13);' onMouseout='changeCtrlImg(13);'><img src='/sichuan/images/wn13.gif' name='w13' width='13' height='17' border='0' alt=''></a>";
	if (pauseSrc.indexOf("12") == -1) 
	{	document.getElementById("pauseDiv").innerHTML = wn12Src;
	} 
	else 
	{	document.getElementById("pauseDiv").innerHTML = wn13Src;
	}
}

function setContents(index) 
{	if(index) 
	{	num = index;
		articleNum = index;
		document.getElementById("ajaxDivTop").innerHTML = topArray[index];
		changeImg(index);
		setControls();
		if (prevIndex == index) 
		{	if(index >= topArray.length-1) 
			{	b = 1;
			} 
			else 
			{	b = index+1;
			}
			num = 100;
			pause = false;
			clearTimeout(timer);
			setTimeout("setContents()",1000);
		}
		prevIndex = index;
	} 
	else 
	{	if (num == 100) 
		{	document.getElementById("ajaxDivTop").innerHTML = topArray[b];
			articleNum = b;
			changeImg(b);
			setControls();
			b++;
			if (b >= topArray.length) 
			{	b = 1; 
			}
			timer = setTimeout("setContents()",10000);	 
		} 
		else 
		{	return;
		}
	}
}

function callMeasurement(button) 
{	if(button == "prevButton") 
	{	tabContentMeasure("en_GB",prev);
	}
	if(button == "nextButton") 
	{	tabContentMeasure("en_GB",next);
	}
}
