/* ************************************************* */
/* ******** Switch Content in Main Headline ******** */
/* ************************************************* */

// Global
var strLayoutType = 'type1'; // layout type of story
var intCurrentStory = 1; // id of story showing now
var intNewStory = 1; // id of story to switch to


// Replaces story in headline-main with 
function showStory(buttonNum,storyFile,layoutType)  {
	strLayoutType = layoutType;
	intNewStory = buttonNum;
	
	if (intCurrentStory != intNewStory) { // only load if new story
		var objContent = getObj("headline-content");
		var strContentUrl = '/english/14/main_hdline/' + storyFile + ''; // path to new story

		// display loading message
		objContent.innerHTML='<p>Loading news headline. . .</p>';

		// ---------------------------
		// ---- Get story content ----
		// ---------------------------		
		var strUrl = '';

		// add random number to prevent request from being cached
		var cache = false;
		var r;
	  	(cache) ? r='cache' : r=Math.random();

		strUrl = strContentUrl.indexOf("?")>=0 ? strContentUrl+"&amp;r="+r : strContentUrl+"?r="+r;

		// execute XMLHTTPRequest
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = changeStoryContent;
			req.open("GET", strUrl, true);
			req.send(null);
	
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req){
				req.onreadystatechange = changeStoryContent;
				req.open("GET", strUrl, true);
				req.send();	
			}
		}
	}
} // end


// Called when XMLHttpRequest is complete
function changeStoryContent() {
	if (req.readyState == 4 && req.status == 200){

		var objContainer = getObj("headline-main");
		var objContent = getObj("headline-content");

		// change number buttons
 		getObj("btn-" + intCurrentStory + "").className = '';
 		getObj("btn-" + intNewStory + "").className = 'on-story';		

		// write class for layout type
		if (strLayoutType == 'type1') { objContainer.className = ''; }
		else if (strLayoutType == 'type2') { objContainer.className = 'type2'; }

		// update content
		var strNewContent = req.responseText;
		objContent.innerHTML = strNewContent;

		intCurrentStory = intNewStory;
	}
}

