/**
 * Set of static methods to handle slections
 * on input elements
 */
TSelection = function() { }

/**
 * Set selection into an element starting on 'start' offset
 * and counting 'length' characters
 *
 * @param {Element} el: DOM element
 * @param {Integer} start: start charctacter offset
 * @param {Integer} length: selection length
 */
TSelection.set = function(el, start, length) {
  if (!TDOM.isVisible(el)) { return; }
  if (el.setSelectionRange) {   // Mozilla (input elements)
    el.setSelectionRange(start, start+length);
  } else
  if (document.createRange) {   // Mozilla again
		var range = document.createRange();
		range.selectNode(el);
		range.setStart(el, start);
		range.setEnd(el, start+length);
  } else
  if (el.createTextRange) {     // IE
    var range = el.createTextRange();
    range.collapse(true);
    range.moveStart("character", start);
    range.moveEnd("character", length);
    range.select();
  }
};

/**
 * Get selection range from document and return
 * a selection object.
 *
 * @param {Element} input: HTML Input element
 * @param {Integer} start: start charctacter offset
 * @param {Integer} length: selection length
 * @return {Object} {start: sel start offset from target
 *                   length: selection length
 *                   target: selection target element
 *                   text: selected plain text
 *                   html: selected html text }
 */
TSelection.get = function(input) {
  var html = '';
  var text = '';
  var target = null;
  var start = count = 0;
  input = typeof input == "string" ? TDOM.getElement(input) : input;
  // Mozilla input selections
  if (input && input.selectionStart) {
    target = input;
    start = input.selectionStart;
    count = input.selectionEnd - start;
    if (input.value) {
      text = input.value.substr(start, count);
      html = text;
    }
  } else
  // Mozilla Selection Range
  // (does not work on inputs, only HTML selections)
  if (window.getSelection) {
  	text = window.getSelection();
  	// W3C
  	if (text != '') {
    	if (text.getRangeAt) {
    		var range = text.getRangeAt(0);
    	// Safari!
    	} else {
    		var range = document.createRange();
    		range.setStart(text.anchorNode, text.anchorOffset);
    		range.setEnd(text.focusNode, text.focusOffset);
    	}
      text = text.toString();
    	// Get HTML from selection
    	var div = document.createElement('div');
      div.appendChild(range.cloneContents());
    	html = div.innerHTML;
      if (html.indexOf('<') === 0) {
        var regex = new RegExp(/<.*?>([\s\S]*)<\/.*>/);
    	  regex.exec(html);
    	  html = RegExp.$1;
      }
      target = TDOM.resolveTextNode(range.commonAncestorContainer);
    	count = text.length;
    	// TODO: Missing selection start
  	}
  } else
  // IE TextRange
  // Work both on inputs and HTML selections
  if (document.selection) {     // IE
    if (document.selection.type == 'Text' || input) {
      var range = document.selection.createRange();
      if (range) {
        text = range.text;
        html = range.htmlText;
        count = text.length;                  // Selection Length
        target = range.parentElement();       // Get selection target
        var copy = range.duplicate();         // Make a copy of the selection
        copy.expand('textedit');              // Select all the content in the copied selection
        if (input != target) {
          copy.moveToElementText(target);     // Move selection start to the target element
        }
        copy.setEndPoint('EndToEnd',range);   // Move end point to the end point of the original selection
        start = copy.text.length - count;     // Calculate Selection Start
      }
    }
  }
  return {start:start, length:count, target:target, text:text, html:html};
};
