﻿/*
    Updated @ 2009-09-08
	License: GNU General Public License v3
	Developer: Ehsun Behravesh - www.ehsunbehravesh.com
	email: ehsun7b@gmail.com	
	Tested on Firefox 3.5.2, Opera 9.52, Internet Explorer 8, Google Chrome 2.0
*/

var VKKey = Class.create({
    initialize: function(id, char1, char2, persianChar1, persianChar2, caption, style, secondaryStyle) {
        this.char1 = char1;
        this.char2 = char2;
        this.persianChar1 = persianChar1;
        this.persianChar2 = persianChar2;
        this.caption = caption;
        
        this.element = new Element("div", {'class': 'VKKEY', id: id});        
        this.element.update(this.caption == undefined ? char1 : this.caption);        
        if(style != undefined) {
            this.element.setStyle(style);
        }
        
        if(secondaryStyle != undefined) {
            this.element.setStyle(secondaryStyle);
        }
    }
});

/*******************************************************************/

var VirtualKeyboard = Class.create({
    initialize: function(console, container, form) {
        try {        
            this.console    = console;
            this.container  = container;
            this.form       = form;
            this.shift      = false;
            this.caps       = false;
            this.persian    = false;
            
            //  --------------------------------------
                      
            if (this.container.toLowerCase != undefined) {
							this.container = $(this.container);
						}
						
						if (this.console.toLowerCase != undefined) {
							this.console = $(this.console);
						}
			            
            this.container.setStyle({direction: 'ltr'});			            
            
            //  --------------------------------------
            
            var commonStyle = {
                width: '20px', 
                height: '15px', 
                border: '1px solid black', 
                /*display: 'inline-block', */
                float: 'left',
                font: 'normal normal 12px tahoma', 
                textAlign: 'center', 
                overflow: 'hidden', 
                margin: '1px', 
                cursor: 'pointer',
                padding: '1px 0px'
            };
            
            this.keys = new Array();
            
            this.keys[0] = new VKKey('vkkey0', '`', '~', '÷', '×',  null, commonStyle);            
            this.keys[1] = new VKKey('vkkey1', '1', '!', '1', '!', null, commonStyle);                      
            this.keys[2] = new VKKey('vkkey2', '2', '@', '2', '@', null, commonStyle);
            this.keys[3] = new VKKey('vkkey3', '3', '#', '3', '#', null, commonStyle);
            this.keys[4] = new VKKey('vkkey4', '4', '$', '4', '$', null, commonStyle);
            this.keys[5] = new VKKey('vkkey5', '5', '%', '5', '%', null, commonStyle);
            this.keys[6] = new VKKey('vkkey6', '6', '^', '6', '^', null, commonStyle);
            this.keys[7] = new VKKey('vkkey7', '7', '&', '7', '&', null, commonStyle);
            this.keys[8] = new VKKey('vkkey8', '8', '*', '8', '*', null, commonStyle);
            this.keys[9] = new VKKey('vkkey9', '9', '(', '9', ')', null, commonStyle);
            this.keys[10] = new VKKey('vkkey10', '0', ')', '0', '(', null, commonStyle);
            this.keys[11] = new VKKey('vkkey11', '-', '_', '-', '_', null, commonStyle);
            this.keys[12] = new VKKey('vkkey12', '=', '+', '=', '+', null, commonStyle);            
            this.keys[13] = new VKKey('vkkey13', '', '', '', '', 'Backspace', commonStyle, {fontSize: '10px', width: '60px',fontWeight: 'bold'}); // BACKSPACE
            this.keys[14] = new VKKey('vkkey14', '', '', '', '', 'Tab', commonStyle, {fontSize: '10px', width: '30px',fontWeight: 'bold'}); // TAB KEY
            this.keys[15] = new VKKey('vkkey15', 'q', 'Q', 'ض', ' ً', null, commonStyle);
            this.keys[16] = new VKKey('vkkey16', 'w', 'W', 'ص', ' ٌ', null, commonStyle);
            this.keys[17] = new VKKey('vkkey17', 'e', 'E', 'ث', ' ٍ', null, commonStyle);
            this.keys[18] = new VKKey('vkkey18', 'r', 'R', 'ق', 'ريال', null, commonStyle);
            this.keys[19] = new VKKey('vkkey19', 't', 'T', 'ف', '،', null, commonStyle);
            this.keys[20] = new VKKey('vkkey20', 'y', 'Y', 'غ', '؛', null, commonStyle);
            this.keys[21] = new VKKey('vkkey21', 'u', 'U', 'ع', ',', null, commonStyle);
            this.keys[22] = new VKKey('vkkey22', 'i', 'I', 'ه', ']', null, commonStyle);
            this.keys[23] = new VKKey('vkkey23', 'o', 'O', 'خ', '[', null, commonStyle);            
            this.keys[24] = new VKKey('vkkey24', 'p', 'P', 'ح', '\\', null, commonStyle);            
            this.keys[25] = new VKKey('vkkey25', '[', '{', 'ج', '}', null, commonStyle);
            this.keys[26] = new VKKey('vkkey26', ']', '}', 'چ', '{', null, commonStyle);
            this.keys[27] = new VKKey('vkkey27', '', '', '', '', 'Enter', commonStyle, {fontSize: '10px', width: '50px', fontWeight: 'bold'}); // ENTER KEY
            this.keys[28] = new VKKey('vkkey28', '', '', '', '', 'Caps Lock', commonStyle, {fontSize: '10px', width: '70px',fontWeight: 'bold'}); // CAPS LOCK KEY
            this.keys[29] = new VKKey('vkkey29', 'a', 'A', 'ش', ' َ', null, commonStyle);
            this.keys[30] = new VKKey('vkkey30', 's', 'S', 'س', ' ُ', null, commonStyle);
            this.keys[31] = new VKKey('vkkey31', 'd', 'D', 'ی', ' ِ', null, commonStyle);
            this.keys[32] = new VKKey('vkkey32', 'f', 'F', 'ب', ' ّ', null, commonStyle);
            this.keys[33] = new VKKey('vkkey33', 'g', 'G', 'ل', 'ۀ', null, commonStyle);
            this.keys[34] = new VKKey('vkkey34', 'h', 'H', 'ا', 'آ', null, commonStyle);
            this.keys[35] = new VKKey('vkkey35', 'j', 'J', 'ت', 'ـ', null, commonStyle);
            this.keys[36] = new VKKey('vkkey36', 'k', 'K', 'ن', '«', null, commonStyle);
            this.keys[37] = new VKKey('vkkey37', 'l', 'L', 'م', '»', null, commonStyle);
            this.keys[38] = new VKKey('vkkey38', ';', ':', 'ک', ':', null, commonStyle);
            this.keys[39] = new VKKey('vkkey39', "'", '"', 'گ', '"', null, commonStyle);
            this.keys[40] = new VKKey('vkkey40', '\\', '|', 'پ', '|', null, commonStyle, {width: '34px'});
            this.keys[41] = new VKKey('vkkey41', '', '', '', '', 'SHIFT', commonStyle, {fontSize: '10px', width: '64px',fontWeight: 'bold'}); // LEFT SHIFT
            this.keys[42] = new VKKey('vkkey42', 'z', 'Z', 'ظ', 'ة', null, commonStyle);
            this.keys[43] = new VKKey('vkkey43', 'x', 'X', 'ط', 'ي', null, commonStyle);
            this.keys[44] = new VKKey('vkkey44', 'c', 'C', 'ز', 'ژ', null, commonStyle);
            this.keys[45] = new VKKey('vkkey45', 'v', 'V', 'ر', 'ؤ', null, commonStyle);
            this.keys[46] = new VKKey('vkkey46', 'b', 'B', 'ذ', 'إ', null, commonStyle);
            this.keys[47] = new VKKey('vkkey47', 'n', 'N', 'د', 'أ', null, commonStyle);
            this.keys[48] = new VKKey('vkkey48', 'm', 'M', 'ئ', 'ء', null, commonStyle);
            this.keys[49] = new VKKey('vkkey49', ',', '<', 'و', '<', null, commonStyle);
            this.keys[50] = new VKKey('vkkey50', '.', '>', '.', '>', null, commonStyle);
            this.keys[51] = new VKKey('vkkey51', '/', '?', '/', '؟', null, commonStyle);
            this.keys[52] = new VKKey('vkkey52', '', '', '', '', 'SHIFT', commonStyle, {fontSize: '10px', width: '64px',fontWeight: 'bold'}); // RIGHT SHIFT
            this.keys[53] = new VKKey('vkkey53', '', '', '', '', 'CTRL', commonStyle, {fontSize: '10px', width: '40px',fontWeight: 'bold'}); // LEFT CTRL
            this.keys[54] = new VKKey('vkkey54', '', '', '', '', 'ALT', commonStyle, {fontSize: '10px', width: '40px',fontWeight: 'bold'}); // LEFT ALT
            this.keys[55] = new VKKey('vkkey55', ' ', ' ', ' ', ' ', '', commonStyle,  {fontSize: '10px', width: '150px'}); // SPACE BAR
            this.keys[56] = new VKKey('vkkey56', '', '', '', '', 'ALT', commonStyle, {fontSize: '10px', width: '40px',fontWeight: 'bold'}); // RIGHT ALT
            this.keys[57] = new VKKey('vkkey57', '', '', '', '', 'CTRL', commonStyle, {fontSize: '10px', width: '40px',fontWeight: 'bold'}); // RIGHT CTRL            
            this.keys[58] = new VKKey('vkkey58', '', '', '', '', 'فارسي', commonStyle, {fontSize: '10px', width: '42px',fontWeight: 'bold', color: 'blue'}); // SWITCH LANGUAGE            
        } catch(e) {
            window.status = e.message;
        }
    },
    
    render: function() {
        try {
            for (i = 0; i < this.keys.length; ++i) {
                this.keys[i].element.onclick = this.keyPress.bind(this, i);
                this.keys[i].element.onmousemove = this.mouseEnter.bind(this, i);
                this.keys[i].element.onmouseout = this.mouseLeave.bind(this, i);
                
                this.container.appendChild(this.keys[i].element);
                if (i == 13 || i == 27 || i == 40 || i == 52) {
                    var sep = new Element("br");
                    sep.setStyle({width: '0px', clear: 'left'});
                    this.container.appendChild(sep);
                }
            }
        } catch(e) {
            window.status = e.message;
        }
    },
    
    keyPress: function(i) {
        switch(i) {
            case 58:
                this.persian = ! this.persian;
                this.keys[58].element.update(this.persian ? 'English' : 'فارسي');
                this.updateCaptions();
                break;
            case 28:
                this.caps = ! this.caps;
                this.keys[28].element.setStyle(this.caps ? {backgroundColor: 'black', color: 'white'} : {backgroundColor: 'white', color: 'black'});
                this.updateCaptions();
                break;
            case 41:
            case 52:
                this.shift = ! this.shift;
                this.keys[41].element.setStyle(this.shift ? {backgroundColor: 'black', color: 'white'} : {backgroundColor: 'white', color: 'black'});
                this.keys[52].element.setStyle(this.shift ? {backgroundColor: 'black', color: 'white'} : {backgroundColor: 'white', color: 'black'});
                this.updateCaptions();
                break;
            case 13:
                this.console.value = this.console.value.substring(0, this.console.value.length - 1);
                break;
            case 27:
                if (this.form != undefined)
                    this.form.submit();
                break;    
            default:
                this.updateConsole(i);
                
                this.shift = false;
                this.keys[41].element.setStyle(this.shift ? {backgroundColor: 'black', color: 'white'} : {backgroundColor: 'white', color: 'black'});
                this.keys[52].element.setStyle(this.shift ? {backgroundColor: 'black', color: 'white'} : {backgroundColor: 'white', color: 'black'});
                                
                this.updateCaptions();
        }
    },
    
    updateCaptions: function() {        
        try {
            for (i = 0; i < this.keys.length; ++i) {
                if (this.persian) {
                    if (this.keys[i].caption == undefined) {                        
                        this.keys[i].element.update(this.isAlternateChar() ? this.keys[i].persianChar2 : this.keys[i].persianChar1);
                    }
                } else {
                    if (this.keys[i].caption == undefined) {                        
                        this.keys[i].element.update(this.isAlternateChar() ? this.keys[i].char2 : this.keys[i].char1);
                    }
                }
            }
        } catch(e) {
            window.status = e.message;
        }
    },
    
    updateConsole: function(i) {
        if (this.persian) {
            this.console.value += this.isAlternateChar() ? this.keys[i].persianChar2 : this.keys[i].persianChar1;
        } else {
            this.console.value += this.isAlternateChar() ? this.keys[i].char2 : this.keys[i].char1;
        }
    },
    
    isAlternateChar: function() {        
        if (this.caps && this.shift)
            return false;
        else if (!this.caps && !this.shift)
            return false;
        else if (this.caps || this.shift)
            return true;
    },
    
    mouseEnter: function(i) {
        if ((i == 28 && this.caps) || ((i == 41 || i == 52) && this.shift))
            return;
        this.keys[i].element.setStyle({backgroundColor: '#d9e8ff'});
    },
    
    mouseLeave: function(i) {
        if ((i == 28 && this.caps) || ((i == 41 || i == 52) && this.shift))
            return;
        this.keys[i].element.setStyle({backgroundColor: '#ffffff'});
    }
});