/* <![CDATA[ */

/*	Project:	Math Notes Content Engine V2
*	Developer:	Stephen Lim
*	----------------------------------------------------------
*	Please do not redistribute this code without prior consent
*/

var currentPage = null;
var v2e_userBrowser = null;
var v2e_pageDocument = null;
var v2e_pageHistory = null;

function v2e_ClientBrowser ()
{
	/* Class properties */

	this.user_browser_id = 0;

	/* class methods */

	this.getBrowserID = function ()
	{
		return this.user_browser_id;
	}

	this.isBrowserIE = function ()
	{
		if(this.user_browser_id == 1)
			return true;

		return false;
	}

	this.isBrowserOpera = function ()
	{
		if(this.user_browser_id == 2)
			return true;

		return false;
	}

	this.isBrowserChrome = function ()
	{
		if(this.user_browser_id == 3)
			return true;

		return false;
	}

	this.isBrowserFireFox = function ()
	{
		if(this.user_browser_id == 4)
			return true;

		return false;
	}

	/* constructor */

	if(/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		this.user_browser_id = 1;
	}

	if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		this.user_browser_id = 2;
	}

	if(/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		this.user_browser_id = 4;
	}

	if(/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		this.user_browser_id = 3;
	}
}

function v2e_PageParent (parent_id, parent_name)
{
	/* Class properties */

	this.page_parent_id	= parent_id;
	this.page_parent_name	= parent_name;

	/* Class methods */

	this.getId = function ()
	{
		return this.page_parent_id;
	}

	this.getName = function ()
	{
		return this.page_parent_name;
	}
}

function v2e_PageResponse (xmlContent)
{
	/* class properties */

	this.page_title		= xmlContent.getElementsByTagName("v2pagetitle")[0].childNodes[0].nodeValue;
	this.page_content	= xmlContent.getElementsByTagName("v2pagecontent")[0].childNodes[0].nodeValue;
	this.page_parents	= null;

	/* class methods */

	this.parseParents = function (xmlContent)
	{
		var page_heirarchy_id	= xmlContent.getElementsByTagName("v2parentid");
		var page_heirarchy_name	= xmlContent.getElementsByTagName("v2parentname");

		this.page_parents	= new Array();

		for(i=0; i<page_heirarchy_id.length; i++)
		{
			this.page_parents[i] = new v2e_PageParent(page_heirarchy_id[i].childNodes[0].nodeValue, page_heirarchy_name[i].childNodes[0].nodeValue);
		}
	}

	this.getPageTitle = function ()
	{
		return this.page_title;
	}

	this.getPageContent = function ()
	{
		return this.page_content;
	}

	this.getParentPages = function ()
	{
		return this.page_parents;
	}

	this.parseParents(xmlContent);
}

