(function($){
	/*
		$(function(){
			$('.validate').wsValidate();
		});
	*/
	$.fn.wsValidate=function(o){
		return new $ws(this,o);
	};
	$.ws.wsValidate=function(input,o){
		this.o=$.extend({
			alert:{
				empty:'Empty field(s)',
				media:'Wrong file type',
				email:'Invalid email address',
				str:'Not a String',
				num:'Not a Number',
				empty:'Empty field'
			},
			input:input,
			error:0,
			height:0,
			re:{
				empty:/^$/,
				email:/^[\w\d\._\-\!\#\$\%\&\'\*\+\/\=\?\^`\{\}\|\~]+@[\w\d-\.]+\.\w{2,4}(\.\w{2})?$/,
				num:/^\d+$/,
				str:/^[a-z\-\'_ \?]+$/i,
				media:/\.(jpe?g|png|gif|avi|mov|3gp|wmv|mpe?g|wav|mp4|mp3|aiff|aac|wma|flv|doc|txt)$/ig,
				line:/\\n|\\r/g
			}
		},o||{});
		this.init(this.o);
	};
	var $ws=$.ws.wsValidate;
	$ws.fn=$ws.prototype={wsValidate:'0.0.2'};
	$ws.fn.extend=$.extend;
	$ws.fn.extend({
		init:function(o){
			o.input=$('*[@type!=hidden]',o.input).filter(':input[@type!=submit]').filter(':input[@type!=reset]');
			if(!o.error&&o.input.length>0){
				$(o.input).each(function(i,v){
					var e=$(v);
					if(e.is('.req')){
						if(!e.is('.media')){
							v.value=v.value.replace(o.re.line,'');
						}
						if( v.value.match(o.re.empty) ){
							$ws.fn.error(v,o.alert.empty,o);
						}
						else if( e.is('.email') && !v.value.match(o.re.email) ){
							$ws.fn.error(v,o.alert.email,o);
						}
						else if( e.is('.str') && !v.value.match(o.re.str) ){
							$ws.fn.error(v,o.alert.str,o);
						}
						else if( e.is('.num') && !v.value.match(o.re.num) ){
							$ws.fn.error(v,o.alert.num,o);
						}
						else if( e.is('.media') && v.value.search(o.re.media)==-1 ){
							$ws.fn.error(v,o.alert.media,o);
						}
						return o;
					}
				});
			}
			return;
		},
		error:function(v,a,o){
			if(!o.error){

				o.error=1;

				if(!o.enhanced){

					var f=$(v).parents('.validate').find('label').removeClass('error').end();

					if(f.children('.status').length==0){
						$('<div class="status"><span></span></div>').prependTo(f);
					}

					var e=$('.status',f).fadeIn();
					var m=e.children('span').text(a);
					var s=f.find('input[@name=submit]').attr('disabled',true);

					o.height=(o.height<=0)?m.height():o.height;

					m.height(0).animate({height:o.height},'slow');

					var l=f.find('label[@for='+v.id+']').addClass('error');

					var status=setTimeout(function(status){
						o.error=0;
						e.fadeOut();
						m.animate({height:'toggle'},'slow');
						l.removeClass('error');
						s.attr('disabled',false);
						return clearTimeout(status);
					},3000);

					v.focus();

				}
				else {
					alert(a);
				}
			}
			return o;
		}
	});
})(jQuery);
