var XmlReader = Class.create();
XmlReader.prototype = {
	id: 'womens_apparel',
	selectedClass: 'selected',
	activeClass: 'sitemaplink',
	initialize: function() {
		// check cookie for current id
		var test = getCookie();
		if (test != null) {
			this.setID(test);
		}
		this.loadData();
	},
	getID: function() {
		return this.id;
	},
	setID: function(docID) {
		if (docID != null) {
			this.id = docID;
		} else if (docID.indexOf("furnish")>=0) {
			this.id = "apartment_furnish";
		} else {
			this.id = "womens_apparel";
		}
	},
	getSelectedClass: function() {
		return this.selectedClass;
	},
	getActiveClass: function() {
		return this.activeClass;
	},
	loadData: function() {
		// create url
		var url = "http://" + location.host + "/urban/" + this.id + ".xml";
		// update cookie
		setCookie(this.id);
		// issue ajax request to retrieve xml doc
		new Ajax.Request(url, {
			method: 'get',
			onComplete: function(response) {
				// return xml dom
				var ajaxResponse = Try.these(
					function() { 
						// mozilla
						return new DOMParser().parseFromString(response.responseText, 'text/xml'); 
					},
					function() { 
						// microsoft
						var xmldom = new ActiveXObject('Microsoft.XMLDOM');
						xmldom.loadXML(response.responseText); 
						return xmldom; 
					}
				);
				
				var output = '<ul style="width:100%;">';
				
				var items = ajaxResponse.getElementsByTagName('loc');
				for (i=0; i<items.length; i++) {
					var cur = items[i].firstChild.nodeValue;
					var prefix = "http://www.urbanoutfitters.com/urban/jump/category/";
					var linkName = cur;	
						
					if (linkName.indexOf(prefix) >= 0) {						
						linkName = linkName.substring(prefix.length, linkName.length);
						linkName = linkName.substring(0, linkName.indexOf("/"));
					}
					
					if (x.getID().indexOf("furnish") >= 0) {
						linkName = linkName.replace("A_FURN_","APARTMENT FURNISH ");
						linkName = linkName.replace("A_ENT_","APARTMENT ENTERTAIN ");
						linkName = linkName.replace("P_GIFTS","SALE");
					} else if (url.indexOf("help") >= 0) {
						linkName = linkName.replace(".jsp","");
						var domain = "http://www.urbanoutfitters.com/urban/";
						linkName = linkName.replace(domain + "help/helpinfo","HELP &amp; INFO");
						linkName = linkName.replace(domain + "help/ordering_payment","ORDERING PAYMENT");
						linkName = linkName.replace(domain + "help/returns_exchanges","RETURNS &AMP; EXCHANGES");
						linkName = linkName.replace(domain + "help/shipping_information","SHIPPING INFORMATION");
						linkName = linkName.replace(domain + "help/popup_sizechart","SIZE CHART");
						linkName = linkName.replace(domain + "help/privacy_security","PRIVACY SECURITY");
						linkName = linkName.replace(domain + "help/terms_of_use","TERMS OF USE");
						linkName = linkName.replace(domain + "signup/emailunsubscribe","EMAIL UNSUBSCRIBE");
						linkName = linkName.replace(domain + "help/RequestCatalog","REQUEST CATALOG");
						linkName = linkName.replace(domain + "help/contactus","CONTACT US");
						linkName = linkName.replace(domain + "help/getuo_txt","GET UO TXT");
						linkName = linkName.replace(domain + "help/store_locator","STORE LOCATOR");
						linkName = linkName.replace(domain + "help/help_emailpage","EMAIL HELP");
						linkName = linkName.replace(domain + "help/uo_affiliate","UO AFFILIATE");
						linkName = linkName.replace(domain + "help/international","INTERNATIONAL SHIPPING");
					} else {
						linkName = linkName.replace("M_","MENS ");
						linkName = linkName.replace("W_","WOMENS ");
						linkName = linkName.replace("ACC_","ACCESSORIES ");
						linkName = linkName.replace("APP_","APPAREL ");
						linkName = linkName.replace("P_GIFTS","SALE");
						linkName = linkName.replace("COLL_","COLLECTION ");
					}
					
					// replace all underscores with whitespace
					linkName = linkName.replace("_"," ");
					
					// concatenate content for children div
					output += '<li style="list-style:none; float:left; width:325px; margin:2px 0px 2px 0px;"><a href="' + cur + '">' + linkName + '</a></li>';
				}
				output += '</ul>';
				$('children').innerHTML = output;
			}
		});
	}
};

var x = new XmlReader();

function init_menu() {
	var items = $$('.sitemaplink');
	
	// get all elements with sitemaplink class
	items.each(function(item){
		Event.observe(item, 'click', function() {
			var linkID = this.getAttribute('id');
			// remove active links
			removeSelectedLinks();
			// add selected link to current
			this.removeClassName(x.getActiveClass());
			this.addClassName(x.getSelectedClass());
			// update current id
			x.setID(linkID);
			// blur object
			this.blur();
			// load xml data
			x.loadData();
		}.bind(item));
	});	
	
	var cur = x.getID();
	$(cur).removeClassName(x.getActiveClass());
	$(cur).addClassName(x.getSelectedClass());
}

// get all elements with selected class
function removeSelectedLinks() {
	var cur = x.getID();
	// set default selected link class
	$(cur).removeClassName(x.getSelectedClass()); 
	$(cur).addClassName(x.getActiveClass()); 
}

// attach to window onload
Event.observe(window, 'load', init_menu);

// set cookie when user toggles between sitemaps
function setCookie(id) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + 1);
	document.cookie = "sitemapID=" + id + ";expires=" + exdate.toGMTString();
}

// get cookie
function getCookie() {
	var c = null;
	// check if cookies have been set
	if (document.cookie.length > 0) {
		var name = "sitemapID";
		var start = document.cookie.indexOf(name+"=");
		// check for sitemap cookie
  		if (start >= 0) { 
			start += (name.length + 1); 
    		var end = document.cookie.indexOf(";", start);
    		if (end > 0) {
				c = document.cookie.substring(start, end);
    		} 
  		}
	}
	return c;
}