/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**************************
 * AJAX functions
 **************************/

var net = new Object();
net.READY_STATE_UNINTIALIZED = 0;
net.READY_STARE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

net.ContentLoader = function(url, toCall, onload, args, onerror) {
	this.url = url;
	this.req = null;
	this.toCall = toCall;
	this.callbackArgs = args;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.loadXMLDoc(url);
}
net.ContentLoader.prototype = {
	loadXMLDoc:function(url) {
		if(window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(this.req) {
			try {
				var loader = this;
				this.req.onreadystatechange = function() {
					loader.onReadyState.call(loader);
				}
				this.req.open('GET', url, true);
				this.req.send(null);
			} catch (e) {
				this.ononerror.call(this)
			}
		}
	},
	onReadyState:function() {
		var req = this.req;
		var ready = req.readyState;
		if(ready == net.READY_STATE_COMPLETE) {
			var httpStatus = req.status;
			if(httpStatus == 200 || httpStatus == 0) {
				this.onload.call(this.toCall, this, this.callbackArgs);
			} else {
				this.onerror.call(this);
			}
		}
	},
	defaultError:function() {
		alert("error fetching data!"
			+ "\n\nreadyState: " + this.req.readyState
			+ "\nstatus: " + this.req.status
			+ "\nheaders: " + this.req.getAllResponseHeaders());
	}
}

var navigation = new Object();

navigation.NAVIGATION_XML = "/navigation.xml";

navigation.PageItem = function(id, title,url, target) {
    this.id = id;
    this.title = title;
    this.url = url;
    this.target = target;
    this.subPages = new Array();
}
navigation.PageItem.prototype = {
    addPage:function(page) {
        this.subPages.push(page);
    }
}

navigation.Menu = function(containerId, url) {
    this.container = document.getElementById(containerId);
    this.dataUrl = (url) ? url : navigation.NAVIGATION_XML;
    this.pageArray = new Array();
    this.activeFirstLink = null;
    this.activeSubLink = null;
}
navigation.Menu.prototype = {
    loadData:function() {
        new net.ContentLoader(this.dataUrl, this, this.onDataRespond, null);
    },
    onDataRespond:function(loader, args) {
        var xmlDoc = loader.req.responseXML;
        var pages = xmlDoc.getElementsByTagName("page");
        for (var i = 0; i < pages.length; i++) {
            var page = new navigation.PageItem(pages[i].attributes.getNamedItem("id").nodeValue, pages[i].getElementsByTagName("title")[0].firstChild.nodeValue, pages[i].getElementsByTagName("url")[0].firstChild.nodeValue, pages[i].getElementsByTagName("target")[0].firstChild.nodeValue);
            var subPages = pages[i].getElementsByTagName("sub-page");
            for (var j = 0; j < subPages.length; j++) {
                var spage = new navigation.PageItem(subPages[j].attributes.getNamedItem("id").nodeValue, subPages[j].getElementsByTagName("title")[0].firstChild.nodeValue, subPages[j].getElementsByTagName("url")[0].firstChild.nodeValue, subPages[j].getElementsByTagName("target")[0].firstChild.nodeValue);
                page.addPage(spage);
            }
            this.pageArray.push(page);
            this._renderMainLink(page);
        }
        var subLinks = document.createElement("div");
        subLinks.setAttribute("id", this.container.id + "_subLinks");
        this.container.appendChild(subLinks);
        this._renderSubLinks(this.pageArray[0]);
        this.container.firstChild.className = "firstLevelLinkActive";
        top.MAIN_MENU.activeFirstLink = document.getElementById(this.pageArray[0].id);
    },
    _renderMainLink:function(page) {
        var link = document.createElement("a");
        link.setAttribute("id", page.id);
        link.setAttribute("href", page.url);
        link.setAttribute("target", page.target);
        link.className = "firstLevelLink";
        link.innerHTML = page.title;
        link.onclick = this.onMainClick;
        this.container.appendChild(link);
   },
    _renderSubLinks:function(page) {
        var container = document.getElementById(this.container.id + "_subLinks");
        while(container.firstChild != null)
            container.removeChild(container.firstChild);
        for(var i = 0; i < page.subPages.length; i++) {
            var link = document.createElement("a");
            var spage = page.subPages[i];
            link.setAttribute("id", spage.id);
            link.setAttribute("href", spage.url);
            link.setAttribute("target", spage.target);
            link.className = "subLevelLink";
            link.innerHTML = spage.title;
            link.onclick = this.onSubClick;
            container.appendChild(link);
        }
    },
    onMainClick:function(event) {
        if (top.MAIN_MENU.activeFirstLink != null) {
            top.MAIN_MENU.activeFirstLink.className = "firstLevelLink";
        }
        top.MAIN_MENU.activeFirstLink = this;
        top.MAIN_MENU.activeFirstLink.className = "firstLevelLinkActive";
        for(var i = 0; i < top.MAIN_MENU.pageArray.length; i++) {
            if (top.MAIN_MENU.pageArray[i].id == top.MAIN_MENU.activeFirstLink.id) {
                top.MAIN_MENU._renderSubLinks(top.MAIN_MENU.pageArray[i]);
                break;
            }
        }
    },
    onSubClick:function(event) {
        if (top.MAIN_MENU.activeSubLink != null) {
            top.MAIN_MENU.activeSubLink.className = "subLevelLink";
        }
        top.MAIN_MENU.activeSubLink = this;
        top.MAIN_MENU.activeSubLink.className = "subLevelLinkActive";
    }
}
navigation.GetMenu = function(containerId, url) {
    if(containerId != null) {
        top.MAIN_MENU = new navigation.Menu(containerId, url);
        top.MAIN_MENU.loadData();
    }
    return top.MAIN_MENU;
}