var keyboard = {
	up: 0,
	up1: 0,
	up2: 0,
	S: $('#message'),
	//funkcja wstawiajaca kod klawiatury w div o zadanym id
	generate: function(id){
		if ($('#'+id)){
			$('#'+id).html('<div class="k_pl fl"><input type="button" value="ą" /><input type="button" value="ć" /><input type="button" value="ę" /><div class="clr"></div><input type="button" value="ł" /><input type="button" value="ń" /><input type="button" value="ó" /><div class="clr"></div><input type="button" value="ś" /><input type="button" value="ż" /><input type="button" value="ź" /></div><div class="k_ru fl"><input type="button" value="й" /><input type="button" value="ц" /><input type="button" value="у" /><input type="button" value="к" /><input type="button" value="е" /><input type="button" value="н" /><input type="button" value="г" /><input type="button" value="ш" /><input type="button" value="щ" /><input type="button" value="з" /><input type="button" value="х" /><input type="button" value="ъ" /><div class="clr"></div><input type="button" value="ф" class="ml13" /><input type="button" value="ы" /><input type="button" value="в" /><input type="button" value="а" /><input type="button" value="п" /><input type="button" value="р" /><input type="button" value="о" /><input type="button" value="л" /><input type="button" value="д" /><input type="button" value="ж" /><input type="button" value="ё" /><div class="clr"></div><input type="button" value="э" class="ml20" /><input type="button" value="я" /><input type="button" value="ч" /><input type="button" value="с" /><input type="button" value="м" /><input type="button" value="и" /><input type="button" value="т" /><input type="button" value="ь" /><input type="button" value="б" /><input type="button" value="ю" /><div class="clr"></div><input type="button" class="space" value=" " /></div><div class="k_de fl"><input type="button" value="ä" class="ml20" /><div class="clr"></div><input type="button" value="ö" /><input type="button" value="ß" /><input type="button" value="ü" /></div><div class="k_spec fl"><input type="button" value="." /><input type="button" value="," /><input type="button" value="!" /><div class="clr"></div><input type="button" value="(" /><input type="button" value=")" /><input type="button" value="?" /><div class="clr"></div><input type="button" id="'+id+'caps" class="caps" value=""/></div>');
		}
	},
	//przekazanie duzej lub malej litery do dodania
	addLetter: function(elem,is_email)
	{
		upper = this.up1;
		if (is_email == 0){
			//utils.removeDefaultText($('#translate_text'));
			upper = this.up;
		}
		if (is_email == 2){
		    upper = this.up2;
	    }
		if (upper)
			this.A($(elem).val().toUpperCase(),is_email);
		else
			this.A($(elem).val(),is_email);
	},
	
	//wstawia literke w ospowiednie miejsce
	A: function(x,is_email)
	{
		D = document;
		if (is_email == 1){
			F = $('#message');
			S = this.S;
			range = this.range1;
		}
		else{
		    if (is_email == 2){
    			F = $('#dict_input');
    			S = F;
    			range = this.range2;
		        
		    }
		    else{
    			F = $('#translate_text');
    			S = F;
    			range = this.range;
		    }
		}
		if(S[0].selectionStart||S[0].selectionStart=='0'){
			var start = S[0].selectionStart+1;
			S.val(S.val().substring(0,S[0].selectionStart)+x+S.val().substring(S[0].selectionEnd,S.val().length));
			S.focus();
			S[0].setSelectionRange(start,start);
		} else if (D.selection){
			S.focus();
			if (!range)
				range = document.selection.createRange();
			range.text=x;
			range.collapse(true);
			range.select();
			S.focus();
		}
		else{
			F.val(F.val + x);
			F.focus();
		}
		if (is_email == 2){
		    $("#dict_input").keydown();
		}
	},
	show: function(elem)
	{
		$('#'+elem).slideToggle("slow");
	},
	
	//generowanie i wlaczanie funkcjonalnosci klawiatury.
	
	observe: function(){
		var keyboards = Array('translator_keyboard', 'email_keyboard');
		for (var j=0; j < keyboards.length; j++){
			if ($('#'+keyboards[j])){
				keyboard.generate(keyboards[j]);
				board = $('#'+keyboards[j]);
				keys = $('#'+keyboards[j]+' input');
				keys.each(
					function(){
						if (keyboards[j] == 'translator_keyboard')
							if (!this.id){
								$(this).click(function(){
									keyboard.addLetter(this,0);
								});
							}
							else{
								$(this).click(function(){
									if (keyboard.up == 1) keyboard.up = 0; else keyboard.up = 1; $('#keyboardcaps').toggleClass('active'); $('#translate_text').focus();
								});
							}
						else
							if (!this.id){
								$(this).click(function(){
									keyboard.addLetter(this,1);
								});
							}
							else{
								$(this).click(function(){
									if (keyboard.up1 == 1) keyboard.up1 = 0; else keyboard.up1 = 1; $('#email_keyboardcaps').toggleClass('active'); keyboard.S.focus();
								});
							}
					}
				);
			}
		}
		$('#translate_text').bind('click select keyup', function(){
			if (document.selection) keyboard.range = document.selection.createRange();
		});
		$('#message').bind('click select keyup', function(){
			if (document.selection) 
				keyboard.range1 = document.selection.createRange();
			keyboard.S = $('#message');
		});
		$('#Subject').bind('click select keyup', function(){
			if (document.selection) 
				keyboard.range1 = document.selection.createRange();
			keyboard.S = $('#Subject');
		});
		keyboard.S = $('#message');
	},
	observe_dict: function(){
	    $('#dict_keyboard input').each(function(){
	        $(this).click(function(){
				keyboard.addLetter(this,2);
			});
	    });
	    $('#dict_input').bind('click select keyup', function(){
			if (document.selection) keyboard.range2 = document.selection.createRange();
		});
	}
};

