/*****************************************************************
 * Main CEC Public JavaScript file
 *
 * @author    Lawrence Coffin <lcoffin@cio.com>
 * @since     Nov 12, 2008
 *****************************************************************/

/**
 * Set up the main CEC namespace
 */
CEC_Namespace('CEC');


/*****************************************************************
 * Sets up the named namespace
 *
 * Example:
 * 
 *    CEC_Namespace('CEC.group.add_members');
 *
 * Creates:
 *
 *    CEC.group.add_members == new Object();
 *
 *****************************************************************/
function CEC_Namespace(str) {
	var parts = str.split('.');
	
	var el = window;
	
	var part;
	
	while (part = parts.shift()) {
		if (!el[part]) {
			el[part] = new Object();
		}
		
		el = el[part];
	}
	
}


/*****************************************************************
 * Gets the element referenced by 'el'
 * 
 * If 'el' is a string assumes it's an id
 *****************************************************************/
CEC.get = function(el) {
	if (typeof(el) == 'string') {
		return document.getElementById(el);
	}
	else {
		return el;
	}
}


/*****************************************************************
 * Finds the previous element with the given tag.
 * 'Previous' means -- previous sibling within the same
 * group of elements, or any element within the previous
 * sibligs in this group or any parent group.
 * I.e. the last defined element in the HTML file. For example:
 *
 * <div>
 *     <div>
 *			<textarea name="first"></textarea>
 *          <textarea name="second"></textarea>
 *     </div>
 *     <div>
 *         <input type="button" name="Trigger" value="Click Me">
 *         <textarea name="third"></textarea>
 *     </div>
 * </div>
 *
 * If the button named 'Trigger' calls CEC.getPreviousTag('textarea', this),
 * the textarea returned will be the one named 'second'.
 *****************************************************************/
CEC.getPreviousTag = function(tag, btn) {
	// First, check backward through our previous siblings
	var sib = btn; //btn.previousSibling;
	while (sib = sib.previousSibling) {
		if (sib.tagName && sib.tagName.toLowerCase() == tag.toLowerCase()) {
			return sib;
		}
		else if (sib.getElementsByTagName) {
			matches = sib.getElementsByTagName(tag);
			if (matches.length) {
				// return the *last* element of any matches -- that is the first previous element
				return matches[matches.length-1];
			}
		}
	}
	
	// Not found in our immediate previous siblings
	
	// Work back through our parent tree, finding the last tag element
	// in any previous branches
	var parent = btn;
	while (parent = parent.parentNode) {
		sib = parent;
		
		while (sib) {
			if (sib.tagName && sib.tagName.toLowerCase() == tag.toLowerCase()) {
				return sib;
			}
			else if (sib.getElementsByTagName) {
				matches = sib.getElementsByTagName(tag);
				if (matches.length) {
					// return the *last* element of any matches -- that is the first previous element
					return matches[matches.length-1];
				}
			}
			
			// get the next previousSibling at the *end* of the loop, so we check the first parent node too
			sib = sib.previousSibling;
		}
	}
	
	str = '';
	for (v in btn) {
		str += "," + v;
	}
	
//	debug("GetPrevious(): No previous " + tag + " found! Button: " + str);

	// not found, return null
	return null;
}


/*****************************************************************
 * Displays the event registration window
 *
 * @param string id       masked event id
 * @param string name     event title
 * @param string action   optional, specify as 'cancel' to cancel the registration
 * @param boolean big     optional, specify as true to open the window big
 *                        (otherwise a 300x200 window is used)
 *****************************************************************/
CEC.event_registration_form = function(id, name, action, big) {
	var window_width = 300;
	var window_height = 200;
	
	// Pop-up centered on the screen
	var screenX = (screen.width - window_width) / 2;
	var screenY = (screen.height - window_height) / 2;
	
	var cancel = '';
	
	if (action == 'cancel') {
		if (confirm("Are you sure you want to cancel your registration for " + name + "?")) {
			cancel = '&unreg=y';
		}
		else {
			return;
		}
	}
	
	var param_string = '';
	
	if (!big) {
		param_string = 'width=' + window_width + ',height=' + window_height + ',scrollbars=1,screenX=' + screenX + ',screenY=' + screenY + ',left=' + screenX + ',top=' + screenY;
	}
	
	window.open('/to/event_registration.html?event_id=' + id + cancel, '', param_string);
}


/******************************************************************************
 * Dims the browser window behind the popup window
 *
 * I.e. displays a partially transparent div behind the overdiv
 *
 * @author  Steve Ciccolo <sciccolo@cio.com>
 * @since   Dec 2008
 ******************************************************************************/
