(function($){
$.fn.attachVK = function(options){
	var _opt = $.extend({ //extending the options parameter with defaults.
		visible: 1,	// is VK visible or hidden (only physical keyboard input possible if hidden)
		turnedOn: 0, // is VK turned on or off  initially (toggled by the vk icon)
		noVKImgs: 0, //no toggling icons
		VKdiv: 0, //no constant VK div
		imgVKon: 'img/jsvk.png', // default VK icons for on and off states. can be made equal if toggling shouldn't change the outlook of an icon
		imgVKoff: 'img/jsvkoff.png'
		// ,layouts: [] // the list of default layouts for matching inputs, e.g. ['RU Russian', 'US US']
	}, options||{})
	,_inputs = this // the closure array of inputs that should be modified with VK
	,_curVKdiv=_opt.VKdiv // the closure current VK encompassing div element
	,shown=0;
	this.each(function(){ // for each of the matching input
		var _ind = _inputs.index(this) //the closure index for a current VKized input
		,_cur_inp = this //the closure ref. to the current input
		if(!shown & _opt.visible) {
			VirtualKeyboard.show(_cur_inp, _curVKdiv);
			shown=1;
		}

		function VKhere(){
			VirtualKeyboard.attachInput(_cur_inp); // link VK to the current VK div
			VirtualKeyboard.show(_cur_inp, _curVKdiv);
			if(typeof _opt.layouts =='array'){
				VirtualKeyboard.switchLayout(_opt.layouts[_ind])
			}
			VirtualKeyboard.onSwitchLayout = function(code){
				_opt.layouts[_ind]=code
			} //remember the user's layout choice
		}
		function VKon(){  // to turn VK on
			VKoff(); // turn off the previous VK if any
			VKhere();
		}
		function VKoff(){ // to turn VK off
			//if visible, !turnedOn
			VirtualKeyboard.close()
		}
		function imgVKon(){ // clicking the icon to turn on the VK
			$(this).attr('src', _opt.imgVKoff); //swap image
			VirtualKeyboard.close();
			$(_cur_inp).bind('focus', VKon).unbind('focus', VKoff) //swap focus handlers for the current input
			.blur(function(){}) //TO DO - currently onblur one cannot change layout - VK disappears
			.focus() //move focus back to input from the icon
		}
		function imgVKoff(){ // clicking the icon to turn off the VK
			$(this).attr('src', _opt.imgVKon); //swap image
			VirtualKeyboard.close();
			$(_cur_inp).bind('focus', VKoff).unbind('focus', VKon) //swap focus handlers for the current input
			.focus() //move focus back to input from the icon
		}
		if(!_opt.VKdiv){
			_curVKdiv=$(this) //start from the current input el.
			// append icon and VK div to the current input el.
			.after("<img style='position: absolute;'  src='"+ (_opt.turnedOn ? _opt.imgVKoff : _opt.imgVKon) +
			"' /><div style='position: absolute;' class='_vkdiv_'></div>")
			.bind('focus', _opt.turnedOn ? VKon : VKoff)  // set onfocus handler
			.next() // select img el. for the icon
			.toggle(imgVKoff, imgVKon) // set onclick toggling handlers for the icon
			.next()[0]; // select VK div el.
			if(!_opt.visible) $('div._vkdiv_').hide(); // hide all the VK div elements
		}else{
			$(this).bind('focus',VKhere) 
		}
	});
/*	if(_opt.VKdiv){
		_ind = 0;
		_cur_inp = _inputs[0];
		VKhere()
	}
*/
	return _inputs; //you can work with VKized input elements further e.g. $('input[type=text],textarea').attachVK().css('...')
}
})(jQuery);
