function formShade(el,notify) {
	$('div.alert',el).remove();
	$(el).fadeOut(400, function() {
		if (notify != undefined && notify != '') {
			$(el).before('<div class="notify">'+notify+'</div>');
		}
	});
}
function formReveal(el,str,kf) {
	$('div.notify').remove();
	$(el)
		.fadeOut(200,function() {
			if (kf==true) {
				$(this).prepend(str);
			} else {	
				$(this).html(str);
			}
		})
		.fadeIn(400);
}



$(function() {
	
	var bg = new Image();
	$(bg)
		.load(function() {
			$(this).hide();
			$('#meta').append(this).css('backgroundImage','url(/cr/images/main_bg_2.jpg)');
			//$(this).fadeIn();
		})
		.error(function() {
		})
		.attr('src','/cr/images/main_bg_2.jpg');
	
	
	
	// Form :: Email Subscription
	if ($('form.em-sub').length)
	{
		if ($.browser.msie && $.browser.version <= 7) {
			$('form.em-sub button').click(function() {
				$('form.em-sub').submit();
			});
		}
		var emSubValidate = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formShade(f);
				$.ajax({
					type:'POST',
					url:'/cr/php/processor.php',
					dataType:'json',
					data:{
						action:'em-sub',
						'em-sub':'1',
						jssub:'1',
						fname:$('input#em-sub-fname',f).val(),
						lname:$('input#em-sub-lname',f).val(),
						email:$('input#em-sub-email',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<div class="alert"><h1>Thanks for your interest!</h1><p>'+json.msg+'</p></div>';
						} else {
							m = '<div class="alert"><h1>There was a problem subscribing.</h1><p>'+json.msg+'</p></div>';
						}
						formReveal(f,m);
					},
					error:function(x,t,e) {
						formReveal(f,'<div class="alert"><h1>There was a problem subscribing</h1></div>');
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form.em-sub').validate(emSubValidate);
	}
	
	
	
});
