/* ********************************************************************************************************************
**
**	Name: 			bsPrep
**	
**	Version: 		0.1																			
** 	Author: 		Ben Sekulowicz
**
******************************************************************************************************************** */

var TvccBusac = Class.create();

TvccBusac.prototype = {	
   	initialize: function(url) {
		// Set up vars
		this.current = "";
		this.cache = Array();
		this.base_url = url;
		
		// Start the whole shebang
		Event.observe(window, "load", this.onLoadWindow.bindAsEventListener(this));
	},
	
	/* *********************************************************************************************************************
	
	EVENTS:
	
	********************************************************************************************************************* */
	
	onLoadWindow: function(e) {
		// If we have the items available ...
		if (($("searchDirectory1")) && ($("sector"))) {			
			// Monitor the dropdown for change
			Event.observe($("sector"), "change", this.onChangeSector.bindAsEventListener(this));
		}
		
		this.onChangeSector();
	},
	
	/* ****************************************************************************************************************** */
	
	onChangeSector: function(e) {		
		// If the value isn't all	
		if ($("sector").value != "all") {
			
			// If we have it cached already
			if (this.cache[$("sector").value]) {
				this.ajaxSuccess(this.cache[$("sector").value]);
				
			// otherwise, get it from AJAX
			} else {
				// Fill the select box ...
				new Ajax.Request(this.base_url + $("sector").value + "/", { method: "get",	onSuccess: this.ajaxSuccess.bind(this)});
			}
		}
	},
	
	/* *********************************************************************************************************************
	
	AJAX:
	
	********************************************************************************************************************* */
	
	ajaxSuccess: function(ajax) {
		// Get the data from the JSON string and add to local variable
		var json = eval(ajax.responseText);
		
		// Test for the DIV we are looking for - if not found, die
		if ((!json) || (!json.busacs) || (json.busacs.length < 1)) { return false; }
		
		// Add the response to the cache array too ...
		this.cache[$("sector").value] = ajax;		
		
		// Remove all of the options from the SELECT
		$("activity").immediateDescendants().each(function(opt) { opt.remove(); });
		
		// Cycle through JSON object and add to the select box
		json.busacs.each(function(busac) {
			var option = this.createHTML("option", "", "");
			option.setAttribute("value", busac.id);
			option.appendChild(document.createTextNode(busac.description));
			
			$("activity").appendChild(option);
		}.bind(this));
		
		new Effect.Highlight($("activity"));
	},
	
	/* *********************************************************************************************************************
	
	UTILITY FUNCTIONS
	
	********************************************************************************************************************* */
	
	createHTML: function(e, i, c) {
		var newElement = document.createElement(e);			
		if (i != "") { newElement.setAttribute("id", i); }			
		if (c != "") { newElement.className = c; }			
		return newElement;
	}

	/* *********************************************************************************************************************
	
	FIN
	
	********************************************************************************************************************* */
}
