/**
 * jQuery extension
 *
 * @author Bronislav Klucka <Bronislav.Klucka@bauglir.com>
 * @copyright Copyright (c) 2009+, Bronislav Klučka
 * @license http://licence.bauglir.com/bsd.php BSD License
 * @version $Id: bauglir.jquery.js,v 1.4 2010-01-25 22:48:29 root Exp $
 * @package BauglirWebCore
 * @subpackage Javascript
 */

/*global window, document, Bauglir, $$, jQuery */

(function ($) {
  /**
   * absolute left position (according to document)
   * @return int
   */
  jQuery.fn.absLeft = function (jElement)
  {
    try
    {
      if (jElement == 'undefined')
      {
        var p = this.offset();
        return Math.round(p.left);
      }
      else
      {
	var ret = this.positionLeft();
        if (this.offsetParent().length && (this.offsetParent().eq(0).id() != jElement.id())) ret += this.offsetParent().eq(0).absLeft(jElement);
        return ret;

        
      }
    }
    catch (e)
    {
      return null;
    }
  };

  /**
   * return absolute top position (according to document)
   */
  jQuery.fn.absTop = function (jElement)
  {
    try
    {
//      var p = this.offset();
//      return Math.round(p.top);
      if (jElement == 'undefined')
      {
        var p = this.offset();
        return Math.round(p.top);
      }
      else
      {
        var ret = this.positionTop();
        if (this.offsetParent().length && (this.offsetParent().eq(0).id() != jElement.id())) ret += this.offsetParent().eq(0).absTop(jElement);
        return ret;


      }

    }
    catch (e)
    {
      return null;
    }
  };

  /**
   * retutns caret position in input
   * @return int
   */
  jQuery.fn.caretPosition = function ()
  {
    try
    {
      var element = this.get(0),
      sel;
      element.focus();
      try
      {
        sel = document.selection.createRange();
        sel.moveStart('character', -element.value.length);
        return (sel.text.length);
      }
      catch (e)
      {
        return (element.selectionStart);
      }
    } catch (e1)
    {
      return null;
    }
  };

  /**
   * bind/unbind function to trigger if element is changed or blured
   * @param function|null changedFunction
   * @param string bind namespace
   * /
  jQuery.fn.changedNoKey = function(changedFunction, nameSpace)
  {
    if ($$.defined(nameSpace)) nameSpace = '.' + nameSpace;
    else nameSpace = '';
    if (changedFunction !== null)
    {
      this.bind('change' + nameSpace, changedFunction);
      this.bind('blur' + nameSpace, changedFunction);
    }
    else
    {
      this.unbind('change' + nameSpace, changedFunction);
      this.unbind('blur' + nameSpace, changedFunction);
    }
  }

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


  /**
   * return inner box height without border and padding
   * @return int
   */
  $.fn.contentHeight = function ()
  {
    var result = this.innerHeight();
    //result -= Bauglir.Int(this.css('borderTopWidth'), 0);
    //result -= Bauglir.Int(this.css('borderBottomWidth'), 0);
    result -= $$.Int(this.css('paddingTop'), 0);
    result -= $$.Int(this.css('paddingBottom'), 0);
    return result;
  };

  /**
   * return inner box width without border and padding
   * @return int
   */
  $.fn.contentWidth = function ()
  {
    var result = this.innerWidth();
    //result -= Bauglir.Int(this.css('borderLeftWidth'), 0);
    //result -= Bauglir.Int(this.css('borderRightWidth'), 0);
    result -= $$.Int(this.css('paddingLeft'), 0);
    result -= $$.Int(this.css('paddingRight'), 0);
    return result;
  };

  /**
   * disable selection
   */
  $.fn.disableSelection = function ()
  {
    try
    {
      if (typeof document.body.onselectstart !== 'undefined')
      {
        this.bind("selectstart", function (e) {
          e.stopPropagation();
          return false;
        });
      }
      else
      {
        this.bind("mousedown", function (e) {
          e.stopPropagation();
          return false;
        });
      }
      this.css("cursor", "default");
    }
    catch (f) {}
  };

  /**
   * enable selection
   */
  $.fn.enableSelection = function ()
  {
    try
    {
      if (typeof document.body.onselectstart !== 'undefined')
      {
        this.bind("selectstart", function (e) {
          e.stopPropagation();
          return true;
        });
      }
      else
      {
        this.bind("mousedown", function (e) {
          e.stopPropagation();
          return true;
        });
      }
    }
    catch (f) {}
  };

  /**
   * whether element is hidden
   * @return bool
   */
  $.fn.hidden = function ()
  {
    return this.css('display') === 'none';
  };

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

  /**
   * return full HTML  of element
   * @return string
   */
  $.fn.outerHtml = function ()
  {
    return jQuery("<p>").append(this.eq(0).clone()).html();
  };

  /**
   * left offset according to parent
   * @return int
   */
  $.fn.positionLeft = function ()
  {
    try
    {
      var p = this.position();
      return Math.round(p.left);
    }
    catch (e)
    {
      return 0;
    }
  };

  /**
   * top offset according to parent
   * @return int
   */
  $.fn.positionTop = function ()
  {
    try
    {
      var p = this.position();
      return Math.round(p.top);
    }
    catch (e)
    {
      return 0;
    }
  };

  /**
   * return selected text
   * @return string
   */
  $.fn.selectedText = function ()
  {
    try
    {
      var element = this.get(0);
      try
      {
        return document.selection.createRange().text;
        
      }
      catch (e)
      {
        return element.value.substring(element.selectionStart, element.selectionEnd);
      }
    } catch (e1)
    {
      return null;
    }
  };

  /**
   * select text
   * @param int startPosition
   * @param int endPosition
   */
  $.fn.selectText = function (startPosition, endPosition)
  {
    try
    {
      var element = this.get(0),
      range;
      try
      {
        range = element.createTextRange();
        range.collapse();
        range.moveStart('character', startPosition);
        range.moveEnd('character', endPosition - startPosition); //end - start
        range.select();
      }
      catch (e)
      {
        element.selectionStart = startPosition;
        element.selectionEnd = endPosition;
      }
    } 
    catch (e1)
    {
      return null;
    }
  };

  /**
   * return tag name in lowercase
   * @return string
   */
  $.fn.tagName = function ()
  {
    return this.get(0).tagName.toLowerCase();
  };

}(jQuery));

//	Mazel100


