/*
Design and programming: Linked by Air
http://linkedbyair.net
*/


var ie = /msie \d/.test(navigator.userAgent.toLowerCase());


// Distribute nav menu links horizontally so they take the full width of the page, with equal space between them

Event.observe(window, 'load', function() {
	$$('ul.navmenu').each(function(navmenu) {
		var margin = navmenu.getWidth();
		var lis = navmenu.select('li');
		lis.each(function(li) { margin -= li.getWidth(); });
		margin = parseInt(margin / (lis.length - 1)) + (ie ? 4 : 0);
		for (var i = 0; i < (lis.length - 1); i++) lis[i].setStyle({marginRight: margin + 'px'});
	});
});


// Expand height of some divs to include the height of some absolutely positioned elements within them

Event.observe(window, 'load', function() {
	$$('div.expandable-height').each(function(div) {
		var height = div.getHeight();
		var origHeight = height;
		div.select('div, img, ul').each(function(expand) {
			var bottom = expand.offsetTop + expand.getHeight();
			if (bottom > height) height = bottom;
		});
		if (height > origHeight) div.setStyle({height: height + 'px'});
	});
});


// Image gallery

var imageNum = 0;

function gallery(num, url, width, height, caption) {
	$('thumbnail-' + imageNum).removeClassName('current');
	imageNum = num;
	$('current-image').update('<img src="' + url + '" width="' + width + '" height="' + height + '" />');
	$('current-caption').update(caption);
	$('thumbnail-' + imageNum).addClassName('current');
}


// Clippings gallery

var clippingsGalleries = [];

function clippingsGalleryPage(postIdNum, direction) {
	var clippingsGallery = clippingsGalleries[postIdNum];
	clippingsGallery.pageNum += direction;
	if (clippingsGallery.pageNum >= clippingsGallery.numPages - 1) {
		clippingsGallery.pageNum = clippingsGallery.numPages - 1;
		$('clippings-' + postIdNum + '-next').setStyle({visibility: 'hidden'});
	}
	else {
		$('clippings-' + postIdNum + '-next').setStyle({visibility: 'visible'});
	}
	if (clippingsGallery.pageNum <= 0) {
		clippingsGallery.pageNum = 0;
		$('clippings-' + postIdNum + '-previous').setStyle({visibility: 'hidden'});
	}
	else {
		$('clippings-' + postIdNum + '-previous').setStyle({visibility: 'visible'});
	}
	$('clippings-' + postIdNum).update(clippingsGallery.pages[clippingsGallery.pageNum]);
}


// CSS Browser Selector   v0.2.5
// Documentation:         http://rafael.adm.br/css_browser_selector
// License:               http://creativecommons.org/licenses/by/2.5/
// Author:                Rafael Lima (http://rafael.adm.br)
// Contributors:          http://rafael.adm.br/css_browser_selector#contributors
var css_browser_selector = function() {
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):'notie '+is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
	var c=b+os+' js';
	h.className += h.className?' '+c:c;
}();
