Array.prototype.remove = function(value)
{
	var retval = new Array();
	for(var i = 0; i < this.length; i++)
		if(this[i] != value)
			retval.push(this[i]);
			
	return retval;
}

Persist = {
	hash : new Object(),
	loadhash : null,
	callback : null,
	req : null,
	
	refresh : function()
	{
		var hashstrs = new Object();
		for(term in this.hash)
		{
			if(this.hash[term].length != 0)
			{
				hashstrs[term] = term + "=" + this.hash[term].join(',');
			}
		}
				
		hashstr = "";
		for(hashelem in hashstrs)
			hashstr += ("&" + hashstrs[hashelem]);
		location.hash = "#" + hashstr.substr(1).replace('#', '');
		
		if(document.getElementById('ical'))
		{
			if(hashstr.substr(1).replace('#', '').length > 0)
			{
				document.getElementById('ical').href = "webcal://www.stevens.edu/scheduler/core/calendar.php?" + hashstr.substr(1).replace('#', '') + "&format=3";
				document.getElementById('ical').style['display'] = 'block';
			}
			else
				document.getElementById('ical').style['display'] = 'none';
		}
	},
	
	rebuild : function()
	{
		this.hash = new Object();
		var hashstr = location.hash.replace('#', '');
			
		var termstrs = hashstr.split('&');
		if(termstrs.length == 0 || termstrs[0] == "")
			return;
			
		var terms = new Array();
		for(var i = 0; i < termstrs.length; ++i)
		{
			temp = termstrs[i].split("=");
			this.hash[temp[0]] = temp[1].split(",");
		}
	},
	
	Add : function(course)
	{
		if(!(course.Term in this.hash))
			this.hash[course.Term] = new Array();

		this.hash[course.Term].push(course.Call);
		this.refresh();
	},
	
	Remove : function(course)
	{
		if(this.hash[course.Term] == undefined)
			return;
			
		this.hash[course.Term] = this.hash[course.Term].remove(course.Call);
		this.refresh();
	},
	
	init : function(callback)
	{
		this.rebuild();
		this.loadhash = new Object();
		for(term in this.hash)
			this.loadhash[term] = this.hash[term].slice();
		
		if (window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		// convenient way of pulling a term out -- return at the end of the loop
		for(term in this.loadhash)
		{
			this.req.onreadystatechange = function() { Persist.loadterm(); };
			this.callback = callback;
			this.req.open("GET", "core/core.php?cmd=getxml&term=" + term, true);
			this.req.send(null);
			return;
		}

		this.refresh();
		callback(null);
	},
	
	loadterm : function()
	{
//		alert(this.req.readyState);
		if(this.req.readyState == 4)
		{
			var lastloadedterm;
			
			// load up the term that was just downloaded
			for(term in this.loadhash)
			{
				// do stuff to the first loadhash item, which should be ready
				coursearray = this.loadhash[term];
				var response = this.req.responseXML.documentElement;
				var courses = response.getElementsByTagName('Course');
	
				for(course = 0; course < courses.length; course++)
				{
					for(var i = 0; i < this.loadhash[term].length; i++)
					{
						if(courses[course].getAttribute("CallNumber") == this.loadhash[term][i])
						{
							new_course = new Course(courses[course]);
							new_course.Saved = true;
							new_course.Render();
						}
					}
				}
			
				delete this.loadhash[term];
				lastloadedterm = term;
				break;
			}
					
			// now trigger the next term load
			for(term in this.loadhash)
			{
				if (window.XMLHttpRequest) {
					this.req = new XMLHttpRequest();
				} else if (window.ActiveXObject) {
					this.req = new ActiveXObject("Microsoft.XMLHTTP");
				}
			
				this.req.onreadystatechange = function() { Persist.loadterm(); }
				this.req.open("GET", "core/core.php?cmd=getxml&term=" + term, true);
				this.req.send(null);
		
				return;
			}

			this.refresh();
			this.callback(term);
		}
	}
};