// Nicer margins for images in def blocks
(function($){
  $(".def img").each(function() { 
    if ( $(this).css("float") == "left" ) {
      $(this).css("margin-left", "0px");
    } else if ( $(this).css("float") == "right" ) { 
      $(this).css("margin-right", 0);
    }
  });
})(jQuery);

// Form Hint
(function($){

  var template = $('<span class="hintPopup"><span class="text"></span></span>');

  $(".hasHint .hint").bind({
    mouseenter: function() {
      var pos = getOffsets(this);
      $(".text", template).text( $(this).html() ) ;
      template
      .css({
        "opacity": "0",
        "display": "block"
      })
      .appendTo("body")
      .css({
        "left": pos.x-219+"px",
        "top": pos.y+25+"px",
        "opacity": "1"
      })
    },
    mouseleave: function() { 
      template.css({
        "display": "none"
      });
    }
  });

})(jQuery);


function getOffsets (el) {        
  var o = {
    x : el.offsetLeft,
    y : el.offsetTop
    };
  if (el.offsetParent != null) {
    var po = getOffsets(el.offsetParent);
    o.x += po.x;
    o.y += po.y;
  }
  return o;
}
$.translate = function(word, path) {
  var translation = word;
  if ($.translations && $.translations[path] && $.translations[path][word]) {
    translation = $.translations[path][word];
  } else if (DEVMODE) {
    $.post('json.php?controller=general&action=translate', {
      path : path,
      word : word
    });
  }
  return translation;
};

$.json = function(controller, action, data, callback, method) {
  if (!callback && $.isFunction(data)) {
    callback = data;
    data = {};
  }
  if(method != 'post') {
    $.getJSON('json.php?controller=' + controller + '&action=' + action, data, function(j, textStatus) {
      var status = 'success';
      if (typeof(callback) == 'function') {
        if(j.status != 'ok')
          status = 'failure';
        callback(j, status);
      }
    });
  } else {
    $.post('json.php?controller=' + controller + '&action=' + action, data, function(j, textStatus) {
      if (typeof(callback) == 'function') {
        var status = 'success';
        if (typeof(callback) == 'function') {
          if(j.status != 'ok')
            status = 'failure';
          callback(j, status);
        }
      }
    }, 'json');
  }


};

$.unique_id = function() {
  return ((new Date()).getTime() + "" + Math.floor(Math.random() * 1000000)).substr(0, 18);
};

// nav dropdown
(function($) {
  var dropdown_html = "<div id=\"navPopup\" style=\"left: 50%\"> \
      <div class=\"top\"><!-- --></div> \
      <div class=\"mid\"> \
      </div> \
      <div class=\"bottom\"><!-- --></div> \
    </div>";
  var dropdown = $(dropdown_html);
  var hoveringOnNav = false;
  var hoveringOnNavPopup = false;
  var timer;
  var currentActiveNav;
  var closeTimeout = 100;
  var menuClosed = true;

  $("#nav .hasSubmenu").bind({
    mouseenter: function() {
      currentActiveNav = this;
      hoveringOnNav = true;
      menuClosed = false;
      $("a", this).addClass("hover");
      var offsets = getOffsets(this);
      $(".mid", dropdown).html("");
      /* populate current items to popup */
      $("ul .item", this).each(function(i) {
        a = $(this).clone();
        if (i==0) {
          a.addClass("first");
        }
        $(".mid", dropdown).append(a);
      });
      dropdown.css({"left": offsets.x-17+"px"})
      dropdown.unbind().bind({
        mouseenter: function() {
          hoveringOnNavPopup = true;
        },
        mouseleave: function() {
          hoveringOnNavPopup = false;
          timer = setTimeout(__close, closeTimeout);

          function __close() {
            if (!menuClosed && !hoveringOnNav && !hoveringOnNavPopup) {
              menuClosed = true;
              dropdown.remove()
              $("> a", currentActiveNav).removeClass("hover");
            }
          }
        }
      });
      $("body").append(dropdown);
    },
    mouseleave: function() {
      hoveringOnNav = false;
      timer = setTimeout(__close, closeTimeout);

      function __close() {
        if (!menuClosed && !hoveringOnNav && !hoveringOnNavPopup) {
          menuClosed = true;
          dropdown.remove();
          $("> a", currentActiveNav).removeClass("hover");
        }
      }
    }
  });

})(jQuery); 
