


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

fuzzController.currentModal = new Object();
fuzzController.imageButtons = new Object();

// initialize the history



///CONTAINERS
fuzzController.displayContainer = function displayContainer(control, action, submitData, isJSONData, onConfirm, jsCallbackFn, optionsArr){
	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){
		if(submitData) submitData = JSON.parse(submitData);
		if(optionsArr) optionsArr = JSON.parse(optionsArr);
	}

	

	// set the data that will be accessible to the control
	if(! submitData) submitData = "";
	if(! optionsArr) optionsArr = "";
	
	// Make sure the div id
	if(! cDiv) {
		throw("Unable to display container: "+control+". It seams the container element was not initialized.");
	}
	else{

		// see if we need to close the modal
		if(optionsArr && optionsArr.closeModal){
			fuzzController.closeModal();
		}

		if(optionsArr.doHideAll && optionsArr.doHideAll === true){
			hideElement(cDiv);
		}
		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;
		callbackData.optionsArr = optionsArr;
		
		
		fuzzAsync.callAction("fuzzController","display-container",methodArgs,fuzzController.displayContainerCallback,callbackData);
	}
	
}


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

		if(response.callbackData && response.callbackData.optionsArr && response.callbackData.optionsArr.scrollTop){
			var viewportXY = getViewportXY();
			var scrollXY = getScrollXY();
			
			tPos = YAHOO.util.Dom.getXY(cDiv);

			if(tPos[1] < scrollXY[1]){
				window.scrollBy(0,tPos[1]-scrollXY[1])
			}

		}

		

		if(! cDiv) throw("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){
				//alert("add scripts");
				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+"()");
			}

			// if we got here without an error, add this to the history
			fuzzController.handleHistory(response);

			fuzzController.hideWaitDiv(response.callbackData.control);
			
			// 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.initializeContainer = function (response){
	// 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){
				//alert("add scripts");
				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+"()");
			}

			// if we got here without an error, add this to the history
			fuzzController.handleHistory(response);
}

fuzzController.handleHistory = function handleHistory(response){
	// make sure there is a uniqueId
//dump(response);

	if(! response.currentFuzzController) return;

	//create the location string
	c = response.currentFuzzController;
	uP = "c="+c.control+"&a="+c.action;
	if(c.container && c.container != "") uP += "&t="+c.container;

	//set the new unique Id
	uP = "c="+c.control+"&a="+c.action;
	if(c.container && c.container != "") uP += "&t="+c.container;
	if(response.callbackData && response.callbackData.optionsArr && response.callbackData.optionsArr.forwardHistory && response.callbackData.optionsArr.forwardHistory == "false"){
		// try to get the iframe object
		iframe = window.frames["fuzzController-history-iframe"];

		frameHDiv = iframe.document.getElementById("fuzzController-location-history-iframe-div-id");
		if(! frameHDiv) return;

		// set the current page history
		frameUIdDiv = iframe.document.getElementById("fuzzController-historyUId-iframe-div-id");
		if(! frameUIdDiv) return;

		hDiv = document.getElementById("fuzzController-location-history-div-id");
		if(! hDiv) return;
		hDiv.innerHTML = frameHDiv.innerHTML;

		// set the current page history
		uIdDiv = document.getElementById("fuzzController-historyUId-div-id");
		if(! uIdDiv) return;
		uIdDiv.innerHTML = frameUIdDiv.innerHTML;

	}
	else{
		uId = response.uniqueId;

		// set the current page history
		hDiv = document.getElementById("fuzzController-location-history-div-id");
		if(! hDiv) return;
		hDiv.innerHTML = uP;

		// set the current page history
		uIdDiv = document.getElementById("fuzzController-historyUId-div-id");
		if(! uIdDiv) return;
		uIdDiv.innerHTML = uId;

		//add the submit data
		if(c.submitData){

			php = new PHP_JS();
			uP +="&s="+php.urlencode(JSON.stringify(c.submitData));

		}



		uP = "h="+uId+"&"+uP;
		// try to get the iframe object
		iframeObj = document.getElementById("fuzzController-history-iframe-id");
		if(! iframeObj) return;
		//window.location = "?"+uP;
		iframeObj.src = fuzzFrameworkBaseURL+"/fuzzMVC/historyController.php?"+uP;

	}

		
		//iframeObj.src = "/";


}

