var wwwroot = "http://www.chaosserver.net";
var wwwHomePage = wwwroot + "/index.html";
var wwwArcRoot = wwwroot + "/archives";
var xmlHttpRequest;
var debugLevel = 0;

function getDateTag(yearsAgo) {
    var dateString = (new Date()).getDate();
    if(dateString < 10) {
        dateString = "0" + dateString;
    }
    
    var monthString = (new Date()).getMonth();
    monthString += 1;
    if(monthString < 10) {
        monthString = "0" + monthString;
    }
    var yearString = (new Date()).getYear();
    yearString += 1900 - yearsAgo;    
    
    if(debugLevel > 1) alert("Getting (yearString + monthString + dateString): "
	    + yearString + " + " + monthString + " + " + dateString);
    
    return (yearString + "" + monthString + "" + dateString);
}

function getArchiveUrl() {
    var monthString = (new Date()).getMonth();
    monthString += 1;
    if(monthString < 10) {
        monthString = "0" + monthString;
    }
    var yearString = (new Date()).getYear();
    yearString += 1899;
    
    return (wwwArcRoot
        + yearString 
        + "/" 
        + monthString
        + "/index.html");        
}

/*
  Triggers a request to get back the HTML page for one year ago.
*/
function insertArchiveEntry() {
    var fullLocation = getArchiveUrl();
    
    if (window.XMLHttpRequest) {
        xmlHttpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    if(debugLevel > 0) alert("Getting Archive Page [" + fullLocation + "]");
	
    xmlHttpRequest.onreadystatechange = processReqChange;
    xmlHttpRequest.open("GET", fullLocation, true);
    xmlHttpRequest.send(null);
}

/**
 This method is called on the result.
*/
function processReqChange() {
    if (xmlHttpRequest.readyState == 4) {
        if (xmlHttpRequest.status == 200) {
            var dateTag = getDateTag(1);
	    if(debugLevel > 3) alert("Matching archive entry of: [" + dateTag + "]");
            myregexp = new RegExp("<h2><a name=\""+dateTag+"\"></a>(.*?) \\((../../....)\\)</h2>");
            mymatch = myregexp.exec(xmlHttpRequest.responseText);
            if(mymatch) {
                var title = mymatch[1];
                var date = mymatch[2];
                
                var mainContent = document.getElementById('main-content');
                // alert(mainContent.tagName);
                var contentChild = mainContent.firstChild;
                var count = 0;
                
                while(contentChild.tagName != 'DIV'
                        || contentChild.className != 'story'
                        || count < 1) {
                    
					/*		
                    alert(contentChild 
                        + ":" + contentChild.tagName
                        + ":" + contentChild.className
                        + ":" + count);
					*/
                        
                    if(contentChild.tagName == 'DIV'
                        && contentChild.className == 'story') {
                        
                        count++;
                    }
                    
                    contentChild = contentChild.nextSibling;
                }
                
                var newStory = document.createElement("div");
                newStory.id = "old-story";
                newStory.className = "story";
                
                var heading = document.createElement("h2");
                heading.appendChild(document.createTextNode("" + title + " (" + date + ")"));
                newStory.appendChild(heading);
                mainContent.insertBefore(newStory, contentChild);
                
                var childParagraph = document.createElement("p")
                var childLink = document.createElement("a");
				
                childLink.appendChild(document.createTextNode("One year ago: read this old story from the archives..."));
                childLink.href = getArchiveUrl() + "#" + dateTag;
                
                childParagraph.appendChild(childLink);
                newStory.appendChild(childParagraph);
               
                
            } else {
				// alert("no match...");
			}
        }
    }
}

function loadPermalink() {
  // First check the current homepage
  if (window.XMLHttpRequest) {
      xmlHttpRequest = new XMLHttpRequest(); 
  } else if (window.ActiveXObject) {
      xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
  }
  
  xmlHttpRequest.onreadystatechange = processHomepageArchiveSearch;
  xmlHttpRequest.open("GET", wwwHomePage, true);
  xmlHttpRequest.send(null);  
}

/**
 This method is called on the result.
*/
function processHomepageArchiveSearch() {
    if (xmlHttpRequest.readyState == 4) {
        if (xmlHttpRequest.status == 200) {
            var documentHash = document.location.hash.substring(1);

            if(debugLevel > 3) alert("Matching archive entry of: [" + documentHash + "]");
            myregexp = new RegExp("<h2><a name=\""+documentHash+"\"></a>(.*?) \\((../../....)\\)</h2>");
            mymatch = myregexp.exec(xmlHttpRequest.responseText);
            if(mymatch) {
                document.location=wwwHomePage + "#" + documentHash;
            } else {
               var year = documentHash.substring(0,4);
               var month = documentHash.substring(4,6);
               
               if (window.XMLHttpRequest) {
                   xmlHttpRequest = new XMLHttpRequest(); 
               } else if (window.ActiveXObject) {
                   xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
               }
  
               xmlHttpRequest.onreadystatechange = processArchiveSearch;
               xmlHttpRequest.open("GET", wwwArcRoot + "/" + year + "/" + month + "/index.html", true);
               xmlHttpRequest.send(null);  
           }
        }
    }
    document.getElementById("errormessage").style.visibility = 'visible';
}

function processArchiveSearch() {
    if (xmlHttpRequest.readyState == 4) {
        if (xmlHttpRequest.status == 200) {
            var documentHash = document.location.hash.substring(1);

            if(debugLevel > 3) alert("Matching archive entry of: [" + documentHash + "]");
            myregexp = new RegExp("<h2><a name=\""+documentHash+"\"></a>(.*?) \\((../../....)\\)</h2>");
            mymatch = myregexp.exec(xmlHttpRequest.responseText);
            if(mymatch) {
               var year = documentHash.substring(0,4);
               var month = documentHash.substring(4,6);
               
                document.location=wwwArcRoot + "/" + year + "/" + month + "/index.html#" + documentHash;
            } else {
            }
        } else {
        }
    }
    document.getElementById("errormessage").style.visibility = 'visible';
}


