jQuery(document).ready(function($) {
	// Wrap left and main columns
	$('#sc_col1,#sc_main').wrapAll('<div class="clearing" />');

	// Wrap every pair of .featured_prod in a clearing div
	var pairs = [];
	$('.featured_prod').each(function(i, div) {
	  var i_over_2 = Math.floor(i / 2);
	  if (!pairs[i_over_2]) pairs[i_over_2] = $();
	  pairs[i_over_2] = pairs[i_over_2].add(div);
	});
	$.each(pairs, function(i, p) {
	  $(p).wrapAll('<div class="clearing featured_prod_row" />');
	});
	
	// Open external links in new window
	$('a[rel=external]').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	// Add helper class to last row of featured products
	$('.featured_prod_row:last').addClass('last-row');
	
	// Resize #sc_main window when payment method is selected
	$('.pay_method_row .radio, .pay_method_row label').click(function(){
		$('#sc_main').height($('#sc_main div').outerHeight(true));
	});
	
	// Add helper divs to testimonial page
	$('.customer_testimonials .testimonial').wrapInner('<div class="tm-content" />');
	
	// Make option descriptions pop up on hover
	$('.radio-choice').hover(function(){
		$(this).find('.option-details').toggle();
	});
	
	// Do main banner slideshow
	$('#banner').cycle({
		fx:'fade',
		timeout:4000
	});
	
	// Do slideshow on jwf landing page
	$('.jwf-slideshow').cycle({
		fx:'fade'
	});
	
	if($('#journal-entries').length > 0) {
		$.ajax({
			url: '/journal-entries.php',
			type:'GET',
			dataType:'html',
			beforeSend:function() {
				// $('.ajax-loader').show();
			},
			success:function(data) {
				$('#journal-entries').append(data);
			},
			complete:function(data){
				// $('.ajax-loader').hide();
				// Refire the load event so heights are recalculated accurately.
				// $(window).trigger('load');
			}
		});
	}

});

jQuery(window).load(function(){
	// Equalize heights of box_inner on category listing page
	jQuery('.cat_products .box_row').each(function(){
		jQuery(this).find('.box_inner').equalHeights();
	});
	
	// Make left column and main column the same height
	// Do this after you apply heights to the box_inner divs, so that this new height is accounted for
	jQuery('#sc_col1,#sc_main').equalHeights();
	// jQuery('.callout a').equalHeights();
	
	// Make last nav_block red border extend to the bottom of the page if sc_main is longer
	jQuery('#sc_col1 .nav_block:last').css('margin-bottom','0');
	var sc_col1_height = jQuery('#sc_col1').height();
	var nav_block_heights = 0;
	jQuery('#sc_col1 .nav_block').not('#sc_col1 .nav_block:last').each(function(){
		nav_block_heights += jQuery(this).outerHeight(true);
	});

	// 2px of border on top and bottom of nav_block
	var last_nav_block_height = sc_col1_height - nav_block_heights -2;
	
	jQuery('#sc_col1 .nav_block:last').css('height',last_nav_block_height + 'px');
	
	// Put reCaptcha on pages where applicable
	if(jQuery('#reCaptcha').length > 0) {
		Recaptcha.create("6Lf8zbwSAAAAAJL8uH0ANLFzcW6F5HuqW531n-9V", "reCaptcha",{theme: "red",callback: Recaptcha.focus_response_field});
	}
});