///CONTAINERS
//fuzzController.displayModelFadeInCallback = function displayModelFadeInCallback(control, action, height, width, submitData, isJSONData, onConfirm, jsCallbackFn){


///CONTAINERS
fuzzController.closeModal = function closeModal(){
	modalContainer = document.getElementById("fuzzController_modal_container_div_id");
	modalFadeinDiv = document.getElementById("fuzzController_modal_fadein_div_id");
	modalView = document.getElementById("fuzzController_modal_view_div_id");
	modalViewContainer = document.getElementById("fuzzController_modal_view_container_div_id");

	modalView.innerHTML = "";
	
	hideElement(modalContainer);
	hideElement(modalFadeinDiv);
	hideElement(modalView);
	hideElement(modalViewContainer);
	fuzzController.modal = new Object();
	
}

fuzzController.displayModalContainer = function displayModalContainer(control, action, container,height, width, title, showClose, submitData, isJSONData, jsCallbackFn){
	fuzzController.displayModal(control, action, height, width, title, showClose, submitData, isJSONData, jsCallbackFn,container);
}

fuzzController.displayModal = function displayModal(control, action, height, width, title, showClose, submitData, isJSONData, jsCallbackFn,container){
	
	// set the data that will be accessible to the control
	if(! submitData) submitData = "";
	if(! jsCallbackFn) jsCallbackFn = "";
	
	if(isJSONData){
		submitData = JSON.parse(submitData);
	}
	else isJSONData = "";
	
	// init the modal
	if(! height) height = 600;
	if(! width) width = 800;
	if(! title) title = "";
	if(! showClose) showClose = false;
	
	modalContainer = document.getElementById("fuzzController_modal_container_div_id");
	modalFadeinDiv = document.getElementById("fuzzController_modal_fadein_div_id");
	
	modal = new Object();
	modal.control = control;
	modal.action = action;
	modal.container = container;
	modal.height = height;
	modal.width = width;
	modal.showClose = showClose;
	modal.title = title;
	modal.submitData = submitData;
	modal.isJSONData = isJSONData;
	modal.showClose = showClose;
	modal.jsCallbackFn = jsCallbackFn;
	modal.modalFadeinDiv = modalFadeinDiv;
	modal.modalContainer = modalContainer;
	modal.fadeTime = 800;
	modal.fadeInterval = 500 / 15;
	
	fuzzController.currentModal = modal;
	
	var methodArgs = new Object();
	var callbackData = new Object();
	
	fuzzController.setOpacity(modalFadeinDiv, 0);
	showElement(modalContainer);
	showElement(modalFadeinDiv);
	
	var postData = "";
	
	
	fuzzController.fadeInModal(0,60);
}

fuzzController.showContainer = function showContainer(control){
	var divId = "fuzzController_container_"+control+"_div_id";

	var cDiv = document.getElementById(divId);
	
	if(! cDiv) {
		throw("Unable to display container: "+control+". It seams the container element was not initialized.");
	}

	showElement(cDiv);
}

