/**
 * EzKeyboard 1.1.1
 * https://github.com/enzozhong/EzKeyboard.git
 *
 * Copyright (c) 2012 EnzoZhong
 *
 * Licensed - MIT License
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Author : EnzoZhong
 * Email : enzo.lvi@gmail.com
 * Date : 2012.08.26
 */
$.fn.Keyboard = function( args ){
      
      var buttonId = "id" + new Date().getTime();
      var inputClass = "ezkbinput";
      
      this.addClass( inputClass );
      this.after( "<button id=\'" + buttonId + "\'></button>" );
      
      var $button = $( "#" + buttonId ).append( "▼" ).button();
      var doubleFontSize = args.doubleFontSize == undefined ? 1.8 : doubleFontSize;
      var doubleButtonSize = args.doubleButtonSize == undefined ? 3.1 : doubleButtonSize;
      
      var $input = this.css( "font-size" , doubleFontSize + "em" ); 
      var zindex = args.zindex == undefined ? 100 : args.zindex;
       
      $input.css( "width" , "70%" ).css( "float" , "left" );
      $button.css( "width" , "28%" ).css( "margin" , 0 );
      
      var layout;
      switch( args.layout ){
            case "total" : {
                  layout =  [
                  [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "0" ],
                  [ "q" , "w" , "e" , "r" , "t" , "y" , "u" , "i" , "o" , "p" ],
                  [ "a" , "s" , "d" , "f" , "g" , "h" , "j" , "k" , "l" , "{b}" ],
                  [ "z" , "x" , "c" , "v" , "b" , "n" , "m" , "&nbsp;",  "-", "{a}" ]
                  ]    
                  break;
            }
            case "qwer" : {
                  layout =  [
                  [ "q" , "w" , "e" , "r" , "t" , "y" , "u" , "i" , "o" , "p" ],
                  [ "a" , "s" , "d" , "f" , "g" , "h" , "j" , "k" , "l" , "{b}" ],
                  [ "z" , "x" , "c" , "v" , "b" , "n" , "m" , "&nbsp;",  "-", "{a}" ]
                  ]    
                  break;
            }
            case "phone" : {
                  layout = [
                  ["+","0","1","2","3","4","{b}"],
                  ["_","5","6","7","8","9","{a}"]
                  ]
                  break;
            }
            case "clock" : {
                  layout = [
                  ["0","1","2","3","4","{b}"],
                  ["5","6","7","8","9","{a}"]
                  ]
                  break;
            }
            default : {
                  layout = args.layout;
                  break;
            }
      }
      var acceptCallback = args.acceptCallback == null ? function(){} : args.acceptCallback;
      var acceptFunction = function( event ){
            event.stopPropagation();
            $kbBody.hide( 310 );  
            acceptCallback( $input.val() );
      }
      var backspaceFunction = function( event ){
            event.stopPropagation(); 
            var val = $input.val();
            $input.val( val.substr( 0, val.length - 1 ) ).focus();
      }
     
      var specialCharMap = {
            "&nbsp;" : " "
      }
      var regexNameMap = {
            "{a}" : "√",
            "{b}" : "←"
      }
      var regexFunctionMap = {
            "{a}" : acceptFunction,
            "{b}" : backspaceFunction
      }
   
      var $kbBody = $( "body" ).append( "<div>" ).find( "div:last" ).hide()
      .addClass( "ui-keyboard-body" ).css( "-khtml-user-select" , "none" )
      .css( "-moz-user-select" , "none" ).css( "-webkit-user-select" , "none" )
      .css( "z-index" ,  zindex );
      
      for( var x = 0 ; x < layout.length ; x++ ){
            var array = layout[ x ];
            
            var row = $kbBody.append( "<div>" ).find( "div:last" ).addClass( "ui-keyboard-row" );
            
            for( var y = 0 ; y < array.length ; y++ ){
                  var str = array[ y ].toString();
                  
                  var button = row.append( "<a>" ).find( "a:last" )
                  .append( regexNameMap[ str ] || str ).button().attr( "string" , str )
                  .click( regexFunctionMap[ str ] || function( event ){
                        var tempString = $( this ).attr( "string" );
                        var cha = specialCharMap[ tempString ] == undefined ? tempString : specialCharMap[ tempString ];
                        $input.val( $input.val() + cha ).focus();
                        event.stopPropagation(); 
                  });
                  $( button ).css( "width" , doubleButtonSize + "em" ).css( "height" , doubleButtonSize + "em" )
                  .addClass( "ui-keyboard-button" );
                  
            }
      }  
      
      $button.toggle( function( event ){
            event.stopPropagation(); 
            var height = parseInt( $( this ).css( "height" ) );
            var top = $input.offset().top;
            var left = $input.offset().left; 
            $kbBody.css( "top" , top + height + 2 ).css( "left" , left );
            $kbBody.show( 310 ); 
      },function(){
            $kbBody.hide( 310 );
      });
   
      $.fn.Keyboard.val = function(){
            //为了获取当前的值
            return $input.val(); 
      } 
     
      $.fn.Keyboard.reset = function(){
            //为了清空输入条
            $input.val( "" ); 
      } 
   
      return this;
}