function v2e_PageRequest (page_request_parameters)
{
	/* Class Properties */

	this.request_parameters = page_request_parameters;

	/* Class Methods */

	this.getXmlHTTPObject = function ()
	{
		var obj_xmlHttp = null;

		try
		{
			obj_xmlHttp = new XMLHttpRequest();
		}
		catch (error)
		{
			try
			{
				obj_xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (error)
			{
				obj_xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	
		return obj_xmlHttp;
	}

	this.noXmlHTTPSupport = function ()
	{
		document.getElementById("content").innerHTML = "<meta http-equiv=\"Refresh\" content=\"0; url='v2_noscript.php'\"/>";

		return null;
	}

	this.requestResource = function ()
	{
		var xmlHttpObject = this.getXmlHTTPObject();

		if(xmlHttpObject == null)
			return this.noXmlHTTPSupport();

		xmlHttpObject.open("GET","v2_pagerequest.php" + this.request_parameters,true);
		xmlHttpObject.onreadystatechange = function ()
		{
			if(xmlHttpObject.readyState == 4)
			{
				if(xmlHttpObject.responseXML != null)
				{
					v2e_pageDocument.openPage(new v2e_PageResponse(xmlHttpObject.responseXML),this.request_parameters);
				}
			}
		}

		xmlHttpObject.send(null);
	}
}

function v2e_Page ()
{
	/* Class properties */

	this.page_title		= document.title;
	this.page_navigation	= document.getElementById("top_menu").getElementsByTagName("li");
	this.page_top_info	= document.getElementById("top_info");
	this.page_content	= document.getElementById("content");
	this.page_linkanchor	= document.getElementById("go_back");
	this.page_loading	= document.getElementById("loading");

	/* Class methods */

	this.getContentArea = function ()
	{
		return this.page_content;
	}

	this.getAnchorArea = function ()
	{
		return this.page_linkanchor;
	}

	this.getPageTitle = function ()
	{
		return this.page_title;
	}

	this.getPageMenu = function ()
	{
		return this.page_navigation;
	}

	this.getTopInfo = function ()
	{
		return this.page_top_info;
	}

	this.resetMenuButtons = function ()
	{
		for(i=0; i<this.page_navigation.length; i++)
		{
			this.page_navigation[i].setAttribute((document.all ? "className" : "class"), "");
			this.page_navigation[i].getElementsByTagName("a")[0].setAttribute((document.all ? "className" : "class"), "link");
		}
	}

	this.setStateLoading = function ()
	{
		this.page_loading.setAttribute((document.all ? "className" : "class"), "");
	}

	this.setStateLoaded = function ()
	{
		this.page_loading.setAttribute((document.all ? "className" : "class"), "hidden");
	}
}

function v2e_PageUpdate (page_document)
{
	/* Class properties */

	this.page_document = page_document;

	/* Class methods */

	this.createParentLink = function (linkId, linkText)
	{
		var linkOutput = " &#187; <a href=\"#?pid=";

		linkOutput += linkId;
		linkOutput += "\" onclick=\"RequestLoadPage(\'?pid=";
		linkOutput += linkId;
		linkOutput += "\')\">";
		linkOutput += linkText;
		linkOutput += "</a>";

		return linkOutput
	}

	this.updateMenu = function (page_response)
	{
		var page_hierarchy = page_response.getParentPages();

		var page_current_item = document.getElementById("link_" + page_hierarchy[0].getId());

		var page_top_info = "";

		this.page_document.resetMenuButtons();

		if(page_current_item == null)
			page_current_item = document.getElementById("link_1");

		page_current_item.setAttribute((document.all ? "className" : "class"), "current");
		page_current_item.getElementsByTagName("a")[0].setAttribute((document.all ? "className" : "class"), "current");

		for(i=0; i<page_hierarchy.length; i++)
		{
			page_top_info += this.createParentLink(page_hierarchy[i].getId(),page_hierarchy[i].getName());
		}

		this.page_document.getTopInfo().innerHTML = page_top_info;
	}

	this.updatePage = function (page_response)
	{
		document.title = page_response.getPageTitle();

		this.updateMenu(page_response);

		this.page_document.getContentArea().innerHTML = page_response.getPageContent();

		delete page_response;
	}

	this.openPage = function (page_request, page_request_parameters)
	{
		this.updatePage(page_request);

		this.page_document.setStateLoaded();
	
		delete page_request;
	}

	this.requestPage = function (request_parameters, request_history)
	{
		var page_request = new v2e_PageRequest(request_parameters);

		this.getPageDocument().setStateLoading();

		v2e_pageHistory.setCurrentPage(request_parameters,request_history);

		page_request.requestResource();

	}

	this.getPageDocument = function ()
	{
		return this.page_document;
	}
}

function v2e_HistoryManager (page_updater)
{
	this.page_current = null;
	this.page_document = page_updater;
	this.page_IE_listener = null;

	this.setCurrentPage = function (page_hash, page_history_update)
	{
		this.page_current = page_hash;
	
		if(v2e_userBrowser.isBrowserIE() == false)
		{
			this.page_document.getPageDocument().getAnchorArea().innerHTML = "<div id=\"" + this.page_current + "\" style=\"display: none;\">&nbsp;</div>";
		}
		else
		{
			if(page_history_update)
				return;

			this.page_IE_listener.document.open();
			this.page_IE_listener.document.write('<body>' + page_hash + '</body>');
			this.page_IE_listener.document.close();
		}		
	}

	this.getHistory = function ()
	{
		var pageHistory = GetPageAnchorHash();

		if(pageHistory == null)
			pageHistory = "#";

		pageHistory = pageHistory.substr(1);

		return pageHistory;
	}

	this.checkHistory = function ()
	{
		if(v2e_userBrowser.isBrowserIE() == false)
		{
			var pageHistory = this.getHistory();

			if(pageHistory == this.page_current)
				return;

			this.page_document.requestPage(pageHistory,true);
		}
		else
		{
			var pageHistory = this.page_IE_listener.document.body.innerHTML;

			if(pageHistory == this.page_current)
				return;

			window.location.hash = pageHistory;

			this.page_document.requestPage(pageHistory,true);
		}
	}

	this.initialize = function ()
	{
		if(v2e_userBrowser.isBrowserIE())
		{
			var IE_history_frame = document.createElement("IFRAME");

			IE_history_frame.style.display = "none";

			document.body.appendChild(IE_history_frame);

			this.page_IE_listener = (IE_history_frame.contentWindow) ? IE_history_frame.contentWindow : (IE_history_frame.contentDocument.document) ? IE_history_frame.contentDocument.document : IE_history_frame.contentDocument;
		}
	}
}


function GetPageAnchorHash ()
{
	var hashIndex = window.location.href.indexOf("#");

	if(hashIndex < 0)
		return null;

	return window.location.href.substr(hashIndex);
}

function CheckHistory ()
{
	v2e_pageHistory.checkHistory();
}

function RequestLoadPage (parameters)
{
	v2e_pageDocument.requestPage(parameters,false);
}

function InitializePage ()
{
	var requestedPage = document.location.search;
	var requestedHash = GetPageAnchorHash();

	if(requestedPage == "")
	{
		if((requestedHash != null) && (requestedHash != "#"))
		{
			requestedPage = requestedHash.substr(1);
		}
		else
		{
			requestedHash = "#";
		}
	}
	else
	{
		requestedHash = requestedPage;
	}

	window.location.hash = requestedHash;

	v2e_userBrowser = new v2e_ClientBrowser();
	v2e_pageDocument = new v2e_PageUpdate(new v2e_Page());
	v2e_pageHistory = new v2e_HistoryManager(v2e_pageDocument);

	v2e_pageHistory.initialize();

	RequestLoadPage(requestedPage);

	setInterval(CheckHistory, 250);
}

/* ]]> */
