/*!
 * jQuery placeholders plugin
 * https://github.com/renderedtext/jquery-placeholders
 *
 * Copyright 2011 Marko Anastasov, Rendered Text
 * MIT licensed.
 */

// Puts placeholder text as a value of an input field.
//
jQuery.fn.placeholder = function() {

  this.each(function() {  	
    var hint = $(this).attr("placeholder");

	$(this).removeClass('placeholder').addClass('placeholder');
    if ($(this).val() === "") {
      $(this).val(hint);
      $(this).removeClass('placeholder').addClass('placeholder');
    }
    
    /*$(this).focus(function(){
    	if($(this).val() === hint){
    		$(this).selectRange(0,0);
    	}
    });*/

    $(this).focus(function() {
      if ($(this).val() === hint) {
        $(this).val("");
        $(this).removeClass('placeholder');
      }
    }).blur(function() {
      if ($(this).val() === "") {
        $(this).val(hint);
        $(this).removeClass('placeholder').addClass('placeholder');
      }
    });
  });
};

// The function to use. Call from a form element that contains
// the input fields. It will pull the hint text from the 'placeholder'
// HTML5 attribute, and hook into the submit event to prevent submission
// of placeholder text.
//
// Example:
//
//   $("#new_post").placeholders(["#title", "#body"]);
//
jQuery.fn.placeholders = function(fields) {

  this.each(function() {
    var size = fields.length;
    var i;

    for (i = 0; i < size; ++i) {
      var input = fields[i];
      $(input).placeholder();
    }
	//alert($(this).html());
    $(this).submit(function() {
		
      var i;
      for (i = 0; i < size; ++i) {
        var input = fields[i];
        var hint = $(input).attr("placeholder");

        if ($(input).val() === hint) {
          $(input).val('');
        }
      }
    });
    
    
    
  });
};

/*set cursor position*/
$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};
