﻿// 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();
	}
	
}
/*
***********************/

/*
*************************************************
	txuPopupCalendar

		name => unique name for the popup calendar	
		container => id of the calendar object container
		trigger => id of link to trigger the popup
		textbox => id of text box to receive date
	
*************************************************
*/

TXUPopupCalendar = function(calendarEl, containerEl, triggerEl, textboxEl) {

	this.calendarEl = calendarEl;
	this.containerEl = containerEl;
	this.triggerEl = triggerEl;
	this.textboxEl = textboxEl;
	
	this.calendar = null;
	this.container = null;
	this.textbox = null;
	this.trigger = null;

	this.isMouseOver = false;
	this.isVisible = false;

	this.init = function() {
			this.container = YAHOO.util.Dom.get(this.containerEl);

			// Add the container element if it's not present	
			if (this.container == null) {
				this.container = document.createElement('div');
				this.container.setAttribute('id', this.containerEl);
				this.container.setAttribute('style', 'position: absolute; display: none; z-index: 500;');
				document.body.appendChild(this.container);
			}
			
			this.calendar = new YAHOO.widget.Calendar(this.calendarEl, this.container);
			this.calendar.selectEvent.subscribe(this.getDate, this, true);
			this.calendar.renderEvent.subscribe(this.render, this, true);
			
			this.trigger = YAHOO.util.Dom.get(this.triggerEl);
			this.trigger.onclick = function() { return false; };
			this.textbox = YAHOO.util.Dom.get(this.textboxEl);
			
			YAHOO.util.Event.addListener(this.trigger, 'click', this.show, this, true);
			YAHOO.util.Event.addListener(this.trigger, 'blur', this.hide, this, true);
			
			this.calendar.render();
			this.calendar.hide();
	}

	this.render = function () {
		YAHOO.util.Event.addListener(this.container, 'mouseover', this.overCal, this, true);
		YAHOO.util.Event.addListener(this.container, 'mouseout', this.outCal, this, true);
	}
	
	this.overCal = function() {
		this.isMouseOver = true;
		over_cal = true;
	}

	this.outCal = function() {
		this.isMouseOver = false;
		over_cal = false;
	}

	this.show = function() {
		if (this.isVisible) {
			this.hide();
			return;
		}
		
		var date = YAHOO.util.Dom.get(this.textbox).value;
		var testDate = new Date(date);
		if (testDate == 'Invalid Date' || testDate == 'NaN') date = null;
		if (date) {
			this.calendar.cfg.setProperty('selected', date);
			this.calendar.cfg.setProperty('pagedate', new Date(date), true);
			this.calendar.render();
		}
		YAHOO.util.Dom.setStyle(this.container, 'display', 'block');
		this.isVisible = true;
		
		var triggerRegion = YAHOO.util.Dom.getRegion(this.trigger);
		YAHOO.util.Dom.setXY(this.container, [ triggerRegion.left, triggerRegion.bottom + 3 ]);
	}

	this.hide = function() {
		if (!this.isMouseOver) {
				YAHOO.util.Dom.setStyle(this.container, 'display', 'none');
				this.isVisible = false;
		}
	}

	this.getDate = function() {
		var calDate = this.calendar.getSelectedDates()[0];
		calDate = (calDate.getMonth() + 1) + '/' + calDate.getDate() + '/' + calDate.getFullYear();
		this.textbox.value = calDate;
		this.isMouseOver = false;
		this.hide();
	}

	YAHOO.util.Event.addListener(window, 'load', this.init, this, true);
}

/*
***********************/

/*
*******************************************************************************
	txuContextMenu
	Initialize Context Menu
	
	el =>					id of element to be used as the menu.
				
	triggerEl =>  String / Element / Array representing the trigger
								elements(s) for the menu.
								
*******************************************************************************
*/
TXUContextMenu = function(el, triggerEl) {

	this.element = el;
	this.menu = null;
	this.triggers = triggerEl;
	
	this.show = function(e) {
		var trigger = YAHOO.util.Event.getTarget(e);
		var triggerRegion = YAHOO.util.Dom.getRegion(trigger);
		// NOTE: Here, "this" refers to the yahoo Menu object
		this.menu.cfg.setProperty("xy", [triggerRegion.right + 3, triggerRegion.top + 3]);
		this.menu.show();
	}
	
	YAHOO.util.Event.onContentReady(el, function () {
		this.menu = new YAHOO.widget.Menu(this.element, { position: "dynamic", constraintoviewport: false });
		this.menu.render();
		YAHOO.util.Event.addListener(this.triggers, "click", this.show, this, true);
	}, this, true);

}

/*
***********************/
