// pa.js- the main javascript file for the Pacific Advocates web site
// handles sidebar and viewer AJAX requests, tabs, and sidebar animation

///////////////////////////////////////////////////
//browser nonsenses
// globals, Copyright 2001-2006 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
var isIE = false;
if (window.ActiveXObject) {
        isIE = true;
}

//alert(navigator.userAgent.toLowerCase());
var xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xIE6,xIE7,xNN4,xFF1,xFF2,xFF3,xSAF,xUA=navigator.userAgent.toLowerCase();

//alert(xUA);

if(xUA.indexOf('safari') != -1){
	xSAF = true;
}

if(window.opera){
  var i=xUA.indexOf('opera');
  if(i!=-1){
    var v=parseInt(xUA.charAt(i+6));
    xOp7Up=v>=7;
    xOp6Dn=v<7;
  }
}

else if(navigator.vendor!='KDE' && document.all && xUA.indexOf('msie')!=-1){
  xIE4Up=parseFloat(navigator.appVersion)>=4;
  xIE4=xUA.indexOf('msie 4')!=-1;
  xIE5=xUA.indexOf('msie 5')!=-1;
  xIE6=xUA.indexOf('msie 6')!=-1;
  xIE7=xUA.indexOf('msie 7')!=-1;
}

else if(xUA.indexOf('firefox')!=-1) {
  xFF1=xUA.indexOf('firefox/1')!=-1;
  xFF2=xUA.indexOf('firefox/2')!=-1;
  xFF3=xUA.indexOf('firefox/3')!=-1;
}

else if(document.layers){xNN4=true;}
xMac=xUA.indexOf('mac')!=-1;
///////////////////////////////////////////////////
function initLoad () {
	var hometab = xGetElementById("pa_home_tab");
	selectTab(hometab);
}

function selectTab(tab) {
	if (tab == null) {
		//TODO: add that even thingy
		return;
		alert("tab was null");
	}
	//deselect all of the tabs
	var tabs = xGetElementById("pa_tabs");
	
	for (i = 0; i < tabs.childNodes.length; i++) {
		var cli = tabs.childNodes[i];
		//alert(cli.tagName);
		if (cli.tagName == "LI") {
			cli.firstChild.className = "";
			//alert(cli.firstChild.tagName);
		}
	}
	tab.className = "selected";
	if (isIE) {
		getSubCategoriesIE(tab.name);		
	} else {
		getSubCategories(tab.name);
	}
}

function selectCat(cat) {
	if (cat == null) {
		//TODO: add the event thingy
		return;
	}
	if (isIE) {
		getPageIE(cat.name);	
	} else {
		getPage(cat.name);
	}
}

function getSubCategoriesIE(cat) {
	/*var xml = document.createElement("xml");
	xml.src = "xml/" + cat + "_cat.xml";
	document.body.appendChild(xml);
	var result = xml.XMLDocument;
	document.body.removeChild(xml);
	createSubCategories(result);
	*/
	
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) {
				createSubCategories(xmlDoc);
			}
		};
	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load("xml/" + cat + "_cat.xml");
}

function getSubCategories(cat) {
	setSubCategoriesLoading();
	
    /* Set up the request */
    var xmlhttp =  getReq();
    xmlhttp.open('GET', "xml/" + cat + "_cat.xml", true);
    
    /* The callback function */
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
			//this will always be 0 i think because there's no actual server.
            if (xmlhttp.responseXML) {
				//alert(xmlhttp.responseText);
				result = xmlhttp.responseXML;
                createSubCategories(result);
			} else {
                alert("request failed: code: " + xmlhttp.status);
				alert(xmlhttp.responseText);
			}
        }
    }
    /* Send the GET request */
    xmlhttp.send(null);
}

function getPageIE(cat) {
	/*var xml = document.createElement("xml");
	xml.src = "xml/" + cat + ".xml";
	document.body.appendChild(xml);
	var result = xml.XMLDocument;
	document.body.removeChild(xml);
	fillPage(result);
	*/
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) {
				fillPage(xmlDoc);
			}
		};
	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load("xml/" + cat + ".xml");
}


function getPage(cat) {
	setPageLoading();
	
    /* Set up the request */
    var xmlhttp =  getReq();
    xmlhttp.open('GET', "xml/" + cat + ".xml", true);
    
    /* The callback function */
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
			//this will always be 0 i think because there's no actual server.
            if (xmlhttp.responseXML) {
				//alert(xmlhttp.responseText);
                fillPage(xmlhttp.responseXML);
			} else {
                alert("request failed: code: " + xmlhttp.status);
				alert(xmlhttp.responseText);
			}
        }
    }
    /* Send the GET request */
    xmlhttp.send(null);
}

function createSubCategories(response) {
	var subcats = response.getElementsByTagName("subCategories")[0];
	var sidebarlist = xGetElementById("pa_sidebar_list");
	sidebarlist.innerHTML = getElementTextNS("content", "encoded", subcats, 0);
	var firstcat = xGetElementById("firstcat");
	selectCat(firstcat);
}

function fillPage(response) {
	var pagedata = response.getElementsByTagName("page")[0];
	var pageframe = xGetElementById("pa_viewer_content");
	pageframe.innerHTML = getElementTextNS("content", "encoded", pagedata, 0);
}

function setSubCategoriesLoading() {
	var sidebarlist = xGetElementById("pa_sidebar_list");
	sidebarlist.innerHTML = "loading...";
}

function setPageLoading() {
	var pageframe = xGetElementById("pa_viewer_content");
	pageframe.innerHTML = "loading...";
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && (isIE || xFF3)) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

function getReq(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        var req = new XMLHttpRequest();
		return req;
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        var req = new ActiveXObject("Microsoft.XMLHTTP");
		return req;
    }
}

function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}


