/*
 * jQuery form utilities plugin
 * Example: jQuery.estilo.tools.form.isDefaultValue("INPUT FIELD OBJECT");
 *
 * Copyright (c) 2009 Arnolds Verins
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
	
(function(jQuery) {
	function EstiloFormUtilities(){
		var scope = this;
		this.tips = new Array();	
		setTimeout(function(){
			scope._setDefaultParams();
		}, 300);
	};
	
	jQuery.extend(EstiloFormUtilities.prototype, {
		hideTip: function(tt, change_type){	
			try{
				var scope = this;
				if((tt.nodeName.toLowerCase() == "textarea" || tt.nodeName.toLowerCase() == "input") && tt.name){;
					if(!this.tips[tt.name]){
						this.tips[tt.name] = new Array();
						this.tips[tt.name]["value"] = tt.value;
					}
					if(tt.value == this.tips[tt.name]["value"]){
						if(!tt.orginal_type) tt.orginal_type = tt.type;
						if(change_type){						
							/**
							  * IE issue
							*/
							var input = document.createElement('input');
							input.onfocus = tt.onfocus;
							input.name = tt.name;
							input.className = tt.className;
							input.id = tt.id;
							input.type = change_type;
							input.orginal_type = tt.orginal_type;
							
							jQuery(tt).replaceWith(input);	
							tt = input;
						}
						tt.value = "";
						tt.select();
					}
					tt.onblur = function(){
						scope.showTip(tt);
					}
				}
			}catch(ex){
				if(debug) alert(ex.message);	
			}
		},
		showTip: function(tt){
			var scope = this;
			try{
				if((tt.nodeName.toLowerCase() == "textarea" || tt.nodeName.toLowerCase() == "input") && tt.name){;
					if(this.tips[tt.name] && tt.value == ""){
						tt.empty = true;
						if(this.tips[tt.name]["value"]){
							if(tt.orginal_type != tt.type){
								/**
								  * IE issue
								*/
								var type = tt.type;
								var input = document.createElement('input');
								input.onfocus = function(){
									scope.hideTip(this, type);
								};
								input.name = tt.name;
								input.className = tt.className;
								input.id = tt.id;
								input.type = tt.orginal_type;
								
								jQuery(tt).replaceWith(input);	
								tt = input;
							}
							tt.value = this.tips[tt.name]["value"];
						}
					}else 
						tt.empty = false;
				}
			}catch(ex){
				if(debug) alert(ex);	
			}
		},
		isDefaultValue: function(tt){
			try{
				this.hideTip(tt);
				this.showTip(tt);
				if(tt.empty) return true;
				else 
					return false;
			}catch(ex){
				if(debug) alert(ex);				
			}
			return true;
		},
		hasDefaultValue: function(tt){
			try{
				if(tt.default_value == tt.value || jQuery.trim(tt.value) == '') return true;
				else 
					return false;
			}catch(ex){
				if(debug) alert(ex);				
			}
			return true;
		},
		resetAllForms: function(a){
			var forms = jQuery("form");
			for(var i=0; i<forms.length; i++)
				forms[i].reset();	
		},
		_setDefaultParams: function(){
			var elements = jQuery('[type=text]');
			for(var i=0; i < elements.length; i++){
				try{
					if(elements[i].default_value == undefined) elements[i].default_value = elements[i].value;	
				}catch(ex){ }
			}
		}
	});
	
	jQuery(document).ready(function() {
		if(jQuery.estilo == undefined)
			jQuery.estilo = new Object();	
		if(jQuery.estilo.tools == undefined)
			jQuery.estilo.tools = new Object();	
			
		jQuery.estilo.tools.form = new EstiloFormUtilities(); // singleton instance
	});
})(jQuery);
