/*
 * jTopLeft
 * http://www.karalamalar.net/
 *
 * Copyright (c) 2009 İzzet Emre Erkan
 * Licensed under Creative Commons Attribution-Share Alike 3.0 Unported License
 * http://creativecommons.org/licenses/by-sa/3.0/
 *
 * Date: 2009-11-08 18:45:24 +0300 (Sun, 08 Nov 2009)
 * Revision: 4
 */
(function($){  
  $.fn.extend({
    top: function(obj) {
      var o = findPos(this.get(0));
      return obj ? o - findPos(obj) : o;
    },
    left: function(obj) {
      var o = findPos(this.get(0),true);
      return obj ? o - findPos(obj,true) : o;
    },
    pos: function(obj) {
      var pos = {},o;
      
      o = findPos(this.get(0));
      pos.top = obj ? o - findPos(obj) : o;
      
      o = findPos(this.get(0),true);
      pos.left = obj ? o - findPos(obj,true) : o;
      
      return pos;
    }
  });
  function findPos(o,l){
    var e = o;
    var x = 0;
    if (o.offsetParent) {
      x = l ? o.offsetLeft : o.offsetTop;
      while (o = o.offsetParent){
        if (o.offsetParent == document.body && o.style.position == 'absolute')
          break;
        x += l ? o.offsetLeft : o.offsetTop;
      }
    }
    do {
      if (! /^body|html$/i.test(e.tagName))
        x -= (l ? e.scrollLeft : e.scrollTop ) || 0;
    } while (e = e.parentNode);
    return x;
  }
})(jQuery);