var scripttag = document.getElementsByTagName('script')[1];
wwwbase = scripttag.src.match(/(.*)public\/js.*/)[1];

Sweboo.Cart = {
	Init: function(options)
	{
		this.options = {};
		$.extend(this.options, options);
		// add to cart
		$('a#add_to_cart').click(function() {
			Sweboo.Cart.addToCart();
		});
		// remove from cart
		$('a.remove_from_cart').each(function() {
			$(this).click(function() {
				parts = $(this).get(0).id.replace('item_', '').split("_");
				Sweboo.Cart.removeFromCart(parts[0], parts[1]);
			});
		});
		// add to wishlist
		$('a#add_to_wishlist').click(function() {
			Sweboo.Cart.addToWishList();
		});
		// remove from wishlist
		$('a#remove_from_wishlist').click(function() {
			Sweboo.Cart.removeFromWishlist();
		});
		
		//$('#shopping_cart tbody tr:odd').addClass('odd');
		
		$('.remove_from_wishlist').each(function() {
			$(this).click(function() {
				id = $(this).get(0).id.replace('item_', '');
				Sweboo.Cart.deleteFromWishlist(id);
			});
		});
		
		$('.quantity').change(function() {
			key = $(this).get(0).id.replace('quantity_', '');
			price = parseFloat($('#price_' + key).html()) * $(this).val();
			$('#total_' + key).html(price.toFixed(2));
			parts = key.split('_');
			Sweboo.Cart.updateCart(parts[0], parts[1], $(this).val());
		});
		
//		$('#shipping').change(function() {
//			Sweboo.Cart.updateShippingPrice($(this).val());
//		});
	},
	// used in the product view page
	addToCart: function() {
		$('#flash_message').hide('slow');
		if ($('#quantity').val() * 1) {
			parts = $('.product_size.selected').get(0).id.split("_");
			size = parts[0];
			$.ajax({
				url: wwwbase + 'shopping_cart/xhr',
				data: 'method=add_to_cart&id=' + Sweboo.Cart.options.id + '&quantity=' + ($('#quantity').val() * 1) + '&size=' + size,
				success: function(html){
					if (html.indexOf('success') != -1) {
						$('#flash_message').html('<p style="margin-bottom: 5px;">' + Sweboo.Cart.options.add_to_cart_success + '</p>');
						
						// uprate items in cart block count
						quantity = parseInt($('#items_in_cart').html()) + ($('#quantity').val() * 1);
						$('#items_in_cart').html(quantity);
						if (quantity == 1) {
							$('#items_in_cart_labels').html(_translations['SHOPPINGCART']['single_item_label']);
						} else {
							$('#items_in_cart_labels').html(_translations['SHOPPINGCART']['plural_item_label']);
						}
						
						// update free shipping block
						parts = html.split('::');
						if (parts[2] == 0) {
							message = _translations['SHOPPINGCART']['freeshipping_achieved'];
							$('#shopping_bag var').html(message);
							$('#flash_message').append('<var>' + message + '.</var>');
						} else {
							message = _translations['SHOPPINGCART']['freeshipping_away'].replace('%1', currency).replace('%2', parts[3]);
							$('#shopping_bag var').html(message);
							$('#flash_message').append('<var>' + message.replace('<br />', ' ') + '.</var>');
						}
						
						message = _translations['SHOPPINGCART']['freeshipping_amount'].replace('%1', currency).replace('%2', freeshipping);
						$('#flash_message').append('<var>' + message + '.</var>').addClass('green').show('slow');
					}
				}
			});			
		} else {
			message = '<p>' + Sweboo.Cart.options.add_to_cart_error + '</p>'
			$('#flash_message').html(message).removeClass('green').show('slow');
		}		
	},
	
	removeFromCart: function(id, size) {
		$('.flash_messages').hide('slow');
		$.ajax({
			url: wwwbase + 'shopping_cart/xhr',
			data: 'method=remove_from_cart&id=' + id + '&size=' + size,
			success: function(html){
				if (html.indexOf('success') != -1) {
					quantity = parseInt($('#items_in_cart').html()) - ($('#quantity_' + id + '_' + size).val() * 1);
					$('#items_in_cart').html(quantity);
					if (quantity == 1) {
						$('#items_in_cart_labels').html(_translations['SHOPPINGCART']['single_item_label']);
					} else {
						$('#items_in_cart_labels').html(_translations['SHOPPINGCART']['plural_item_label']);
					}					
					$('#cart_item_' + id + '_' + size).hide();
					$('#subtotal').html(parseFloat($('#subtotal').html()) - parseFloat($('#total_' + id + '_' + size).html()));
					$('#total_price').html(parseFloat($('#total_price').html()) - parseFloat($('#total_' + id + '_' + size).html()));
					$('#cart_item_' + id + '_' + size).remove();
					//$('#shopping_cart tbody tr').removeClass('odd');
					//$('#shopping_cart tbody tr:odd').addClass('odd');
					
					if (!$('#shopping_cart tbody tr').length) {
						$('#shopping_cart, #total, .two_columns, #checkout').hide();
						$('#flash_message').html('<p>' + Sweboo.Cart.options.empty_cart + '</p>');
						$('#shopping_bag var').html('');
					} else {
						$('#flash_message').html('<p>' + Sweboo.Cart.options.remove_from_cart_success + '</p>');
						
						parts = html.split('::');
						
						shipping_price = parseInt(parts[2]) == 0 ? 'FREE' : parseFloat(parts[2]).toFixed(2);
						$('#shipping_price').html(shipping_price);
						if (shipping_price == 'FREE') {
							$('#shipping_price_currency').hide();
						} else {
							$('#shipping_price_currency').show();
						}
						
						if (parts[2] == 0) {
							$('#shopping_bag var, #total .info .freeshipping').html(_translations['SHOPPINGCART']['freeshipping_achieved']);
						} else {
							$('#shopping_bag var, #total .info .freeshipping').html(_translations['SHOPPINGCART']['freeshipping_away'].toString().replace('%1', currency).replace('%2', parts[3]));
						}							
						
					}
					$('#flash_message').show('slow');
				}
			}
		});			
	},
	
	// update cart
	updateCart: function(id, size, quantity) {
		$('.flash_messages').hide('slow');
		$.ajax({
			url: wwwbase + 'shopping_cart/xhr',
			data: 'method=update_cart&id=' + id + '&size=' + size + '&quantity=' + quantity + '&shipping=' + $('#shipping').val(),
			success: function(html){
				if (html.indexOf('success') != -1) {
					$('#flash_message').html('<p>' + Sweboo.Cart.options.updated_cart_success + '</p>');
					$('#flash_message').show('slow');
					parts = html.split('::');
					quantity = parts[1];
					if (quantity == 1) {
						$('#items_in_cart_labels').html(_translations['SHOPPINGCART']['single_item_label']);
					} else {
						$('#items_in_cart_labels').html(_translations['SHOPPINGCART']['plural_item_label']);
					}					
					$('#items_in_cart').html(quantity);
					$('#subtotal').html(parseFloat(parts[2]).toFixed(2));
					
					shipping_price = parseInt(parts[3]) == 0 ? 'FREE' : parseFloat(parts[3]).toFixed(2);
					$('#shipping_price').html(shipping_price);
					if (shipping_price == 'FREE') {
						$('#shipping_price_currency').hide();
					} else {
						$('#shipping_price_currency').show();
					}

					if (parts[3] == 0) {
						$('#shopping_bag var, #total .info .freeshipping').html(_translations['SHOPPINGCART']['freeshipping_achieved']);
					} else {
						$('#shopping_bag var, #total .info .freeshipping').html(_translations['SHOPPINGCART']['freeshipping_away'].toString().replace('%1', currency).replace('%2', parts[4]));
					}					
					
					$('#total_price').html((parseFloat(parts[2]) + parseFloat(parts[3])).toFixed(2));
				}
			}
		});			
	},
	
	updateShippingPrice: function(shipping) {
		$('.flash_messages').hide('slow');
		$.ajax({
			url: wwwbase + 'shopping_cart/xhr',
			data: 'method=update_shipping_price&shipping=' + shipping,
			success: function(html){
				if (html.indexOf('success') != -1) {
					$('#flash_message').html('<p>' + Sweboo.Cart.options.updated_cart_success + '</p>');
					$('#flash_message').show('slow');
					parts = html.split('::');
					$('#shipping_price').html(parseFloat(parts[1]).toFixed(2));
					subtotal = parseFloat($('#subtotal').html());
					$('#total_price').html((subtotal + parseFloat(parts[1])).toFixed(2));
				}
			}
		});				
	},
	
	// used in the product view page
	addToWishList: function() {
		$('#flash_message').hide('slow');
		$.ajax({
			url: wwwbase + 'users/xhr',
			data: 'method=add_to_wishlist&id=' + Sweboo.Cart.options.id,
			success: function(html){
				if (html == 'success') {
					$('#flash_message').addClass('green').html('<p>' + Sweboo.Cart.options.add_to_wishlist_success + '</p>');
					$('#flash_message').show('slow');
					$('a#add_to_wishlist').hide();
					$('a#remove_from_wishlist').show();
				}
			}
		});				
	},
	// used in the product view page
	removeFromWishlist: function() {
		$('#flash_message').hide('slow');
		$.ajax({
			url: wwwbase + 'users/xhr',
			data: 'method=remove_from_wishlist&id=' + Sweboo.Cart.options.id,
			success: function(html){
				if (html == 'success') {
					$('#flash_message').addClass('green').html('<p>' + Sweboo.Cart.options.remove_from_wishlist_success + '</p>');
					$('#flash_message').show('slow');	
					$('a#remove_from_wishlist').hide();
					$('a#add_to_wishlist').show();
				}
			}
		});			
	},
	// used in the wishlist
	deleteFromWishlist: function(id) {
		$('.flash_messages').hide('slow');
		$.ajax({
			url: wwwbase + 'users/xhr',
			data: 'method=remove_from_wishlist&id=' + id,
			success: function(html){
				if (html == 'success') {
					$('#flash_message').html('<p>' + Sweboo.Cart.options.remove_from_wishlist_success + '</p>');
					$('#wishlist_' + id).hide('slow');
					$('#flash_message').show('slow');	
					setTimeout(function() {
						$('#wishlist_' + id).remove();
						if (!$('.wishlist_items').length) {
							$('#wishlist_container').hide('slow');
						}
					}, 500);
				}
			}
		});				
	}
}

