/* Picture fade-out transition */
function picTrans(picId, picUrl) {
	var pic = $j('#'+picId);
	newpic = pic.clone();
	newpic.attr('id', '#new'+picId);
	newpic.css('margin', '0');
	newpic.css('padding', '0');
	newpic.css('border', 'none');
	newpic.prependTo(pic);
	pic.css('background-image', 'url(\''+picUrl+'\')');
	newpic.fadeOut(2000, function() { $j(this).remove(); });
}

/* Cycle through the sidebar pics */
function cycleSidePics(idx) {
	next = function(i) {
		while (i >= sidepics.length) {
			i -= sidepics.length
		}
		return i
	}
	for (j=0; j<3; j++) {
		var i = next(idx + j);
		var thumb = sidepics[i];
		picTrans('pic'+(j+1), thumb);
	}
	// Pick a random next starting index
	j = idx;
	while (j == idx) {
		j = random(0, sidepics.length-1);
	}
	setTimeout('cycleSidePics('+j+')', 5000);
}

function random(a, b) {
	return Math.floor((b-a+1)*Math.random()) + a;
}

function prepareScroll() {
		var spacerHeight = $j('#menu').position().top - $j('#menu_container').position().top;
		var containerTop = $j('#menu').position().top;
		var containerBottom = $j('#ft').position().top - spacerHeight;
		var menuHeight = $j('#menu').outerHeight();
		
		function moveMenu() {
			var top = $j().scrollTop() + spacerHeight;
			var bottom = top + menuHeight;
			if (top < containerTop) {
				top = containerTop;
			}
			if (bottom > containerBottom) {
				top = containerBottom - menuHeight;
			}
			var time = Math.abs($j('#menu').position().top - top) / 0.5;
			$j('#menu').stop().animate({top: top + 'px'}, time);
		}
		
		$j(window).scroll(moveMenu);
		moveMenu()
}

function menuHover() {
	$j('#menu li').each(function() {
		var node = $j(this).context;
		node.onmouseover = function() { this.className += ' over'; };
		node.onmouseout = function() { this.className = this.className.replace('over', ''); };
		$j(this).click(function() {
			if ( ! clicked) {
				clicked = true;
				document.location = $j(this).children('a').attr('href');
			}
		});
	});
}
		
function imgUnderlineBug() {
	/*		$j(this).mouseover(function() { $j(this).toggleClass('over', true); });
		$j(this).mouseout(function() { $j(this).toggleClass('over', false); });*/
	$j('#content a').each(function() {
		if ($j(this).children('img').length > 0) {
			var node = $j(this).context;
			$j(this).css('background-color', 'transparent');
			node.onmouseover = function() { this.className += ' over'; };
			node.onmouseout = function() { this.className = this.className.replace('over', ''); };
		}
	});
}

function enlargePicTitle() {
	$j('#content a').each(function() {
		if ($j(this).attr('rel').contains('lightbox')) {
			$j(this).children().attr('title', 'Click to enlarge picture');
		}
	});
}

function externalLinks() {
	$j('#content a').each(function() {
		if ($j(this).attr('target').contains('_blank')) {
			$j(this).attr('title', 'Opens link in external window');
			if ($j.browser.msie) {
				$j(this).addClass('external');
			}
		}
	});
}

function loadSidePics() {
	if (sidepics.length >= 3) {
		// Preload pictures
		for (var i = 0; i<sidepics.length; i++) {
			$j('<img>').attr('src', sidepics[i]).remove();
		}
		cycleSidePics(random(0, sidepics.length-1));
	}
}

function fixCSS() {
	$j('p+p').css('margin', '1.4em 0 0 0');
}

var clicked = false;
var $j = jQuery.noConflict();

$j().ready(function() {
	menuHover();
	prepareScroll();
	imgUnderlineBug();
	//fixCSS();
	enlargePicTitle();
	externalLinks();
	loadSidePics();
});

