$(window).load(function(){
	$('#mini_page_wrapper').height($('[order=0]').outerHeight());
});

$(document).ready(function(){
	$('select').focus(function(){
		$(this).css('color', '#000');
	}).blur(function(){
		if($(this).val() == ''){
			$(this).css('color', '#777');
		}
	});
	//Prefilled
	var pArray = $("input[rel='prefilled']");
	$.each(pArray, function(index, element){
		$(element).val(element.title).css('color', '#777').click(function(){
			if(element.value == element.title)
				$(element).val('').css('color', '#000');
		}).blur(function(){
			if(element.value == '')
				$(element).val(element.title).css('color', '#777');
				
		}).focus(function(){
			if(element.value == element.title)
                $(element).val('');
			$(element).css('color', '#000');
		});
	});
	
	$('#business_form').validate({
		rules: {
			first_name: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			last_name: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			state: {
				required: true,
				minlength: 1
			},
			zip: {
				required: true,
				minlength: 5,
				number: true,
				notDefault: true
			},
			email: {
				required: true,
				email: true,
				notDefault: true
			},
			phone: {
				required: true,
				phoneUS: true,
				notDefault: true
			}
		},
		errorClass: 'form_error',
		errorPlacement: function(error, element) {},
		submitHandler: function() {
			$.post("/saveForm", $('#business_form').serialize());
			$('#business_form').empty().append('<div class="thanks">Thank You.<br /><br />An ADT Pulse Representative will be contacting you shortly.</div>');
		}
	});
	
	var i = 0;
	$('#mini_page_wrapper div').each(function(){
		if(i != 0){$(this).css('opacity', 0);}
		$(this).attr('order', i++);
	});
	i = 0;
	$('.page_tabs li').each(function(){$(this).attr('tab', i++);}).click(function(){
		$('.page_tabs li').each(function(){$(this).removeClass('current');});
		$(this).addClass('current');
		$('.mini_page').css('opacity', 0).css('z-index', 0);
		$('[order='+$(this).attr('tab')+']').css('opacity', 1).css('z-index', 1);
		$('#mini_page_wrapper').height($('[order='+$(this).attr('tab')+']').outerHeight());
	});
	
	/* Form Validation */
	$('#home_form').validate({
		rules: {
			first_name: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			last_name: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			state: {
				required: true,
				minlength: 1
			},
			zip: {
				required: true,
				minlength: 5,
				number: true,
				notDefault: true
			},
			email: {
				required: true,
				email: true,
				notDefault: true
			},
			phone: {
				required: true,
				phoneUS: true,
				notDefault: true
			}
		},
		errorClass: 'form_error',
		errorPlacement: function(error, element) {},
		submitHandler: function() {
			$.post("/saveForm", $('#home_form').serialize());
			$('#home_form').empty().append('<div class="thanks">Thank You.<br /><br />An ADT Pulse Representative will be contacting you shortly.</div>');
		}
	});
	$('#footer_form').validate({
		rules: {
			first_name: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			last_name: {
				required: true,
				minlength: 2,
				notDefault: true
			},
			state: {
				required: true,
				minlength: 1
			},
			zip: {
				required: true,
				minlength: 5,
				number: true,
				notDefault: true
			},
			email: {
				required: true,
				email: true,
				notDefault: true
			},
			phone: {
				required: true,
				phoneUS: true,
				notDefault: true
			}
		},
		errorClass: 'form_error',
		errorPlacement: function(error, element) {},
		submitHandler: function() {
			$.post("/saveForm", $('#footer_form').serialize());
			$('#footer_form').empty().append('<div class="thanks">Thank You.<br /><br />An ADT Pulse Representative will be contacting you shortly.</div>');
		}
	});
	
});
