var c = navigator.userAgent.toLowerCase()
var IE6 = (Prototype.Browser.IE && parseFloat(c.substring(c.indexOf('msie ')+5))<7)?true:false;

var buttonsKB = {
  'ru': [
	  {'value':'ё','style':{'marginLeft': Prototype.Browser.IE && IE6 ? 20 : 42}},{'value':':'},{'value':'*'},{'value':'&', 'title': '&amp;'},{'value':'\\'},{'value':'/'},{'value':'£'},{'value':'$'},{'value':'?','break':true},
	  {'value':'й'},{'value':'ц'},{'value':'у'},{'value':'к'},{'value':'е'},{'value':'н'},{'value':'г'},{'value':'ш'},{'value':'щ'},{'value':'з'},{'value':'х'},{'value':'ъ','break':true},
	  {'value':'ф','style':{'marginLeft': Prototype.Browser.IE && IE6 ? 7 : 16}},{'value':'ы'},{'value':'в'},{'value':'а'},{'value':'п'},{'value':'р'},{'value':'о'},{'value':'л'},{'value':'д'},{'value':'ж'},{'value':'э','break':true},
	  {'value':'я','style':{'marginLeft': Prototype.Browser.IE && IE6 ? 20 : 42}},{'value':'ч'},{'value':'с'},{'value':'м'},{'value':'и'},{'value':'т'},{'value':'ь'},{'value':'б'},{'value':'ю'}
	],
	'de': [
	  {'value':'~','style':{'marginLeft': Prototype.Browser.IE && IE6 ? 20 : 42}},{'value':':'},{'value':'*'},{'value':'&', 'title': '&amp;'},{'value':'\\'},{'value':'/'},{'value':'£'},{'value':'$'},{'value':'?','break':true},
	  {'value':'q'},{'value':'w'},{'value':'e'},{'value':'r'},{'value':'t'},{'value':'z'},{'value':'u'},{'value':'i'},{'value':'o'},{'value':'p'},{'value':'ü'},{'value':'ß','break':true},
	  {'value':'a','style':{'marginLeft': Prototype.Browser.IE && IE6 ? 7 : 16}},{'value':'s'},{'value':'d'},{'value':'f'},{'value':'g'},{'value':'h'},{'value':'j'},{'value':'k'},{'value':'l'},{'value':'ö'},{'value':'ä','break':true},
	  {'value':'y','style':{'marginLeft': Prototype.Browser.IE && IE6 ? 20 : 42}},{'value':'x'},{'value':'c'},{'value':'v'},{'value':'b'},{'value':'n'},{'value':'m'},{'value':','},{'value':'.'}
	]
};

function loadImg( _id, _link ) {
  if ( !(_id = $(_id)) ) {
    return;
  }

  var img = document.createElement('img');
  var ePluse = new Effect.Pulsate(_id, {pulses: 20, duration: 10});
  _id.onclick = null;
  _id.setStyle({'cursor': 'default'});
  img.onload = function () {
    ePluse.cancel();
    _id.show();
    _id.setOpacity(1);
    var width = this.width;
    var height = this.height;
    if ( this.width > _id.width ) {
      _id.onclick = function () {
        var nWidth = Prototype.Browser.IE ? width + 18 : width;
        var nHeight = Prototype.Browser.IE ? height + 26 : height;
        __windowOpen(_link, 'imgWin', nWidth, nHeight, 200, 100);
      }
      _id.setStyle({'cursor': 'pointer'});
    }

    new Effect.Morph(
      _id,
      {
        style: 'width: ' + width + 'px; height: ' + height + 'px;',
        duration: 0.4,
        afterFinish: function () {
          _id.src = _link;
        }
      }
    );
  }
  img.src = _link;
}

function toggleKeyBoard(_div, _instr) {
  var div = $(_div);
  var instr = $(_instr);

  var tmp = instr.onclick;
  instr.onclick = null;
  var params = {
    duration: 0.2,
    afterFinish: function() {
      instr.onclick = tmp; 
    }
  }
  if ( !div.visible() ) {
    new Effect.BlindDown(div,params);
  }
  else {
    new Effect.BlindUp(div,params);
  }
}

function changeKeyBoard(_lang, _kb) {
  if ( !buttonsKB[_lang] ) {
    return;
  }

  var cfg = _kb._config;
  cfg.buttons = buttonsKB[_lang];
  new Effect.Opacity(
    cfg.renderTo,
    {
      from: 1,
      to: 0.1,
      duration: 0.5,
      afterFinish: function() {
        _kb.applyConfig(cfg);
        new Effect.Opacity(cfg.renderTo, {from: 0.1, to: 1, duration: 0.5});
      }
    }
  );
}

function changeKeyBoardInput(_input, _kb) {
  var cfg = _kb._config;
  cfg.input = _input;
  _kb.applyConfig(cfg);
}

function __windowOpen( url, name, width, height, top, left ) {
	if ( !name )
		return false;

	if ( !width )
		width = 400;
	if ( !height )
		height = 300;

	param = 'location,width='+width+',height='+height+',toolbar=no,maximize=no,resizable=no,menubar=no,status=no';
	if ( top != '' )
		param += ',top='+top;
	if ( left != '' )
		param += ',left='+left;

	_window = window.open( url, name, param );
	_window.focus();

	return _window;
}
