//TODO: Finjustera och lägg till validatorer
var Validate = (function(V, $) {	
	var options = {},
	validators = {
		email : function(value)  { 		
			return value.match(/^[0-9A-Za-z._%+-]+@[0-9A-Za-z.-]+\.[A-Za-z]{2,6}$/) !== null;		
		},
		standard : function(value) {
			return $.trim(value).length > 2;
		}
	},
	
	tooltip = function(el, text) {		
		var qtip = el.qtip({
			content: text, 
			position: {				
				corner: {					
					target: 'topLeft',
					tooltip: 'bottomLeft'
				},
				adjust : {
					x : 20
				}
			},
			style : {
				border: {
					width: 2,
					radius: 4,
					color: '#e2590f' 
				},				
				
				background: '#e2590f',				
				fontWeight : 'bold',
				fontFamily : 'Arial',
				color: '#FFF',
				fontSize:10	,				
				tip: {
					corner  : 'bottomLeft',
					size: { x: 10, y: 10 } 
				} 
			},
			show : false,			
			hide: { when: { event: 'unfocus' } }
		}).click(function() {				
			var me = $(this);
			if (me.data('qtip')) {
				me.qtip('destroy');
			}
		}).qtip('api').show({
			effect : {
				type : 'fade',
				length : 200
			}
		});
	};
	
	V.init = function() {			
		$('#commentform input[type=text][name!="url"]').each(function(index, el) {
			var node = $(el);
			options[el.id] = {
				errorMsg : node.attr('tooltip') || 'Invalid value',
				validate : function() {
					var fn = node.attr('validate') || null;				
					if (fn) {
						return result = validators[fn](node.val());					
					}
					return validators.standard(node.val());
				}				
			};
		});
		
		$('#commentform textarea').each(function(index, el) {
			var node = $(el);
			options[el.id] = {
				errorMsg : node.attr('tooltip') || 'Invalid value',
				validate : function() {
					return validators.standard(node.val());
				}				
			};
		});
	};
	
	V.run = function() {					
		var result = function() {
			for (var key in options) {
				var node = options[key],
				el = $('#'+key);
				if (el.length > 0) {	
					if (!node.validate()) {
						tooltip(el, node.errorMsg);
						return false;
					}
				}
			}			
			return true;
		}();
		if (result){
			return true;
		}
		else
			return false;
	};
	
	return V;	
})({}, jQuery);
	
