/*
 * KButton elements provide RsR L&F and delegates
 * onclick event to the first child element.
 * Strategy is to hide the original button element
 * inside the KButton table. Click events are 
 * trapped by the new button and delegated to the
 * original hidden button element. All other
 * events are ignored.
 * Requires: dom-utils.js, event-utils.js
 * <script src="/applications/wpk/html/js/com/kronos/event.js">
 * <script src="/applications/wpk/html/js/com/kronos/dom-utils.js">
 */
function Button_onmouseover(element){
	var button=getAncestorByTagName(element, 'TD')
	Button_hover(button)
	if(button){
		if(!button.onclick){
			button.onclick=function(event){
				event=document['com.kronos.event'].wrapInspection(event)
				var buttonContainer=this.getElementsByTagName('var')[0]
				if(buttonContainer){
					var delegateClick=buttonContainer.getElementsByTagName('*')[0]
					if(delegateClick){
						// prevent infinite loop
						this.onclick=null
						if(delegateClick.onclick){
							if(delegateClick.click) {
								return delegateClick.click()
							}
							return (delegateClick.onclick).apply(delegateClick, [event])
						} else {
							if(delegateClick.click) {
								return delegateClick.click()
							}						
						}
					}
				}
			}
		}
		var a = button.getElementsByTagName('A')[0]
		if(a){
			a.click=function(event){
				var myButtonTD = getAncestorByTagName(this, 'TD')
				if(myButtonTD){
					myButtonTD.onclick()
				}
			}
		}
	}
}

function Button_hover(button){
	if(button){
		tarClassName(button, 'Hover', 1)
		if(!button.onmouseout){
			button.onmouseout=function(event){
				tarClassName(this, 'Hover', 0)
			}
		}
	}
}

/**
	Given the Id of an element contained within 
	a TABLE.Button element, invoke the action 
	for the given button.
 */
function Button_doAction(buttonId){
	var a=document.getElementById(buttonId)
	if(a){
		// ensure the action delegation code
		// is hooked up to this button ...
		Button_onmouseover(a)
		var button=getAncestorByTagName(a, 'TD')
		if(button&&button.onclick){
			button.onclick()
		}
	}
}

window['com.kronos.button'] = {
	/**
		Given the Id of an element contained within a
		TABLE.Button element, endable the button behavior
		and remove the Disabled class from the TABLE.Button 
		element.
	*/
	enable:function(id){
		var a=document.getElementById(id)
		if(a){
			// some actions are implemented on the Anchor itself
			this.restoreOnClick(a)
			// some actions are implemented on the TD
			this.restoreOnClick(getAncestorByTagName(a, 'TD'))
			// remove the Disabled class from the TABLE.Button element
			tarClassName(getAncestorByClassName(a,'Button'), 'Disabled', false)
		}
	},
	/**
		Given the Id of an element contained within a
		TABLE.Button element, disable the button behavior
		and add the Disabled class to the TABLE.Button 
		element.
	*/
	disable:function(id){
		var a=document.getElementById(id)
		if(a){
			this.cacheOnClick(a)
			this.cacheOnClick(getAncestorByTagName(a, 'TD'))
			// add the Disabled class to the TABLE.Button element
			tarClassName(getAncestorByClassName(a,'Button'), 'Disabled', true)
		}
	},
	/**
		Given the Id of an element contained within a
		TABLE.Button element, determine if the button
		is ENABLED or not. Returns true if button is
		enabled, false if it is disabled.
	*/
	isEnabled:function(id){
		var a=document.getElementById(id)
		if(a){
			return !isClass(getAncestorByClassName(a,'Button'), 'Disabled')
		}
	},
	// private convenience methods
	cacheOnClick:function(element){
		if(element&& !element['com.kronos.button:onclick-cache']){
			element['com.kronos.button:onclick-cache'] = element.onclick
			element.onclick = function(e){document['com.kronos.event'].wrapInspection(e).stopPropagation()}
		}
	},
	restoreOnClick: function(element){
		if(element&&element['com.kronos.button:onclick-cache']){
			element.onclick = element['com.kronos.button:onclick-cache']
			element['com.kronos.button:onclick-cache'] = null
		}
	}
}

