$('document').ready(function() {
	$('#qSlovnikForm').focus();
	$('#qSlovnikForm').select();

	$('ul#prepinace a').mousedown(function(){
		if($('#qSlovnikForm').val()!=''){
		  var vars    = [], hash;
		  var hashes  = $(this).attr('href').slice($(this).attr('href').indexOf('?') + 1).split('&');
		  var base    = $(this).attr('href').substring(0, $(this).attr('href').lastIndexOf('/')+1);
		  //nacitanie parametrov URL do asociativneho pola
		  for(var i = 0; i < hashes.length; i++){
			  hash = hashes[i].split('=');
			  vars.push(hash[0]);
			  vars[hash[0]] = hash[1];
		  }

		  $(this).attr('href', base + '?q='+ $('#qSlovnikForm').val());

		  if(vars['i9'] != undefined){
			$(this).attr('href', $(this).attr('href') + '&i9='+ vars['i9']);
		  }
		}
	});

	$('#slov').submit(function() {
		if ($('#qSlovnikForm').val().length > 0) {
			if($('#sel').is('ul')) {
				var urlTranslate = '';
				$('ul#sel li ul li').each(function(){
					if ($(this).find('a').is('a') && $(this).find('a').html() == $('ul#sel li a').html()) {
						urlTranslate = $(this).find('a').attr('href');
					}
				});
				if(($('#slov').attr('action').indexOf('?', 0)==-1) ){
					$('#slov').attr('action', urlTranslate + '?q='+ encodeURI($('#qSlovnikForm').val()));
				} else {
					$('#slov').attr('action', urlTranslate + '&q='+ encodeURI($('#qSlovnikForm').val()));
				}
			}
			else{
				if(($('#slov').attr('action').indexOf('?', 0)==-1) ){
					$('#slov').attr('action',$('#slov').attr('action')+'?q='+ encodeURI($('#qSlovnikForm').val()));
				} else {
					$('#slov').attr('action',$('#slov').attr('action')+'&q='+ encodeURI($('#qSlovnikForm').val()));
				}
			}
			return true;
		} else {
			return false;
		}
	});
	$('#qSlovnikForm').keydown(function(e){
		if (e.keyCode==9){
			$('ul#sel li:first').click();
			$('ul#sel ul li a:first').focus();
			return false;
		}
	});
	$('ul#sel ul').click(function(){
		return false;
	});
	$('ul#sel ul li').click(function(){
		if ($(this).find('a').is('a')){
			$('ul#sel li a:first').html($(this).find('a').html());
			$('ul#sel li a:first').attr('name',$('ul#sel ul li a').index($(this).find('a')[0]));
			closeUl();
			if ($('#qSlovnikForm').val().length > 0) {
				$('#slov').submit();
			} else {
				$('#qSlovnikForm').focus();
			}
		}
		return false;
	});
	$('ul#sel li:first').click(function(){
		if($('ul#sel ul').is(':visible')) {
			closeUl();
		} else {
			$('ul#sel ul').show();
			$('ul#sel').css('background-attachment','fixed');
			if (!$('ul#sel ul li a.hover').is('a')) {
				$('ul#sel ul li a:first').addClass('hover');
			}
			$('ul#sel ul li a:first').focus();
			$('html').bind('click',function(e) {
				closeUl();
			});
			
		}

		return false;
	});
	$('ul#sel').keydown(function(e){
		if (e.keyCode==40) {
			var liNext = $('#sel ul li a.hover').parent().next();
			if (liNext.hasClass('jazyk')) {
				liNext = liNext.next();
			}
			if (liNext.is('li')) {
				$('#sel ul li a.hover').removeClass('hover');
				liNext.find('a').addClass('hover');
			}
			return false;
		} else if (e.keyCode==38){
			var liPrev = $('#sel ul li a.hover').parent().prev();
			if (liPrev.hasClass('jazyk')) {
				liPrev = liPrev.prev();
			}
			if (liPrev.is('li')) {
				$('#sel ul li a.hover').removeClass('hover');
				liPrev.find('a').addClass('hover');
			}
			return false;
		} else if (e.keyCode==13){
			$('#sel ul li a.hover').click();
			$('#qSlovnikForm').focus();
			return false;
		} else if (e.keyCode==9) {
			closeUl();
			$('#qSlovnikForm').focus();
			return false;
		}
		return true;
	});
	function closeUl() {
		$('ul#sel').css('background-attachment','scroll');
		$('ul#sel ul').hide();
		$('html').unbind('click');

	}
	$('a.lupa img').click(function(){
		var lupa = $(this).parent();
		var showKeyWord = true;
		var url = '';

		if(lupa.next("div").html() == ''){
			$.getJSON('/preklad_ajax/',{dict:urlAjax, q:lupa.prev().find('span').text()},function(json){
				htmlTxt = '<table class="ajax-slovnik">';
				url = json['params']['url'];

				if (typeof(json) != 'undefined') {
					$.each(json['data'], function(index, value){
						if(showKeyWord){
							//htmlTxt			= htmlTxt + '<tr><td style="color: rgb(63, 107, 212);">' + lupa.prev().html() + '</td><td class="sipka">&nbsp;\u2192&nbsp;</td><td>' + value + '</td></tr>';
							htmlTxt			= htmlTxt + '<tr><td style="color: rgb(63, 107, 212);">' + lupa.prev().text() + '</td><td class="sipka">&nbsp;\u2192&nbsp;</td><td><a href="' + url + (i9 != '' ? i9+'&q=' : '?q=') + encodeURI(value) + '">' + value + (json['add_query'][index] != undefined ? json['add_query'][index] : '') + '</a>' + '</td></tr>';
							showKeyWord = false;
						}
						else{
							//htmlTxt			= htmlTxt + '<tr><td></td><td class="sipka">&nbsp;\u2192&nbsp;</td><td>' + value + '</td></tr>';
							htmlTxt			= htmlTxt + '<tr><td></td><td class="sipka">&nbsp;\u2192&nbsp;</td><td><a href="' + url + (i9 != '' ? i9+'&q=' : '?q=') + encodeURI(value) + '">' + value + (json['add_query'][index] != undefined ? json['add_query'][index] : '') + '</a>' + '</td></tr>';
						}
					});
				}
				htmlTxt = htmlTxt + '</table>';
				lupa.next("div").html(htmlTxt);
				lupa.next("div").toggle();
			});
		}
		else{
			lupa.next("div").toggle();
		}
	});

	$('#licence_author_link').click(function(){		
		changePosition($(this),'licence_author_hint',125,10);
	});
	$('#licence_publisher_link').click(function(){
		changePosition($(this),'licence_publisher_hint',125,10);
	});
	$('#licence_history_link').click(function(){
		changePosition($(this),'licence_history_hint',125,10);
		});
	$('#licence_author_close_link').click(function(){
		$('html').unbind('click');
		$('#Buble').hide();
	});

	$('#licence_publisher_link_hp').click(function(){
		changePosition($(this),'licence_publisher_hint', 15, -15);
	});
	$('#licence_author_link_hp').click(function(){
		changePosition($(this),'licence_author_hint', 15, -15);
	});
	$('#licence_author_close_link_hp').click(function(){
		$('html').unbind('click');
		$('#Buble').hide();
	});

	function changePosition(eventObject,showDiv,yoffset,xoffset) {
		$('html').unbind('click');
		var elePos = eventObject.position();
		$('#Buble').css('z-index', 10);
		$('#Buble').css('top', elePos.top + yoffset);
		$('#Buble').css('left',	elePos.left + xoffset);
		$('#BubleMiddle div').hide();
		$('#Buble').show();
		$('#'+showDiv).fadeIn(400);
		$('html').bind('click',function(e){
			var canClose = true;
			var obj = $(e.target);
			while (obj[0].parentNode	 != null) {
				if (obj[0].id == 'Buble' || obj[0] == eventObject[0]) {
					canClose = false;
				}
				obj = $(obj[0].parentNode);
			}
			if (canClose) {
				$('html').unbind('click');
				$('#Buble').hide();
			}
		});
	}
	$('#show-more-history').click(function(){
		$('#more-history').show();
		$(this).hide();
		return false;
	});
	$('#vymazat-history').click(function(){
		document.cookie="slovnik_historia='';expires=01/01/1970 00:00:00;path=/";
		$('#historia-box-table tr').hide();
		$('#show-more-history').hide();
		$('#more-history').hide();
		//$(this).hide();
		return false;
	});
});

