
addEvent( window, "load", attachRolloverButtonEvents, false );

//Hack for Firefox .click function
if (navigator.userAgent.indexOf("Firefox")!=-1) {
	HTMLElement.prototype.click = function() {
		var evt = this.ownerDocument.createEvent('MouseEvents');
		evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
		this.dispatchEvent(evt);
		} 
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}


function fireOnclick(elementId) {
	document.getElementById( elementId ).click();
}

function attachRolloverButtonEvents() {
	if(window.Prototype) {
		$$('.aplosRolloverButton').each(function(aplosRolloverBtn) {
			aplosRolloverBtn.observe('mouseover', function() {
				
				var btnRightDiv = aplosRolloverBtn;
				divElements = btnRightDiv.getElementsByTagName( "div" );
				var btnLeftDiv = divElements[ 0 ];
				var btnMiddleDiv = divElements[ 1 ];
				btnRightDiv.style.backgroundImage = btnRightDiv.style.backgroundImage.replace( "btnRight", "btnRight_over" );
				btnLeftDiv.style.backgroundImage = btnLeftDiv.style.backgroundImage.replace( "btnLeft", "btnLeft_over" );
				btnMiddleDiv.style.backgroundImage = btnMiddleDiv.style.backgroundImage.replace( "btnMiddle", "btnMiddle_over" );
				document.body.style.cursor = 'pointer';
			} );   
		});
		$$('.aplosRolloverButton').each(function(aplosRolloverBtn) {
			aplosRolloverBtn.observe('mouseout', function() {
				var btnRightDiv = aplosRolloverBtn;
				divElements = btnRightDiv.getElementsByTagName( "div" );
				var btnLeftDiv = divElements[ 0 ];
				var btnMiddleDiv = divElements[ 1 ];
				btnRightDiv.style.backgroundImage = btnRightDiv.style.backgroundImage.replace( "btnRight_over", "btnRight" );
				btnLeftDiv.style.backgroundImage = btnLeftDiv.style.backgroundImage.replace( "btnLeft_over", "btnLeft" );
				btnMiddleDiv.style.backgroundImage = btnMiddleDiv.style.backgroundImage.replace( "btnMiddle_over", "btnMiddle" );
				document.body.style.cursor = 'default';
			} );   
		});
	}
}

/**
 * The onclick handler for ShufflerRenderer.
 *
 * @param formClientId  the clientId of the enclosing UIForm component
 * @param clientId      the clientId of the Shuffler component
 */
function _aplos_click_event( formClientId, clientId)
{
  var form = document.forms[formClientId];
  var input = form[clientId];
  if (!input) // if the input element does not already exist, create it and add it to the form
  {
    input = document.createElement("input");
    input.type = 'hidden';
    input.name = clientId;
    form.appendChild(input);
  }
  input.value = 'clicked';
  form.submit();
}
