/*
Copyright 2006-2007 Wellspring Technologies, LLC
All Rights Reserved
Unauthorized Use Prohibited
*/
var INTERNAL_ddmlButtons = new Array();
function ddmlButton(text, oFunction, className, id, name) {
	this.text = text;
	this.targetFunction = oFunction;
	this.className = className || 'ddmlButton';
	this.id = id;
	this.name = name;
	this.over = false;
	this.down = false;
	//Tie-in functions:
	//this.onMouseOver(button)
	//this.onMouseOut(button)
}

ddmlButton.prototype.getClass = function() {
	var newClass = this.className;
	newClass += (this.over)?'Over':'';
	newClass += (this.down)?'Down':'';
	return newClass;
}

ddmlButton.prototype.getObject = function() {
	var newButton = document.createElement('div');
	newButton.innerHTML = this.text.replace(/&(?!amp;)/g, '&amp;');
	newButton.className = this.className;
	if(this.id) newButton.id = this.id;
	if(this.name) newButton.name = this.name;
	xAddEventListener(newButton, 'mouseover', ddmlButtonOverE);
	xAddEventListener(newButton, 'mouseout', ddmlButtonOutE);
	xAddEventListener(newButton, 'mousedown', ddmlButtonDownE);
	xAddEventListener(newButton, 'mouseup', ddmlButtonUpE);
	xAddEventListener(newButton, 'selectstart', ddmlButtonSelect);
	if(this.targetFunction) xAddEventListener(newButton, 'click', this.targetFunction);
	newButton.ddmlButton = this;
	newButton.ddmlButton.nodeIndex = INTERNAL_ddmlButtons.length;
	INTERNAL_ddmlButtons.push(newButton);
	return newButton;
}

ddmlButton.prototype.getHTML = function(sExtra) {
	if(!sExtra) sExtra = '';
	var sFunctionName = this.targetFunction.toString().replace(/[\n\r]/g, '');
	sFunctionName = sFunctionName.replace(/function\s*(\w*).*/gim, '$1');
	return '<div id="' + this.id + '" name="' + this.name + '" class="' + this.className + '"' +
		' onmouseover="ddmlButtonOver(this);" onmouseout="ddmlButtonOut(this);"' +
		' onmousedown="ddmlButtonDown(this);" onmouseup="ddmlButtonUp(this);"' + 
		' onclick="' + sFunctionName + '(event);" onselectstart="ddmlButtonSelect();" ' + sExtra + '>' +
		this.text + '</div>';
}

ddmlButton.prototype.getNode = function() {
	if(this.nodeIndex !== null && INTERNAL_ddmlButtons.length > this.nodeIndex) return INTERNAL_ddmlButtons[this.nodeIndex];
}

/*
		BUTTON EVENTS
		
These have some quirks because of the differences between object-created buttons and HTML created buttons.

*Object-generated buttons get a ddmlButtonEventE gateway function that passes the node into the regular event function

*Event functions rely on a link to the button object to manage state.  Since HTML created buttons can't be assigned JS
properties, each event handler can rebuild a button object on the node if it doesn't exist.

*/

function ddmlButtonFixHTMLButton(node) {
	node.ddmlButton = new ddmlButton(node.innerHTML, null, node.className, node.id, node.name);
	node.ddmlButton.nodeIndex = INTERNAL_ddmlButtons.length;
	INTERNAL_ddmlButtons.push(node);
}

function ddmlButtonOverE(e) {
	var myEvent = new xEvent(e);
	ddmlButtonOver(myEvent.target);
}
function ddmlButtonOver(node) {
	if(node.ddmlButton) {
		node.ddmlButton.over = true;
		if(node.ddmlButton.onMouseOver) node.ddmlButton.onMouseOver(node.ddmlButton);
		node.className = node.ddmlButton.getClass();
	} else {
		ddmlButtonFixHTMLButton(node);
		ddmlButtonOver(node)
	}
}

function ddmlButtonOutE(e) {
	var myEvent = new xEvent(e);
	ddmlButtonOut(myEvent.target);
}
function ddmlButtonOut(node) {
	if(node.ddmlButton) {
		node.ddmlButton.over = false;
		if(node.ddmlButton.onMouseOut) node.ddmlButton.onMouseOut(node.ddmlButton);
		node.className = node.ddmlButton.getClass();
	} else {
		ddmlButtonFixHTMLButton(node);
		ddmlButtonOut(node);
	}
}

var ddml_INTERNAL_buttonWatch;
function ddmlButtonDownE(e) {
	var myEvent = new xEvent(e);
	ddmlButtonDown(myEvent.target);
}
function ddmlButtonDown(node) {
	if(node.ddmlButton) {
		xAddEventListener(document, 'mousemove', ddmlButtonWatch);
		ddml_INTERNAL_buttonWatch = node;
		node.ddmlButton.down = true;
		node.className = node.ddmlButton.getClass();
	} else {
		ddmlButtonFixHTMLButton(node);
		ddmlButtonDown(node);
	}
}

function ddmlButtonUpE(e) {
	var myEvent = new xEvent(e);
	ddmlButtonUp(myEvent.target)
}
function ddmlButtonUp(node) {
	if(node.ddmlButton) {
		xRemoveEventListener(document, 'mousemove', ddmlButtonWatch);
		ddml_INTERNAL_buttonWatch = null;
		node.ddmlButton.down = false;
		node.className = node.ddmlButton.getClass();
	} else {
		ddmlButtonFixHTMLButton(node);
		ddmlButtonUp(node);
	}
}

function ddmlButtonWatch(e) {
	//This is bound to the document on mousedown to avoid problems with buttons maintaining their hover state when the mouse is moved away with the button down.
	var myEvent = new xEvent(e);
	var currTarget = e.target || window.event.srcElement;
	if(currTarget != ddml_INTERNAL_buttonWatch) {
		ddmlButtonUp(ddml_INTERNAL_buttonWatch);
	}
}

function ddmlButtonSelect(e) {
	return false;
}

