$(document).ready(function() {
	//Open External Links in a New Window/tab
	$('a[rel*=external]').click( function() {
		this.target = "_blank";
	});
	$('body').addClass('hasJS');
	$('.hideNoJS').show();
	// Colorbox
	$('.youtube').colorbox({iframe:true, innerWidth: 475, innerHeight:344});
	//In-Field Labels
	$("div#leftSide label").addClass('absolute');
	$("div#leftSide label").inFieldLabels();
	$("div#rightSide label").addClass('absolute');
	$("div#rightSide label").inFieldLabels();
	$("div#sidebar label").addClass('absolute');
	$("div#sidebar label").inFieldLabels();
	// jQuery Cycle Plugin (withOUT Transition Definitions)
    $('#banner').cycle({
		cleartypeNoBg: true, // enable cleartype corrections
		timeout:       4500,  // milliseconds between slide transitions (0 to disable auto advance)
		pause:         true      // true to enable "pause on hover" 
	});
	// Contact Form Functions			   	
	$("div.loading").hide();
	$("div#thanks").hide();
	$("div#spammer").hide();
	$("form.retry").hide();
	$("div#noMoreChances").hide();
	
	jQuery.validator.addMethod("phone", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1(.|-)?)?(\([2-9]\d{2}\)|[2-9]\d{2})(.|-)?[2-9]\d{2}(.|-)?\d{4}$/);
	}, "Invalid Phone Number");	
	jQuery.validator.messages.required = "Required";
	$("form.validate").validate({
		messages: {
			email: {
				email: "Invalid Email Address"
			}
		}
	});
	//jQuery FAQ
	$('#faq').makeFAQ();
	var url = window.location.href;
	if (url.match('the-unico-system')) {
		$("div.unico").show();
		$("div.unico").prev().addClass('faqopened');
	}
	if (url.match('bryant-heating-cooling-systems')) {
		$("div.bryant").show();
		$("div.bryant").prev().addClass('faqopened');
	}
	if (url.match('north-american-technician-excellence')) {
		$("div.nate").show();
		$("div.nate").prev().addClass('faqopened');
	}
	if (url.match('air-conditioning-contractors-of-america')) {
		$("div.acca").show();
		$("div.acca").prev().addClass('faqopened');
	}
	if (url.match('bradford-white-corporation')) {
		$("div.bradford").show();
		$("div.bradford").prev().addClass('faqopened');
	}
	if (url.match('nadca')) {
		$("div.nadca").show();
		$("div.nadca").prev().addClass('faqopened');
	}

	//Careers Form
	$('p.closeSection').show();
	$('form#career fieldset div').hide();
	$('form#career fieldset p.wide').hide();
	$('form#career fieldset').addClass('closed');
	$('form#career fieldset h2').toggle(function() {
		$(this).parent().children('div').show();
		$(this).parent().children().children('div').show();
		$(this).parent().children('p.wide').show();
		$(this).parent().children().children('p.wide').show();
		$(this).parent().removeClass('closed').addClass('open');
		$('form#career div#terms-of-application').hide();
		$('form#career div#equal-opportunity-employer').hide();
	}, function() {
		$(this).parent().children('div').hide();
		$(this).parent().children('p.wide').hide();
		$(this).parent().removeClass('open').addClass('closed');
	});
	$('form#career fieldset p.closeSection').click(function() {
		$(this).parent().parent().children('div').hide();
		$(this).parent().parent().children('p.wide').hide();
		$(this).parent().parent().removeClass('open').addClass('closed');
	});
	$('form#career fieldset p.submit input.submit').click(function() {
		$('form#career fieldset div').show();
		$('form#career fieldset p.wide').show();
		$('form#career div#terms-of-application').hide();
		$('form#career div#equal-opportunity-employer').hide();
	});
	$('a.modal').facebox();
	$('a.modal').addClass('link');
	//Google +1 Styling
	if($('.metadata div:first-child').length) {
		$('.metadata div:first-child').css({'margin':'5px','float':'right'});
	}
	
	$('#consumersCustomer').click(function() {
		if($('#consumersCustomer:checked').val() == 1) {
			$('#submit_button').html('<input class="submit" id="freebulb" type="submit" onclick="submitForm();" value="Click and get your kit today!" />');
		} else {
			$('#submit_button').html('');
		}
	});
});

