﻿var sCurrentSection = "latestnews";
var aHomepagemaincontentSections = new Array("latestnews","events","postevents");
var aHomepagemaincontentContent = new Array()    

function showContent(newContent) {
    sCurrentSection = newContent;
    oLoader = document.getElementById("homepagemaincontent_loading");
    oDiv = document.getElementById("homepagemaincontent_content");

    oLoader.style.display = "block";
    oDiv.style.display = "none";

    // change the linkbuttons
    for (i=0; i < aHomepagemaincontentSections.length; i++) {
        var oLinkButton = document.getElementById("homepagemaincontent_"+aHomepagemaincontentSections[i]);
        oLinkButton.className = "homepagemaincontent_heading"
        if (newContent == aHomepagemaincontentSections[i]) {
            oLinkButton.className += " selected"; 
            
            if (aHomepagemaincontentContent[newContent]) {
                oDiv.innerHTML = aHomepagemaincontentContent[newContent];
                oLoader.style.display = "none";
                oDiv.style.display = "block";                             
            } else {
                // load it from AJAX
                var contentLoader = new billosJAX();
                contentLoader.sendGetRequest("process.aspx", "action=HOMEPAGE_"+newContent, "contentLoaderprocessRetunedData");
            }
        }
    }    
}   

function contentLoaderprocessRetunedData(responseXML, responseText) {
    //alert(responseText)
    var sContent = "";
    var status = responseXML.getElementsByTagName("status")[0].childNodes[0].nodeValue
    if (status == "OK") { 
    
        var oItemNodes = responseXML.getElementsByTagName("item");       
        
        for (i = 0; i < oItemNodes.length; i++) {
            var sID = "", sHeading = "", sLocation = "", sDateTime = "", sDesc = "", sThumb = "", sCSSClass = "", linkURL = "", linkText = "";
            
            if (sCurrentSection == "latestnews") {
                sID = oItemNodes[i].getAttribute("id")
                sHeading = oItemNodes[i].getAttribute("heading")
                sLocation = "Author: " + oItemNodes[i].getAttribute("author")
                sDateTime = oItemNodes[i].getAttribute("articledate")
                sLinkText = "READ ARTICLE" 
                sLinkURL = oItemNodes[i].childNodes[1].childNodes[0].nodeValue
                if (sLinkURL) {                  
                    sLinkTarget = "_blank"                
                } else {
                    sLinkURL = "/newsmedia_detailed.aspx?ID="+sID
                    sLinkTarget = "_top"                
                }

                sThumb = oItemNodes[i].getAttribute("thumb")
                sDesc = oItemNodes[i].childNodes[0].childNodes[0].nodeValue
                
            } else {    // it's events or post events.
                sID = oItemNodes[i].getAttribute("id")
                sHeading = oItemNodes[i].getAttribute("title")
                sLocation = oItemNodes[i].getAttribute("suburb")+", "+oItemNodes[i].getAttribute("state")
                if (sLocation == ", ") { sLocation = ""; }
                sDateTime = oItemNodes[i].getAttribute("eventdate")
                
                sLinkURL = "/event_details.aspx?ID="+sID
                sLinkTarget = "_top"
                sLinkText = "VIEW DETAILS"                 
               
                
                sDesc = oItemNodes[i].childNodes[0].childNodes[0].nodeValue
            }

            sContent += '<div class="eventlistsitem clearfix">';
            if (sThumb) {
                sCSSClass = "_narrow";
                sContent += '    <div class="eventlistsitem_image"><a href="'+sLinkURL+'" target="' + sLinkTarget + '"><img src="'+sThumb+'" alt="" width="100" height="69" border="0" /></a></div>';
            }
            sContent += '    <div class="eventlistsitem_details'+sCSSClass+'">';
            sContent += '        <div class="eventlistsitem_heading">'+sHeading+'</div>';
            sContent += '        <div class="eventlistsitem_location">'+sLocation+'</div>';
            sContent += '        <div class="eventlistsitem_datetime">'+sDateTime+'</div>';
            sContent += '        <div class="eventlistsitem_desc">'+sDesc+'</div>';
            sContent += '        <a href="'+sLinkURL+'" target="' + sLinkTarget + '" class="greenarrow">'+sLinkText+'</a>';
            sContent += '    </div>';
            sContent += '</div>'; 
                         
        }

        if (!sContent) {
            if (sCurrentSection == "latestnews") {
                sContent = "There are currently no news items";
            } else if (sCurrentSection == "events") {
                sContent = "There are currently no upcoming events";
            } else {
                sContent = "There are currently no previous events"
            }                
        }         


        aHomepagemaincontentContent[sCurrentSection] = sContent; 
        showContent(sCurrentSection);
    } else {
        displayAjaxErrorMsg();     
    }
}

aHomepagemaincontentContent[sCurrentSection] = document.getElementById("homepagemaincontent_content").innerHTML
//showContent(sCurrentSection);