var Abo = new Class({
	Implements: [Events, Options], 
	Binds: ['showPricesAndOptions', 'removeHtml', 'checkRemoval'], 
	Options: {
		abos: [
			{ code: 'abo3s', 	name: 'Drei Sinfoniekonzerte (ohne Neujahrskonzert)', 						prices: [{ chf: 113, title: '1. Platz' }, { chf: 98, title: '2. Platz' }, { chf: 80, title: '3. Platz' }], 		days: ['Samstag', 'Sonntag'], 	times: [], 						discounts: [{ off:50, title: '50% für 6 bis 16 Jahre'}, { off:25, title: '25% für 16 bis 25 Jahre' }] },
			{ code: 'abo4s', 	name: 'Vier Sinfoniekonzerte (mit Neujahrskonzert)', 						prices: [{ chf: 150, title: '1. Platz' }, { chf: 130, title: '2. Platz' }, { chf: 105, title: '3. Platz' }], 	days: ['Samstag', 'Sonntag'],	times: ['15 Uhr', '19 Uhr'], 	discounts: [{ off:50, title: '50% für 6 bis 16 Jahre'}, { off:25, title: '25% für 16 bis 25 Jahre' }] }, 
			{ code: 'abo3sk',	name: 'Drei Sinfoniekonzerte und Kammerkonzert (ohne Neujahrskonzert)', 	prices: [{ chf: 133, title: '1. Platz' }, { chf: 118, title: '2. Platz' }, { chf: 95, title: '3. Platz' }], 	days: ['Samstag', 'Sonntag'], 	times: [], 						discounts: [{ off:50, title: '50% für 6 bis 16 Jahre'}, { off:25, title: '25% für 16 bis 25 Jahre' }] }, 
			{ code: 'abo4sk', 	name: 'Vier Sinfoniekonzerte und Kammerkonzert (mit Neujahrskonzert)', 		prices: [{ chf: 170, title: '1. Platz' }, { chf: 150, title: '2. Platz' }, { chf: 120, title: '3. Platz' }], 	days: ['Samstag', 'Sonntag'], 	times: ['15 Uhr', '19 Uhr'], 	discounts: [{ off:50, title: '50% für 6 bis 16 Jahre'}, { off:25, title: '25% für 16 bis 25 Jahre' }] }, 
			{ code: 'junior', 	name: 'Drei Sinfoniekonzerte, für Jugendliche bis 25 Jahren mit Ausweis',	prices: [{ chf: 25, title: 'Platzzuteilung' }],										 							days: ['Samstag'], 				times: [], 						discounts: [] }
		]
	}, 
	
	initialize: function(args) {
		this.setOptions(args);
		this.id = (Math.random() * new Date().getTime()).toString().toMD5().substring(0, 6);
		this.createHtml();
	}, 
	
	createHtml: function() {
		this.pa = new Element('p');
		
		// Label + Dropdown Konzert
		new Element('label', { 'for' : 'abo-'+this.id, 'text': 'Abo' }).inject(this.pa);
		this.se = new Element('select', { 'id': 'abo-'+this.id, 'name': 'abos['+this.id+']', 'events': { 'change': this.showPricesAndOptions }}).inject(this.pa);
		this.Options.abos.each(function(ab) { new Element('option', { 'value': ab.name, 'text': ab.name }).inject(this.se); }.bind(this));
		new Element('br').inject(this.pa);
		
		// Label + Dropdown Preis
		new Element('label', { 'for' : 'preis-'+this.id, 'text': 'Platz' }).inject(this.pa);
		this.pr = new Element('select', { 'id': 'preis-'+this.id, 'name': 'preise['+this.id+']' }).inject(this.pa);
		new Element('br').inject(this.pa);
		
		// Normale Zeiten
		new Element('label', { 'text': 'Wann?' }).inject(this.pa);
		this.sz = new Element('span', { 'class': 'group' }).inject(this.pa);
		
		// Neujahrskonzert
		this.nl = new Element('label', { 'text': 'Neujahrskonzert' }).inject(this.pa);
		this.nk = new Element('span', { 'class': 'group' }).inject(this.pa);
		
		// Label + Dropdown Rabatte
		new Element('label', { 'for' : 'rabatt-'+this.id, 'text': 'Ermässigung' }).inject(this.pa);
		this.er = new Element('select', { 'id': 'rabatt-'+this.id, 'name': 'rabatte['+this.id+']' }).inject(this.pa);
		new Element('br').inject(this.pa);
		
		
		
		
		// Löschlink
		this.re = new Element('a', { 'class': 'remove', 'text': 'Dieses Abo entfernen', 'href': 'javascript:;', 'events': { 'click': this.removeHtml, 'check': this.checkRemoval } } ).inject(this.pa);
		
		// alles einfügen
		this.pa.inject($('abos'));
		
		// Füllen und prüfen
		this.showPricesAndOptions();
		this.callOthers();
	}, 
	
	showPricesAndOptions: function() {
		// Preise neu füllen
		this.pr.empty();
		this.Options.abos[this.se.selectedIndex].prices.each(function(p) { new Element('option', { 'text': p.title+': '+p.chf+' CHF' }).inject(this.pr); }.bind(this));
		// Spielzeiten neu füllen
		this.sz.empty();
		this.Options.abos[this.se.selectedIndex].days.each(function(d, i) { 
			new Element('input', { 'type': 'radio', 'id': 'tag-'+this.id+'-'+i, 'checked': i == 0 ? true : false, 'name': 'tag['+this.id+']', 'value': d }).inject(this.sz);
			new Element('label', { 'class': 'inline', 'for': 'tag-'+this.id+'-'+i, 'text': d }).inject(this.sz);
		}.bind(this));
		// Neujahrskonzertzeiten neu füllen
		this.nk.empty();
		if(this.Options.abos[this.se.selectedIndex].times.length > 0) {
			this.nk.removeClass('inactive');
			this.nl.removeClass('inactive');
			this.Options.abos[this.se.selectedIndex].times.each(function(d, i) { 
				new Element('input', { 'type': 'radio', 'id': 'zeit-'+this.id+'-'+i, 'checked': i == 0 ? true : false, 'name': 'zeit['+this.id+']', 'value': d }).inject(this.nk);
				new Element('label', { 'class': 'inline', 'for': 'zeit-'+this.id+'-'+i, 'text': d }).inject(this.nk);
			}.bind(this));
		}
		else {
			this.nk.addClass('inactive');
			this.nl.addClass('inactive');
		}
		// Rabatte
		this.er.empty();
		new Element('option', { 'value': '', 'text': 'Keine' }).inject(this.er);
		this.Options.abos[this.se.selectedIndex].discounts.each(function(d) { 
			new Element('option', { 'value': d.title, 'text':d.title }).inject(this.er);
		}.bind(this));
		
	}, 
	
	removeHtml: function() {
		if($$('#abos p').length > 1)
			this.pa.dispose();
		this.callOthers();
	}, 
	
	callOthers: function() {
		$$('.remove').each(function(el) { el.fireEvent('check'); });
	},
	
	checkRemoval: function() {
		if($$('#abos p').length > 1) {
			this.re.removeClass('inactive');
			this.re.removeEvent('click', this.removeHtml);
			this.re.addEvent('click', this.removeHtml);
		}	
		else {
			this.re.addClass('inactive');
			this.re.removeEvent('click', this.removeHtml);
		}
	}
});

