var WaitlistWidget = {
	
	init: function() {
		$('.btn_waitlist').each(function(index) {
			
			
			// Hide quantity and add to cart elements
			// Here we are going to top and search for <tr>
			var tr_el = $(this).parents('tr');
			var td_els= tr_el.children('td');
			td_els.children('input:text').remove();
			tr_el.children('td.sc_right_cell').children('a').remove();

			$(this).bind('click', function(e){
				var action = 'add';
				if ($(this).attr('for_remove') == 1) {
					action = 'delete';
				}
				var _this = $(this);

				$.ajax({
					url: "/waitlist/"+action+"/"+_this.attr('product_id'),
					cache: false,
					success: function(res, d, r){
						if (res instanceof Object) {
							var res = res;
						} else {
							var res = eval("(" +res+")");
						}
						if (res.success) {
							if (res.message) {
								ITc.ui.alert.show(res.message, 'Information');
							}
							
							if (action == 'add') {
								_this.html('Do not email me when back in stock');
								_this.attr('for_remove', '1');
								_this.unbind('click');
								_this.bind('click', e.handleObj.handler);
							} else {
								_this.html('Email me when back in stock');
								_this.attr('for_remove', '0');
								_this.unbind('click');
								_this.bind('click', e.handleObj.handler);
							}
						} else {
							if (res.message) {
								ITc.ui.alert.show(res.message, 'Notice');
							} else {
								ITc.ui.alert.show('Some problems occured, please try later.', 'Error');
							}
						}
					}
				});
			});
		});
	}
};
ITc.onReady(WaitlistWidget.init, WaitlistWidget);
