try {
	addLoadEvent(makeExpander);
} catch(e) {}
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", makeExpander, false);
}

var request = false;

try {
	request = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			request = false;
		}
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function makeExpander() {
	if (document.getElementById("signup") && document.getElementById("sign_up")) {
		var message = document.getElementById("message");
		message.style.display = 'none';
		var messageText = message.innerHTML;
		var sign_up = document.getElementById("sign_up");
		sign_up.innerHTML = messageText;
		message.parentNode.removeChild(message);
	}

	var tables = document.getElementsByTagName("table");

	for (i=0;i<tables.length;i++) {
		if (tables[i].className == "appListing") {	
			var links = document.getElementsByTagName("a");
			for (i=0;i<links.length;i++) {
				if (links[i].className == 'expanding_arrow') {
					if (!links[i].setAttribute("onclick","expandRow(this); return false;")) {
						links[i]["onclick"] = function () { expandRow(this); return false; }
					}
					var nextLink = links[i].nextSibling;
					if (nextLink.nodeName != "A") {
						while (nextLink.nodeName != "A") {
							nextLink = nextLink.nextSibling;						
						}
					}
					if (!nextLink.setAttribute("onclick","expandRow(this); return false;")) {
						nextLink["onclick"] = function () { expandRow(this); return false; }
					}

				}
			}
		}
	}
}

function expandRow(obj) {
	var node = obj.parentNode;

	while (node.nodeName != "TR") {
		node = node.parentNode;
	}

	if (node.nodeName == "TR") {	
		if (node.className == 'expanded') {
			node.className = '';
		} else {
			node.className = 'expanded';
		}

		var rows = node.nextSibling;

		while (rows.nodeName != "TR") {
			var rows = rows.nextSibling;
		}

		if (rows.className == 'expandedDescription') {
			if (rows.style.display == 'none') {
				rows.style.display = '';	
			} else {
				rows.style.display = 'none';	
			}
		}
	}

	// Determine O-C String
	while (node.nodeName != "TABLE") {
		node = node.parentNode;
	}

	var oc_string = '';

	if (node.nodeName == "TABLE") {	
		var ocRows = node.getElementsByTagName("tr");

		for (i=0;i<ocRows.length;i++) {
			if (ocRows[i].className != 'expandedDescription' && ocRows[i].className != 'caption') {
				if (ocRows[i].className == 'expanded' || ocRows[i].className == 'flat') {
					oc_string = oc_string + '1';				
				} else {
					oc_string = oc_string + '0';
				}
			}
		}
	}

	var uri = window.location.pathname;
	var url = '/themes/cog3/set_cookie.php?uri=' + uri + '&oc_string=' + oc_string;

	request.onreadystatechange = processReqChange;
	request.open("GET", url, true);
	request.send("");

	return(false);
}

function processReqChange() {
	if (request.readyState == 4) {

	}
}