/* Apostrophe - keyboard layout plugin for jQuery
 * @version: 0.5 (7-DEC-2009)
 * 
 * @author: Taai http://taai.info
 * For any improvemets please contact me, so I can update this project!
 * 
 * @url http://taai.info/code/apostrophe
 * @url http://code.google.com/p/apostrophe4jquery/
 * 
 * @license MIT http://www.opensource.org/licenses/mit-license.php
 * @license GPL http://www.gnu.org/licenses/gpl.html
 */
;jQuery.apostrophe || (function($){

$.apostrophe = {
	
	version: 0.5,
	
	// active characters
	chr: [34,39],
	
	// character map(s) container
	map: {},
	
	// default character map
	defaultmap: "lv",
	
	// replace selection in text
	replaceSelection: function(obj, text){
		var scroll_top = obj.scrollTop;
		if(document.selection){ /* IE and Opera */
			obj.focus();
			var range = document.selection.createRange();
			range.text = text;
			range.select();
		}else if(obj.setSelectionRange){
			var selection_start = obj.selectionStart;
			obj.value = obj.value.substring(0,selection_start) + text + obj.value.substring(obj.selectionEnd);
			obj.setSelectionRange(selection_start + text.length,selection_start + text.length);
		}
		obj.focus();
		obj.scrollTop = scroll_top;
	}
	
};


$.fn.apostrophe = function(options){
	
	// unbind event
	this.unbind("keypress.apostrophe");
	
	// deactivate, if `options` === false
	if(options === false) return this;
	
	// set map, if `options` is a string
	if(typeof(options) == "string") options = {map: options};
	
	options = $.extend({
		chr: $.apostrophe.chr,
		map: $.apostrophe.defaultmap
	}, options || {});
	
	// set `map`, if `map` is a string, otherwise it should be an object
	if(typeof(options.map) == "string") options.map = $.apostrophe.map[options.map];
	
	// if `options` invalid, log error and keep object chainable
	if(typeof(options.map) != "object" || typeof(options.chr) != "object"){
		log("apostrophe: skipping - options invalid");
		return this;
	}
	
	var active; // contains active character
	
	// bind event
	this.bind("keypress.apostrophe", function(e){
		
		// react with characters only
		if(!(e.which >= 32 && e.which <= 126)) return true;
		
		if(active){
			
			var ac = active; // active character
			active = null; // deactivate
			
			// write convertable character
			if(typeof(options.map[e.which]) != "undefined"){
				$.apostrophe.replaceSelection(this, String.fromCharCode(options.map[e.which]));
				return false;
			}
			
			// write active character and unconvertable character
			$.apostrophe.replaceSelection(this, String.fromCharCode(ac));
			return true;
			
		}
		
		// set active character, if typed
		var i = $.inArray(e.which, options.chr);
		if(i >= 0){
			active = options.chr[i];
			return false;
		}
		
	});
	
	// keep object chainable
	return this;
}


// latvian language map
$.apostrophe.map.lv = {65:256,67:268,69:274,71:290,73:298,75:310,76:315,78:325,79:213,82:342,83:352,85:362,90:381,97:257,99:269,101:275,103:291,105:299,107:311,108:316,110:326,111:333,114:343,115:353,117:363,122:382};


})(jQuery);