if (typeof Podbop == 'undefined') Podbop = {};
if (typeof Podbop.Festival == 'undefined') Podbop.Festival = {};

Podbop.Festival.Schedule = {
	EVENT_CHECKBOX_CLASS: 'event',
	EDIT_FORM_ID:         'edit_schedule',
	LOAD_BUTTON_ID:       'load_schedule',
	SAVE_BUTTON_ID:       'save_schedule',
	STATUS_ID:            'status',

	init: function() {
		var checkboxes = document.getElementsByClassName(Podbop.Festival.Schedule.EVENT_CHECKBOX_CLASS);
		for (var i = 0; i < checkboxes.length; i++) {
			var checkbox = checkboxes[i];
			new Form.Element.EventObserver(checkbox, Podbop.Festival.Schedule.selectEvent);
		}

		Event.observe(Podbop.Festival.Schedule.SAVE_BUTTON_ID, 'click', Podbop.Festival.Schedule.save);
		Event.observe(Podbop.Festival.Schedule.LOAD_BUTTON_ID, 'click', Podbop.Festival.Schedule.load);

		var url = $(Podbop.Festival.Schedule.EDIT_FORM_ID).getAttribute('action');
		Podbop.Festival.Schedule.loadCurrentEvents(url);
	},
	clearEvents: function() {
		var checkboxes = document.getElementsByClassName(Podbop.Festival.Schedule.EVENT_CHECKBOX_CLASS);
		for (var i = 0; i < checkboxes.length; i++) {
			var checkbox = checkboxes[i];
			if (checkbox && checkbox.checked) {
				checkbox.click();
			}
		}
	},
	setEvents: function(events) {
		for (var i = 0; i < events.length; i++) {
			var checkbox = $(events[i]);
			if (checkbox && ! checkbox.checked) {
				checkbox.click();
			}
		}
	},
	setName: function(name) {
		$(Podbop.Festival.Schedule.EDIT_FORM_ID).elements['name'].value = name;
	},
	selectEvent: function(input, value) {
		new Ajax.Request(input.form.getAttribute('action'), {
			parameters: Form.Element.serialize(input),
			onSuccess: function(req) {
			},
			onFailure: function(req) {
				alert('Error selecting event: ' + req.responseText);
			}
		});
	},
	_load: function(url, parameters, clear) {
		Podbop.Festival.Schedule.setStatus('Loading schedule...');
		new Ajax.Request(url, {
			parameters: parameters,
			onSuccess: function(req) {
				var json = Podbop.Festival.Schedule.evalJSON(req);
				if (json) {
					if (json.events) {
						if (clear) {
							Podbop.Festival.Schedule.clearEvents();
						}

						Podbop.Festival.Schedule.setEvents(json.events);
					}

					if (json.url && json.name) {
						Podbop.Festival.Schedule.setName(json.name);
						Podbop.Festival.Schedule.setStatus('Loaded schedule.', 'Share your schedule: ', json.url);
					}
					else {
						Podbop.Festival.Schedule.setStatus('Enter a name to save your entire schedule.', 'Must be at least 5 characters and is case-sensitive!');
					}
				}
			},
			onFailure: function(req) {
			}
		});
	},
	loadCurrentEvents: function(url) {
		Podbop.Festival.Schedule._load(url, "", true);
	},
	load: function(e) {
		var form = $(Podbop.Festival.Schedule.EDIT_FORM_ID);
		var parameters = $H({ name: form.elements['name'].value, act: 'Load' });

		Podbop.Festival.Schedule._load(form.getAttribute('action'), parameters.toQueryString(), false);

		Event.stop(e);
	},
	_save: function(url, parameters) {
		Podbop.Festival.Schedule.setStatus('Saving schedule...');
		new Ajax.Request(url, {
			parameters: parameters,
			onSuccess: function(req) {
				var json = Podbop.Festival.Schedule.evalJSON(req);
				if (json && json.url) {
					Podbop.Festival.Schedule.setStatus('Saved schedule.', 'Share your schedule: ', json.url);
				}
				else {
					Podbop.Festival.Schedule.setStatus('Error saving schedule!');
				}
			},
			onFailure: function(req) {
				Podbop.Festival.Schedule.setStatus('Failed to save schedule: ', req.status);
			}
		});
	},
	save: function(e) {
		var form = $(Podbop.Festival.Schedule.EDIT_FORM_ID);
		var parameters = $H({ name: form.elements['name'].value, act: 'Save' });

		Podbop.Festival.Schedule._save(form.getAttribute('action'), parameters.toQueryString());

		Event.stop(e);
	},
	evalJSON: function(req) {
		try {
			return eval('(' + req.responseText + ')');
		}
		catch (e) {}
	},
	setStatus: function(line1, line2, url) {
		var status = $(Podbop.Festival.Schedule.STATUS_ID);
		if (! status) {
			alert(line1);
			return;
		}

		var p = document.createElement('p');
		p.setAttribute('id', Podbop.Festival.Schedule.STATUS_ID);
		p.appendChild(document.createTextNode(line1));
		if (line2) {
			p.appendChild(document.createElement('br'));
			p.appendChild(document.createTextNode(line2));
		}

		if (url) {
			var link = document.createElement('a');
			link.setAttribute('href', url);
			link.appendChild(document.createTextNode(url));
			p.appendChild(link);
		}

		var parent = status.parentNode;
		Element.remove(status);
		parent.appendChild(p);
	}
};

Event.observe(window, 'load', Podbop.Festival.Schedule.init);
