var Archive = Class.create();
var Tab = Class.create();

Archive.prototype = {

	initialize: function() {

		this.ajaxPath = "ACT_archive.cfm";

		// Setup Dates
		this.date = new Date();
		this.months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		this.startMonth = this.date.getMonth();
		this.startYear = this.date.getYear();
		this.endMonth = this.startMonth;
		this.endYear = this.startYear;
		this.setStartDate(0);
		this.setEndDate(-2);

		// Setup Archive Components
		this.theElement = $("archive");
		this.theElement.id = "archive-js";
		this.archiveContainer = $("box-archive");
		this.archiveContainer.id = "box-archive-js";
		this.archiveControls = $("archive-controls");
		this.theElement.nextMonth = $("next-month");
		this.theElement.prevMonth = $("prev-month");
		
		// Check next button exists else create it
		if (this.theElement.nextMonth == null) {
			this.theElement.nextMonth = document.createElement("a");
			this.theElement.nextMonth.id = "next-month";
			this.theElement.nextMonth.style.display = "none";
			this.archiveControls.appendChild(this.theElement.nextMonth);
		}
		
		// Check previous button exists else create it
		if (this.theElement.prevMonth == null) {
			this.theElement.prevMonth = document.createElement("a");
			this.theElement.prevMonth.id = "prev-month";
			this.theElement.prevMonth.style.display = "none";
			this.archiveControls.appendChild(this.theElement.prevMonth);
		}
		
		// Setup next and previous controls
		this.theElement.prevMonth.href = "#archive-js";
		this.theElement.nextMonth.href = "#archive-js";
		this.theElement.nextMonth.onclick = this.clickNext.bindAsEventListener(this);
		this.theElement.prevMonth.onclick = this.clickPrev.bindAsEventListener(this);

	},

	clickNext: function(e) {
	
		// Hide Next and Previous for AJAX request
		this.theElement.nextMonth.style.display = "none";
		this.theElement.prevMonth.style.display = "none";

		// Increment Dates
		this.setStartDate(1);
		this.setEndDate(1);

		// Shift Tabs
		var tabs = $("tab-1","tab-2","tab-3");
		for (x=0;x<tabs.length;x++) tabs[x].setAttribute("id","");
		this.theElement.removeChild(tabs[2]);
		tabs[0].setAttribute("id","tab-2");
		tabs[1].setAttribute("id","tab-3");
		
		var tabsContent = $("tab-1-content","tab-2-content","tab-3-content");
		for (x=0;x<tabsContent.length;x++) tabsContent[x].setAttribute("id","");
		this.theElement.removeChild(tabsContent[2]);
		tabsContent[0].setAttribute("id","tab-2-content");
		tabsContent[1].setAttribute("id","tab-3-content");

		// Create New Tab
		var newTab = new Tab(this.months[this.startMonth], this.startYear, "tab-1");
		this.theElement.insertBefore(newTab.articles, this.theElement.firstChild);
		this.theElement.insertBefore(newTab.theElement, this.theElement.firstChild);

		// Set Active Tab
		var activeTab = document.getElementsByClassName("activeTab");
		if (activeTab.length < 1) tabsContent[0].className = "activeTab";

		// Get the Articles
		this.getArticles(this.startMonth, this.startYear, newTab.theElement.id);
		
	},

	clickPrev: function(e) {
	
		// Hide Next and Previous for AJAX request
		this.theElement.nextMonth.style.display = "none";
		this.theElement.prevMonth.style.display = "none";

		// Decrement Dates
		this.setStartDate(-1);
		this.setEndDate(-1);

		// Shift Tabs
		var tabs = $("tab-1","tab-2","tab-3");
		for (x=0;x<tabs.length;x++) tabs[x].setAttribute("id","");
		this.theElement.removeChild(tabs[0]);
		tabs[2].setAttribute("id", "tab-2");
		tabs[1].setAttribute("id", "tab-1");

		var tabsContent = $("tab-1-content","tab-2-content","tab-3-content");
		for (x=0;x<tabsContent.length;x++) tabsContent[x].setAttribute("id","");
		this.theElement.removeChild(tabsContent[0]);
		tabsContent[2].setAttribute("id","tab-2-content");
		tabsContent[1].setAttribute("id","tab-1-content");

		// Create New Tab
		var newTab = new Tab(this.months[this.endMonth], this.endYear, "tab-3");
		this.theElement.insertBefore(newTab.theElement, this.theElement.firstChild);
		this.theElement.insertBefore(newTab.articles, this.theElement.firstChild);
		
		// Set Active Tab
		var activeTab = document.getElementsByClassName("activeTab");
		if (activeTab.length < 1) tabsContent[2].className = "activeTab";

		// Get the Articles
		this.getArticles(this.endMonth, this.endYear, newTab.theElement.id);
		
	},

	getArticles: function(theMonth, theYear, tabID) {

		var articleVars = "month=" + theMonth + "&year=" + theYear + "&tabid=" + tabID;		
		var ajaxReq = new Ajax.Request(this.ajaxPath, { method:'get', parameters: articleVars, onComplete: this.gotAjaxResponse });

	},

	gotAjaxResponse: function(xml) {

		var theXML = xml.responseXML;

		// Collect XML Nodes
		var xmlTabID = theXML.getElementsByTagName("tabid");
		var xmlArticles = theXML.getElementsByTagName("entry");
		
		// Get Tab ID
		var xmlTabID = xmlTabID[0].childNodes[0].nodeValue;
		var xmlTabContent = xmlTabID + "-content";
		var thisTab = $(xmlTabContent);

		//Loop through the articles
		if (xmlArticles.length > 0) {
			for(x=0; x<xmlArticles.length; x++) {
				var newTabArticle = thisTab.appendChild(document.createElement("li"));
				var newTabArticleLink = newTabArticle.appendChild(document.createElement("a"));
				
				var articleTitle = xmlArticles[x].getElementsByTagName("title");
				var articleLink = xmlArticles[x].getElementsByTagName("url");
				
				var newTabArticleText = newTabArticleLink.appendChild(document.createTextNode(articleTitle[0].childNodes[0].nodeValue));
				newTabArticleLink.href = articleLink[0].childNodes[0].nodeValue;
			}
		} else {
			var newTabArticle = thisTab.appendChild(document.createElement("li"));
			var newTabArticleLink = newTabArticle.appendChild(document.createTextNode("There are no archived articles for this month"));	
		}
		
		// Get Next and Previous Values
		var xmlPrev = theXML.getElementsByTagName("prev");
		var xmlNext = theXML.getElementsByTagName("next");
		var nextFlag = xmlNext[0].childNodes[0].nodeValue;
		var prevFlag = xmlPrev[0].childNodes[0].nodeValue;
		
		// Reinstate Next and Previous Buttons
		var nextMonth = $("next-month");
		if (nextFlag == "true") nextMonth.style.display = "block";		
		var prevMonth = $("prev-month");
		if (prevFlag == "true") prevMonth.style.display = "block";

	},

	setStartDate: function(numMonths) {
		
		this.startMonth = this.startMonth + numMonths;
	
		if (this.startMonth < 0) {
			this.startMonth = this.startMonth + 12;
			this.startYear = this.startYear - 1;	
		}
		
		if (this.startMonth > 11) {
			this.startMonth = this.startMonth - 12;
			this.startYear = this.startYear + 1;
		}
		
		if (this.startYear < 1900) this.startYear = this.startYear + 1900;
		
	},
	
	setEndDate: function(numMonths) {
	
		this.endMonth = this.endMonth + numMonths;
	
		if (this.endMonth < 0) {
			this.endMonth = this.endMonth + 12;
			this.endYear = this.endYear - 1;
		}
		
		if (this.endMonth > 11) {
			this.endMonth =this.endMonth - 12;
			this.endYear = this.endYear + 1;
		}
		
		if (this.endYear < 1900) this.endYear = this.endYear + 1900;
	
	}

};

