// Contains javascript functions to handle actions for site controls

// Set the global namespace for YUI
YAHOO.namespace("txu.ui");

/*
*************************************************
	txuInitDialog
	Initialize Dialog box
*************************************************
*/
function txuInitDialog(elem, cancelElem) {
	var dlg = new YAHOO.widget.Dialog(
		elem
		, {
				width: "360px"
				, fixedcenter: true
				, close: false
				, visible: false
				, modal: true
				, draggable: false
				, underlay: "none"
			} 
		);

	/* 
	 * Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=167801 !!?
	 */
	if (YAHOO.env.ua.gecko) {

		dlg.showEvent.subscribe(function() {
			YAHOO.util.Dom.addClass(dlg.form, "caretfix");
			YAHOO.util.Dom.setStyle(dlg.form, "display", "none");

			var fixDisplay = function() {
				YAHOO.util.Dom.setStyle(dlg.form, "display", "block");
					try {
							dlg.firstFormElement.focus();
					} catch (e) {
							// Not related to the workaround, I just try/catch focus calls
							// do avoid testing for the various conditions in which they could
							// fail.
					}
			}
			setTimeout(fixDisplay, 0);
		});
	}
	
	// This hooks up the specified control's "click" event to close the dialog
	YAHOO.util.Event.addListener(cancelElem, "click", dlg.hide, dlg, true);
	
	dlg.render();
	
	return dlg;
}
/*
***********************/

/*
*************************************************
	txuCollapsibleSignin
	Collapsible signin form object
*************************************************
*/
TXUCollapsibleSignin = function(id, props) {

	this.element = document.getElementById(id);
	if (this.element == null) {
		return; // nothing to do
	}
	
	this.expandingElement = null;
	this.expandTriggerElement = null;

	this.isExpanding = false;
	this.isCollapsing = false;
		
	this.expand = function(e) {

			if (!this.Expanding && !this.isCollapsing) {

				this.isExpanding = true;

				this.expandingElement.style.display = "block";

				if (this.collapseTriggerElement == null) {
					// No collapse trigger was found - reassign the expand trigger to be the collapse trigger
					YAHOO.util.Event.removeListener(this.expandTriggerElement, "mouseover");
					YAHOO.util.Event.addListener(this.expandTriggerElement, "mouseover", this.collapse, this, true);
					// reset the class so CSS can do it's work
					YAHOO.util.Dom.replaceClass(this.expandTriggerElement, "expand", "collapse");
					var thisObj = this;
					setTimeout(function() { thisObj.isExpanding = false; }, 100);
				}
			}

			return false;
	}
	
	this.collapse = function(e) {

			if (!this.isExpanding && !this.Collapsing) {

				this.isCollapsing = true;
				
				this.expandingElement.style.display = "none";

				if (this.collapseTriggerElement == null) {
					// No collapse trigger was found - reassign the expand trigger
					YAHOO.util.Event.removeListener(this.expandTriggerElement, "mouseover");
					YAHOO.util.Event.addListener(this.expandTriggerElement, "mouseover", this.expand, this, true);
					// reset the class so CSS can do it's work
					YAHOO.util.Dom.replaceClass(this.expandTriggerElement, "collapse", "expand");
					var thisObj = this;
					setTimeout(function() { thisObj.isCollapsing = false; }, 100);
				}
			}

			return false;
	}
	
	// Look for the expanding element (class="signin-body")
	var expandingElements = YAHOO.util.Dom.getElementsByClassName("signin-body", null, this.element, null);
	if (expandingElements.length > 0) {
		this.expandingElement = expandingElements[0]; // use the first one
	}
	
	// Look for the expand trigger element (class="expand")
	var expandTriggerElements = YAHOO.util.Dom.getElementsByClassName("expand", null, this.element, null);
	if (expandTriggerElements.length > 0) {
		this.expandTriggerElement = expandTriggerElements[0]; // use the first one
		YAHOO.util.Event.addListener(this.expandTriggerElement, "mouseover", this.expand, this, true);
	}
	
	this.collapseTriggerElement = null;
	
	// Look for the collapse trigger element (class="collapse")
	var collapseTriggerElements = YAHOO.util.Dom.getElementsByClassName("collapse", null, this.element, null);
	if (collapseTriggerElements.length > 0) {
		this.collapseTriggerElement = collapseTriggerElements[0]; // use the first one
		YAHOO.util.Event.addListener(this.collapseTriggerElement, "mouseover", this.collapse, this, true);
	}
	
	if (props != null) {
	
		if (typeof props.collapsed != "undefined") {
			if (props.collapsed == true) {
				this.collapse();
			}
			else {
				this.expand();
			}
		}
		
	}

}
/*
***********************/

