/***
 *  Brentt Smith
 * 
 *  Common javascript utilities
 */



var dragObject  = null;
var mouseOffset = null;

function mouseCoords(ev){
	var x;
	var y;
	
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	if(document.body && (ev.clientX || ev.clientY)) {
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	}//*** end if
}

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
	
	if(dragObject != null) {
		dragObject.style.position = 'absolute';
		dragObject.style.top      = mousePos.y - mouseOffset.y;
		dragObject.style.left     = mousePos.x - mouseOffset.x;
		return false;
	}//*** end if
}

function mouseUp(){
	dragObject = null;
}

function makeDraggable(itemName){
	var item = document.getElementById(itemName);
		
	if(!item) {
		return;
	}//*** end if
	else {
		item.onmousedown = function(ev) {
			dragObject  = this;
			mouseOffset = getMouseOffset(this, ev);
			return false;
		}//*** end function
	}//*** end else
}

/**************************************************************
 * Toggle element functions 
 *  - if a [+] is found, turns to [-] and vice versa
 *
 **************************************************************/
function toggleModule(moduleName, newStyle, linkControl) {
	var targetModule = document.getElementById(moduleName);
	if(newStyle == null) {
		newStyle = 'inline';
	}//*** end if
	
	if(targetModule.style.display == 'none') {
		targetModule.style.display = newStyle;
		if(linkControl != null) {
			changeLink(linkControl);
		}//*** end if
	}//*** end if
	else {
		targetModule.style.display = 'none';
		if(linkControl != null) {
			changeLink(linkControl);
		}//*** end if
	}//*** end else
}

function changeLink(linkControl) {
	if(document.getElementById(linkControl)) {
		var targetModule = document.getElementById(linkControl);
		var linkText = targetModule.innerHTML;
		if(linkText.indexOf("[+]") > -1) {
			linkText = linkText.replace(/\+/, '-');
			targetModule.innerHTML = linkText;
		}//*** end if
		else if(linkText.indexOf("[-]") > -1) {
			linkText = linkText.replace(/-/, '+');
			targetModule.innerHTML = linkText;
		}//*** end if
	}//*** end if
}

function newToggleModule(moduleName, objectName, closeOthers, newStyle) {
	var targetModule = document.getElementById(moduleName);
	if(newStyle == null || newStyle == '') {
		newStyle = 'inline';
	}//*** end if
	
	if(closeOthers) {
		document.getElementsByName(objectName).style.display = 'none';
		if(document.getElementById(objectName + '-arrows') != null) {
			document.getElementById(objectName + '-arrows').innerHTML = '&raquo';
		}//*** end if
	}//*** end if
	
	if(targetModule != null) {
		targetModule.style.display = newStyle;
		if(document.getElementById(moduleName + '-arrows') != null) {
			document.getElementById(moduleName + '-arrows').innerHTML = '&laquo';
		}//*** end if
	}//*** end if
}

/***************************************************************
 * Help floater functions.
 * - These require the calling page to have a div or span tag 
 *   to act upon.  The tag must have display set to none in a 
 *   style attribute, not in a css sheet.
 **************************************************************/ 
function showHelpFloater(floaterId, evt) {
	//*** find X and Y with scrolling
	var docX = 0;
	var docY = 0;
	if(evt) {
		if(typeof(evt.pageX) == 'number') {
			docX = evt.pageX;
			docY = evt.pageY;
		}//*** end if
		else {
			docX = evt.clientX;
			docY = evt.clientY;
		}//*** end else
	}//*** end if
	else {
		evt = window.event;
		docX = evt.clientX;
		docY = evt.clientY;
	}//*** end else

	if(document.documentElement
		&& (document.documentElement.scrollTop
		|| document.documentElement.scrollLeft)) {

		docX += document.documentElement.scrollLeft;
		docY += document.documentElement.scrollTop;

	}//*** end if
	else if( document.body
		&& ( document.body.scrollTop
		 || document.body.scrollLeft)) {

		docX += document.body.scrollLeft;
		docY += document.body.scrollTop;

	}//*** end else

	var offset = 20;

	//*** find element width and height
	var elementWidth = document.getElementById(floaterId).style.width;
	elementWidth = elementWidth.substr(0, elementWidth.indexOf("px"));
	elementWidth = elementWidth * 1;
	var elementHeight = document.getElementById(floaterId).style.height;
	elementHeight = elementHeight.substr(0, elementHeight.indexOf("px"));
	elementHeight = elementHeight * 1;
	
	var windowWidth = document.body.offsetWidth;
	var windowHeight = document.body.offsetHeight;

	//*** if there isn't enough room to the right, pop to the left
	//alert("docX: " + docX + " elementWidth: " + elementWidth + " window: " + windowWidth);
	if((docX + elementWidth + offset) > windowWidth) {
		docX = docX - elementWidth - offset;
	}//*** end if
	else {
		docX += offset;
	}//*** end else

	//*** if there isn't enough room below, let them scroll.  popping above never works well.
	//*** it usually disappears under a toolmar or nav frame.
	docY += offset;


	document.getElementById(floaterId).style.top = docY;
	document.getElementById(floaterId).style.left = docX;
	document.getElementById(floaterId).style.display = 'inline';

}

function hideHelpFloater(floaterId) {
	document.getElementById(floaterId).style.display = 'none';
}

/*********************************************************
 * Counts characters in a text field or textarea then
 * displays in given element
 *********************************************************/
function characterCount(field, element) {
    var count = document.forms[0].elements[field].value.length;
    document.getElementById(element).innerHTML= "&nbsp;&nbsp;&nbsp;<font class=\"tiny1\">Count: " + count + "</font>";
}


/****
 * Load these tasks as close as possible to the document being loaded
 * to reduce chance of errors
 ***/

document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;
 
