function showSubscription() {
	new Ajax.Request('subscribe.php', { onSuccess: parseResponse } );
}
function showOrder() {
	form.reset();
	new Ajax.Request('form.php', { onSuccess: parseResponse } );
}

function boxSizeChanged(box) {
	var cont = $(box.parentNode);
	var ics = 0;
	switch(box.value) {
		case 'mellem':
			ics = 4;
			cont.down('[rel=5]').hide();
			cont.down('[rel=4]').show();
			break;
		case 'stor':
			ics = 5;
			cont.down('[rel=5]').show();
			cont.down('[rel=4]').show();
			break;
		case 'lille':
		default:
			ics = 3;
			cont.down('[rel=5]').hide();
			cont.down('[rel=4]').hide();
			break;
	}
	
}

function updateContent(newContent) {
	var dialog = $('ajax_dialog');
	var spinner = $('ajax_spinner');
	if(dialog) {
		dialog.hide();
		dialog.show();
	} else {
		dialog = new Element('div', { id: 'ajax_dialog' } );
		document.body.appendChild(dialog);
	}
	if(!spinner) {
		spinner = new Element('div', { style: 'display: none;', id: 'spinner' } );
		document.body.appendChild(spinner);
	}
	dialog.update(newContent);
	dialog.insert({ bottom: '<input type="button" id="ajax_dialog_btn_close" value="Luk vindue" onclick="hideDialog();"/>' });
	$('ajax_dialog').show();
	var hider = $('ajax_dialog_hider');
	if(hider)
		hider.show();
	else {
		hider = new Element('div', { id: 'ajax_dialog_hider', onclick: 'hideDialog();' } );
		document.body.appendChild(hider);
	}
	$('errors').update('');
}
function hideDialog() {
	$('ajax_dialog').hide();
	$('ajax_dialog_hider').hide();
}
function parseResponse(response) {
	if(response.responseJSON) {
		var obj = response.responseJSON;
		if(obj.response)
			updateContent(obj.response);
		if(obj.errors && obj.errors.length > 0) {
			var errors = obj.errors.join('\n');
			alert(errors);
			//$('errors').update(errors);
		}
	} else {
		updateContent(response.responseText);
	}
}

