Event.observe(window, "load", function() {
	/*
	var mediaCache = {};

	$$('body').first().addClassName('loaded');
	
	if ($('nakedbus')) $('nakedbus').innerHTML = '<a href="http://affiliates.naked.co.nz/1068-5-1-41.html" target="_blank" rel="nofollow"><img border="0" src="http://affiliates.naked.co.nz/banners/nakedpassport_336x280.gif" style="width: 336px; height: 280px;" /></a>';
	
	var navSecondaryHTML = $('nav-secondary').innerHTML;
	$$('#nav-primary a').each(function(i) {
		i.observe('mouseover', setMenu);
		i.observe('mouseout', unsetMenu);
	});
	$('nav-secondary').observe('mouseover', setSecondaryMenu);
	$('nav-secondary').observe('mouseout', unsetMenu);
	
	function setMenu(event) {
		resetMenu();
		var menuItem = Event.element(event);
		if (menuItem.tagName.toLowerCase() != 'a') menuItem = menuItem.up('a');
		var menuHref = menuItem.readAttribute('href').replace(/\/|experiencequeenstown/g, "");
		$('nav-primary').addClassName('hover');
		menuItem.up('li').addClassName('hover');
		menuStatus = false;
		
		if (menuHref in menu) {
			$('nav-primary').addClassName('has-children-hover');
			$('nav-secondary').addClassName('has-children-hover');
			var menuChildren = menu[menuHref];
			var insert = "<ul class='flat'>";
			if (typeof(menuChildren) == 'string') {
				insert += "<p>" + menuChildren + "</p>";
			}
			else {
				$H(menuChildren).each(function(i){
					insert += "<li><a href='" + i.value.URLSegment + "/'>" + i.value.Title + "</a>";
				});
			}
			insert += "</ul>";
			$('nav-secondary').update(insert);
		} else {
			$('nav-primary').addClassName('no-children');
			$('nav-secondary').addClassName('no-children');
			$('nav-secondary').update('');
		}
	}
	function setSecondaryMenu(event) {
		menuStatus = false;
	}
	function unsetMenu(event) {
		resetCounter();
	}
	
	var count = 0;
	var menuStatus = false;
	var counter = function() {
		count++;
		if (count > 2 && menuStatus == true) {
			resetMenu();
			menuStatus = false;
		}
	}
	setInterval(counter, 1000);
	
	function resetCounter() {
		count = 0;
		menuStatus = true;
	}
	function resetMenu() {
		$('nav-primary').removeClassName('has-children-hover').removeClassName('no-children').removeClassName('hover');
		$('nav-secondary').removeClassName('has-children-hover').removeClassName('no-children').update(navSecondaryHTML);
		$$('#nav-primary li.hover').invoke('removeClassName', 'hover');
	}
	
	// Setup back button, for video and photo loaders
	// TODO: Append properly!
	
	if ($('left')) {
  	$('left').insert({'top': "<div id='back'><span><a href='" + location.href + "'>close</a></span></div>"});
  	$('back').hide();
  	$('back').observe('click', function(event){
  		$$('#left > div').invoke('hide');
  		$('main').show();
  		Event.stop(event);
  	});
  }
	
	$$('#content a[href*="media/"], #content #media .controls a').each(function(i) {
		if (!i.up('.photo-gallery'))
			i.observe('click', initiateMedia);
	});
	function initiateMedia(event) {
		$('main').hide(); // and probably all other first divs
		var a = Event.element(event);
		if (a.tagName.toLowerCase() != 'a') a = a.up('a');
		var container = $$("#media .container").first() ? $$("#media .container").first() : false;
		
		if (container) {
			Effect.Fade(container, { duration: 1, afterFinish: function() {
				$('media').show();
				loadMedia(a);
			}});
		} else {
			new Insertion.Bottom($('left'), "<div id='media' class='Photo'><div class='wrap'><div class='container'><h1></h1><div class='loading'></div><div id='media-container'></div></div></div><div class='content'><p class='controls'><a href='#' class='button left previous'><span>Previous</span></a><a href='#' class='button right next'><span>Next</span></a></p><p class='by'></p><p class='description'></p><p class='tags'></p></div></div>");
			$$('.controls a').each(function(i) {
				i.observe('click', initiateMedia);
			});
			loadMedia(a);
		}
		
		$('back').show();
		Event.stop(event);
	}
	var loadMedia = function(a) {
		if (mediaCache[a.href]) {
			insertMedia(mediaCache[a.href]);
		} else {
			new Ajax.Request(a.href, {
				onSuccess: function(transport) {
					mediaCache[a.href] = transport.responseJSON;
					insertMedia(mediaCache[a.href]);
				}
			});
		}
	}
	var insertMedia = function(media) {
		$$("#media .container").first().show();
		$$("#media .container .loading").first().show();
		$$("#media h1").first().update(media.Title);
		$$("#media .description").first().update(media.Description);
		if (media.By) {
			if (media.ByLink) {
				var by = media.Type + " by: <a href='" + media.ByLink + "'>" + media.By + "</a>";
			} else {
				var by = media.Type + " by: " + media.By;
			}
		} else {
			var by = '&nbsp;';
		}
		$$("#media .by").first().update(by);
		$$("#media .description").first().update(media.Description);

		var tags = "Tags: ";
		if (media.Tags.length) {
			media.Tags.each(function(i) {
				tags += i;
				if (i != media.Tags.last()) 
					tags += ', ';
			});
		}
		else {
			tags += "<em>no tags</em>";
		}
		$$("#media .tags").first().update(tags.substr(0));
		
		//setPrevAndNext(media.Type + 's', document.baseURI + 'Media/View/' + media.ID);
		// Put them all in one now
		setPrevAndNext('content', document.baseURI + 'media/' + media.ID);
		if (media.Type == 'Photo') {
			$$("#media-container").first().update("<p><img src='" + media.Media + "' class='photo' alt='" + media.Title + "' /></p>");
			$$("#media .photo").first().observe("load", photoLoaded);
		} else {
			$$("#media-container").first().update("<object width='557' height='443'><param name='movie' value='" + media.Media + "&hl=en&fs=1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><param name='wmode' value='transparent'></param><embed src='" + media.Media + "&hl=en&fs=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='557' height='443' wmode='transparent'></embed></object>");
			resizeMediaContainer(443);
		}
	}
	var photoLoaded = function(event) {
		var image = Event.element(event);
		var imageHeight = image.getHeight();
		addReflection(image);
		Event.stopObserving(image, 'load', photoLoaded);
	}
	var addReflection = function(image) {
		var imageHeight = image.getHeight();
		Reflection.add(image, { height: 30 / imageHeight, opacity: 2/4 });
		resizeMediaContainer(imageHeight);
	}
	var resizeMediaContainer = function(height) {
		$$("#media .wrap").first().morph("height: " + (height + 57) + "px;", { afterFinish: function() { 
			Effect.Fade($$("#media .loading").first(), { duration: 1 });
		}});
	}
	$$("#media img.photo").each(function(image) { addReflection(image); });
	if ($$("#media object").length) resizeMediaContainer(443);
	
	var urls = new Hash;
	var titles = new Hash;
	function setPrevAndNext(container, url) {
		// Set this up once only
		if (!urls.get(container)) {
			var collectedUrls = new Array;
			var collectedTitles = new Array;
			$$('#' + container + ' .thumb ul a').each(function(i) {
				collectedUrls.push(i.href);
				collectedTitles.push(i.title);
			});
			urls.set(container, collectedUrls);
			titles.set(container, collectedTitles);
		}
		var index = urls.get(container).indexOf(url);
		var previous = index == 0 ? urls.get(container).size() - 1 : index - 1;
		var next = index == urls.get(container).size() - 1 ? 0 : index + 1;
		var previousButton = $$("#media .previous").first();
		var nextButton = $$("#media .next").first();
		previousButton.setAttribute('href', urls.get(container)[previous]);
		previousButton.setAttribute('title', titles.get(container)[previous]);
		nextButton.setAttribute('href', urls.get(container)[next]);
		nextButton.setAttribute('title', titles.get(container)[next]);
	}
	//if ($$("#media.Photo").length) { setPrevAndNext('Photos', document.location.href); }
	//if ($$("#media.Video").length) { setPrevAndNext('Videos', document.location.href); }
	// Put them all in one now
	if ($$("#media").length) { setPrevAndNext('MediaTab', document.location.href); }
	
  */
	// Tabs
	var tab_on = /* get_cookie ? cookie : */ 'MediaTab';
	$$('ul.tabs li a').each(function(i, count) {
		var tab = get_tab_id(i);
		// hide all tabs except for tab_on, or hide all except 1st (broken, can't think)
		if (tab != tab_on || (!tab_on && count > 0)) 
			$(tab).hide();
		else 
			i.addClassName('on');
			
		i.observe('click', function(event) {
			var tab = Event.element(event);
			if (tab.tagName.toLowerCase() != 'a') tab = tab.up('a');
			$$('#panel .tabs a').invoke('removeClassName', 'on');
			tab.addClassName('on');
			$$('#panel .content > div').invoke('hide');
			$(get_tab_id(tab)).show();
			// set cookie ?
			// stop event, but this stops the url rename
			Event.stop(event);
		});
	});
	
	function get_tab_id(tab) {
		return tab.getAttribute('href').split('#')[1];
	}
	
	// Add bottom to wide column
	$$('.column.wide').invoke('insert', {'bottom': '<div class="bottom"></div>'});

	new TipHandler('a[href*="media/"] img');
	
	// Facebook comments
	window.fbAsyncInit = function() {
	  FB.init({appId: 'your app id', status: true, cookie: true,
	           xfbml: true});
	};
	(function() {
	  var e = document.createElement('script'); e.async = true;
	  e.src = document.location.protocol +
	    '//connect.facebook.net/en_US/all.js';
	  document.getElementById('fb-root').appendChild(e);
	}());

	
});