// JavaScript Document

$(function() {
	$('input[placeholder], textarea[placeholder]').placeholder();
	//FORM MESSAGE
	$('#form-message').live('click',function(e) {
		$(this).slideUp(function() {
			$(this).remove();
		});
	});
	
	//TICKER CYCLE
	$('#ticker .rotate').cycle({
		fx : 'fade',
		timeout : 6000,
		speed : 1000
	});
	
	//TESTIMONIALS CYCLE
	$('#customer-testimonials .rotate').cycle({
		fx : 'fade',
		timeout : 6000,
		speed : 2000
	});
	
	$('#choose-your-section select').change(function() {
		var val = $('option:selected',this).val();
		window.location.href = val;
	});
	
	$('#content.contact > .questions > li > .showQuestion').click(function() {
		$('.answer',$(this).parent()).slideToggle();
		return false;
	});


	$('#referrals-calculator input[name=num_clients], #referrals-calculator input[name=avg_invoice]').bind('keyup',function(e) {
		updateTotalEarnings();
	});
	var updateTotalEarnings = function() {
		var numClients = $('#referrals-calculator input[name=num_clients]').val();
		var avgInvoice = $('#referrals-calculator input[name=avg_invoice]').val();
		if(numClients=='NaN' || numClients=='') { numClients = 0; }
		if(avgInvoice=='NaN' || avgInvoice=='') { avgInvoice = 0; }
		var totalEarnings = parseInt(numClients)*(parseInt(avgInvoice)*.10);
		$('#referrals-calculator input[name=total_earnings]').val('$'+totalEarnings);
	};

	$('#referrals-faq > li > .showQuestion').click(function() {
		$('.answer',$(this).parent()).slideToggle();
		return false;
	});
	$('#referrals-form .section .section-header input[name=add-more-referrals]').live('click',function(e) {
		e.preventDefault();
		var additionalReferrals = $('#additional-referrals');
		var moreReferrals = $('#referrals-form .more-referral');
		var nextCount = moreReferrals.length;
		var newCol = moreReferrals.last().clone();
		newCol
			.attr('id','referral-'+nextCount)
			.appendTo(additionalReferrals);
		$('.name-col input[type=text]',newCol).attr('name','referral['+nextCount+'][name]');
		$('.phone-col input[type=text]',newCol).attr('name','referral['+nextCount+'][phone]');
		$('.relationship-col input[type=text]',newCol).attr('name','referral['+nextCount+'][relationship]');
		$('input[placeholder], textarea[placeholder]',newCol).placeholder();
	});
	$.fn.verifyClient = function() {
		var isClient = ($(this).val()=='Yes' && $(this).hasClass('member-check-yes') && $(this).is(':checked')) ? 1 : 0;
		if(!isClient) {
			alert('You have to be an existing client of Beverly Hills Carpet Cleaners to quality for this program.\nIf you are unsure we can check our system, give us a call at (310) 621-4856');
			return false;
		}
		else { return true; }
	};
	$('#referrals-form input.member-check-no').live('click',function(e) {
		var verify = $(this).verifyClient();
		if(!verify) { return false; }
	});
	$('#referrals-form form').bind('submit',function(e) {
		var verify = $('input.member-check-yes',this).verifyClient();
		if(!verify) { return false; }
	});
	
});
