var Pay = {
	values: {},
	click_counter: {},
	timeout: {},
	ajax: {},
	// changeQuantity: function(input) {
	// 	if (input.value == '') return false;
	// 	if (!(input.name in Pay.values)) {
	// 		Pay.values[input.name] = '';
	// 		Pay.click_counter[input.name] = 0;
	// 	}
	// 
	// 	if (Pay.values[input.name] == input.value) { // dont continue if value didnt change
	// 		return false;
	// 	}
	// 	Pay.values[input.name] = input.value;
	// 	Pay.click_counter[input.name]++;
	// 
	// 	Pay.timeout[input.name] = setTimeout(function() {
	// 		Pay._changeQuantity(input, Number(Pay.click_counter[input.name]));
	// 	}, 200);
	// },

	_changeQuantity: function(input, click_count) {
		input = $(input);
		var name = input.attr('name');
		if (Pay.click_counter[name] == click_count) {
			Pay.stopAjax(name);
			clearTimeout(Pay.timeout[name]);
			var attr = name.substr(9).split('_');
			Pay.ajax[name] = Pay._submitQuantityChange(attr[0], attr[1], attr[2], input.val());
		}
		return false;
	},

	// removeItem: function(product_id, size_id, color_id) {
	// 	Pay._submitQuantityChange(product_id, size_id, color_id, 0);
	// },

	_submitQuantityChange: function(product_id, size_id, color_id, quantity, options) {
		var input = $(['#quantity', product_id, size_id, color_id].join('_'));
		$(input).next().show();
		return $.post('/shop/basket/quantity/' + [product_id, size_id, color_id, quantity].join('/')).complete(function() {
			$(input).next().hide();
			delete(Pay.ajax[input.name]);
		});
	},
	
	toggleItemsLoader: function(state) {
		$('#items_loading')[state]();
	},

	stopAjax: function(id) {
		if (id in Pay.ajax && Pay.ajax[id]) {
			try {
				Pay.ajax[id].abort();
			} catch(e) {}
		}
	},
	removeItem: function(element) {
		$(element).find('td').fadeOut(1000, function() { $(this).parent().remove(); });
	}
};


$(function() {
	$('#country_select').change(function(e) {
		var $self = $(this);
		var country_id = Number($self.val());
		if (country_id > 0) {
			Pay.stopAjax('ship');
		  $('#shipping_rates, #total_box, #address_form_cont, #continue_button').hide();
			$('#shipping_loader').show();

			Pay.ajax.ship = $.post('/shop/shipping/find_rates/' + country_id).success(function() {
				$('#shipping_loader').hide();
				$('#shipping_rates, #address_form_cont').show();
				Symbolika.track('/shop/pay/shipping');
				}).error(function() {
					$('#shipping_loader').hide();
				});
		}
	});

	$('#the_rates li').live('click', function(e) {
		var selected = $(this);
		selected.addClass('clicked');
		selected.siblings().removeClass('clicked');
		Pay.stopAjax('ship');
		$('#select_rate').show();
		// $('#shipping_loader').show();
		Pay.ajax.ship = $.post('/shop/shipping/select/' + this.id.replace('ship_rate_', '')).complete(function() {
			$('#select_rate').hide();
			// $('#shipping_loader').hide();
		});
	});

	$('#use_diff_shipping').live('click', function(e) {
		var checked = this.checked;
		if (!checked) {
			// new Ajax.Request('/shop/pay/remove_shipping_address', {
			// 	onComplete: function() {
			// 		new Effect.Fade('shipping_address');
			// 	}
			// });
			$('#shipping_address').fadeOut();
		} else {
			$('#shipping_address').fadeIn();
		}

		var country_select = $('#country_select');
		var selected = country_select.val();
		var billing_country = $('select[name="billing_address[country_id]"]');

		billing_country.find('option').each(function(i) {
			if (this.value == selected) {
				billing_country[0].selectedIndex = i;
				return false;
			}
		});

		billing_country.attr('disabled', !checked);
	});

	$('.quantity input').keyup(function(e) {
		var input = this;
		var name = input.name;
		if (input.value == '') return false;
		if (!(name in Pay.values)) {
			Pay.values[name] = '';
			Pay.click_counter[name] = 0;
		}

		if (Pay.values[name] == input.value) { // dont continue if value didnt change
			return false;
		}
		Pay.values[name] = input.value;
		Pay.click_counter[name]++;

		Pay.timeout[name] = setTimeout(function() {
			Pay._changeQuantity(input, Number(Pay.click_counter[name]));
		}, 200);
	});

	// #items.editable .img img:dblclick, 
	$('#items.editable .quantity span').click(function(e) {
		var image = this.nodeName == 'IMG' ? $(this) : $(this).next('input');
		var ids = image.attr('id').split('_');
		Pay._submitQuantityChange(ids[1], ids[2], ids[3], 0);
	});

	$('.the_button').click(function() {
	  var timeout = 0;
	  if (this.id == 'submit_payment_form') {
	    timeout = 5000;
	    Symbolika.track('/shop/pay/paypal');
	  }
    // loader
    var loader = $('#' + this.id + '_loader');
    if (loader.length) {
  	  $(this).hide();
  	  loader.show();
    }

	  var form_id = this.id.replace(/^submit_/,  '');
	  setTimeout(function() {
			$('#' + form_id).submit();
		}, timeout);
	});

	$('#remove_coupon, #try_again').live('click', function(event) {
		$('#coupon_loading').show();
		$.post(this.href);
		return false;
	});

	$('#coupon_form').live('submit', function() {
		$('#coupon_message').hide();
		$('#coupon_loading').show();
		$.post(this.action, $(this).serialize()).complete(function() {
			$('#coupon_loading').hide();
		});
		return false;
	});
});