function submitForm() {
	var formType = $('.type').val();
	var sidebar = $('.sidebar').val();
	var postdata = $('#' + formType).serialize();
	var formValid = $('#' + formType).valid();
	
	if(formValid == true) {
		$(".submit").hide();
		$("div.loading").show();
		$.get("/includes/php/mailer.php", {postdata: ""+postdata+""}, function(data){
			if(data.response == 'true') {			
				$("div.loading").hide();
				$("form#" + formType).slideUp("slow");
				$("div#spammer").show();
				$(".submit").show();
				$("form.retry").slideDown("slow");
			} else {
				$("form#" + formType).slideUp("slow");
				$("div#thanks").slideDown("slow");
				if (sidebar == 'sidebar') {
					_gaq.push(['_trackPageview', '/' + sidebar + 'Submitted']);
				} else {
					_gaq.push(['_trackPageview', '/' + formType + 'Submitted']);
				}
			}
		}, "json");
	}
}

function submitRetryForm() {
	var formType = $('.type').val();
	var sidebar = $('.sidebar').val();
	var answer = $('#answer').val();
	var postdata = $('#' + formType).serialize();

	$(".submit").hide();
	$("div.loading").show();
	$.get("/includes/php/mailer.php", {postdata: ""+postdata+"", answer: ""+answer+"", retry: "true"}, function(data){
		if(data.response == 'failure') {
			$("div.loading").hide();
			$("form.retry").slideUp("slow");
			$("div#noMoreChances").slideDown("slow");	
			$("div#spammer").hide();
		} else {
			$("form.retry").slideUp("slow");
			$("div#spammer").hide();
			$("div#thanks").slideDown("slow");
			if (sidebar == 'sidebar') {
				_gaq.push(['_trackPageview', '/' + sidebar + 'Submitted']);
			} else {
				_gaq.push(['_trackPageview', '/' + formType + 'Submitted']);
			}
		}
	}, "json");
}

/** 
 * For gift certificate mailer 
 */

$('form#certificate').submit(function(e) {
	IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	if(IE7 === true) return true;
	e.preventDefault ? e.preventDefault() : e.returnValue = false;
	//e.preventDefault();
	var formValid = $('#certificate').valid();
	
	if(formValid == true) {
		$('#submit_contact').html('<img src="/images/loading.gif" alt="Loading..." title="Loading..." class="right submitLoading" />');
		var postdata = $(this).serialize();
		$.post("/mailhandler.php", {data: postdata, type: 'ajax'}, function(data) {
			if(data.success == true) {
				var html = $('#cert_link').html();
				$('#contact_form').fadeOut(1000);
				$('#success_message').fadeIn(1500);
				$('#cert_link').html(html + '<a target="_blank" class="button" href="/lp/certificate.php?id=' + data.pub_id + '" title="Click here to download your gift certificate">Download and print it here</a>');
				$('.arrow').hide();
				_gaq.push(['_trackPageview', '/certificateSubmitted']);
			} else if(data.success == 'dupe') {
				alert('We\'re sorry, it appears you\'ve already collected your $50 Gift Certificate.');
			} else {
				$('html, body').animate({ scrollTop: $("#error_message").offset().top }, 500);
				$('#error_message').fadeIn(1000);
				$('#submit_contact').html('<input type="submit" class="submit" value="Submit Form" />');
			}
		}, "json");
	}
	
	return false;
});

/** 
 * For gold plan plus mailer 
*/

$('form#gold_plan').submit(function(e) {
	IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	if(IE7 === true) return true;
	e.preventDefault ? e.preventDefault() : e.returnValue = false;
	//e.preventDefault();
	var formValid = $('#gold_plan').valid();
	
	if(formValid == true) {
		$('#submit_contact').html('<img src="/images/loading.gif" alt="Loading..." title="Loading..." class="right submitLoading" />');
		var postdata = $(this).serialize();
		$.post("/gold_mailhandler.php", {data: postdata, type: 'ajax'}, function(data) {
			if(data.success == true) {
				$('#contact_form').fadeOut(1000);
				$('#success_message').fadeIn(1500);
				_gaq.push(['_trackPageview', '/goldplanSubmitted']);
			} else {
				$('html, body').animate({ scrollTop: $("#error_message").offset().top }, 500);
				$('#error_message').fadeIn(1000);
				$('#submit_contact').html('<input type="submit" class="submit" value="Submit Form" />');
			}
		}, "json");
	}
	
	return false;
});

