/**
 * Common AJAX setup
 */
jQuery(document).ready(function() {

  jQuery(function() {
    
    jQuery("a.ajax").livequery("click", function() {
      jQuery.get(this.href);
      return false;
    });
//
//    jQuery(".ajaxAppForm select.ajax, .ajaxAppForm input[type=text].ajax").livequery("change", function() {
//      jQuery(this.form).ajaxSubmit();
//      return false;
//    });

    // pro Nette formulare (Nette\NForm)
    jQuery("form.ajaxAppForm :submit").livequery(function () {
      this.oldonclick = this.onclick;
      this.onclick = null;
      jQuery(this).click(function() {
        if (jQuery.isFunction(this.oldonclick) && !this.oldonclick())
          return false;
        jQuery(this).ajaxSubmit();
          return false;
      });
    }, function () {});

  });

  // ajax spinner
  jQuery(function () {
    jQuery('<div id="ajax-spinner"><span></span></div>').hide().ajaxStart(function () {
      jQuery(this).show();
    }).ajaxStop(function () {
      jQuery(this).hide();
    }).appendTo("body");
  });

  // skrývání flash zpráviček
//  jQuery("div.flash").livequery(function () {
//    var el = jQuery(this);
//    setTimeout(function () {
//      el.animate({"opacity": 0}, 2000);
//      el.slideUp();
//    }, 7000);
//  });

  jQuery("div.flash").livequery("click", function() {
    jQuery(this).hide();
  });

  // calculation/correction form
  jQuery('form textarea.user-text').livequery(function() {
    jQuery(this).pageCount({
      countOnly: false,
      counter: '#pages-count',
      forseMessage: '#speed-bubble',
      basicElm: '#frmtranslationCalculationForm-speed-0',
      expressElm: '#frmtranslationCalculationForm-speed-1',
      expressLabel: 'label[for=frmtranslationCalculationForm-speed-1]',
      maxExpressPages: 7,
      lettersPerPage: 1800
    });
  });

  // calculation/correction form - cleaner
  jQuery('form #empty-text').livequery('click', function() {
    if(!jQuery('form textarea.user-text').attr('disabled')) {
      jQuery('form textarea.user-text').val('');
      jQuery('form textarea.user-text').pageCount();
    }
  });

  jQuery('#speed-bubble .close, .error .close').livequery('click', function() {
    jQuery(this).parent().hide();
  });

  jQuery('a.tooltip').hover(function() {
    if((jQuery(window).width() - jQuery(this).offset().left - 250) <= 0) {
      jQuery(this).children('span').children('span').css('left', '-290px');
    }
  });

});

