var CyStoreCart = new Class({
	Implements: Options,
	options: {
		cartContainer: 'cyCart',
		cartSmallContainer: 'cyCartSmall',
		smallCartUrl: 'includes/smallCart.php',
		cartUrl: 'includes/cart.php',
		orderOutofStock: false,
		checkoutUrl: '/cycle/server/frontend/CyStoreCheckout.php',
		paymentUrl: '/cycle/server/frontend/CyStorePayment.php',
		addItemMessage: 'Varan är lagd i varukorgen.',
		saveItemMessage: 'Varan är sparad.',
		notInStockMessage: 'Varan finns ej i lager.',
		setQuantityMessage: 'Du måste välja ett antal.',
		noProductsInCartMessage: 'Du måste godkänna köpvillkoren.',
		agreeToTermsMessage: 'Du har inga produkter i kundvagnen.',
		postforskottPris: 50,
		invoiceFee: 19
		
	},
	initialize: function(options) {
		this.setOptions(options);
		this.loadCookie();
	},
	loadCookie: function() {
		this.cart = new Hash.Cookie('cyCart',{duration: 14, path: '/'});
		this.cart = this.cart.load();
	},
	addItem: function(articleId, quantity) {
	
		if($("productOption")) var option = $("productOption").get("value");
		else var option = 0;
		
		articleOptionId = articleId+'-'+option;
	
		if(option == 'notInStock' && orderOutofStock == false) alert(this.options.notInStockMessage);
		else if(quantity == 0) alert(this.options.setQuantityMessage);
		else {
		
			if(this.cart.has(articleOptionId)) {
				// Updates the existing cart item if there is one
				var currentCartItem = new Hash(this.cart.get(articleOptionId));
				var currentQuantity = currentCartItem.get('quantity');

				currentCartItem.set('quantity',currentQuantity.toInt()+quantity.toInt());
				this.cart.set(articleOptionId,currentCartItem);
				
			} else {
				// Creates a new cart item
				var cartItem = new Hash();
					cartItem.include('articleId',articleId);
					cartItem.include('option',option);
					cartItem.include('quantity',quantity);
				this.cart.include(articleOptionId,cartItem);
			}
			
			this.loadSmallCart();
			// updates the second smallCart if it exists
			if(cart2) cart2.loadCookie();
			if(cart2) cart2.loadSmallCart();
			alert(this.options.addItemMessage);
			
		}
		
	},
	saveItem: function(articleOptionId, quantity) {
		
		// Updates the existing cart item if there is one
		var currentCartItem = new Hash(this.cart.get(articleOptionId));

		currentCartItem.set('quantity',quantity);
		this.cart.set(articleOptionId,currentCartItem);
		
		this.loadSmallCart();
		this.loadCart();
		
		alert(this.options.saveItemMessage);
		
	},
	removeItem: function(articleOptionId) {

		this.cart.erase(articleOptionId);
		this.loadCart();
		
	},
	buildXML: function() {
		var xml = '<cart>'
		this.cart.each(function(value,key) {
			value = new Hash(value);
			xml += '<cartItem articleId="'+value.get('articleId')+'" quantity="'+value.get('quantity')+'" option="'+value.get('option')+'"></cartItem>';
		});
		xml += '</cart>';
		return xml;
	},
	loadCart: function() {
	
		var noFreight = 'false';
		if($('noFreight')) {
			if($('noFreight').checked) noFreight = 'true';
		}
		var postforskott = 'false';
		if($('postforskott')) {
			if($('postforskott').checked) postforskott = this.options.postforskottPris;
		}
		var invoiceFee = 'false';
		if($('kreditor')) {
			if($('kreditor').checked) invoiceFee = this.options.invoiceFee;
		}
		
		var xml = this.buildXML();
		
		thisOptions = this.options;
		new Request({url: this.options.cartUrl, onSuccess: function(responseText) {
			$(thisOptions.cartContainer).set('html',responseText);
		}}).send('cartXML='+xml+'&noFreight='+noFreight+'&postforskott='+postforskott+'&invoicefee='+invoiceFee);
	
	},
	loadSmallCart: function() {
	
		var xml = this.buildXML();
		
		var thisOptions = this.options;
		new Request({url: this.options.smallCartUrl, onSuccess: function(responseText) {
			$(thisOptions.cartSmallContainer).set('html',responseText);
		}}).send('cartXML='+xml);
	
	},
	order: function() {
		var xml = this.buildXML();
		
		
		if($('comments')) var comments = $('comments').get('value');
		else var comments = '';
		
		var payment = false;
		if($$('.payment')[0]) {
			$$('.payment').each(function(item) {
				if(item.checked) payment = item.value;
			});
		}
		var thisOptions = this.options;
		var thisCart = this.cart;
		var thisPaymentUrl =  this.options.paymentUrl;
		var noFreight = 'false';
		var postforskott = 'false';
		var invoiceFee = 'false';
		if($('noFreight')) {
			if($('noFreight').checked) noFreight = 'true';
		}
		if($('postforskott')) {
			if($('postforskott').checked) postforskott = this.options.postforskottPris;
		}
		if($('kreditor')) {
			if($('kreditor').checked) invoiceFee = this.options.invoiceFee;
		}
		if($('discountCode')) var discountCode = $('discountCode').get('value');
		else var discountCode = '';
		if(thisCart.getLength() == 0) {
			alert('Du har inga produkter i kundvagnen.');
		} else if($('agreement').checked == false) {
			alert('Du måste godkänna köpvillkoren.');
		} else if(!payment) {
			alert('Du måste välja ett betalsätt.');
		} else {
			new Request({url: this.options.checkoutUrl, method: 'post', onSuccess: function(responseText) {
				window.location = thisPaymentUrl+'?payment='+payment+'&discountCode='+discountCode+'&noFreight='+noFreight+'&postforskott='+postforskott+'&invoicefee='+invoiceFee;
			}}).send('cartXML='+xml+'&comments='+comments);
		}
	},
	eraseCookie: function() {
		this.cart.empty();
	}

});