var keyboard_loaded = false;
function g(e){
	if(!document.getElementById(e)){
		return false;
	}
	return document.getElementById(e);
}
function sf(select, send) {
	var s_val = select.value.split('-');
	var kb_lang = new Array("ru");
	kb_lang = ","+kb_lang.toString()+",";
	if(g("qSlovnikForm").value != '' && send == true) {
		select.form.submit();
	}
}
function trans(l) {
	var t = g("selectSlovnikForm");
	var q = g("qSlovnikForm");
	var n = t.value;n = n.split("-");
	if(l == n[0] || l == n[1]) {
		n = n[1]+"-"+n[0];
	}
	else {
		n = l+"-sk";
	}
	for(var i=0; i<t.length; i++) {
		if(t[i].value == n) {
			t.selectedIndex = i;
			t.focus();
		}
	}
	sf(t, false);
}

function GetHttpRequest(){   
	if ( window.XMLHttpRequest ) // Gecko 
		return new XMLHttpRequest() ; 
	else if ( window.ActiveXObject ) // IE 
		return new ActiveXObject("MsXml2.XmlHttp"); 
}  

function includeJS(sId, source)  {
	if ( ( source != null ) && ( !document.getElementById( sId ) ) ) {  
		var oHead = document.getElementsByTagName('head').item(0); 
		var oScript = document.createElement( "script" ); 
		oScript.language = "javascript"; 
		oScript.type = "text/javascript"; 
		oScript.id = sId; 
		//oScript.defer = true;
		oScript.src = source;
		oHead.appendChild( oScript ); 
	}  
}  
function includeCSS(sId, source)  {
	if ( ( source != null ) && ( !document.getElementById( sId ) ) ) {  
		var oHead = document.getElementsByTagName('head').item(0); 
		var oScript = document.createElement( "link" ); 
		oScript.type = "text/css"; 
		oScript.id = sId; 
		oScript.rel = 'stylesheet'; 
		//oScript.defer = true; 
		oScript.href = source; 
		oHead.appendChild( oScript ); 
	}  
}  

