Album = Class.create ({
	album_title: null,
	lyrics_cache: new Hash(),
	close_lyrics_function: null,
	loading_lyrics: null,
	initialize: function(album_title){
		this.album_title = album_title;
		this.close_lyrics_function = this.closeLyrics.bind(this);
		this.startObservers();		
		var top_anchor = new Element("a", {name: "top"});
		$$('body').first().insert({top: top_anchor});
	},
	startObservers: function() {
		var f = this.openLyrics.bind(this);
		$$('.lyrics_link').each(function(l){
			Event.observe(l, "click", this.dispatchClickEvent.bindAsEventListener(this, f));
		}, this);
	},
	dispatchClickEvent: function(event, callback, cancel) {
		if(cancel != false) {
			event.stop();
		}
		var el = Event.element(event);
		var link;
		switch(el.tagName.toLowerCase()) {
			case 'a':
				link = el.href;
				title = el.title;
				break;
			default:
				return;
				break;
		}
		callback(el, link, title);
	},
	openLyrics: function(anchor, link, title) {
		if(this.loading_lyrics == link) {
			return;
		}
		if(anchor.hasClassName("close_lyrics")) {
			this.closeLyrics();
		} else if (!this.lyrics_cache.keys().include(link)) {
			anchor.update("Loading...");
			this.loading_lyrics = link;			
			var req = new Ajax.Request(link, {
				method: 'get',
				parameters: {ajax: 'true'},
				onSuccess: function(transport){
					var div = new Element("div", {"class": "lyrics"});
					var h2 = new Element("h2", {"class": "lyrics_title"});
					var lyrics = transport.responseText.replace(/\n/g, "<br />");
					h2.update(title.substr(10));
					div.update(lyrics);
					this.lyrics_cache.set(link, {
						title: h2,
						lyrics: div
					});
					this.loading_lyrics = null;
					this.updateLyrics(anchor, title, this.lyrics_cache.get(link));
				}.bind(this)
			});
		} else {
			this.updateLyrics(anchor, title, this.lyrics_cache.get(link));
		}
	},
	updateLyrics: function(anchor, title, elements) {
		var id = 'current_lyrics';
		if($(id) != undefined) {
			this.closeLyrics(true);
		}
		var current_anchor = new Element("a", {id: "current_anchor", name: "lyrics"});
		var close_anchor = new Element("a", {"class": "close_anchor"}).update("Close");
		var close_wrap = new Element("p").insert(close_anchor);
		var wrapper = new Element("div", {id: id, "class":"lyrics_wrapper"});
		wrapper.insert(elements.title.cloneNode(true));
		wrapper.insert(close_anchor.cloneNode(true));
		wrapper.insert(elements.lyrics.cloneNode(true));
		wrapper.insert(close_wrap);
		wrapper.hide();
		$(anchor).parentNode.insert(wrapper);
		$(anchor).update("Close Lyrics");
		$(anchor.addClassName("close_lyrics"));
		$(anchor).up("li").insert({top: current_anchor});		
		window.location.href = window.location.href.split(/#/).first() + "#lyrics";	
		$$(".close_anchor").each(function(a) {
			Event.observe(a, "click", this.close_lyrics_function);
		}, this);
		Effect.BlindDown(wrapper);		
	},
	closeLyrics: function(quick) {
		var el = $('current_lyrics');
		el.id = "";
		$$(".close_anchor").each(function(a) {
			Event.stopObserving(a, "click", this.close_lyrics_function);
		}, this);		
		el.previous("a.lyrics_link").update("Lyrics");
		$$(".close_lyrics").first().removeClassName("close_lyrics");
		window.location.href = window.location.href.split(/#/).first() + "#lyrics";		
		$("current_anchor").remove();
		if (quick === true) {
			el.remove();
		} else {
			Effect.BlindUp(el);
			window.setTimeout(function() {
				el.remove();
			}, 1000);
		}
	}
});
