$(document).ready(function() {
	$("input[name='shippingOption']").click(function() {
		$(".shippingCost").hide();
		$(".shippingCost-usa").hide();
		$(".shippingCost-international").hide();
		
		if($(this).val() == "USA")
			$(".shippingCost-usa").show();
		else if($(this).val() == "International")
			$(".shippingCost-international").show();
		else
			$(".shippingCost").show();
			
		$(".checkout").removeClass("disabled");
		$(".checkout").removeAttr("disabled");
	});
	
	$(".checkoutButton").click(function() {
		if($(".checkout").attr("disabled")) {
			$(".error").hide();
			$(".notice").hide();
			$(".error").html("Please select a shipping option before checkout.");
			$(".error").fadeIn("slow");
		}
	});
	
	$("select[name='billing_state']").change(function() {
		if($(this).val() == "Outside")
			$(".billingProvince").show();
		else
			$(".billingProvince").hide();
	});
	
	$("select[name='shipping_state']").change(function() {
		if($(this).val() == "Outside")
			$(".shippingProvince").show();
		else
			$(".shippingProvince").hide();
	});
	
	$(".listing").click(function() {
		if($(this).find("ul").hasClass("hidden")) {
			$(this).find("ul").slideDown("slow");
			$(this).find("ul").removeClass("hidden");
		} else {
			$(this).find("ul").slideUp("fast");
			$(this).find("ul").addClass("hidden");
		}				
	});
	
	$(".shippingApo").click(function() {
		$("#shipApo").slideDown("slow");
	});
	
	$("a[rel^='prettyPhoto']").colorbox({
		photo: true
	});
	
	var latestBlogHeight = $("#latestBlog").height();
	if(latestBlogHeight != null)
		$("aside").css("paddingTop", latestBlogHeight-255);
	
	$(".content img").each(function() {
		if($(this).css('float') == "right")
			$(this).addClass("right");
		else if($(this).css('float') == "left")
			$(this).addClass("left");
	});
	
	/**
	 * DIV Equal Height
	 *
	 * Any divs with equal heights will be given
	 * the height of the tallest div.
	 */
	var equalHeight = 0;
	$(".column").each(function(){
		thisHeight = $(this).height();
		if(thisHeight > equalHeight) {
			equalHeight = thisHeight;
		}
	}).height(equalHeight);
	
	/**
	 * FAQ Answer Toggle
	 *
	 * Adds click event to question. When clicked,
	 * answer toggles current state. Either slides
	 * down to show, or slides up to hide. Also
	 * scrolls the page to put the question at the
	 * top.
	 */
	$(".faq-Question").click(function() {
		var faqID = $(this).attr("href");
		$(faqID).slideToggle(400);
		$.scrollTo(this, 1000);
		return false;
	});
	
	/**
	 * Form input classes
	 * 
	 * Applys classes to form items to easily apply
	 * css to form items.
	 */
	$('input:text').addClass('inputText');
	$('input:password').addClass('inputPassword');
	$('input:checkbox').addClass('inputCheckbox');
	$('input:radio').addClass('inputRadio');
	$('textarea').addClass('textarea');
	$('select').addClass('select');
	$('input:submit').addClass('inputSubmit');
	$('input:image').addClass('inputImage');
	
	/**
	 * Default form text
	 * 
	 * Applys click event to clear default text on any
	 * input with class .default. Also adds the default
	 * value in the data object of the input so when
	 * the form is submited, the default value is not
	 * sent to the server.
	 */
	$('input.default').each(function() {
		var input = $(this);
		input.data("val", input.val());
		input.click(function() {
			if(input.val() == input.data("val")) {
				input.val("");
				input.removeClass("default");
			}
		});
	});
	$('form').submit(function() {
		var input = $(this);
		input.find('input.default').each(function() {
			input.val("");
		})
	});
	
	window.fbAsyncInit = function() {
	  FB.init({appId: '127471297263601', status: true, cookie: true,
	           xfbml: true});
	};
	(function() {
	  var e = document.createElement('script'); e.async = true;
	  e.src = document.location.protocol +
	    '//connect.facebook.net/en_US/all.js';
	  document.getElementById('fb-root').appendChild(e);
	}());
});