function AjaxPage(sId, url, type){  
	var oXmlHttp = GetHttpRequest();
	oXmlHttp.open('GET', url, true);
	alert(url);
	oXmlHttp.onreadystatechange = function() {
		alert(oXmlHttp.readyState + ' '  + oXmlHttp.status);
		if (oXmlHttp.readyState == 4 && oXmlHttp.status == 200) {
		alert('hotovo');
			if (oXmlHttp.responseText) {
				if(type == 'css') {
					includeCSS(sId, oXmlHttp.responseText); 
				}
				else if(type == 'js') {
					includeJS(sId, oXmlHttp.responseText);
					VKI_buildKeyboardInputs();
					keyboard_loaded=true;
				}				
			}
		}
	}
	oXmlHttp.send(null);
}
function loadKeyboard() {
	var kb_elmnt = document.getElementById('keyboardInputMaster');
	if(!kb_elmnt) {
		includeCSS("fly_css", css_url+'keyboard.css?v=20100618');
		includeJS("fly_js", js_url+'keyboard.js?v=20100618');
		keyboard_loaded=true;
		if (window.addEventListener) {
			window.addEventListener('click',clickBind, false);
		} else if (window.attachEvent) {
			document.attachEvent('onclick', clickBind);
		}
	}
	else {
		if(keyboard_loaded && kb_elmnt.style.display!='none') {
			document.getElementById('keyboardInputMaster').style.display = 'none';
		} else {
			document.getElementById('keyboardInputMaster').style.display = 'block';
			var iSelect = $('ul#sel li a:first').attr('name') / 2;
			iSelect = Math.floor(iSelect) + 1;
			document.getElementById('keybSelect').selectedIndex = iSelect;
			self.VKI_kt = document.getElementById('keybSelect').value;
			self.VKI_buildKeys();
			if (window.addEventListener) {
				window.addEventListener('click',clickBind, false);
			} else if (window.attachEvent) {
				document.attachEvent('onclick', clickBind);
			}
		}
	}
}

function clickBind(e) {
  var closeKeyb = true;
	if (typeof(e) != 'undefined') {
		var obj = '';
		if (typeof(e.target) != 'undefined') {
			obj = e.target;
		} else if(typeof(e.srcElement) != 'undefined') {
			obj = e.srcElement;
		}
	  while (typeof(obj) != 'undefined' && obj.parentNode != null && closeKeyb == true) {
			if (typeof(obj.id) != 'undefined' && (obj.id == "klavesnica_icon" || obj.id == "klavesnica_box" || obj.id == "slov" ) ) {
				closeKeyb = false;
			}
			obj = obj.parentNode;
		}
		if (closeKeyb) {
			self.VKI_close();
			if (window.removeEventListener) {
				window.removeEventListener('click',clickBind, false);
			} else if(window.dettachEvent) {
				document.detachEvent('onclick',clickBind);
			}
		}
	}
	return true;
}