var currency,
	products = [],
	oCurrency,
	oBtn,
	oMsg,
	oTotalPrice;
	
$(function(){
	oCurrency = $('#OrderCurrency');
	oBtn = $('#submit');
	oMsg = $('#MaxQtyMsg');
	oTotalPrice = $('#TotalPrice');
	
	oCurrency.val(CURRENCY);
	currency = oCurrency.val();

	$('table.cart tr.product').each(function(){
		products.push(new Product($(this)));
	})
	
	oCurrency.change(function(e){ recalcTotalPrice() })
	recalcTotalPrice();
});

function recalcTotalPrice() {
	currency = oCurrency.val();
	var product, qty = 0, total = 0;
	for (var i = 0; i < products.length; i++) {
		product = products[i];
		product.calculate();
		total += product.total;
		qty += product.qty;
	}
	if (qty < 1) {
		//oBtn.disabled = true;
		oBtn.disable();
	} else if (qty > 70) {
		//oBtn.disabled = true;
		oBtn.disable();
		oMsg.css('display', 'table-row');
	} else {
		//oBtn.disabled = false;
		oBtn.enable();
		oMsg.css('display', 'none');
	}	
	
	oTotalPrice.html(formatNumber(total.toFixed(0)));
	updateCurrency(currency);
}

function updateCurrency(currency) {
	$('span[rel="currency"]').each(function(){
		var e = $(this);
		if (e.hasClass(currency)) {
			e.removeClass('hidden');
		} else {
			e.addClass('hidden');
		}
	})
}

/* Product object */
Product = function (jContainer) {
	this.jContainer = jContainer;
	this.oItemPrice = jContainer.find('span._price');
	this.oTotalPrice = jContainer.find('span._totalPrice');
	this.oMsg = jContainer.find('tr.maxQtyError');
	this.oPrice = jContainer.find('input[name="price"]');
	this.oQty = jContainer.find('input[name="qty"]');
	
	this.oNote = jContainer.find('div.note');
	this.oQyNote = jContainer.find('div.qty_note');
	
	this.qty = Number(this.oQty.val());
	this.price = Number(this.oPrice.val()) / RATES[currency];
	this.total = this.qty * this.price;
	this.disabled = false;

	this.attachEvents();
}
Product.prototype = {
	attachEvents: function () {
		var self = this;
	
		this.oQty.mouseup(function(e){
			//if (!self.disabled) {
			//	self.oQyNote.fadeIn(200)
			//}
			recalcTotalPrice()
		})
		.keyup(function(e){
			if (this.value.length <= 2){
				var o = $(this);
				var char = o.attr('char');
				if (char > 57 || char == 45) {
					o.val(o.attr('temp'));
				}{
					o.attr('temp', o.val());
				}
			}
			recalcTotalPrice();
		})
		.focus(function(e){
			var o = $(this);
			o.attr('temp', o.val());
		})
		.keypress(function(e){
			//if (!self.disabled) {
			//	self.oQyNote.fadeOut(200)
			//}
			$(this).attr('char', e.charCode);
		})
		.blur(function(){
			//self.oQyNote.fadeOut(200)
		})

		this.oNote.find('.action a').toggle(
			function(){
				$(this).parent().next().slideDown()
			},
			function(){
				$(this).parent().next().slideUp()
			}
		)
	},
	calculate: function () {
		this.qty = Number(this.oQty.val());
		this.price = Number(this.oPrice.val()) / RATES[currency];
		if (this.qty == 0 && !this.disabled) {
			this.jContainer.addClass('discard');
			//this.oQyNote.hide()
			this.disabled = true;
		}
		if (this.qty > 0 && this.disabled){
			this.jContainer.removeClass('discard');
			this.disabled = false;
		}
		this.total = this.qty * this.price;
		this.oItemPrice.html(formatNumber(this.price.toFixed(0)));
		this.oTotalPrice.html(formatNumber(this.total.toFixed(0)));
	}
}
