


//	***************************************************************************
//	*** Setup AutoCompletes                                                 ***
//	***************************************************************************
//
//	Version: 1.0
//	Last Updated: 15/09/2008
//
//	Function: Creates a dynamic autocomplete for each element that have the
//		class "autocomplete".
//
//	Inputs:
//		none
//
//	Outputs:
//		none

perform_transformations.subscribe(function(type, args) {
	YAHOO.util.Dom.getElementsByClassName("autocomplete", null, args[0].root, function(el) {
		var ac = el.autocomplete = new YAHOO.widget.AutoComplete(el, el.id + "_autocomplete", el.autocomplete.datasource, el.autocomplete);
		
		//	*** Use XML datasource and insert "mode=ajax" into querystring
		ac.datasource.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
		ac.datasource.scriptQueryAppend = "mode=ajax";
		
		//	*** Set header, body and footer
		if (ac.header) ac.setHeader(ac.header);
		if (ac.body) ac.setBody(ac.body);
		if (ac.footer) ac.setFooter(ac.footer);
		
		//	*** Cancel item selection
		if (!ac.itemSelection) ac._selectItem = function() { return true };
		
		//	*** Subscribe events
		if (ac.onDataError) ac.dataErrorEvent.subscribe(ac.onDataError);
		if (ac.onDataReturn) ac.dataReturnEvent.subscribe(ac.onDataReturn);
		
		//	*** Set and unset "loading" class on input elements parent
		ac.dataRequestEvent.subscribe(function(oSelf, sQuery) {
			YAHOO.util.Dom.addClass(YAHOO.util.Dom.getAncestorBy(ac._elTextbox, function() { return true }), "loading");
		});
		ac.dataReturnEvent.subscribe(function(oSelf, sQuery) {
			YAHOO.util.Dom.removeClass(YAHOO.util.Dom.getAncestorBy(ac._elTextbox, function() { return true }), "loading");
		});
		
		//	*** If alwaysShowNonEmptyContainer is true only hide the container if the query is empty
		if (ac.alwaysShowNonEmptyContainer) {
			//	*** Store old _toggleContainer function
			ac.oldToggleContainer = ac._toggleContainer;
			
			//	*** Create overridden _toggleContainer function
			ac._toggleContainer = function(bShow) {
				//	*** Show the container normally
				if (bShow) return ac.oldToggleContainer(bShow);
				
				//	*** Only hide the container if the query is empty
				if (ac._elTextbox.value.match(/^\s*$/) != null) return ac.oldToggleContainer(bShow);
			};
		}
		
		//	*** If alwaysShowContainer or alwaysShowNonEmptyContainer is true display the emptyBody contents
		if (ac.alwaysShowContainer || ac.alwaysShowNonEmptyContainer) {
			//	*** Set default emptyBody contents
			if (!ac.emptyBody) ac.emptyBody = "<p class\"notification warning\">No matching items found.</p>";
			
			//	*** When results are returned
			ac.dataReturnEvent.subscribe(function(sType, aArgs) {
				var resultsBody = ac._elBody;
				var resultsList = YAHOO.util.Dom.getLastChild(resultsBody);
				var errorDiv = YAHOO.util.Dom.getElementsBy(function() { return true }, "div", resultsBody);
				if (errorDiv.length > 0) {
					errorDiv = errorDiv[0];
				} else {
					errorDiv = null;
				}
				//	*** If no results and error doesn't exist
				if (aArgs[2].length < 1) {
					//	*** If error element doesn't exist
					if (!errorDiv) {
						//	*** Create error element
						var errorDiv = resultsBody.insertBefore(document.createElement("div"), resultsList);
						errorDiv.innerHTML = ac.emptyBody;
					}
					
					//	*** Store original results list display style
					if (!resultsList.originalDisplay) resultsList.originalDisplay = YAHOO.util.Dom.getStyle(resultsList, "display");
					
					//	*** Hide results list
					YAHOO.util.Dom.setStyle(resultsList, "display", "none");
				} else {
					//	*** If error element exists
					if (errorDiv) {
						//	*** Remove error element
						errorDiv.outerHTML = "";
					}
					
					//	*** Show results list
					YAHOO.util.Dom.setStyle(resultsList, "display", resultsList.originalDisplay);
				}
			});
		}
		
		//	*** Change output if List Type not "ul"
		switch (ac.listType.toLowerCase()) {
			case "ol":
				//	*** Change list initialisation to use "ol"
				eval("ac._initList = " + ac._initList.toString().replace(/"ul"/ig, "\"ol\""));
				
				//	*** Re-initialise list
				ac._initList();
				
				break;
			case "dl":
				//	*** Change list initialisation to use "dl" and "dt" tags and call function to create "dd" elements
				eval("ac._initList = " + ac._initList.toString().replace(/"ul"/ig, "\"dl\"").replace(/"li"/ig, "\"dt\"").replace(/(this\._maxResultsDisplayed\s*=\s*this\.maxResultsDisplayed;)/ig, "$1this._initDefinitions();"));
				
				//	*** Function to create "dd" elements
				ac._initDefinitions = function() {
					//	*** Create array of "dd" elements
    				ac._aDListItems = [];
					
					//	*** For each list item
					YAHOO.util.Dom.getElementsBy(function() { return true }, "dt", this._elBody, function(el) {
						//	*** Create "dd" tag
						var parentList = el.parentNode;
						var definition = document.createElement("dd");
						if (el.nextSibling) {
							definition = parentList.insertBefore(definition, el.nextSibling);
						} else {
							definition = parentList.appendChild(definition);
						}
        				ac._aDListItems[el._nItemIndex] = definition;
        				ac._initListItem(definition, el._nItemIndex);
					});
				}
				
				//	*** Re-initialise list
				ac._initList();
				
				//	*** If exists, store old doBeforeExpandContainer function
				if (ac.doBeforeExpandContainer) ac.oldDoBeforeExpandContainer = ac.doBeforeExpandContainer;
				
				//	*** Populate "dd" elements
				ac.doBeforeExpandContainer = function(elTextbox, elContainer, sQuery, aResults) {
					//	*** For each "dt" element
					YAHOO.util.Dom.getElementsBy(function() { return true }, "dt", elContainer, function(el) {
						//	*** Get corresponding "dd" element
						var definition = ac._aDListItems[el._nItemIndex];
						//	*** If result for the list item exists
						if (aResults[el._nItemIndex]) {
							//	*** Populate "dd" element
							definition.innerHTML = aResults[el._nItemIndex][1];
							definition.style.display = "";
							definition._sResultKey = el._sResultKey;
            				definition._oResultData = el._oResultData;
						} else {
							//	*** Hide "dd" element
							definition.innerHTML = null;
							definition.style.display = "none";
							definition._sResultKey = null;
							definition._oResultData = null;
						}
					});
					
					//	*** If exists, execute old doBeforeExpandContainer function
					if (ac.oldDoBeforeExpandContainer) {
						return ac.oldDoBeforeExpandContainer();
					} else {
						return true;
					}
				};
				
				break;
		}
	});
});


