// addLoadEvent() {{{
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
// This rendition stolen from lightbox.js

function addLoadEvent(func) {       
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
} // }}}

// Code for handling cookies from QuirksMode at http://www.quirksmode.org/js/cookies.html {{{
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
} // }}}

var bgs; // bg chooser divs

addLoadEvent(function() {
	// Captions need a hover effect if they contain a link
	var caps = $$('.caption a');
	for(var i=0; i<caps.length; i++) {
		caps[i].parentNode.onmouseover = function() {
			this.bgo = this.style.backgroundColor;
			$(this).setStyle({backgroundColor: '#1e2708'});
		}
		caps[i].parentNode.onmouseout  = function() {
			$(this).setStyle({backgroundColor: this.bgo});
		}
	}

	// Layout chooser needs to save the current hash as a query var, if present
	// otherwise store.php won't know to add it when it redirects the user back
	$$('#layout-chooser a').each(function(e) {
		e.onclick = function() {
			if(location.hash != null) this.href = this.href + "&hash=" + encodeURIComponent(location.hash.substr(1));
		}
	});

	// Attach event handler to bgchooser divs
	(bgs = $$('#bgchooser div')).reverse().each(function(e) { e.onclick = function() { choosebg(this); } });

	$('hide-hint').onclick = function() { hint.hide(); return false; }

	hint.show(1);
});

var hint = {
	num: 0,
	data: [
		'<strong>Vidste du,</strong> at du kan benytte nummertasterne <code>1</code>, <code>2</code>, <code>3</code> og <code>4</code> til at skifte sidens baggrundsfarve?',
		'<strong>Her kan du</strong> bruge piletasterne (&larr; og &rarr;) til at skifte mellem billeder.'
	],

	// Update hint with requested item, and show it if the user hasn't requested
	// it be hidden.
	show: function(num) {
		this.num = num;
		if(!readCookie('hide-hint-' + this.num)) {
			var h = $('hint-body');
			h.update(this.data[this.num-1]).parentNode.id = 'hint-' + this.num;
			$(h.parentNode).show();
		} else {
			var h = $($('hint-body').parentNode);
			if(h.visible())
				h.hide();
		}
	},

	// Set a cookie telling us the user doesn't want to see the tip anymore.
	// Use script.aculo.us to blind up the hint box.
	hide: function() {
		createCookie('hide-hint-' + this.num, true, 365);
		new Effect.BlindUp('hint-' + this.num);
	},
	
	// Utility function to clear hint-related cookies.
	reset: function() {
		(this.data.length).times(function(i) { eraseCookie('hide-hint-' + (i+1)); });
	}
};


var b; // body element

// Set body class to whatever the passed bgchooser div has
function choosebg(e) {
	b = (b) ? b : $$('body').first(); // cache body element for subsequent calls
	b.className = e.className;
	createCookie('bgcolor', e.className, 365*10); // 10 years from now, yo
}

// Attach event handler for keys 1, 2, 3 and 4, so they trigger switching of
// background color. Don't change bgcolor if active element is a textarea,
// input field or select box.
Event.observe(document, 'keydown',
function(e) {
	if(!e.altKey && !e.shiftKey && !e.ctrlKey)
		if($R(49, 52).include(e.keyCode) && !e.element().tagName.match(/textarea|input|select/i)) {
			choosebg(bgs[e.keyCode-49]);
		}
});

function toolbar(e, draw) {
	var tb = new jsToolBar(e);
	$(tb.toolbar).setStyle({width: $(tb.textarea).getStyle('width')}); // Get toolbar width from contained textarea
	if(draw)
		tb.draw();

	return tb;
}

addLoadEvent(function() {
	var ta = $$('textarea.jstoolbar');
	ta.each(function(e) {
		var tb = toolbar(e);
		tb.setHelpLink('<a href="#" onclick="alert(\'Der er ingen hjælp! Haha!\'); return false;">Hjælp</a>');
		tb.draw();
	});
});

// Utility function to display a notice box through javascript
function notice(title, content) {
	$('content').insert({top: '<div class="notice"><h3>' + title + '</h3><p>' + content + '</p></div>'});
}
