$(function(){
		   
	$('div.repeat_box').hide();
	 $('div.repeat_box:first').fadeIn('slow');
	
 	$('#nav li').removeClass('active');
	$("#nav li").eq(0).addClass("active");
	$('#nav li').bind('click', function() {
		$('#nav li').removeClass('active');
		$('div.repeat_box').hide();
		var curRel = $('a', $(this)).attr('rel');
		$(this).addClass('active');
		$('#' + curRel).fadeIn('slow');
		return false;
 });

	$('#show-demo-form').bind('click',function(){
		if ($(".details").is(":hidden")) {
			$('.form-close').show();
			$("div.details").slideDown("slow");
			$('#show-demo-form').hide();
			$('#submit-demo-form').show();
		} else {
			$('#show-demo-form').show();
			$('#submit-demo-form').hide();
		}
	});

	$('.form-close').bind('click', function() {
		$("div.details").slideUp('slow');
		$('.form-close').hide();
		$('#submit-demo-form').hide();
		$('#show-demo-form').show();


	});
	
	$('.open-form').bind('click',function(){
		if ($(".details").is(":hidden")) {
			$('.form-close').show();
			$("div.details").slideDown("slow");
			$('#show-demo-form').hide();
			$('#submit-demo-form').show();
		} else {
			$('#show-demo-form').show();
			$('#submit-demo-form').hide();
		}
	});
	

	$('#submit-demo-form').bind('click', function() {
		var shared_email = $.trim($('#email').val());
		if ( shared_email.length > 0 ) {
			$.get("/munchkin_key.php?email=" + shared_email, function(mkey) {
				var name_parts = $.trim($('#name').val()).split(' ');

				var description = $.trim($('#question').val());
				if ( $('#broker').attr('checked') ) {
					description += '...is broker';
				}
				if ( $('#landlord').attr('checked') ) {
					description += '...is landlord';
				}

				var company = $.trim($('#company').val());
				company = (company.length === 0) ? 'None provided' : company;

				description += window.location.href.indexOf('/pricing') != -1 ? 'cHJpY2luZw' : 'ZGVmYXVsdA';

				var munch_opts = {
					FirstName: name_parts[0],
					LastName: name_parts[1],
					Company: company,
					Phone: $.trim($('#phone').val()),
					Email: shared_email,
					Description: description
				};

				var sales_url = "/sales_trigger.php?email=" + shared_email + '&name=' +  $.trim($('#name').val()) + '&company=' +  $.trim($('#company').val()) + '&phone=' +  $.trim($('#phone').val()) + '&description=' +  description;
				$.get(sales_url, function(rv) {
					$('#footer').append(rv);
				});

				var was_sent = mktoMunchkinFunction('associateLead',munch_opts, mkey);

				alert('Your form has been submitted and we will be contacting you shortly.'); // this should be a fancyzoom overlay
				//clean out...
				$('#submit-demo-form').hide();
				$('#show-demo-form').show();
				$("div.details").slideUp('slow');

				$('#name').val('');
				$('#phone').val('');
				$('#email').val('');
				$('#company').val('');
				$('#question').val('');
				$('#broker').attr('checked',false);
				$('#landlord').attr('checked',false);


			});
		} else {
			alert('Please enter an email.');
		}
	});

});