/*
*************************************************
	txuCollapsiblePanel
	Collapsible panel object
*************************************************
*/
TXUCollapsiblePanel = function(id, props) {

	this.element = YAHOO.util.Dom.get(id); //document.getElementById(id);
	if (this.element == null) {
		return; // nothing to do
	}
	
	this.initiallyCollapsed = false;
	this.statusLabelElements = null;
	this.statusTextExpanded = null;
	this.statusTextCollapsed = null;
	
	if (props != null) {

		// Initial state	
		if (typeof props.collapsed != "undefined") {
			if (props.collapsed == true) {
				this.initiallyCollapsed = true;
			}	else {
				this.initiallyCollapsed = false;
			}
		}
		
		// Status Label
		if (typeof props.statusLabel != "undefined") {
			if (props.statusLabel) {
				this.statusLabelElements = YAHOO.util.Dom.getElementsByClassName(props.statusLabel, null, this.element, null);
			}
		}
		
		// Status Label Text
		if (typeof props.statusTextExpanded != "undefined") {
			if (props.statusTextExpanded) {
				this.statusTextExpanded = props.statusTextExpanded;
			}
		}
		if (typeof props.statusTextCollapsed != "undefined") {
			if (props.statusTextCollapsed) {
				this.statusTextCollapsed = props.statusTextCollapsed;
			}
		}

	}

	this.expandingElement = null;
	this.expandTriggerElement = null;

	this.isExpanding = false;
	this.isCollapsing = false;
		
	this.expand = function(e) {
			if (!this.isCollapsing) {

				this.isExpanding = true;

				this.expandingElement.style.display = "block";

				if (this.collapseTriggerElement == null) {
					// No collapse trigger was found - reassign the expand trigger to be the collapse trigger
					YAHOO.util.Event.removeListener(this.expandTriggerElement, "click");
					YAHOO.util.Event.addListener(this.expandTriggerElement, "click", this.collapse, this, true);
					// reset the class so CSS can do it's work
					YAHOO.util.Dom.replaceClass(this.expandTriggerElement, "expand", "collapse");
				}
				
				// Set status text if specified
				if (this.statusLabelElements && this.statusTextExpanded) {
					for (i=0;i<this.statusLabelElements.length;i++) {
						if (this.statusLabelElements[i].innerText != undefined) {
							this.statusLabelElements[i].innerText = this.statusTextExpanded;
						} else {
							this.statusLabelElements[i].textContent = this.statusTextExpanded;
						}
					}
				}
				
				// Set class on outer container to indicate status
				YAHOO.util.Dom.replaceClass(this.element, "collapsed", "expanded");
				
				this.isExpanding = false;
			}
			return false;
	}
	
	this.collapse = function(e) {
			if (!this.isExpanding) {

				this.isCollapsing = true;
				
				if (this.expandingElement != null)
					this.expandingElement.style.display = "none";

				if (this.collapseTriggerElement == null) {
					// No collapse trigger was found - reassign the expand trigger
					YAHOO.util.Event.removeListener(this.expandTriggerElement, "click");
					YAHOO.util.Event.addListener(this.expandTriggerElement, "click", this.expand, this, true);
					// reset the class so CSS can do it's work
					YAHOO.util.Dom.replaceClass(this.expandTriggerElement, "collapse", "expand");
				}

				// Set status text if specified
				if (this.statusLabelElements && this.statusTextCollapsed) {
					for (i=0;i<this.statusLabelElements.length;i++) {
						if (this.statusLabelElements[i].innerText != undefined) {
							this.statusLabelElements[i].innerText = this.statusTextCollapsed;
						} else {
							this.statusLabelElements[i].textContent = this.statusTextCollapsed;
						}
					}
				}
				
				// Set class on outer container to indicate status
				YAHOO.util.Dom.replaceClass(this.element, "expanded", "collapsed");
				
				this.isCollapsing = false;
			}

			return false;
	}
	
	// Look for the expanding element (class="panel-body")
	var expandingElements = YAHOO.util.Dom.getElementsByClassName("panel-body", null, this.element, null);
	if (expandingElements.length > 0) {
		this.expandingElement = expandingElements[0]; // use the first one
	}
	
	// Look for the expand trigger element (class="expand")
	var expandTriggerElements = YAHOO.util.Dom.getElementsByClassName("expand", null, this.element, null);
	if (expandTriggerElements.length > 0) {
		this.expandTriggerElement = expandTriggerElements[0]; // use the first one
		YAHOO.util.Event.addListener(this.expandTriggerElement, "click", this.expand, this, true);
	}
	
	this.collapseTriggerElement = null;
	
	// Look for the collapse trigger element (class="collapse")
	var collapseTriggerElements = YAHOO.util.Dom.getElementsByClassName("collapse", null, this.element, null);
	if (collapseTriggerElements.length > 0) {
		this.collapseTriggerElement = collapseTriggerElements[0]; // use the first one
		YAHOO.util.Event.addListener(this.collapseTriggerElement, "click", this.collapse, this, true);
	}
	
	if (this.initiallyCollapsed) {
		this.collapse();
	} else {
		this.expand();
	}
	
}
/*
***********************/
