var hp = {
	
	setup: function() {
		// convert from basic to standard mode
		$('.basic').remove();
		$('.standard').removeClass('standard');
		
		// setup alerts
		hp.alerts = {
			illustration: "Joe Six Pew says:\n\n\"I may get impatient or start daydreaming if you use too many illustrations. Are you sure you want to take the risk?\"",
			application: "Joe Six Pew says:\n\n\"If you give me too many practical applications, I may end up trying to bite off more than I can chew. Are you sure you want to take the risk?\""
		};
		
		// setup popup and its links
		hp.popup = $('#popup').insertAfter('#page');
		hp.page = $('#page');
		hp.closeLink = $('#closeLink').click(function() {
			hp.close();
			return false;
		});
		hp.optionBeingRead = null;
		hp.chooseLink = $('#chooseLink').click(function() {
			hp.close();
			hp.choose(hp.optionBeingRead);
			return false;
		});
		
		// setup popup links
		$('a.popup').click(function() {
			a = this;
			$.ajax({url: this.href, success: function(data) {hp.read($(a).text(), data)}});
			return false;
		});
		
		// show preaching tip if requested
		if (window.location.hash == "#preaching_tip") $('#tip').click();
		
		// setup choosers
		$('.chooser').each(function(i, chooser) {
			chooser.options = {
				id: chooser.id.slice(2),
				type: chooser.name.length == 6 ? chooser.name : chooser.name.slice(0, -3),
				div: $('#'+chooser.id.replace('c', 'd'))
			};
			chooser.options.toggler = $('#'+chooser.id.replace('c', 't'));
			chooser.options.title = chooser.options.toggler.text();
			chooser.options.toggler.click(function() {
				$.ajax({url: this.href, success: function(data) {hp.read(chooser.options.title, data, chooser)}});
				return false;
			});
			chooser.options.chooseUrl = webroot+chooser.options.type+'s/choose/'+chooser.options.id;
			chooser.options.removeUrl = chooser.options.chooseUrl.replace('choose', 'remove');
			chooser.options.destination = $('#my'+chooser.options.type);
			chooser.options.chosen = '#'+chooser.options.type+chooser.options.id;
			$(chooser).click(function() {
				if (this.type == 'checkbox' && this.checked == false) hp.removeIllApp(this);
				else hp.choose(this);
			});
		});
	},
	
	read: function(title, body, chooser) {
		$('#title').text(title);
		$('#text').html(body);
		hp.page.fadeTo('fast', .4, function() {
			hp.popup.show().click(function(e){e.stopPropagation()});
			if (chooser && !chooser.checked) {
				hp.chooseLink.show();
				hp.optionBeingRead = chooser;
			}
		})
		$('body').click(hp.close);
		$(document).keyup(function(event){if (event.keyCode == 27) hp.close()});
	},
	
	close: function() {
		$('#title').empty();
		$('#text').empty();
		hp.popup.hide('fast', function() {
			hp.chooseLink.hide();
			hp.optionBeingRead = null;
		});
		hp.page.fadeTo('fast', 1);
		$('body').unbind('click');
		$(document).unbind('keyup');
	},
	
	choose: function(chooser) {
		$.ajax({
			url: chooser.options.chooseUrl,
			success: function(response) {
				li = $('<li id="'+chooser.options.chosen.slice(1)+'" style="display: none">'+chooser.options.title+'</li>');
				if (chooser.options.type == 'lesson') {
					chooser.options.destination.html(li);
					$('.lesson').removeClass('selected');
				} else chooser.options.destination.append(li);
				chooser.options.div.addClass('selected');
				chooser.checked = true;
				$(chooser.options.chosen).css({
					backgroundColor: '#f68f1e',
					color: '#ffffff'
				}).show('fast', function() {
					if (response == 'alert') {
						keep = confirm(hp.alerts[chooser.options.type]);
						if (!keep) hp.removeIllApp(chooser);
					} else keep = true;
					if (keep) $(this).animate({
						backgroundColor: '#DEE2EF',
						color: '#000000'
					}, 2000);
				});
			}
		});
	},
	
	removeIllApp: function(chooser) {
		$.ajax({
			url: chooser.options.removeUrl,
			success: function() {
				$(chooser.options.chosen).remove();
				chooser.options.div.removeClass('selected');
				chooser.checked = false;
			}
		});
	}
	
}

$(document).ready(hp.setup);

