/** 
 * @projectDescription library of methods related to manipulating ranges. 
 * formerly jquery.text-selection-0.1.js
 *
 * @author Luke Cuthbertson
 * @version 0.1 
 */
(function($){
	
	$.ranges = {
	
		/**
		 * select text - abstracts away browser specific text slection code
		 * @param {Object} start index of character to start selection on
		 * @param {Object} end index of character to end selection on
		 * @return {Boolean} true if browser supports text selection. otherwise false
		 */
		setSelected: function (elem, start, end){
				var value = '';
				if(elem.tagName.toLowerCase() == 'input'){
					value = elem.value;
				}
				if(typeof end == 'undefined'){
					end = value.length;
				}
				if(elem.createTextRange){//IE
					var range = elem.createTextRange();
					range.moveStart("character", start);
					range.moveEnd("character", value.length - end);//0 index from end
					range.select();	
					return true;
				}else if(elem.setSelectionRange){//FF
					elem.setSelectionRange(start, end);
					return true;
				}else{
					return false;
				}		
		},
		
		/**
		 * get selected text
		 * @return {String} the currently selected text
		 */
		getSelected: function (elem){
			var selectedText = '';
			if (document.selection){//IE
				var sel = document.selection.createRange();
				selectedText = sel.text;
			}
			else if (typeof elem.selectionStart != 'undefined'){//FF
				var startPos = elem.selectionStart;
				var endPos = elem.selectionEnd;
				selectedText = elem.value.substring(startPos, endPos)
				if(!selectedText){ selectedText == '' }; 
			};
			return selectedText;
		},
		
	   /**
	   * Returns the caret (cursor) position of the specified text field.
	   * @param {HTMLElement} oField input
	   * @return {Integer} value range is 0-oField.length.
	   */
	   getCaretPos: function(oField) {
	
	     // Initialize
	     var iCaretPos = 0;
	
	     // IE Support
	     if (document.selection) { 
	
	       // Set focus on the element
	       oField.focus ();
	  
	       // To get cursor position, get empty selection range
	       var oSel = document.selection.createRange ();
	  
	       // Move selection start to 0 position
	       oSel.moveStart ('character', -oField.value.length);
	  
	       // The caret position is selection length
	       iCaretPos = oSel.text.length;
	     }
	
	     // Firefox support
	     else if (oField.selectionStart || oField.selectionStart == '0')
	       iCaretPos = oField.selectionStart;
	
	     // Return results
	     return (iCaretPos);
	   },
	
	
	   /**
	   * Sets the caret (cursor) position of the specified text field.
	   * @param {HTMLElement} oField input
	   * @param {Integer} iCaretPos caret position - valid positions are 0-oField.length.
	   */
	   setCaretPos: function (oField, iCaretPos) {
	
	     // IE Support
	     if (document.selection) { 
	
	       // Set focus on the element
	       oField.focus ();
	  
	       // Create empty selection range
	       var oSel = document.selection.createRange ();
	  
	       // Move selection start and end to 0 position
	       oSel.moveStart ('character', -oField.value.length);
	  
	       // Move selection start and end to desired position
	       oSel.moveStart ('character', iCaretPos);
	       oSel.moveEnd ('character', 0);
	       oSel.select ();
		   return true;
	     }
	
	     // Firefox support
	     else if (oField.selectionStart || oField.selectionStart == '0') {
	       oField.selectionStart = iCaretPos;
	       oField.selectionEnd = iCaretPos;
	       oField.focus ();
		   return true;
	     }
		 return false;
	   }

	
	}
	
	$.fn.selection = function(start, end){
		var elem = this.eq(0)[0];
		if (arguments.length > 0) {
			$.ranges.setSelected(elem, start, end);
			return this;
		}
		else {
			return $.ranges.getSelected(elem);
		};
	};
	
})(jQuery);

