﻿window.onload = getKSBRSSNews;

function initMenu(){
    // Check to see if the "MainNav" DIV exists.
    if(document.getElementById("MainNav")){
        // Get the "MainNav" element.
        var mainNav = document.getElementById("MainNav");
        // Get all og the img tags within the "MainNav" element.
        var navButtons = mainNav.getElementsByTagName("img");
        // Loop through each of the elements in the "navButtons" array.
        for(i=0; i<navButtons.length; i++){
           navButtons[i].originalSrc = navButtons[i].src;
           // Get the first part of the "src" property up until the last DOT (i.e. the string up to the file extension).
           var src_pt1 = navButtons[i].src.substr(0, navButtons[i].src.lastIndexOf("."))
           // Get the second part of the "src" property (i.e. the file extension, including the DOT).
           var src_pt2 = navButtons[i].src.substr(navButtons[i].src.lastIndexOf("."), navButtons[i].src.length);
           // Assign the "mouseOverSrc" property a string value.
           // The string value is made up of the first and second parts of the original "src" property and inserts the "Over" string in between them.
           navButtons[i].mouseOverSrc = src_pt1+"Over"+src_pt2;
           // Check to see if the link on the current button is equal to the current page.
           if(navButtons[i].parentNode.href == window.location){
               // Set the "src" attribute of the current button to be the same as the "mouseOverSrc" property.
               navButtons[i].src = navButtons[i].mouseOverSrc;
               // Do NOT assign onmousover or onmouseout event handlers to the current image.
           }
           else{       
               // Assign a function to the "onmouseover" event handler.
               navButtons[i].onmouseover = mouseOver;
               // Assign a function to the "onmouseout" event handler.
               navButtons[i].onmouseout = mouseOut;
           }
           // End of IF Else statement.
        }
        // End of FOR Loop.
    }
    // End of IF statemnent.
}
// End of function "initMenu()"

// Define the mouse over and mouse out functions.
function mouseOver(){ this.src = this.mouseOverSrc; }
function mouseOut(){ this.src = this.originalSrc; }

function getKSBRSSNews(){
    // Check to see if the "newsbox" DIV exists.
    if(document.getElementById("newsbox")){
        // Get the "newsbox" element.
        var newsbox = document.getElementById("newsbox");
        // Send a request to get the appropriate information.	
	    // Create XmlHttpObject
	    var xmlHttp = createHttpRequestObj();
	    // Check to see if the "xmlHttp" object was created
	    if (xmlHttp == null){
		    // The "xmlHttp" object was NOT created
		    alert ("Browser does not support HTTP Request");
		    return;
	    }
	    // Build URL and append the relevenat querystring variables
	    var url;
	    url = "xml/KSBRSSProxy.asp";
	    url += "?sid="+Math.random();
	    xmlHttp.onreadystatechange = function() {
		    if(xmlHttp.readyState == 4){
			    // Get the XML Document object.
			    var xmlDoc = xmlHttp.responseXML;
			    // Get the XML Text.
			    var xmlText = xmlHttp.responseText;
				// Clear the newsbox.
				newsbox.innerHTML = "";
			    // Get all of the news stories.
			    var news = xmlDoc.getElementsByTagName("item");
				// Check to see if there is at least ONE news item to be displayed.
			    if(news.length > 0){
					// Create the news box header.
					var newsHeader = new Image();
					newsHeader.src = "graphics/NewsHeader.gif";
					newsHeader.alt = "Keep Scotland Beautiful News"
					newsHeader.setAttribute("title", "Keep Scotland Beautiful News");
					newsHeader.setAttribute("usemap", "KSBLogo")
					// Append the newsbox header to the newsbox.
					newsbox.appendChild(newsHeader);
					// cerate a new list element.
			        var newsList = document.createElement("ul");
			        // Loop through each of the news items.
			        for(i=0; i<news.length; i++){
			            // Get the current news story details.
			            var storyTitle = getNodeValue(news[i].getElementsByTagName("title")[0]);
			            var storyLink = getNodeValue(news[i].getElementsByTagName("link")[0]);
						
						// Create the story link (anchor).
			            var storyLinkAnchor = document.createElement("a");
			            storyLinkAnchor.href = storyLink;
			            storyLinkAnchor.setAttribute("title", "Check out the Keep Scotland Beautiful News");
			            storyLinkAnchor.onclick = function(){ return doExternalLink(this); };
			            storyLinkAnchor.appendChild(document.createTextNode(storyTitle));
			            
						// Create a new list item and append the the story link.
			            var storyItem = document.createElement("li");
			            storyItem.appendChild(storyLinkAnchor);
						
						// Append the new list item to the list element.
			            newsList.appendChild(storyItem);
			        }
			        // End of FOR Loop.
			        newsbox.appendChild(newsList);
			    }
			    else{
					// Show the user that the news is not available.
			    	newsbox.innerHTML = "No news available.";
			    }
			    // End of IF Else statment.
		    }
			else{
				// Show the user that the news is being retrieved.
				newsbox.innerHTML = "Retrieving news from <a href=\"http://www.keepscotlandbeautiful.org\" title=\"Keep Scotland Beautiful\" onclick=\"return doExternalLink(this);\">Keep Scotland Beautiful</a>...";	
			}
		    // End of IF Else statement
	    }
	    // End of "xmlHttp.onreadystatechange" function
	    // Open a GET request to the appropriate URL in asynchronus mode.
	    xmlHttp.open("GET", url, true);
	    // Send the request.
	    xmlHttp.send(null);
    }
    // End of IF statement.
}
// End of function "getKSBRSSNews()".