var IcecreamForm = Class.create();
IcecreamForm.prototype = {
	icecreams: {},
	enabled: 0,
	checkbox: null,
	total: 0,
	buttonStatus: false,
	submitStatus: false,
	type: '',
	basket: [],
	
	reset: function() {
		this.icecreams = {};
		this.enabled = 3;
		this.checkbox = null;
		this.total = 0;
		this.buttonStatus = true;
		this.submitStatus = true;
		this.type = '';
		this.basket = [];
	},
	
	initialize: function() {
		this.reset();
		new Ajax.Request('ajax.php?ajax=listicecreams', { onSuccess: function(response) {
			this.icecreamList = response.responseJSON;
		}.bind(this) });
	},
	
	changeType: function(type) {
		if(this.type) {
			$(this.type).hide();
		} else {
			$('order').show();
		}
		this.type = type;
		$(this.type).show();
		if(type == 'box') {
			var p = $('_pickup_box');
			p.show();
			p.name = 'order[pickup]';
			p = $('_pickup_cake');
			p.hide();
			p.name = '';
		} else {
			var p = $('_pickup_cake');
			p.show();
			p.name = 'order[pickup]';
			p = $('_pickup_box');
			p.hide();
			p.name = '';
			$('submit').disabled = false;
		}
	},
	
	setEnabled: function(checkbox, en) {
		/*if(!this.enabled) {
			$('icecreams').show();
		}*/
		if(this.total > en) {
			checkbox.checked = false;
			this.checkbox.checked = true;
			return false;
		}
		this.checkbox = checkbox;
		this.enabled = en;
		this.updateCount();
		return true;
	},
	
	updateCount: function() {
		if(this.total == this.enabled) {
			if(this.buttonStatus == true) {
				$('ajax_dialog_btn_basket').disabled = this.buttonStatus = false;
			}
		} else {
			if(this.buttonStatus == false) {
				$('ajax_dialog_btn_basket').disabled = this.buttonStatus = true;
			}
		}
	},
	
	currentIcecream: function(id) {
		return this.icecreams[id]?this.icecreams[id]:0;
	},
	
	fillCart: function() {
		var len = this.basket.length;
		$('cart').update('Kurv:<br/>');
		for(var i = 0; i < len; i++) {
			this.addCartRow(this.basket[i]);
		}
	},
	
	addCartRow: function(obj) {
		var ic = [];
		for(var i in obj.icecreams) {
			var j = obj.icecreams[i];
			for(var k = 0; k < j; k++) {
				ic.push(this.icecreamList[i]);;
			}
		}
		$('cart').insert({bottom: '<div><img src="icns/cross_small.png" onclick="icecreamForm.removeFromBasket(this.parentNode, \''+obj.id+'\');" class="icon" />Boks: '+this.checkbox.value+'; '+ic.join(', ')+'</div>'});
	},
	
	removeFromBasket: function(row, removeId) {
		$(row).remove();
		var arr = [];
		var len = this.basket.length;
		var obj;
		var id = 0;
		for(var i = 0; i < len; i++) {
			if(obj.id == removeId)
				continue;
			obj = this.basket[i];
			obj.id = id++;
			arr.push(obj);
		}
		this.basket = arr;
		this.fillCart();
	},
	
	putInBasket: function() {
		var id = this.basket.length;
		var obj = {
			id: id,
			box: this.checkbox.value,
			icecreams: this.icecreams
		};
		this.basket.push(obj);
		this.addCartRow(obj);
		for(var i in this.icecreams) {
			$('_'+i).update(0);
		}
		this.icecreams = {};
		this.total = 0;
		$('ajax_dialog_btn_basket').disabled = this.buttonStatus = true;
		if(this.submitStatus) {
			$('submit').disabled = this.submitStatus = false;
		}
	},
	
	increaseIcecream: function(id) {
		if(this.total == this.enabled) return;
		this.total++;
		var cur = this.currentIcecream(id);
		cur++;
		this.icecreams[id] = cur;
		this.updateIcecream(id);
		this.updateCount();
	},
	decreaseIcecream: function(id) {
		if(this.total == 0) return;
		var cur = this.currentIcecream(id);
		if(cur == 0) return;
		cur--;
		this.icecreams[id] = cur;
		this.total--;
		this.updateIcecream(id);
		this.updateCount();
	},
	updateIcecream: function(id) {
		$('_'+id).update(this.icecreams[id]);
	},
	
	submit: function(form) {
		var parameters = null;
		if(this.type == 'box') {
			parameters = {};
			var boxes = $('boxes').select('.box');
			if(boxes.length == 0) {
				alert('Der skal v&aelig;lges mindst en boks.');
				return;
			}
			var size;
			var id;
			var icecreams;
			var ic;
			var blen = boxes.length;
			var ilen;
			for(var i = 0; i < blen; i++) {
				obj = boxes[i];
				size = obj.down('[name=size]').value;
				parameters['box['+i+'][box]'] = size;
				icecreams = obj.select('select[name=icecream]');
				ilen = icecreams.length;
				for(var j = 0; j < ilen; j++) {
					ic = $(icecreams[j]);
					if(!ic.visible()) {
						break;
					}
					if(ic.value == '') {
						alert('Alle is i en boks skal udfyldes.');
						return;
					}
					parameters['box['+i+'][icecreams]['+j+']'] = ic.value;
				}
			}
			/*
			for(var i in this.basket) {
				obj = this.basket[i];
				parameters['box['+obj.id+'][box]'] = obj.box;
				for(var j in obj.icecreams) {
					parameters['box['+obj.id+'][icecreams]['+j+']'] = obj.icecreams[j];
				}
			}
			*/
			/*
			for(var ic in this.icecreams) {
				parameters['icecreams['+ic+']'] = this.icecreams[ic];
			}
			*/
		} else {
			parameters = {
				'cake[top]': $('cake_top').value,
				'cake[bottom]': $('cake_bottom').value
			};
		}
		var order = $('order');
		var inputs = order.select('input, textarea, select');
		var inp;
		for(var i in inputs) {
			inp = inputs[i];
			if(inp.type != 'submit') {
				parameters[inp.name] = inp.value;
			}
		}
		if(parameters['order[name]'] == '' || parameters['order[phone'] == '') {
			alert('Navn og telefon skal udfyldes.');
			return;
		}
		parameters['order[type]'] = this.type;
		new Ajax.Request(form.action, { method: form.method, parameters: parameters, onSuccess: parseResponse });
	}
};
var form = new IcecreamForm();
var icecreamForm = form;
function changeEnabled(checkbox, amount) {
	this.form.setEnabled(checkbox, amount);
}
function submitForm(form) {
	this.form.submit(form);
	return false;
}