fuzzController.displayModalView = function displayModalView() {	
	modalView = document.getElementById("fuzzController_modal_view_div_id");
	modalHeader = document.getElementById("fuzzController_modal_header_div_id");
	modalViewContainer = document.getElementById("fuzzController_modal_view_container_div_id");
	hasTitleBar = false;
	

	// see if we need to show the header
	if(modal.title != "" || modal.showClose){
		modalHeader.style.width = modal.width+"px";

		modalTitle = document.getElementById("fuzzController_modal_title_div_id");
		if(modal.title != ""){
			modalTitle.innerHTML = modal.title;
			showElement(modalTitle);
			hasTitleBar = true;
		}
		else hideElement(modalTitle);

		modalClose = document.getElementById("fuzzController_modal_close_href_id");
		if(modal.showClose){
			showElement(modalClose);
			hasTitleBar = true;
		}
		else hideElement(modalClose);
	}
	else hideElement(modalHeader);
	
	// hide it to get the correct measurements
	modalView.style.visibility = "hidden";
	modalHeader.style.visibility = "hidden";

	showElement(modalView);
	showElement(modalViewContainer);

	headerHeight = 0;
	if(hasTitleBar){
		showElement(modalHeader);
		// get the size
		headerHeight = modalHeader.offsetHeight;
		if(! headerHeight || headerHeight < 1) headerHeight = 27;
	}

	modalViewContainer.style.marginTop = "-"+(modal.height/2)+"px";
	modalView.style.height = (modal.height - headerHeight) +"px";
	modalView.style.width = modal.width+"px";

	modalView.style.visibility = "visible";
	modalHeader.style.visibility = "visible";




	//fuzzController.setOpacity(modal.modalViewContainer, 100);

	
	
	//return;
	//fuzzController.displayWaitDiv(control,"Loading, Please Wait...");;
	methodArgs = new Object();
	methodArgs.control = modal.control;
	methodArgs.action = modal.action;
	methodArgs.title = modal.title;
	methodArgs.showClose = modal.showClose;
	methodArgs.submitData = modal.submitData;
	methodArgs.containerDiv = modal.container;
	methodArgs.useHTTPS = true;
	
	callbackData = new Object();
	callbackData.control = modal.control;
	callbackData.action = modal.action;
	callbackData.jsCallbackFn = modal.jsCallbackFn;
	
	fuzzAsync.util.displayProgressDiv("fuzzController_modal_view","Loading, please wait...");	
	fuzzAsync.callAction("fuzzController","display-modal-container",methodArgs,fuzzController.displayModalViewCallback,callbackData);
}

fuzzController.displayModalViewCallback = function displayModalViewCallback(response) {
	modalView = document.getElementById("fuzzController_modal_view_div_id");
	var waitDiv = document.getElementById("fuzzController_modal_view_wait_div_id");
	fuzzAsync.util.hideProgressDiv("fuzzController_modal_view");	
	modalView.innerHTML = response.returnHTML;

	fuzzController.initializeContainer(response);
	
}

fuzzController.setOpacity = function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;

	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

fuzzController.fadeInModal = function fadeIn(opacity,totalOpacity) {
	obj = fuzzController.currentModal.modalFadeinDiv;

	if (opacity <= totalOpacity) {
		//alert(fuzzSlideshow.currentSlides[name].fadeIntervalTime);
		//alert(fuzzSlideshow.currentSlides[name].opacityInterval);
		fuzzController.setOpacity(obj, opacity);
		opacity += 4;
		
		window.setTimeout("fuzzController.fadeInModal("+opacity+","+totalOpacity+")", fuzzController.currentModal.fadeInterval);
	}
	else{
		fuzzController.displayModalView();
	}
}

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;
}

fuzzController.handleImageSubmitOver = function handleImageSubmitOver(imgId, hoverImgId) {
	if(! fuzzController.imageButtons[imgId]){
		fuzzController.imageButtons[imgId] = new Object();
	}
	if(! fuzzController.imageButtons[imgId].imgSubmitObj){
		fuzzController.imageButtons[imgId].imgSubmitObj = new Object();
	}
	
	fuzzController.imageButtons[imgId].imgSubmitObj.doSwitch = false;

	showAndHide(hoverImgId, imgId);
}

fuzzController.handleImageSubmitTimeout = function handleImageSubmitTimeout(imgId, hoverImgId) {

	if(fuzzController.imageButtons[imgId].imgSubmitObj){
		if(fuzzController.imageButtons[imgId].imgSubmitObj.doSwitch){
			fuzzController.handleImageSubmitOut(imgId,hoverImgId);
			showAndHide(imgId,hoverImgId);
		}
	}
}

fuzzController.handleImageSubmitOut = function handleImageSubmitOut(imgId, hoverImgId) {

	if(fuzzController.imageButtons[imgId].imgSubmitObj){
		if(fuzzController.imageButtons[imgId].imgSubmitObj.timeoutVal) clearTimeout(fuzzController.imageButtons[imgId].imgSubmitObj.timeoutVal);
		fuzzController.imageButtons[imgId].imgSubmitObj.timeoutVal = setTimeout("fuzzController.handleImageSubmitTimeout('"+imgId+"','"+hoverImgId+"');",200);
		fuzzController.imageButtons[imgId].imgSubmitObj.doSwitch = true;
	}
}


