$(function() {
	attach_product_view_actions();
});

function attach_product_view_actions() {
	$("a.thumb").mouseover(function() {
		var self = this;
		$("#TB_window #mainimage").attr('src', $(self).find('img').attr('rel'));
		
	});
	
	$("select[name='data[size]']").change(function() {
		// Check quantity in stock
		// tb_show('abc', '/products/index/slings/', null);
	});
	
	$("ul.paging a").click(function() {
		tb_remove_instant();
		var self = this;
		update_dimensions();
		tb_show(null, $(self).attr('href')+"?width="+thickWidth+"&height="+thickHeight, null);
		
		return false;
	});
	
	var options = {
		beforeSubmit: function() {
			if(!$("#InvoiceLineItemSize").attr('value')) {
				alert('Please choose a size');
				return false;
			}
			tb_remove_instant();
			tb_show('Please wait', '#TB_inline?inlineId=progress_add_to_cart&width='+thickWidth+'&height='+thickHeight+'&modal=false' ,null);
		},
		success: function(responseText) {
			// tb_remove_instant();
			tb_show('Shopping Cart', '/shop/cart/?width='+thickWidth+'&height='+thickHeight+'&modal=false', null, true);
			
			// tb_show('Cart', '/invoices/cart/', null, true);
			
			// tb_remove();
		}
	};
	$("#TB_window form.add_to_cart").ajaxForm(options);
	
	$("#TB_window a.continue_shopping").click(function() {
		tb_remove();
		return false;
	});
	
	$("#TB_window a.remove_from_cart").click(function() {
		$("div.shoppingcart").fadeOut();
		var self = this;
		var url = $(self).attr('href');
		$.post(url, {}, function() {
			tb_show('Shopping Cart', '/shop/cart/?width='+thickWidth+'&height='+thickHeight+'&modal=false', null, true);
		});
	
		return false;
	});
	
	$("#InvoiceDiscountCode").change(function() {
		changed = true;
		$("#InvoiceDiscountCodeChanged").val(1);
	});
	
	$("#InvoiceViewForm").submit(function() {
		$("#InvoiceDiscountCode").blur();
	});
	
}