//create the fuzzController object
fuzzController = new Object();

///CONTAINERS
fuzzController.displayContainer = function displayContainer(control, action, submitData, isJSONData, onConfirm, jsCallbackFn){
	if(onConfirm){
		if(! confirm("Are you sure?")) return;
	}
	
	var methodArgs = new Object();
	var callbackData = new Object();
	var divId = "fuzzController_container_"+control+"_div_id";
	var postData = "";
	
	var cDiv = document.getElementById(divId);
	
	if(isJSONData){
		submitData = JSON.parse(submitData);
	}
	
	// set the data that will be accessible to the control
	if(! submitData) submitData = "";
	
	// Make sure the div id
	if(! cDiv) alert("Unable to display container: "+control+". It seams the container element was not initialized.")
	else{
		
		fuzzController.displayWaitDiv(control,"Loading, Please Wait...");;
		methodArgs.control = control;
		methodArgs.action = action;
		methodArgs.submitData = submitData;
		methodArgs.displayContainerDiv = false;
		methodArgs.isAsync = true;
		
		callbackData.control = control;
		callbackData.action = action;
		callbackData.containerDivId = divId;
		callbackData.jsCallbackFn = jsCallbackFn;
		
		
		fuzzAsync.callAction("fuzzController","display-container",methodArgs,fuzzController.displayContainerCallback,callbackData);
	}
	
}


fuzzController.displayContainerCallback = function displayContainerCallback(response){
	if(! response.isError){
		var cDiv = document.getElementById(response.callbackData.containerDivId);
		fuzzController.hideWaitDiv(response.callbackData.control);

		if(! cDiv) alert("Unable to display container (from callback): "+control+". It seams the container element was not initialized.");
		else{
			fuzzAsync.util.replaceDiv(cDiv,response.returnHTML);
			
			// run through the declared fuzz forms and initialize them
			for(var form in response.declaredFuzzForms){
				fuzzForm.initAsyncForm(form);
			}
			// run through the declared fuzz forms and initialize them
			for(var name in response.declaredFuzzSlideshows){
				YAHOO.util.Event.onContentReady("fuzzSlideshow_"+name+"_load_div_id", fuzzSlideshow_static_init, name);
			}
			
			// see if we need to load any scripts
			if(response.scripts && response.scripts.length){
				for(x = 0; x < response.scripts.length; x++){
					tS = response.scripts[x];
					if(! fuzzController.isLoadedScript(tS.fileURL,tS.fileType)) fuzzController.loadScript(tS.fileURL,tS.fileType,tS.isDeclared);
				}
			}
			
			if(response.callbackData.jsCallbackFn){
				eval(response.callbackData.jsCallbackFn+"()");
			}
			
			// see if there is google analytics
			if(pageTracker){
				currControl = "";
				if(response.callbackData.control){
					currControl += response.callbackData.control;
				}
				if(response.callbackData.action){
					if(currControl != "") currControl += "_";
					currControl += response.callbackData.action;
				}
				if(currControl != ""){
					pageTracker._trackPageview(currControl);
				}
			}
		
		}
	}
	else{
		// check for security error
		if(response.errorDescription == "user-security-error"){
			window.location.reload(false);
		}
		else{
			dump(response);
		}
		//alert("Sorry an error has occured. Please try again.");
		//dump(response);
		//cbAsyncJS.displayErrorDiv(response.callbackData.name,response.errorDescription,response.errorTrace,response.errorDetails);
	} 
	
}

fuzzController.testDisplayContainer = function testDisplayContainer(){
	submitData = new Object();
	submitData.a = 1;
	submitData.b = 2;
	fuzzController.displayContainer("select-school-container","select-school",submitData);
}

fuzzController.hideWaitDiv = function hideWaitDiv(control){
	var containerDiv = document.getElementById("fuzzController_container_"+control+"_div_id");
	var waitDiv = document.getElementById("fuzzController_container_"+control+"_wait_div_id");
	
	// hide form
	hideElement(waitDiv);
	
	// display wait div
	showElement(containerDiv);
	
	
}

fuzzController.displayWaitDiv = function displayWaitDiv(control, text){
	var waitDiv = document.getElementById("fuzzController_container_"+control+"_wait_div_id");
	
	// set the text
	//textDiv.innerHTML = text;
	
	if(isHiddenElement(waitDiv)){
		// Resize for screen
	
		fuzzAsync.util.displayProgressDiv("fuzzController_container_"+control,text);	
		// display wait div
		//showElement(waitDiv);
		// hide form
		
		//hideElement(containerDiv);
		//hideElement(errorDiv);
	}
}

fuzzController.loadedScripts = "";

fuzzController.loadScript = function loadScript(filename, filetype, isDeclared) {
	if(! isDeclared){
		if (filetype == "js") { //if filename is a external JavaScript file
			var fileref = document.createElement('script');
			fileref.setAttribute("type", "text/javascript");
			fileref.setAttribute("src", filename);
		} else if (filetype == "css") { //if filename is an external CSS file
			var fileref = document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", filename);
		}
		else return;
		if (typeof fileref != "undefined"){
			document.getElementsByTagName("head")[0].appendChild(fileref);
			fuzzController.loadedScripts += "["+filename+"."+filetype+"]";
		}
	}
	else fuzzController.loadedScripts += "["+filename+"."+filetype+"]";
}

fuzzController.isLoadedScript = function isLoadedScript(filename, filetype) {
	if (fuzzController.loadedScripts.indexOf("["+filename+"."+filetype+"]") == -1) {
		return false;
	} 
	else return true;
}




