
jQuery.fn.blink = function() {
	// this.queue('blink');
	this.slideDown('fast');
	this.fadeIn('fast');
	this.fadeOut('fast');
	this.fadeIn('fast');
	this.fadeOut('fast');
	this.fadeIn('fast');
	this.fadeOut('fast');
	this.fadeIn('fast');
}

jQuery.fn.validityNotice = function(message) {
	var noticeGen = function(msg) {
		return $(document.createElement('div'))
			.append($(document.createElement('span'))
				.addClass('validity-notice')
				.text(msg))
			.hide();
	}	
	var notice = noticeGen(message);
	// Add notice after field, or after label if the label is after the field (for checkboxes/radios)
	(this.next().is('label')?this.next():this).after(notice);
	this.one('click', function() {
		$(notice).remove();
	});
	$(notice).blink();
}

jQuery.fn.checkValidity = function(options) {
	
	var settings = jQuery.extend({
		notify: true,
		namedMessages: true,
		emailRegExp : /^\S+@\S+$/i
	}, options);
	var messages = {
		requiredNamed: '%s er påkrevd',		
		required: 'Feltet er påkrevd',
		email: 'E-postadressen er ikke gyldig'
	}
	
	
	var isValid = true;
	this.each(function() {
		var tag = this.nodeName.toLowerCase();
		var q = $(this);
		var type = q.attr('type');
		type = (typeof type == 'string')?type.toLowerCase():'';
		var labelq = $('label[for="'+this.id+'"]');
		// var label = labelq.get(0);
		// var labelName = labelq.text();
		var fieldName = q.attr('title');
		var message = '';
		
		if (q.attr('disabled')) return true;
		
		// Remove old validity notices
		// Todo: sync with validityNotice
		if (tag=='form') $('.validity-notice', this).remove();
		
		// -- Find errors --
		if ((tag=='form') && !$('input,select,textarea', this).checkValidity(settings)) 
		{
			isValid = false;
		}
		else if ((tag=='input') && (type=='radio') && 
			//q.attr('required') && // Can't use .attr to check for existance in IE - FML
			this.getAttributeNode('required') && 
			$('input[name="'+this.name+'"]:checked').size()==0)
		{
			isValid = false;
		}
		else if ((tag=='input'||tag=='textarea'||tag=='select') &&
			// (q.attr('required') && 
			(this.getAttributeNode('required') && 
			!q.val())) 
		{
			isValid = false;
			if (settings.namedMessages && fieldName)
				message = messages.requiredNamed.replace(/\%s/, fieldName);
			else
				message = messages.required;
		}
		else if ((tag=='input') && (type=='checkbox') && 
			// q.attr('required') && 
			this.getAttributeNode('required') && 
			!q.attr('checked'))
		{
			isValid = false;
			//
			// if (settings.namedMessages && fieldName)
			// 	message = messages.requiredCheckboxNamed.replace(/\%s/, fieldName);
			// elsev
			message = messages.required;			
		}
		else if ((tag=='input' && q.attr('type').toLowerCase()=='email') &&
		 	(!settings.emailRegExp.test(q.val())) ) 
		{
			isValid = false;
			message = messages.email;

		}
		
		// -- Display a notice --
		if (settings.notify && !isValid && message) {
			q.validityNotice(message);
		}
	});
	
	
	
	return isValid;
}

$(document).ready(function() {
	
	// -- Placeholders --
	// if (!jQuery.browser.webkit)
		// $('input').placeholder();
	$('input').each(function() {
		// if (!$(this).attr('placeholder'))
			// return;
		if (!this.getAttributeNode('placeholder'))
			return;
		if (!(jQuery.browser.webkit)) {
			if (this.value=="") {
				this._placeholder = true;
				$(this).val($(this).attr('placeholder')); 
				// TODO: Investigate: In opera, .css can't be chained here o_O (TypeError)
				$(this).addClass('placeholder');
			}
			$(this).focus(function(){ 
				if($(this).val() == $(this).attr('placeholder'))
				{
					this._placeholder = false;
					$(this).val('')
					$(this).removeClass('placeholder');
				}
			});

			$(this).blur(function(){
				if($(this).val() == '')
				{
					this._placeholder = true;
					$(this).val($(this).attr('placeholder')); 
					$(this).addClass('placeholder');
				}
			});
		}
	});
	
	// -- Form validation --
	$('form').submit(function(evt) {
		// evt.preventDefault();
		$('input', this).each(function() {
			if (this._placeholder)
				$(this).val('');
		});
		
		// if (!this._ignoreValidation)
		if (!jQuery.browser.opera && !$(this).checkValidity()) {
			evt.preventDefault();
			return false;
		}
	});
	
	// $('form').submit(function(evt) {
	// 	alert('bla');
	// 	evt.preventDefault();
	// 	return false;		
	// });
});