CEC.dim_window = function () {
	var dim_div = document.getElementById('dim_div');
	var overdiv = layerReference('overDiv');
	if (overdiv) {
		var scrollX = window.pageXOffset ? window.pageXOffset :
			document.documentElement.scrollLeft ? document.documentElement.scrollLeft :
			document.body.scrollLeft;
		var scrollY = window.pageYOffset ? window.pageYOffset :
			document.documentElement.scrollTop ? document.documentElement.scrollTop :
			document.body.scrollTop;
		var top = ((document.documentElement.clientHeight - overdiv.offsetHeight) / 2) + scrollY;
		var left = ((document.documentElement.clientWidth - overdiv.offsetWidth) / 2) + scrollX;
		if ( top > 0) {
			overdiv.style.top = top + 'px';
		}
		else {
			overdiv.style.top = 0 + 'px';
		}
		if ( left > 0) {
			overdiv.style.left = left + 'px';
		}
		else {
			overdiv.style.left = 0 + 'px';
		}
		setTimeout(CEC.check_for_reset, 1000);
		if (!dim_div) {
			dim_div = document.createElement('div');
			dim_div.id = 'dim_div';
			dim_div.style.position = 'absolute';
			dim_div.style.zIndex = overdiv.style.zIndex - 2;
			dim_div.style.top = '0px';
			dim_div.style.left = '0px';
			dim_div.style.background = "#aaa";
			dim_div.style.opacity = 0.75;
			dim_div.style.filter = "alpha(opacity=75)";
			document.body.appendChild(dim_div);
		}
		dim_div.style.height = '2000px';
		dim_div.style.width = '2000px';
		dim_div.style.display = 'block';
	}
}


/******************************************************************************
 * Removes the window dimming when the overdiv is closed
 *
 * @author  Steve Ciccolo <sciccolo@cio.com>
 * @since   Dec 2008
 ******************************************************************************/
CEC.check_for_reset = function () {
	var overdiv = layerReference('overDiv');
	if (overdiv && overdiv.style.visibility == 'visible') {
		setTimeout(CEC.check_for_reset, 200);	// wait
	}
	else {
		var dim_div = document.getElementById('dim_div');
		if (dim_div) {
			dim_div.style.display = 'none';
		}
	}
}


/*******************************************************
 * Make an element visible
 *
 * @param string  id  the id of the element to display
 *
 * @since Dec 2008
 *******************************************************/
CEC.show = function (id) {
	var div = document.getElementById(id);
	if (div) {
		div.style.display = 'block';
	}
}


/*******************************************************
 * Hide an element
 *
 * @param string  id  the id of the element to hide
 *
 * @since Dec 2008
 *******************************************************/
CEC.hide = function (id) {
	var div = document.getElementById(id);
	if (div) {
		div.style.display = 'none';
	}
}


/*******************************************************
 * Opens a centered window
 *
 * Copied from 2.0
 *
 * @author    Lawrence Coffin <lcoffin@cio.com>
 * @since     Mar 18, 2009
 *******************************************************/
CEC.popup_centered = function (url, w, h, scrollbars) {
	var window_width = w || 400;
	var window_height = h || 500;
	
	// Pop-up centered on the screen
	var screenX = (screen.width - window_width) / 2;
	var screenY = (screen.height - window_height) / 2;
	
	window.open(url, '', 'width=' + window_width + ',height=' + window_height + ',scrollbars='+(scrollbars ? 1 : 0)+',screenX=' + screenX + ',screenY=' + screenY + ',left=' + screenX + ',top=' + screenY);
}


/*******************************************************
 * Attaches an event handler to an object
 *
 * Common object/events:
 * window -> load -- onLoad() event handler
 *******************************************************/
CEC.on = function (obj, event, handler) {
	if (obj.addEventListener) {
		obj.addEventListener(event, handler, false);
		return true;
	}
	else if (obj.attachEvent) {
		var r = obj.attachEvent('on' + event, handler);
		return r;
	}
	else {
		obj['on' + event] = handler;
	}
}


/*******************************************************
 * Remove an event handler
 *******************************************************/
CEC.off = function(obj, event, handler) {
	if (obj.detachEvent) { // IE
		obj.detachEvent("on" + event, handler);
	} else if (obj.removeEventListener) { // Gecko / W3C
		obj.removeEventListener(event, handler, true);
	} else {
		obj["on" + event] = null;
	}
};