Tab.prototype = {

	initialize: function(month, year, id) {

		this.theElement = document.createElement("h3");
		this.theElement.setAttribute("id", id);

		this.label = month + " '" + year.toString().substr(2);

		this.articles = document.createElement("ul");
		this.articles.setAttribute("id",id + "-content");
		
		this.link = this.theElement.appendChild(document.createElement("a"));
		this.link.setAttribute("href", "#archive-js");
		this.link.appendChild(document.createTextNode(this.label));
		
		this.theElement.onclick = function() {
			var activeTabs = document.getElementsByClassName("activeTab");
			for (i=0; i<activeTabs.length; i++) activeTabs[i].className = "";
			var theTabContentID = this.id + "-content";
			var theTabContent = $(theTabContentID);
			theTabContent.className = "activeTab";
		}

	}
			
};

function main() {

	// Create the Archive
	var myArchive = new Archive(); 
	
	// Setup preloaded tabs
	var preLoadedTabs = $("tab-1","tab-2","tab-3");
	
	for (var i=0; i<preLoadedTabs.length; i++) preLoadedTabs[i].onclick = function() {
		var activeTabs = document.getElementsByClassName("activeTab");
		for (i=0; i<activeTabs.length; i++) activeTabs[i].className = "";
		var theTabContentID = this.id + "-content";
		var theTabContent = $(theTabContentID);
		theTabContent.className = "activeTab";
	}
	
	var selectedTab = $("tab-1-content");
	selectedTab.className = "activeTab";
	
}

/* Filter out IE 5 for Windows */
var detect = navigator.userAgent.toLowerCase();
if (detect.indexOf("msie 5") == -1) Event.observe(window, "load", main, false);