/* Copyright (c) Fluid Creativit, 2008. */

Site = {
	init: function() {
		Site.addToFavourites();
		Site.poemRandomiser();
		Site.capitaliseForms();
	},
	
	addToFavourites: function() {
		if ($('addToFavourites')) {
			$('addToFavourites').setStyle("cursor", "pointer").addEvent("click", function() {
				var linkUrl = self.location.href;
				var linkTitle = document.title;
			
				if (window.sidebar) {
					window.sidebar.addPanel(linkTitle, linkUrl, "");
				} else if (window.external) {
					window.external.AddFavorite(linkUrl, linkTitle);
				} else if (window.opera && window.print) {
					return true;
				}
			});
		}
	},

	openBookmark: function(url) {
		var popupWindow = window.open(url, 'addBookmark', 'width=770px, height=500px, status=0, location=0, resizable=1, scrollbars=1, left=0, top=100');
	},
	
	poemRandomiser: function() {
		if ($('loadPoem') && $('poemRandomiser')) {
			$('loadPoem').addEvent("click", function(e) {
				e.stop();
				
				$('poemRandomiserTitle').addClass("loading");
				
				new Request.JSON({
					url: '/ajax/random-poem.php?' + new Date().getTime(),
					onComplete: function (data) {
						if (!data) return;

						var animWrapper = new Element("div").setStyles($('poemRandomiser').getCoordinates()).setStyles({ 'position': 'absolute', 'background': 'white' }).inject($(document.body));

						new Fx.Tween(animWrapper, {
							link: 'cancel',
							onComplete: function() {
								// Title
								$('poemRandomiser').empty().adopt(new Element("h3", { text: data.title } ));
								
								var authorMeta = new Element("p").inject('poemRandomiser');
								
								// Author
								if (data.author) {
									authorMeta.set('html', 'by ' + data.author + '<br />');
								}
								
								// Location
								authorMeta.adopt(new Element("em", { text: data.location }));
								
								// Content
								$('poemRandomiser').adopt(new Element("p", { html: data.content }).adopt(new Element("a", { href: data.url, text: "read more"})));

								animWrapper.setStyles($('poemRandomiser').getCoordinates());
	
								new Fx.Tween(animWrapper, {
									link: 'cancel',
									onComplete: function() {
										$('poemRandomiserTitle').removeClass("loading");
										animWrapper.destroy();
									}
								}).start('opacity', 0);
							}
						}).set('opacity', 0).start('opacity', 1);
					}
				}).get();
			});
		}
	},
	capitaliseForms: function() {
		var formElements = $$("#registerform input[name='firstname'], #registerform input[name='lastname'], #registerform input[name='pseudonym'], #registerform input[name='city']");
		formElements.addEvent("keyup",function(e){
			this.value = this.value.charAt(0).toUpperCase() + this.value.slice(1);
		});
	}
}

window.addEvent("domready", Site.init);

