$(document)
.ready(function(){
	$("#contact-link").colorbox({width:"800px", height:425, inline:true, href:"#contact-from-div"});
	
	var jqContactFrom = $('#contact-from');
	
	$('input, textarea', jqContactFrom)
	.focus(function(){
		var jqthat = $(this);
		if( jqthat.parent().hasClass('error') ) return;
		
		var prev = jqthat.prev();
		prev.data( 'width', prev.width() );
		prev.animate({left:- (prev.innerWidth())}, 100);
	})
	.blur(function(){
		var jqthat = $(this);
		var val = jqthat.val();
		var prev = $(this).prev();

		if( jqthat.parent().hasClass('error') ) return;
		
		if( val.length ){
			prev
				.animate({left:0,width:0}, 100)
				.css('border','none');
		}
		else{
			prev
				.animate({left:0,width:'auto'}, 100)
				.css('border','');
		}
	})
	.each(function(){
		var prev = $(this).prev();
		var w = prev.width();
		prev.data( 'width', w );
		prev.data( 'orgwidth', w );
		prev.data( 'txt', prev.children('span').text() );
	});
	
	$('#contact-from')
	.validate({
		onkeyup : false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		rules: {
			txt_name: "required",
			txt_email: {
				required: true,
				email: true
			},
			txt_message: "required"
		},
		submitHandler: function(form) {
			var btn = $('#contact-from-div .rightSide button.btn');
			
			btn
			.attr('disabled','disabled')
			.css('cursor','wait')
			.children('span')
				.text('Sending...');
			
			$(form)
			.ajaxSubmit({
				'dataType' 	: 'json',
				'beforeSubmit' : function( formData, jqForm, options ){
					formData.push({name:'menospam',value:'2'});
					return true;
				},
				'success'	: function(data){
					if( data.response == true ){
						$('#contact-from-div .rightSide')
						.find('ul,p')
							.remove()
						.end()
						.append('<p>Message was sent, I will get back to you very shortly.</p>');
					}
					else{
						$('#contact-from-div .rightSide h2')
						.after('<p>'+ data.response +'</p>')
						
						
						btn
						.attr('disabled','')
						.css('cursor','pointer')
						.children('span')
							.text('Send Away');
					}
				}
			});
		},
		messages: {
			name: "Please specify your name",
			email: {
				required: "We need your email address to contact you",
				email: "Your email address must be in the format of name@domain.com"
			}
		},
		errorPlacement : function( error, element ){
			$(element)
			.prev()
				.children('span')
					.text( error.text() )
				.end()
			.css('width','')
			.css('left',function(){
				return -($(this).width());
			});
		},
		highlight: function(element, errorClass) {
			$(element).parent().addClass('error');
		},
		unhighlight : function( element ){
			var that = $(element)
			var prev = that.prev();
			var w = prev.data('orgwidth');
			prev
				.width( w )
				.css('left', -w)
				.children('span')
				.text( prev.data('txt') );
			that.parent().removeClass('error');
		},
		debug:true
	});
});