// JavaScript Document
/**
* HintPut 2.0
*
* @class hintput
* @param {Object} css parameters
* 	focus {Object}
* 		key {CSS attribute}
* 		value {CSS value}
* 	keydown {Object}
* 		key {CSS attribute}
* 		value {CSS value}
* 	blur {Object}
* 		key {CSS attribute}
* 		value {CSS value}
* 	fade {Object}
* 		time {integer}
*
* 2009 Simon Sarrasin simon@1pixel.ca
* Released under a GNU General Public License v3 (http://creativecommons.org/licenses/by/3.0/)
* ex:
* 	$(".hintput").hintput({
		focus	:	{
			color	:	'#AAA'	// default #999
		},
		keydown	:	{
			color	:	'#000'	// default #000
		},
		blur	:	{
			color	:	'#666'	// default #666
		}
		fade	:	false		// default true {time:500}
	});

*/
//$(document).ready(function(){$(".hintput").hintput();});
(function($) {
	$.fn.hintput = function(options) {
		var opts = $.extend({}, $.fn.hintput.defaults, options);
		var hp_action = function(input){
			var ini = $.trim(input.attr('title'))=="" ? $.trim(input.attr('alt')) : $.trim(input.attr('title'));
			input.css(opts.blur);
			input.focus(function() {
				if (v($(this))) {
					fade(opts.focus,$(this));
					$(this).setCursorPosition(0);
					input.css(opts.focus);
				}
				else fade(opts.keydown,$(this));
			});
			input.keydown(function(event){
				if (v($(this))) {
					fade(opts.keydown,$(this));
					input.val('');
					input.css(opts.keydown);
				}
			});
			input.blur(function(){
				input.val($.trim(input.val()));
				fade(opts.blur,$(this));
				if ($.trim(input.val()) === '') {
					input.val(ini);
					input.css(opts.blur);
				}
			});
			if (input.hasClass('required')) {
				input.parents('form').submit(function(){
					if (v($(this)))	return false;
					return true;
				});
			}
			if (input.hasClass('blank')) {
				input.parents('form').submit(function(){
					if (v($(this))) input.val('');
					return true;
				});
			}
			var v = function(i) {
				return $.trim(i.val()) === ini ? true : false;
			};
		};
		var fade = function(o,input) {
			if(opts.fade === false) return false;
			input.animate(o,opts.fade.time);
			return true;
		};
		this.each(function(){
		    hp_action($(this));
		});
		return true;
	};
	$.fn.hintput.defaults={		
		focus	:	{
			color	:	'#999'
		},
		keydown	:	{
			color	:	'#000'
		},
		blur	:	{
			color	:	'#666'
		},
		fade	:	{
			time	:	500
		}
	};
})(jQuery);
(function($) {
	$.fn.setCursorPosition = function(pos) {
		if ($(this).get(0).setSelectionRange) $(this).get(0).setSelectionRange(pos, pos);
		else if ($(this).get(0).createTextRange) {
			var range = $(this).get(0).createTextRange();
			range.collapse(true);
			range.move('character', pos);
			range.select();
		}
	}
})(jQuery);
