function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
//	return (((sign)?'':'-') + '$' + num + '.' + cents);
	return num+'.'+cents;
}

var ACMShop = {
	init: function(tabcon, tabcon_params, h_form, h_total, h_balance, balance, h_output) {
		this.h_form = $(h_form);
		this.h_total = $(h_total);
		this.h_balance = $(h_balance);
		this.h_output = $(h_output);
		this.total = 0;
		this.balance = balance;
		
		Event.observe(this.h_form, 'submit', this.submit.bindAsEventListener(this), false);
		
		tabcontrol_create(tabcon, tabcon_params);
		this.bindmenus();
		
		this.h_total.innerHTML = formatCurrency(0);
		this.h_balance.innerHTML = formatCurrency(balance);
	},
	
	bindmenus: function() {
		var menus = document.getElementsByClassName('shopmenu');
		for (i = 0; i < menus.length; i++) {
			Event.observe(menus[i], 'mouseover', this.menu_mouseover.bindAsEventListener(this), false);
			Event.observe(menus[i], 'mouseout',  this.menu_mouseout.bindAsEventListener(this), false);
			Event.observe(menus[i], 'click',     this.menu_click.bindAsEventListener(this), false);
		}
	},
	
	menu_mouseover: function(evt) {
		var cell = Event.element(evt);
		if (cell.tagName == 'INPUT') cell = cell.parentNode;
		if (cell.tagName != 'TD') return;
		var cells = cell.parentNode.cells;
		for (i = 0; i < cells.length; i++)
			cells[i].addClassName('over');
	},
	
	menu_mouseout: function(evt) {
		var cell = Event.element(evt);
		if (cell.tagName == 'INPUT') cell = cell.parentNode;
		if (cell.tagName != 'TD') return;
		var cells = cell.parentNode.cells;
		for (i = 0; i < cells.length; i++)
			cells[i].removeClassName('over');
	},
	
	menu_click: function(evt) {
		var cell = Event.element(evt);
		if (cell.tagName != 'TD') return;
		var cells = cell.parentNode.cells;
		var price = parseFloat(cells[1].innerHTML);
//		var add = !cells[2].firstChild.checked;
		var cbox = cells[2].firstChild;
		var add  = !cbox.checked;
		
		if (add) {
			if (price > this.balance) {
				alert("Sorry. You don't have enough credits to purchase this item.");
				return;
			}
			this.total += price;
			this.balance -= price;
			cbox.className = 'bought';
		} else {
			this.total -= price;
			this.balance += price;
			cbox.className = '';
		}
		
		cells[2].firstChild.checked = add;
		
		this.h_total.innerHTML = formatCurrency(this.total);
		this.h_balance.innerHTML = formatCurrency(this.balance);
	},
	
	submit: function(evt) {
		new Ajax.Request(this.h_form.getAttribute('action'), {
			parameters : Form.serialize(this.h_form),
			onSuccess : function(resp) {
				document.getElementsByClassName('bought').each(function(item) {
					item.checked = false;
				});
				
				this.h_total.innerHTML = '0.00';
				this.h_output.innerHTML = resp.responseText;
			}.bindAsEventListener(this)
		});
		Event.stop(evt);
	}
}
