var scannedStr = null;
var scannedPrefix = null;
var scannedSuffix = null;

addEvent( window, "load", attachRolloverButtonEvents, false );


var aplosJavascript = {
	components :{
		ckEditorHelper:{
		url:'ckEditorHelper.js',
		loaded:false
		}
	},
	addComponent:function(component){
		var c = this.components[component];
		if(c && c.loaded === false){
		var s = document.createElement('script');
		s.setAttribute('type', 'text/javascript');
		s.setAttribute('src',c.url);
		document.getElementsByTagName('head')[0].appendChild(s);
		}
	},
	componentAvailable:function(component){
		this.components[component].loaded = true;
		if(this.listener){
			this.listener(component);
		};
	}	
};

/* Hack for Firefox .click function
 *  This was taken out because it was causing a recursive behaviour on the
 *  CssResourceEdit page when the save button was clicked, why it was in
 *  her in the first place is unknown.
 *  
 */ 
//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 monitorKeyPress( evt ) {
	if( scannerPrefix != null && scannerSuffix != null ) {
		var charCode = (evt.which) ? evt.which : evt.keyCode;
		if( evt.ctrlKey && scannerPrefix.indexOf( String.fromCharCode(charCode) ) != -1 || 
				scannerSuffix.indexOf( String.fromCharCode(charCode) ) != -1 ) {
			if( scannedStr == null ) {
				if( scannedPrefix == null ) {
					if( scannerPrefix.indexOf( String.fromCharCode(charCode) ) == 0 ) {
						scannedPrefix = String.fromCharCode(charCode);
					}
				} else if( scannerPrefix.substring(scannedPrefix.length).indexOf( String.fromCharCode(charCode) ) == (scannerPrefix.substring(scannedPrefix.length).length - 1) ) {
					scannedPrefix += String.fromCharCode(charCode);
					if( scannedPrefix == scannerPrefix ) {
						scannedStr = "";
						scannedPrefix = null;  
					}
				} else {
					scannedPrefix = null;
				}
			} else {
				if( scannedSuffix == null ) {
					if( scannerSuffix.indexOf( String.fromCharCode(charCode) ) == 0 ) {
						scannedSuffix = String.fromCharCode(charCode);
					}
				} else if( scannerSuffix.substring(scannedSuffix.length).indexOf( String.fromCharCode(charCode) ) == (scannerSuffix.substring(scannedSuffix.length).length - 1) ) {
					scannedSuffix += String.fromCharCode(charCode);
					if( scannedSuffix == scannerSuffix ) {
	    				registerScan( scannedStr );
						var targetValue = evt.target.value;
						var targetScannedStr = scannerPrefix + scannedStr + scannerSuffix.substring(0,scannerSuffix.length-1);
						if( targetValue != null ) {
							evt.target.value = targetValue.replace( targetScannedStr, scannedStr );
						}
						evt.preventDefault();
						scannedStr = null;
						scannedSuffix = null;
						return false;
					}
				} else {
					scannedStr += scannedSuffix;
					scannedSuffix = null;
				}
			}
		} else {
			if( scannedStr != null ) {
				scannedStr += String.fromCharCode(charCode);
			}
		}
	}
}

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 fireOnEnterPressed( evt, elementId ) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if( charCode == 13 ) {
		document.getElementById( elementId ).click();
	}
}

function fireOnEnterPressedByClass( evt, styleClass ) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if( charCode == 13 ) {
		$j( "." + styleClass ).click();
	}
}

function fireOnclick(elementId) {
	if( document.createEventObject ) {
		document.getElementById( elementId ).click();
	}
}

function forceCapitals(element){
	element.value = element.value.toUpperCase();
}

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();
}

