if ($('div#cpc_content div.section').length) {
	
	var section_num = $('div#cpc_content div.section').length;
	var section_width = $('div#cpc_content div.section').outerWidth();
	
	var offset = $('div.carousel').offset();
	
	var page = 0;
	var pages = Math.ceil(section_num / 4);
	
	if (pages > 1) {
	
		$('div#cpc_content').css({overflow:'hidden'});
		$('div#cpc_content div.inner').css({height:360, width:section_num * section_width, overflow:'hidden', position:'relative', left:0});
	
		$('div.perpage, div.carouselStatus, div.prevButton, div.nextButton').show();
	
		for (var i = 1; i <= pages; i++) { $('div.perpage ul').append('<li><a href="#">' + i + '</a></li>'); }
	
		$('div.perpage ul li:first-child').addClass('first active');
	
		$('div.perpage ul li a').click(function() {
			page = parseInt($(this).html())-1;
			return cpc_update_page_location();
		})
	
		$('div.perpage span').html('1-4 of ' + section_num + ' items');
	
		$('div.prevButton a').click(function() {
			page--;
			return cpc_update_page_location();
		}).show().addClass('inactive');
	
		$('div.nextButton a').click(function() {
			page++;
			return cpc_update_page_location();
		});
	
		if (pages == 1) { $('div.nextButton a').addClass('inactive'); }
	
		$('div.carouselStatus a').click(function() {
			page = 0;
			if ($('div.prevButton:visible').length) {
				$('div#cpc_content div.inner').css({height:'auto', width:700, left:0});
				$('div.perpage ul, div.prevButton, div.nextButton').hide();
				$(this).html('Collapse<img src="/sbd/img/bg/bg_collapse.gif" alt="Collapse" />');
			} else {
				$('div#cpc_content div.inner').css({height:360, width:section_num * section_width, left:0});
				$('div.perpage ul, div.prevButton, div.nextButton').show();
				$('div.perpage ul li').removeClass('active');
				$('div.perpage ul li:nth-child(' + (page+1) + ')').addClass('active');
				$('div.prevButton a').addClass('inactive');
				$(this).html('Expand to See All<img src="/sbd/img/bg/bg_expand.gif" alt="Expand to See All" />');
				$('html, body').animate({scrollTop:offset.top -100}, 'fast');
			}
			return false;
		});
	
	}
	
	function cpc_update_page_location() {
		
		if (page >= 0 && page < pages) {
			
			var left = -(page*(4*section_width));
			
			if (parseInt($('div#cpc_content div.inner').css('left')) == left) { return false; }
			
			$('div#cpc_content div.inner').animate({left:left}, 900, function() {
				
				$('div.perpage ul li').removeClass('active');
				$('div.nextButton a, div.prevButton a').removeClass('inactive');
				
				$('div.perpage ul li:nth-child(' + (page+1) + ')').addClass('active');
				
				if (!page) { $('div.prevButton a').addClass('inactive'); }
				else if (page == pages-1) { $('div.nextButton a').addClass('inactive'); }
			
				var start = (page *4) +1;
				var end = (page+1) * 4;
				if (end > section_num) { end = section_num; }
				if (start == end) { end = ''; } else { end = '-' + end; }
				$('div.perpage span').html(start + end + ' of ' + section_num + ' items');
			
			});
			
		} else if (page < 0) { page = 0; } else if (page >= pages) { page = pages-1; }
		
		return false;
	}
	
}

if ($('a.need_help_deciding').length && $('img.cpc_chart').length) {
	$('a.need_help_deciding').click(function() {
		$(this).toggleClass('cpc_active');
		$('img.cpc_chart').toggle();
		return false;
	});
} else {
	$('a.need_help_deciding').remove();
}

$('p.print_this_page a').click(function() { window.print(); return false; });

$('div.product_row').each(function() {
	
	var resize = false;
	var tallest_height = 0;
	
	$(this).find('div').each(function() {
		if ($(this).outerHeight() > tallest_height) { tallest_height = $(this).outerHeight(); }
		if ($(this).css('background')) { resize = true; }
	});
	
	if (resize) { $(this).find('div').css({height:tallest_height}); }
	
});
