$(document).ready(function() {

	$.localScroll({ duration: 500 });
	
	$('a.extras, a.quote, a.map, a.terms, a.samples, a#subscribe').fancybox({
		overlayColor: '#000',
		overlayOpacity: 0.4,
		autoScale: false
	});
	
	$('form#contact-form').ajaxForm({ 
      	beforeSubmit: function(){
        	return $('form#contact-form.validate').valid()
      	},
      	success: function(){
      		$('form#contact-form button').after('<img src="/images/global/load.gif" alt="load" width="16" height="16" />');
      		$('form#contact-form button').hide();
      		var email = $('#contact-email').val()
        	$('#contact-form-wrap').load('thanks/contact.php', function(){
        		$('#eutld-eutld').val(email);
        	});
      	}
    });
	
	var firstHeight = $('#about-content').height() + "px";
	var secondHeight = $('#prices-content').height() + "px";
	var thirdHeight = $('#recycling-content').height() + "px";
	var fourthHeight = $('#contact-content').height() + "px";
	
	$('#global ul li.about:not(.active) a').live('click', function(){
		$('#contextual').animate({
			height: firstHeight
		}, 300);
		$('.slide').fadeOut();
		$('#about-content').fadeIn(function(){
			$('#contextual').prepend('<em></em>');
			$('#contextual em').fadeIn();
		});
		$('#global ul li').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
	
	$('#global ul li.prices:not(.active) a').live('click', function(){
		$('#contextual').animate({
			height: secondHeight
		}, 300);
		$('.slide').fadeOut();
		$('#prices-content').fadeIn(function(){
			$('#contextual').prepend('<em></em>');
			$('#contextual em').fadeIn();
		});
		$('#global ul li').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
	
	$('#global ul li.recycling:not(.active) a, p a.recycling, li a.recycling').live('click', function(){
		$('#contextual').animate({
			height: thirdHeight
		}, 300);
		$('.slide').fadeOut();
		$('#recycling-content').fadeIn(function(){
			$('#contextual').prepend('<em></em>');
			$('#contextual em').fadeIn();
		});
		$('#global ul li').removeClass('active');
		$('#global ul li.recycling').addClass('active');
		return false;
	});
	
	$('#global ul li.contact:not(.active) a').live('click', function(){
		$('#contextual').animate({
			height: fourthHeight
		}, 300);
		$('.slide').fadeOut();
		$('#contact-content').fadeIn(function(){
			$('#contextual').prepend('<em></em>');
			$('#contextual em').fadeIn();
		});
		$('#global ul li').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
	
	$('#global ul li.active a, .slide a.close').live('click', function(){
		$('#global ul li').removeClass('active');
		$('.slide').fadeOut();
		$('#contextual').animate({
			height: '0px'
		}, 300);
		$('#contextual em').remove();
	});
	
	$('.card').each(function(){
		var self = $(this)
		var top = parseInt(self.offset().top) - $('#fixed').outerHeight()
		var bottom = top + parseInt(self.outerHeight())
		var nav = $('.'+self.attr('id'))
		$(document).scroll(function(){
			var scroll = $(document).scrollTop() // how far we're down
			if (scroll > top && scroll < bottom ) {
			    nav.addClass('active').siblings('.active').removeClass('active')
			}
		});
	});
	
	var checkHeader = function(event){
		var sky_height = parseInt($('#sky').css('height'))	

		// if we've scrolled past the header's bottom
		if ($(document).scrollTop() > sky_height) {
			$('#fixed').addClass('fixed').css('position', 'fixed');
			$('#cards').addClass('fixed');
			$('#fixed #reveal').show();
		} else {
			$('#fixed').removeClass('fixed').css('position', 'relative');
			$('#cards').removeClass('fixed');
			$('#fixed #reveal').fadeOut();
		}
			
	};
	
	setInterval(checkHeader, 1000)
	
	$(document).scroll(checkHeader);

	
	$('#reveal a, .top a').click(function() {
  		$("body").animate({ scrollTop: 0 }, "slow");
  		return false;
	});
	
	$('a#subscribe').hover(function(){
		$(this).animate({ left: '0px' }, 100);
	}, function(){
		$(this).animate({ left: '-3px' }, 100);
	});
	
	// Campaign Monitor
    $(function()
	{
		$("#subscribe-form button").click(function() {	
			
			// First, disable the form from submitting
			$('form#subscribe-form').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#subscribe-form").attr("action");
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			emailId = "xxxxx";
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#subscribe-form").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "../proxy.php",
				type: "POST",
				data: final,
				success: function(html){
					$("#subscribe-form").hide(); // If successfully submitted hides the form
					$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
				}
			});
		});
	});
	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
				
});