$(document).ready(function() {

	// ============ 
	// ! Fancybox   
	// ============ 
	$("a.fancybox").fancybox({
		'padding' : 0
	});
	
	$("a.fancybox_inline").fancybox({
		'hideOnContentClick': false
	});

	// ======================== 
	// ! Site title animation   
	// ======================== 
	$('h1#sitetitle a').hover(
		function() {
			$(this).stop().fadeTo('1000', 0);
		},
		function() {
			$(this).stop().fadeTo('1000', 1);
		}
	);
	
	// ================== 
	// ! Slide down nav   
	// ================== 

	// Add a class to list items on the end of a line
	function add_eols() {
		$('#addon_container li').each(function() {
			// last one?
			if($(this).next().length == 0)
			{$(this).addClass('eol');}
			// could still be last on the line
			else
			{
				if($(this).position().top != $(this).next().position().top)
				{$(this).addClass('eol');}
			}
		});
	}
	
	// Enlarged clickable area for spotlight items
	$("#slide_nav .spotlight_item h3 a").bigTarget({
		hoverClass: 'over', // CSS class applied to the click zone onHover
		clickZone : 'div:eq(0)' // jQuery parent selector
	});

	$('.addons a').click(function() {
		var $link = $(this);
		if($('#slide_nav_content:visible').length > 0)
		{
			$('#slide_nav_content').slideUp(700, function() {$link.removeClass('active');});
		}
		else
		{
			$('#slide_nav_content').slideDown(700);
			add_eols();
			$(this).addClass('active');
		}
		return false;
	});
	
	// =========== 
	// ! Twitter   
	// =========== 
	$("#twitter_link").hover(function() {
		if($("#twitter_link:animated").length == 0) $(this).animate({right: '0'}, 500);
	},
	function() {
		$(this).animate({right: '-173px'}, 500);
	});


	// ================ 
	// ! Support FAQs   
	// ================ 
	$("a.toggle_link").click(function () {
		$(this).siblings('.answer').slideToggle(400, function () {
			// jScrollPanes are currently disabled for initially-invisible codeblocks
		});
		return false;
	});

	// =================== 
	// ! Scrollable code   
	// =================== 
	$('code span:first-child:visible').each(function(index) {
		if($(this).parent('code').length > 0)
		{
			$(this).addClass('jspCustomWrapper');
			$(this).jScrollPane({ mouseWheelSpeed: 5 });
			
			// Add some extra height (to account for the scrollbar height) if a scrollbar is present
			if($('.jspContainer .jspHorizontalBar', this).length > 0)
			{
				$('.jspContainer', this).height( $('.jspContainer', this).height() + 10 );
			}
		}
	});
	
	// ======================== 
	// ! Buy / download forms   
	// ======================== 
	$("#action_toggle a").click(function() {
		$("#buy_items, #download_items").slideToggle();
		if($(this).hasClass('return_link'))
		{
			$(this).html('Download now').removeClass('return_link');
			$("#action_toggle #link_prefix_text").css('visibility', 'visible');
		}
		else
		{
			$(this).html('Return to buy now').addClass('return_link');
			$("#action_toggle #link_prefix_text").css('visibility', 'hidden');
		}
		return false;
	});
	
	$("#download_items .input").focusin(function() {
		if($(this).val() == 'License Code')
		{$(this).val('');}
	});

	$("#download_form").submit(function() {
		var license = $(".input", this).val();
		if(license == '' || license == 'License Code')
		{
			alert('Please enter a valid license code.');
			return false;
		}
	});

});
