// Include all the necessary scripts using a colsure
(function() {
	var files = ['Directory', 'File', 'Request', 'Search'];
	for (var i=0;i<files.length;i++) {
		document.write('\n<script type="text/javascript" src="dsv/resources/javascript/' + files[i] + '.js"></script>');
	}
})();


// Handle for when the window loads
window.onload = function() {
	(function() {
		// Toggle options
		function toggleOptions(e) {
			var o = $('options');
			o.style.display = (o.style.display == "block" || o.style.display == "") ? "none" : "block";
			
			var t = this;
			if (e.srcElement) {
				// IE....
				t = e.srcElement;
			}
			
			t.innerHTML = ((o.style.display == "block") ? "-" : "+") + " Options";
		}
		
		// Toggle search
		function toggleSearch(e) {
			var s = $('search');
			s.style.display = (s.style.display == "block" || s.style.display == "") ? "none" : "block";
			
			var t = this;
			if (e.srcElement) {
				// IE....
				t = e.srcElement;
			}
			
			t.innerHTML = (s.style.display == "block") ? "hide" : "search";
		}
		
		// Add the handlers
		$('options-text').registerEvent('click',toggleOptions);
		$('hide-text').registerEvent('click',toggleSearch);
		
		// REMOVE
		var dir = "..";
		var query_string = document.location.href.split("?")[1];
		if (query_string != undefined) {
			var query_vars = query_string.split("&");
			for (var i=0;i<query_vars.length;i++) {
				var vars = query_vars[i].split("=");
				if (vars[0] == "dir") dir = vars[1];
			}
		}
		
		// Create the root directory
		var d = new Directory(dir);
		d.requestDirectory();
		
		// Initiate the searching
		var s = new Search(d);
	})();
}


/**
*	==========================================================================================
*			Functions that all objects can use
*	==========================================================================================
*/

// Generate a unique id
var uniqueId = (function() {
	var id = 0;
	return function() { return id++; }
})();

// Inspiried by prototypejs.org
// Allows for event listeners to be given a specified scope
Function.prototype.bindAsEventListener = function() {
	var m = this, args = Array.prototype.slice.call(arguments),obj = args.shift();
	return function(event) {
		return m.apply(obj,[event||window.event].concat(args));
	}
}

// Easier way to access elements in the DOM
function $(id) {
	var e = document.getElementById(id);
	
	// Append our custom register event to this element because IE does not allow us to extend the Object prototype
	if (e && e.registerEvent == undefined) {
		e.registerEvent = Object.prototype.registerEvent;
	}
	
	return e;
}

// Need to override document.createElement in order to extend the object prototype in IE...
// http://www.geekdaily.net/2007/06/18/javascript-htmlelement-in-ie/
// Using a closure to avoid cluttering the global NS
(function() {
	if (document.attachEvent) {
		var _createElement = document.createElement;
		document.createElement = function(tag) {
			var e = _createElement(tag);

			if (e.registerEvent == undefined) {
				e.registerEvent = Object.prototype.registerEvent;
			}
			
			return e;
		}
	}	
})();


// Universal event registration
Object.prototype.registerEvent = function(ev,callback) {
	if (window.addEventListener) {
		return this.addEventListener(ev,callback,false);
	} else {
		// IE
		return this.attachEvent('on' + ev, callback);
	}
}


