function reloadJs(){
	$('#add_to_cart_button').unbind('click').click(function(){
		$(this).text("adding...");
		var po = $(this).attr('class').match(/po-([^\s]+)/)[1];
		var p = $(this).attr('class').match(/p-([^\s]+)/)[1];
		$.post('/ajax/cart/addproduct', 
		{
			product_option_id : po,
			product_id : p
		}, function(response){
			if(response.match(/too many/)){
				$('#out_of_stock').slideDown();
				$('#add_to_cart_button').text('add to cart');
				
			} else {
				$('#cartArea').hide().html(response).fadeIn(); 
				$('#add_to_cart_button').text('added!');
				setTimeout("$('#add_to_cart_button').text('add to cart');", 600);
			}
		});
	});
	$('select.product_option').unbind('change').change(function(){
		var myKidCount = $(this).attr('id').replace('select_', '');
		$('os' + myKidCount + '_1').val($(this).val().substring($(this).val().indexOf(':')+1));
		var setID = $(this).attr('class').match(/setID-([^\s]+)/)[1];
		var p = $(this).attr('class').match(/p-([^\s]+)/)[1];
		var me = $(this);
		$.post('/ajax/product/newoptions',
		{
			kidcount : myKidCount+1,
			view : 'page',
			product_id : p,
			option_id : me.val()
			
		}, function(response){
			$('#kids_' + setID).html(response);
		});
	});
}
$(document).ready(function() {
	
	reloadJs();
	$(document).ajaxComplete(reloadJs);
	
	if($('select.product_option').length > 0){
		$('select.product_option option:first').attr('selected', 'selected');
	}
	
	
	$('div.productThumb').hover(function(){
		$(this).addClass('show');
	}, function(){
		$(this).removeClass('show');			
	});
	
	$('select[name=per_page]').change(function(){
		var u = window.location.href.match(/&under=\d+/);
		window.location.href = paginate_url+'/page:1&per_page=' + $(this).val() + (u ? u[0] : '');
	})
	
	$('div.relatedProducts .productThumb:first').addClass('first');
	$('a.other-thumb').click(function(){
		var myID = $(this).attr('class').match(/o-([^\s]+)/)[1]; 
		if($('img.main-images:visible').length < 1){
			$('img.other-' + myID).fadeIn('fast');
			return;
		}
		$('img.main-images:visible').fadeOut('fast', function(){
			$('img.other-' + myID).fadeIn('fast');
		});
	});

	$('.overlay').openDOMWindow({ 
		eventType:'click',
		overlayOpacity:'15',
		width:'600',
		borderSize:'1'
	});
	
	if($('#page_content img').length > 0){
		var img = $('#page_content img:first');
		$('#page_image').attr('src', img.attr('src'));
		setTimeout("$('#page_image').fadeIn('fast')",300);
		img.hide();
		if(img.parent()[0].localName == 'p') img.parent().hide();			
	} else if($('#page_image').length > 0){
		// $('#page_image').fadeIn('fast');
	}
	
	$('a.remove_link').click(function(){
		$(this).parents('.item_line:first').find('input.qty').val('0');
		$('input[value=update]').click();
	})
	$('a.close').click(function(){
		$.closeDOMWindow();
	});
});

