var xhr_file = '/_compile/a_links.php';
var xhr_type = 'GET';
var exlude_hash = 'contact-form';

var g_className_links = 'select';
var g_className_update = 'update';
var g_container_id = 'container';
var g_cookie_name = 'link_hash';

function category_xhr_setup() {
	if(!document.createElement || !document.getElementsByTagName || !document.getElementById) return false;
	
	var container 			= document.getElementById(g_container_id);
	var list_select_array 	= getElementsByClassName(container,'*',g_className_links);
	var list_links_array 	= getElementsByClassName(container,'*',g_className_update);
	var list_select			= list_select_array[0];
	var list_links 			= list_links_array[0];
	
	if(!container || !hasClass(container,'module-links') || !list_select || !list_links) return false;
	
	//if(window.watch) window.watch('location',category_xhr_setup);
	
	category_xhr_connection = XHConn(); // all the elements we need, then setup the connection
	if(!category_xhr_connection) return false; // XMLHTTP not available. exit method/handle error.

	var category_list_array = list_select.getElementsByTagName('a');
	var category_list_array_length = category_list_array.length;
	for(var i=0; i<category_list_array_length; i++) {
		var category_anchor = category_list_array[i];
		var category_anchor_id = category_anchor.href.split('/')[4];
		category_anchor.href = '#'+category_anchor_id;
		// remove the rel attribute 'tag' - http://microformats.org/wiki/rel-tag#Tag_Spaces
		// Tags may not be placed in query parameters or fragment identifiers
		removeRel(category_anchor,'tag');
		addEvent(category_anchor,'click',category_xhr_connect);
		
		//if(getCookie(g_cookie_name)) var first_anchor_id = getCookie(g_cookie_name);
		//else if(i==0) var first_anchor_id = category_anchor_id;
		var first_anchor_id = category_anchor_id;
		
		var location_hash = location.href.split('#')[1];
		if(!location_hash) location.href += '#'+first_anchor_id;
	}
	
	current_hash = category_xhr_setup_default(location_hash); // page loads, if has hash, look to element with that link, set active and call the connection
	
	// loop to check whether the current hash has changed, if so change based upon hash
	/*
	window.setInterval(function(a,b) {
		var check_hash = location.href.split('#')[1];
		if(check_hash && current_hash!=check_hash && check_hash!=exlude_hash) {
			current_hash = category_xhr_setup_default();
		}
	}, 100);
	*/
}

// the default setup of the script, based upon hash
// also has the ability to simulate history
function category_xhr_setup_default(current_hash) {
	var container 			= document.getElementById(g_container_id);
	var list_select_array 	= getElementsByClassName(container,'*',g_className_links);
	var list_links_array 	= getElementsByClassName(container,'*',g_className_update);
	var list_select			= list_select_array[0];
	var list_links 			= list_links_array[0];
	
	var category_list_array = list_select.getElementsByTagName('a');
	var category_list_array_length = category_list_array.length;
	
	var location_hash = location.href.split('#')[1];
	if(!location_hash) return null;

	for(var i=0; i<category_list_array_length; i++) {
		var category_anchor = category_list_array[i];
		removeClass(category_anchor.parentNode,'active'); // remove the active from all
		if(category_anchor.href.split('#')[1].toLowerCase().match(current_hash)) {
			addClass(category_anchor.parentNode,'active'); // add class to active page
		}
	}
	//if(current_hash==location_hash || location_hash==exlude_hash) return location_hash;
		
	// need the loading
	xhr_loading(list_links);
	
	// setup connection, call the xhr
	category_xhr_connection.connect(xhr_file,xhr_type,'category='+current_hash,category_xhr_build);
	return location_hash;
}

function category_xhr_connect(e) {
	var container 			= document.getElementById(g_container_id);
	var list_select_array 	= getElementsByClassName(container,'*',g_className_links);
	var list_links_array 	= getElementsByClassName(container,'*',g_className_update);
	var list_select			= list_select_array[0];
	var list_links 			= list_links_array[0];
	
	var category_list_array = list_select.getElementsByTagName('a');
	var category_list_array_length = category_list_array.length;
	
	for(var i=0; i<category_list_array_length; i++) {
		removeClass(category_list_array[i].parentNode,'active'); // loop and remove 'active' from all the list items
	}
	
	addClass(this.parentNode,'active'); // add the class 'active' to the list item
	
	var anchor_id = this.href.split('#')[1];
	var location_id = location.href.split('#')[1];
	
	if(anchor_id==location_id) {
		this.blur();
		return false;
	}
	category_xhr_setup_default(anchor_id);
	this.blur();
}

function category_xhr_build(oXML) {
	//alert(oXML.responseText);
	if(!oXML.responseXML) return false;
	var xml_container	= oXML.responseXML.getElementsByTagName('link');
	var xml_error		= oXML.responseXML.getElementsByTagName('error');

	var container 			= document.getElementById(g_container_id);
	var list_select_array 	= getElementsByClassName(container,'*',g_className_links);
	var list_links_array 	= getElementsByClassName(container,'*',g_className_update);
	var list_select			= list_select_array[0];
	var list_links 			= list_links_array[0];

	removeChildren(list_links);
	
	xml_container_length = xml_container.length;
	
	if(xml_container_length>0) {
		var el_dl = document.createElement('dl');
		for(var i=0; i<xml_container_length; i++) {
			var container 	= xml_container[i];
			var el_title 	= container.getElementsByTagName('title')[0].firstChild.nodeValue;
			var el_url 		= container.getElementsByTagName('url')[0].firstChild.nodeValue;

			var el_title_text = document.createTextNode(el_title);
			
			var el_dt 	= document.createElement('dt');
			var el_a	= document.createElement('a');
			
			el_a.href = el_url;
			el_a.appendChild(el_title_text);			
			el_dt.appendChild(el_a);
			el_dl.appendChild(el_dt);
			
			if(container.getElementsByTagName('description')[0].firstChild) {
				var el_desc	= container.getElementsByTagName('description')[0].firstChild.nodeValue;
				var el_dd = document.createElement('dd');
				el_dd.innerHTML = el_desc;
				el_dl.appendChild(el_dd);
			}
		}
		list_links.appendChild(el_dl);
		
		// set a cookie of the current hash, so when the come back
		var location_hash = location.href.split('#')[1];
		setCookie(g_cookie_name, location_hash, '', '/');
	}
	else {		
		create_general_feedback(list_links,xml_error,'error');
	}
}
addEvent(window,'load',category_xhr_setup);