// JavaScript Document

var text_area;
var selected;
var escrito  = "";
var tecleado; 
var altura;
var intervalo;

function Virtual_keyboard(trigger){
	text_area = $(trigger);
	init();
	this.reset_text = resetText;
}
function resetText(){
	escrito = "";
	$('.input-anim').val(escrito);
}
function init(){
	pressKey();
	
	document.onkeydown = function(e){
		killBackSpace(window.event);
		e = e? e : window.event;
		var k = e.keyCode? e.keyCode : e.which? e.which : null;
		
		if (k == 188){
			e.preventDefault();
			e.returnValue = null;
			e.keyCode=0;
			e = e.which;
			return false;
		}
		return true;
		
		if(IDBrowser != "IE" && k == 188){
			event.preventDefault();
			return false;
		}else if(IDBrowser == "IE" && k == 188){
			return false;
		}else{
			return true;	
		}
	};
		
	$(window).keyup(function(event){
		killBackSpace(event);
		if(event.keyCode == 32){
	
			var newPos = -17;
			$('#espacio img').css('marginTop', newPos);
			$('#espacio').animate({opacity:.9}, 100, back_to_normal);
		}
		if(event.keyCode == 8){
			
			borra();			
			var newPos = -19;
			$('#borrar img').css('marginTop', '-19px');
			$('#borrar').animate({opacity:.9}, 100, back_to_normal);

		}
	})
	
	$(window).keypress(function(event){
		killBackSpace(event);
		if(event.keyCode == 8){
			borra();
			var newPos = -19;
			$('#borrar img').css('marginTop', '-19px');
			$('#borrar').animate({opacity:.9}, 100, back_to_normal);
		}		
		event.preventDefault();
		if(event.charCode == 44){
			return false;
		}
		if(event.keyCode != 44){
			var leter = String.fromCharCode(event.which);
			selectedLetter(leter);
		}
   });
}
function borra(){
	escrito = escrito.slice(0, escrito.length - 1);
	$('.input-anim').val(escrito);
}
function add(){
	escrito = escrito+" ";
	$('.input-anim').val(escrito);
}
function pressKey(){
	$('#letras div').click(function(){
		if($(this).attr('id') == "borrar"){
			borra();
		}
		if($(this).attr('id') == "espacio"){
			add();
		}
	
		var letra = $(this).attr('id').split('-');
		selectedLetter(letra[1])	
	});
}
function killBackSpace(e){
	e = e? e : window.event;
	var k = e.keyCode? e.keyCode : e.which? e.which : null;
	if (k == 8){
	if (e.preventDefault)
	e.preventDefault();
	return false;
	};
	return true;
	};
	if(typeof document.addEventListener!='undefined')
	document.addEventListener('keydown', killBackSpace, false);
	else if(typeof document.attachEvent!='undefined')
	document.attachEvent('onkeydown', killBackSpace);
	else{
	if(document.onkeydown!=null){
	var oldOnkeydown=document.onkeydown;
	document.onkeydown=function(e){
	oldOnkeydown(e);
	killBackSpace(e);
	};}
	else
	document.onkeydown=killBackSpace;
}
function selectedLetter(leter){
	

	var lowerCase = leter.toLowerCase();
	
	escrito += lowerCase;
	$('.input-anim').val(escrito);
	
	selected = $("#let-" + lowerCase + ' img');
	altura = selected.height();
	var newPos = -(altura/2);

	selected.css('marginTop', newPos);
	selected.animate({opacity:.9}, 100, back_to_normal);
	
}
function back_to_normal(){


	$('#letras img').stop().animate({opacity:1}, 100, function(){	
		$('#letras img').css('marginTop','0px');

	});					
}

