/**
 * jQuery().id()
 * returns id
 */
(function($) {

  $.fn.absLeft = function()
  {
    try
    {
      var p = this.offset();
      return p.left;
    }
    catch(e) {}
  };

  $.fn.absTop = function()
  {
    try
    {
      var p = this.offset();
      return p.top;
    }
    catch(e) {}
  };

  $.fn.caretPosition = function()
  {
    try
    {
      var element = this.get(0);
      element.focus();
      if (Bauglir.Browser.Msie)
      {
        var sel = document.selection.createRange ();
        sel.moveStart ('character', -element.value.length);
        return (sel.text.length);
      }
      else
      {
        return (element.selectionStart)
      }
    } catch(e) {return null;}
  }

  $.fn.changedNoKey = function(changedFunction)
  {
    this.change(changedFunction);
    this.blur(changedFunction);
  }

  $.fn.changed = function(changedFunction)
  {
    this.change(changedFunction);
    this.keyup(changedFunction);
    this.blur(changedFunction);
  }

  $.fn.contentHeight = function()
  {
    var result = this.innerHeight();
    result -= Bauglir.Int(this.css('borderTopWidth'), 0);
    result -= Bauglir.Int(this.css('borderBottomWidth'), 0);
    result -= Bauglir.Int(this.css('paddingTop'), 0);
    result -= Bauglir.Int(this.css('paddingBottom'), 0);
    return result;
  }

  $.fn.contentWidth = function()
  {
    var result = this.innerWidth();
    result -= Bauglir.Int(this.css('borderLeftWidth'), 0);
    result -= Bauglir.Int(this.css('borderRightWidth'), 0);
    result -= Bauglir.Int(this.css('paddingLeft'), 0);
    result -= Bauglir.Int(this.css('paddingRight'), 0);
    return result;
  }


  $.fn.disableSelection = function()
  {
    try
    {
      //msie, chrome, safari
      if(typeof document.body.onselectstart != 'undefined')
        this.bind("selectstart", function(e){e.stopPropagation(); return false;});
      else
      {
        switch (Bauglir.Browser.Application)
        {
          /*case "Firefox":
            this.css("MozUserSelect", "none");
          break;*/
          default: this.bind("mousedown", function(e){e.stopPropagation(); return false;});
        }
        //$($cssPath).css("KhtmlUserSelect", "none");
      }
    }
    catch(e) {}
  };


  $.fn.enableSelection = function()
  {
    try
    {
      //msie, chrome, safari
      if(typeof document.body.onselectstart != 'undefined')
        this.bind("selectstart", function(e){e.stopPropagation(); return true;});
      else
      {
        switch (Bauglir.Browser.Application)
        {
          /*case "Firefox":
            this.css("MozUserSelect", "auto");
          break;*/
          default: this.bind("mousedown", function(e){e.stopPropagation(); return true;});
        }
        //$($cssPath).css("KhtmlUserSelect", "none");
      }
    }
    catch(e) {}
  };

  $.fn.hidden = function()
  {
      return this.css('display') == 'none';
  }

  $.fn.id = function()
  {
      if (arguments.length) return this.eq(0).attr('id', arguments[0]);
      else return this.eq(0).attr('id');
  };

  $.fn.offsetTop = function()
  {
    try
    {
      return this.get(0).offsetTop;
    }
    catch(e) {}
  };

  $.fn.offsetLeft = function()
  {
    try
    {
      return this.get(0).offsetLeft;
    }
    catch(e) {}
  };

  $.fn.oParent = function()
  {
    try
    {
      return $(this.get(0).offsetParent);
    }
    catch(e) {}
  };


  $.fn.outerHtml = function()
  {
    return jQuery("<p>").append(this.eq(0).clone()).html();
  }


  $.fn.positionLeft = function()
  {
      var p = this.position();
      return p.left;
  }

  $.fn.positionTop = function()
  {
      var p = this.position();
      return p.top;
  }

  $.fn.pLeft = function()
  {
      var p = this.position();
      return p.left;
  }

  $.fn.pTop = function()
  {
      var p = this.position();
      return p.top;
  }

  $.fn.selectedText = function()
  {
    try
    {
      var element = this.get(0);
      if (!Bauglir.Browser.Msie)
      {
        return (element.value.substring(element.selectionStart, element.selectionEnd));
      }
      else
      {
       return (document.selection.createRange().text);
      }
    } catch(e) {return null;}
  }

  $.fn.selectText = function(startPosition, endPosition)
  {
    try
    {
      var element = this.get(0);
      if (!Bauglir.Browser.Msie)
      {
        element.selectionStart = startPosition;
        element.selectionEnd = endPosition;
      }
      else
      {
        var range = element.createTextRange();
        range.collapse()
        range.moveStart('character', startPosition);
        range.moveEnd('character', endPosition - startPosition); //end - start
        range.select();
      }
    } catch(e) {return null;}
  }



  $.fn.tagName = function()
  {
      return this.get(0).tagName.toLowerCase();
  }

})(jQuery);


