/****************************************************************
(C) 2008 Kishore Nallan for DesignShack
http://www.kishorelive.com
kishore.nc@gmail.com
*****************************************************************/

jQuery(document).ready(function() {
    
	var shifton = false;
	
	// toggles the keyboard to show or hide when link is clicked
	jQuery("#showkeyboard").click(function(e) {
	    var height = jQuery('#keyboard').height();
		var width = jQuery('#keyboard').width();
		leftVal=e.pageX-40+"px";
		topVal=e.pageY+20+"px";
		jQuery('#keyboard').css({ left: leftVal, top: topVal }).toggle();
	});
	
	// makes the keyboard draggable
	jQuery("#keyboard").draggable();	
	
	// toggles between the normal and the "SHIFT keys" on the keyboard
	function onShift(e) {
		var i;
		if(e==1) {
			for(i=0;i<4;i++) {
				var rowid = "#row" + i;
				jQuery(rowid).hide();
				jQuery(rowid + "_shift").show();
			}
		}
		else {
			for(i=0;i<4;i++) {
				var rowid = "#row" + i;
				jQuery(rowid).show();
				jQuery(rowid + "_shift").hide();
			}
		}
	 }
	
	// function thats called when any of the keys on the keyboard are pressed
	 jQuery("#keyboard input").bind("click", function(e) {

	    if (jQuery(this).val() == 'Backspace') {
	        jQuery('.keyboardText').replaceSelection("", true);
		}

		else if (jQuery(this).val() == "Shift") {
			if(shifton == false) {
				onShift(1);	
				shifton = true;
			}
			
			else {
				onShift(0);
				shifton = false;
			} 
		}
		
		else {

		    jQuery('.keyboardText').replaceSelection(jQuery(this).val(), true);
			
			if(shifton == true) {
				onShift(0);
				shifton = false;
			}
		}
	});
	
});


