// JScript source code
// Script to request most recent hotnew item from server
// and display headline and portion of story text, making 
// entire text a link to the provided URL.
//
// Format:  (xml)
//  <xml>
//  <hotnews>
//      <newsItem>
//          <headline> </headline>
//			<story> </story>
//          <url>   </url>
//      </newsItem>
//  </hotnews>


// Define container var for all other variables (prevent name clash)
var HotNews = {};

// Define the data source
HotNews.DataSrc = rootFlashLocation + 'hot_news/latestNews.php';

// Define max characters for headline and story.  Beyond that 
// we truncate and append ellipses (...)
HotNews.MaxCharHeadline = 58;
HotNews.MaxCharStory = 140;

// Global used for document request.
HotNews.req = null;


// Use XMLHttpRequest to fetch the data file from the server
if (window.XMLHttpRequest) { // code for IE7, Firefox, Opera, Safari, etc.
    HotNews.req = new XMLHttpRequest();
} else if (window.ActiveXObject) { // code for IE6, IE5
    HotNews.req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (null !== HotNews.req) {
    HotNews.req.onreadystatechange = processReqChange;
    HotNews.req.open("GET", HotNews.DataSrc, true);
    HotNews.req.send(null);
} else {
    alert('Your browser cannot handle this script');
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (HotNews.req.readyState == 4) {
        // only if "OK"
        if (HotNews.req.status == 200) {
            displayNewsItem();  // Call display function.
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                  HotNews.req.statusText);
        }
    }
}

// Function to parse returned data and construct output text, then write that text.
function displayNewsItem() {
    var OutputStr = 'Sorry, we are unable to display the current Hot News.<br />Please visit the <a href="../../hot_news/2010.htm">Hot News</a> page instead.';

    var headline = null;
    var story = null;
    var url = null;
    
    var xmlDoc = HotNews.req.responseXML.documentElement;
    x = xmlDoc.getElementsByTagName("newsItem");    // We only care about the first one if there are more.
    if (x) {
	    if (x[0].getElementsByTagName("headline")[0]) {
	        headline = x[0].getElementsByTagName("headline")[0].childNodes[0].nodeValue;
	        if ( headline.length > HotNews.MaxCharHeadline ) {
	        	headline = headline.substr(0,HotNews.MaxCharHeadline) + ' ...';
	        }
	    
		    if (x[0].getElementsByTagName("story")[0]) {
		    	story = x[0].getElementsByTagName("story")[0].childNodes[0].nodeValue;
		        if ( story .length > HotNews.MaxCharStory ) {
		        	story = story .substr(0,HotNews.MaxCharStory) + ' ...';
		        }
		    }
		    if (x[0].getElementsByTagName("url")[0]) {
		        url = x[0].getElementsByTagName("url")[0].childNodes[0].nodeValue;
		    }
		    // Now construct our story. Make the headline be the link.
		    OutputStr = '<table>';
		    // url is optional (?)
		    if ( url ) {
		    	OutputStr += '<tr><td><span class="hotNewsHeadline">' + '<a href="' + url + '">' + headline + '</a></span></td></tr>';
		    }
		    else {
		    	OutputStr += '<tr><td><span class="hotNewsHeadline">' + headline + '</span></td></tr>';
		    }
		    // story text is optional. 
		    if ( story ) {
		    	OutputStr += '<tr><td><span class="hotNewsStory">' + story + '</span></td></tr>';
		    }
	    	OutputStr += '</table>'; // Close the table.
	    } // End if headline.
    }
    // Get handle to location for data.
    dest = document.getElementById("HotNewsFeed");
    // Update inplace. 
    dest.innerHTML = OutputStr ;
}


