/*
	Here is an example of how to use this library:
		//Two lines of code that disable the ALL browser key actions while the Ctrl/control key is down:  (useful when adding CTRL+key functionality to a page)
	window.onKeyDownEvents.push( function(downkey) { if(window.isKeyDown(17)) return "preventDefault"; } );
	window.onKeyUpEvents.push( function(downkey) { if(window.isKeyDown(17)) return "preventDefault"; } );
		
		//Two lines of code to show/hide a div when the Ctrl/control key is down/up (respectively):
	window.onKeyDownEvents.push( function(downkey) { if(downkey==17) document.getElementById('controlKeyMessage').style.display='block'; } );
	window.onKeyUpEvents.push( function(upkey) { if(upkey==17) document.getElementById('controlKeyMessage').style.display='none'; } );
	
		// CTRL+G = show/hide gallery creation dialog
	window.onKeyDownEvents.push( function(downkey) { if(window.isKeyDown(17) && downkey==71) showHideCreateGalleryBox(); } );
		// CTRL+I = show/hide image upload dialog
	window.onKeyDownEvents.push( function(downkey) { if(window.isKeyDown(17) && downkey==73) showHideImageUploadBox(); } );
	
		//Two lines of code to disable the default action for a specific key (up and down):
	window.onKeyDownEvents.push( function(downkey) { if(downkey==13) return "preventDefault"; } );
	window.onKeyUpEvents.push( function(upkey) { if(upkey==13) return "preventDefault"; } );
*/


if(!window.controlClickIsIncluded) {	//this library should only be included once into a webpage, but multiple references will not break the code.
	window.controlClickIsIncluded = true;
	window.controlClickVersion = "1.0";

	window.currentKeyDowns = new Array();
	
	window.onKeyDownEvents = new Array();
	window.onKeyUpEvents = new Array();

	document.onkeydown = function(e) {
		if(!e) e=window.event;
		var key = e.keyCode ? e.keyCode : e.which;
		var nA = new Array();
		for(var i in window.currentKeyDowns)
			if(window.currentKeyDowns[i] != key)
				nA.push(window.currentKeyDowns[i]);
		nA.push(key);
		window.currentKeyDowns = nA;

		preventDefault = false;
		// if you want something to happen when a key is pressed, 
		// register an event in the window.onKeyDownEvents[] array.
		for(var i in window.onKeyDownEvents)
			if(window.onKeyDownEvents[i](key) == "preventDefault")
				preventDefault = true;
		if(preventDefault==true) {
			if(e.returnValue)
				e.returnValue=false;
			if(e.preventDefault)
				e.preventDefault();
		}
		return preventDefault?false:true;
	};

	document.onkeyup = function(e) {
		if(!e) e=window.event;
		var key = e.keyCode ? e.keyCode : e.which;
		var nA = new Array();
		for(var i in window.currentKeyDowns)
			if(window.currentKeyDowns[i] != key)
				nA.push(window.currentKeyDowns[i]);
		window.currentKeyDowns = nA;

		preventDefault = false;
		// if you want something to happen when a key is UNpressed, 
		// register an event in the window.onKeyUpEvents[] array.
		for(var i in window.onKeyUpEvents)
			if(window.onKeyUpEvents[i](key) == "preventDefault")
				preventDefault = true;
		return preventDefault?false:true;
	};

	document.onblur = function() {
		try {
			for(var i in window.onKeyUpEvents)
				for(var key=1; key<230; key++)
					window.onKeyUpEvents[i](key);
			window.currentKeyDowns = new Array();
		}
		catch(e) { }
	};

	window.isKeyDown = function(key) {
		for(var i in window.currentKeyDowns)
			if(window.currentKeyDowns[i]==key)
				return true;
		return false;
	};
}
