$(document).ready(function() {
	$("div.hint-link").hover(
      function () {
        $(this).find('div.hint').show();
      }, 
      function () {
         $('div.hint').hide();
      }
    );
	$('#keywords_lv_unlock').click(function(e){
		e.preventDefault;
		$('#keywords_lv').removeAttr('disabled');
	});
	$('#keywords_ru_unlock').click(function(e){
		e.preventDefault;
		$('#keywords_ru').removeAttr('disabled');
	});
	$('#keywords_lv').each(function() {
        var input = this;
        word_count(input);
        $(this).keyup(function() { word_count(input) });
    });
	$('#keywords_ru').each(function() {
        var input = this;
        word_count(input);
        $(this).keyup(function() { word_count(input) });
    });
	$(".main_cat").click(
	function(e){
		e.preventDefault();
		var id = $(this).attr("id").split("_")[2];
		var new_cont = $(this).parent();
		$.ajax({
			type: "GET",
			url: 'http://'+location.host+"/js/php/get_frontpage_category.php",
			dataType: 'html',
			data: { "id" : id },
			success: function(data){
				new_cont.empty();
				new_cont.append(data);
				var new_height = new_cont.find('.sub_cat').height();
				new_cont.empty();
				new_cont.animate({height: new_height}, 2000, function(){ }).append(data);
			}
		});
	});
	$(".category_link").click(
	function(e){
		e.preventDefault();
		var url = $(this).attr("href");
		var words = $(this).attr("title");
		$.ajax({
			type: "GET",
			url: 'http://'+location.host+"/js/php/register_words.php?words="+words,
			dataType: 'html',
			data: {},
			success: function(data){
				//$(this).slideUp(1000); // effect
				//window.setTimeout(function(){
				document.location.href=url;
				//}, 1000); 
			}
		});
	});
	$('input[name=search]').autoComplete({ajax: 'http://'+location.host+'/autosearch/',preventEnterSubmit: false}); 
	$('.search_button').click(function(){
		$('#mainSearch').submit();
	});
});
function word_count(field) {
    var number = 0;
    var matches = $(field).val().match(/\b/g);
    if(matches) {
        number = matches.length/2;
    }
	if(number>=10){
		$(field).attr('disabled','disabled');
		var toshow = '#' + $(field).attr("name") + '_unlock';
		$(toshow).show();
	}
    //$(count).text( number + ' word' + (number != 1 ? 's' : '') + ' approx');

}

