(function() {
	var yd = YAHOO.util.Dom;
	var ye = YAHOO.util.Event;
	
	function CheckForm(e)
	{
		e = ye.getEvent(e);
		ye.stopEvent(e);
		
		var formVars = this.formCheck.GetFormVars(this.formEl);
		var result   = this.formCheck.Check(formVars);
		
		if(!result.success)
		{
			for(var field in result.errors)
			{
				var errorText = this.formCheck.GetErrorText(field);
				this.ShowTooltip(this.formEl[field], errorText);
				break;
			}
		}
		else if(typeof(this.config.onSuccess) == "function")
		{
			this.config.onSuccess.call(this);
		}
	}
	
	function Init()
	{
		var config = this.config;
		this.formCheck = new FormCheck(config.config);
		
		// Form element
		this.formEl = yd.get(config.form);
		if (!this.formEl) {
			throw "form element not found";
		}
		
		// Submit element
		this.submitEl = yd.get(config.submit);
		if (!this.submitEl) {
			throw "submit element not found";
		}
		
		ye.addListener(this.submitEl, "click", CheckForm, this, true);
		
		// Tooltip element
		var tooltip = {
			visible : false
		};
		tooltip.element = yd.get(config.tooltip);
		if(!tooltip.element)	{
			throw "tooltip element not found";
		}
		
		// Tooltip content element
		tooltip.contentEl = yd.get(config.tooltipContent);
		if(!tooltip.contentEl)	{
			tooltip.contentEl = tooltip.element;
		}
		this.tooltip = tooltip;
		
		// Tooltip close
		tooltip.closeEl = yd.get(config.tooltipClose);
		if(tooltip.closeEl)
		{
			ye.addListener(tooltip.closeEl, "click", this.Hide, this, true);
		}
		
		// Getting all fields which should be validated
		var formCheck = this.formCheck;
		for(var field in formCheck.config.validations)
		{
			var input = this.formEl[field];
			if(!input)
			{
				continue;
			}
			
			switch(input.type)
			{
				case "text":
					ye.addListener(input, "change", ValidateInput, input, this);
					break;
			}
		}
		
		
	}
	
	function ValidateInput(e, input)
	{
		
		var checkResult = this.formCheck.CheckField(input.name, input.value);
			
		if (!checkResult.success) 
		{
			var error = this.formCheck.GetErrorText(input.name, checkResult.errors[0]);
			this.ShowTooltip(input, error);
		}
		else if(checkResult.success)
		{
			this.Hide(null);
		}
	}
	
	FormCheck.ToolTipValidator = function(config)
	{
		this.config = config;
		ye.onContentReady(config.form, Init, this, true);
	}
	FormCheck.ToolTipValidator.prototype = {
		Hide : function(e)
		{
			if(e !== null)
			{
				try
				{
					e = ye.getEvent(e);
					ye.stopEvent(e);
				}
				catch(e)
				{
					
				}
			}
			
			
			if (this.tooltip) 
			{
				this.tooltip.visible = false;
				this.tooltip.element.style.visibility = "hidden";
			}
		},
		ShowTooltip : function(input, text)
		{
			this.tooltip.contentEl.innerHTML = text;
			var xy = yd.getXY(input);
			xy[1] -= this.tooltip.element.offsetHeight;
			yd.setXY(this.tooltip.element, xy);
			this.tooltip.element.style.visibility = "visible";
	
			this.tooltip.visible = true;
		}		
	}
})();