//	***************************************************************************
//	*** Setup AJAX Elements                                                 ***
//	***************************************************************************
//
//	Version: 1.0
//	Last Updated: 9/09/2008
//
//	Function: Sets up AJAX calls on forms and links that have the class "ajax"
//
//	Inputs:
//		none
//
//	Outputs:
//		none

perform_transformations.subscribe(function(type, args) {
	//	*** For each link in document with class "ajax" initialise the AJAX call
	var ajaxElems = YAHOO.util.Dom.getElementsByClassName("ajax", "a", args[0].root, function(el) {
		//	*** Get URL parts
		el.ajaxURL = el.href.match(/^(https?:\/\/[^\?#]*)((?:\?[^#]*)?)((?:#[\d\D]*)?)$/i);
		if (!el.ajaxURL) return false;
		
		//	*** Insert "mode=ajax" into querystring
		if (el.ajaxURL[0] != "") {
			if (el.ajaxURL[2] == "") {
				el.ajaxURL[2] = "?";
			} else {
				el.ajaxURL[2] += "&";
			}
			el.ajaxURL = el.ajaxURL[1] + el.ajaxURL[2] + "mode=ajax" + el.ajaxURL[3];
		} else {
			el.ajaxURL = null;
			return false;
		}
	});
	
	//	*** Add an onClick listener that runs AJAX
	YAHOO.util.Event.addListener(ajaxElems, "click", function(e) {
		//	*** Reruns the event if AJAX call fails
		if (!this.ajax_callback.oldFailure) {
			if (!this.ajax_callback.argument) this.ajax_callback.argument = {};
			this.ajax_callback.argument.ajaxCaller = this;
			this.ajax_callback.oldFailure = this.ajax_callback.failure;
			this.ajax_callback.failure = function(ajax) {
				if (ajax.argument.ajaxCaller.ajax_callback.oldFailure) ajax.argument.ajaxCaller.ajax_callback.oldFailure();
				document.location.href = ajax.argument.ajaxCaller.href;
			};
		}
		if (YAHOO.util.Dom.hasClass(this, "ajax")) YAHOO.util.Connect.asyncRequest('GET', this.ajaxURL, this.ajax_callback);
		YAHOO.util.Event.stopEvent(e);
	});
	
	//	*** For each form in document with class "ajax"
	ajaxElems = YAHOO.util.Dom.getElementsByClassName("ajax", "form", args[0].root, function(el) {
		alert(el);
	});
});


//	***************************************************************************
//	*** Hide Elements                                                       ***
//	***************************************************************************
//
//	Version: 1.0
//	Last Updated: 20/03/2009
//
//	Function: Hides elements that have the class "hidden".
//
//	Inputs:
//		none
//
//	Outputs:
//		none

perform_transformations.subscribe(function(type, args) {
	//	*** For each element in document with class "hidden"
	YAHOO.util.Dom.getElementsByClassName("hidden", null, args[0].root, function(el) {
		//	*** Set original display mode
		el.originalDisplay = YAHOO.util.Dom.getStyle(el, "display");
		
		//	*** Hide element
		YAHOO.util.Dom.setStyle(el, "display", "none");
		
		//	*** Unhide function
		el.unhide = function() {
			YAHOO.util.Dom.setStyle(this, "display", this.originalDisplay);
		};
	});
	
	//	*** For each disabled element in document with type = "submit"
	YAHOO.util.Dom.getElementsBy(function(el) {
			if (el.disabled && el.type && el.type.toLowerCase() == "submit") {
				return true;
			} else {
				return false;
			}
		}, "button", args[0].root, function(el) {
		el.disabled = false;
	});
});


//	***************************************************************************
//	*** Remove Non Linked Links                                             ***
//	***************************************************************************
//
//	Version: 1.0
//	Last Updated: 9/09/2008
//
//	Function: Cancels the onClick event for links (a tags) that have the class
//		"nolink"
//
//	Inputs:
//		none
//
//	Outputs:
//		none

perform_transformations.subscribe(function(type, args) {
	//	*** For each link in document with class "nolink"
	var linkElems = YAHOO.util.Dom.getElementsByClassName("nolink", "a", args[0].root);
	//	*** Add an onClick listener that cancels the event
	YAHOO.util.Event.addListener(linkElems, "click", function(e) {
		if (YAHOO.util.Dom.hasClass(this, "nolink")) YAHOO.util.Event.stopEvent(e);
	});
});