$(document).ready(function() {
        $.iKeyboardFlash = function(x, kbname) {
	    if(x) {
	        $('#' + kbname).css('border-color', '#ff0000');
	        $('#' + kbname).css('background', '#e9d5d5');
	    }
	    else {
	        $('#' + kbname).css('border-color', '#cccccc');
	        $('#' + kbname).css('background', '#e7e7e7');
	    }
    	}
});

(function($) {

	iKeyboard = function(obj) {
	    var kbname;
	    var kbinput = "";
	    var onClose = function(){};
	    var kbobj = obj;
	    var kbDisabled = false;
    
    	    kbobj.keyup(function(event) {
		if(event.keyCode == 27) { // ESC
		    kbDisabled = true;
		    $("#" + kbname).fadeOut('fast', function() {
			$("#" + kbname).remove();
			onClose();
		    });
		    return;
		}
		if(kbDisabled) {
		    return;
		}
		kbobj.val('');

		if(event.keyCode == 8 || event.keyCode == 9)
		    return;

		if($('#' + kbname).length == 0)
		    createBoard();
		else
		    flash();
    	    });

	    this.onClose = function(func) {
		onClose = func;
	    }

    	    var createBoard = function() {
		$('[id$=_keyboard]').remove();

		var kb = $('<div/>').attr('id', kbname).insertAfter('#' + kbinput);

		kb.css("-moz-box-shadow", "3px 3px 3px #A7A7A7");
		kb.css("border-radius", "4px");
    		kb.css("-moz-border-radius", "4px");
    		kb.css("background", "none repeat scroll 0 0 #f1f1f1");
	        kb.css("border", "1px solid #bcbcbc");
		kb.css("color", "#3E3E3E");
		kb.css("margin", "-1px 0 0 -1px");
	        kb.css("padding", "4px");
    	        kb.css("position", "absolute");
    	        kb.css("white-space", "pre-wrap"); 
    	        kb.css("width", "405px");
    	        kb.css("height", "111px");
		kb.css("z-index", "9000");
    	        kb.css("display", "none");

/*		var preview = $("<div/>").attr('id', "_preview")
		.css("border-radius", "4px")
    		.css("-moz-border-radius", "4px")
		.css("position", "relative")
		.css("display", "block")
		.css("background", "#000000")
		.css("width", "111px")
		.css("height", "119px")
		.css("top", "0px")
		.css("left", "309px")
		.css("z-index", "8999").insertAfter(kb);
*/

	        drawKeyboard(kb, false);
	        kb.slideDown(function() {
	          kb.find('.closer').show();
	        });
//	        kb.show();

    	    };

    	    var letsCreate = function() {
		kbinput = kbobj.attr('id');
		kbname = kbobj.attr('id') + "_keyboard";
	    
		if($('#' + kbname).length >= 1)
		    return;

		createBoard();
	    }

	    var flash = function() {
		setTimeout("$.iKeyboardFlash(1, '" + kbname+ "')", 75);
		setTimeout("$.iKeyboardFlash(0, '" + kbname+ "')", 150);
		setTimeout("$.iKeyboardFlash(1, '" + kbname+ "')", 225);
		setTimeout("$.iKeyboardFlash(0, '" + kbname+ "')", 300);
		setTimeout("$.iKeyboardFlash(1, '" + kbname+ "')", 375);
		setTimeout("$.iKeyboardFlash(0, '" + kbname+ "')", 450);
	    }


	    var drawKeyboard = function(kb, shift) {
		kb.html("");
		kb.append($("<img src='/portal/img/icons/delete.png' style='position: absolute; right: -8px; top: -8px; cursor: pointer; display: none;' class='closer' />").click(function() {
		    kbDisabled = true;
		    $('#' + kbinput).focus();
		    $("#" + kbname).fadeOut('fast', function() {
			$("#" + kbname).remove();
			onClose();
		    });
		}));

		var kbid = kb.attr('id');

		var sc = "+!%/=()$[]#&@*0";

		if(!shift) {
		    var nc1 = "qwertzuiop";
		    var nc2 = "asdfghjkl";
		    var nc3 = "yxcvbnm,.-";
		} else {
		    var nc1 = "QWERTZUIOP";
		    var nc2 = "ASDFGHJKL";
		    var nc3 = "YXCVBNM?:_";
		}


		function printButton(btnchar) {
		    kb.append("<button type=\"button\" class=\"hand kyb_button\">" + btnchar + "</button>");
		}

		for(x = 0; x < sc.length; x++) 
		    printButton(sc[x]);
	    
	    
		kb.append("<div style=\"height:5px\"></div><div style=\"width:14px;height:10px;float:left;\"></div>");

		for(x = 0; x < nc1.length; x++) 
		    printButton(nc1[x]);
	    
	    
		kb.append("<button id=\""+kbid+"_backspace\" type=\"button\" class=\"hand kyb_button kyb_button_bs\">&lt;</button>");
		kb.append("<br/><div style=\"width:27px;height:10px;float:left;\"></div>");

		for(x = 0; x < nc2.length; x++) 
		    printButton(nc2[x]);
	    
	    
		kb.append("<button id=\""+kbid+"_enter\" type=\"button\" class=\"hand kyb_button kyb_button_spec\">Enter</button>");
		kb.append("<br/><button id=\""+kbid+"_shift\"  type=\"button\" class=\"hand kyb_button "+((!shift)?"kyb_button_spec":"kyb_button_shiftpressed")+"\">Shift</button>");

		for(x = 0; x < nc3.length; x++) 
		    printButton(nc3[x]);
	    
	    
		var numpad = $("<div/>").attr('id', kbid + "_numeric")
		    .css("position", "relative")
		    .css("display", "block")
		    .css("width", "82px")
		    .css("height", "82px")
		    .css("top", "-81px")
		    .css("left", "324px")
		    .css("z-index", "11000").appendTo(kb);


		for(x = 1; x < 10; x++) 
		    numpad.append("<button type=\"button\" class=\"hand kyb_button\">" + x + "</button>");
		


		$("#" + kbid + " #" + kbid + "_enter").unbind().click(function() {
		    $('#' + kbinput).focus();
		    kb.fadeOut('fast', function() {
			kb.remove();
			onClose();
		    });
		});

		$("#" + kbid + " #" + kbid + "_shift").unbind().click(function() {
		    if(shift)
			drawKeyboard(kb, false);
		    else
			drawKeyboard(kb, true);
	
		});

		$("#" + kbid + " #" + kbid + "_backspace").unbind().click(function() {
		    var str = $('#' + kbinput).val();

		    $('#' + kbinput).val( str.substring(0, str.length-1) );
		});

		$("#" + kbid + " button:not(button[id])").unbind().click(function() {
		    var chr;
		
		    if($(this).html() == "&amp;")
			chr = "&";
		    else 
			chr = $(this).html();

		    kbobj.val(kbobj.val() + chr );

		    if(shift)
			drawKeyboard(kb, false);


		});
	    }

	    kbobj.click(letsCreate).focusin(letsCreate);

	}

    return this;
})(jQuery);

