// -*- Mode: Javascript; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4; -*-
$.fn.idle = function(time) {
	var o = $(this);
	o.queue(function() {
		setTimeout(function() {
			o.dequeue();
		}, time);
	});
	return this;
}
$(function() {
	$('#navcolumn ul').addClass('v2');
	$('#navcolumn ul li').prepend('<span>•</span>');
	
	$('.add_li_points').each(function() {
		var lis = $(this).children('li');
		lis = lis.not(':last');
		lis.after('\n<li><span>‧</span></li>\n');
	})
	
	$('#sitetitle[lg=en] a').html('<img src="/media/img/A-colorful-grey-world-trans.png" width="421" height="54" title="A colorful grey world"/>');
	$('#siteslogan[lg=en]').html('<img src="/media/img/Serge-Emond-virtual-dump-trans.png" width="189" height="25" title="Serge Émond’s virtual dump"/>');
	$('#sitetitle[lg=fr] a').html('<img src="/media/img/Un-monde-pas-si-gris-trans.png" width="406" height="53" title="Un monde pas si gris"/>');
	$('#siteslogan[lg=fr]').html('<img src="/media/img/La-poubelle-de-Serge-Emond-trans.png" width="189" height="25" title="La poubelle de Serge Émond"/>');

	$('#containerwrap').css('min-height', ($('#navcolumn').height() + 50) + 'px');
	
	$('#recentslide').cycle({
		speed: 750,
		timeout: 9000,
		pause: 0,
		before: function () {
			$('#recentslidetitle').html(this.alt);
		}
	});
	
	$('.article ul').addClass('v2');
	$('.article ul li').prepend('<span>•</span>');
	$('.article ol').addClass('v2');
	$('.article ol li').wrapInner('<span></span>');
	
	$('.article table tbody tr:first-child').addClass('first');
	$('.article table tbody tr:nth-child(odd)').addClass('odd');
	$('.article table tbody tr:nth-child(even)').addClass('even');
	$('.article table tbody tr:last-child').addClass('last');
	
	var toc = $('.article #dotoc');
	if (toc.size() > 0) {
		$('.article .text .section > h2').add('.article .text .section > h3').addClass('tocified');
		
		toc.append('<h2>Contents</h2>\n<ol></ol>');
		var tocbase = toc.children('ol');
		
		var ids = new Array();
		var cnts = new Array();
		$('.article .text .section .tocified').each(function () {
			var lvl = parseInt(this.tagName.substr(1,1));
			if (lvl < 2) return;
			
			if (cnts[lvl] === undefined) cnts[lvl] = 0;
			cnts[lvl]++;
			if (cnts[lvl+1] !== undefined) cnts[lvl+1] = undefined;
			
			if (ids[lvl-1] === undefined) ids[lvl-1] = '';
			var id;
			if (ids[lvl-1] === '') {
				ids[lvl] = cnts[lvl].toString();
			} else {
				ids[lvl] = ids[lvl-1] + '-' + cnts[lvl].toString();
			}
			
			var txt = $(this).text();
			
			$(this).prepend(''+ids[lvl].replace('-','.')+'. ').before('<a name="subh-'+ids[lvl]+'"></a>');
			$(this).wrapInner('<a class="subtle" href="#subh-top"></a>');
			
			var tocto;
			if (lvl < 3) {
				tocto = tocbase;
			} else if (cnts[lvl] == 1) {
				tocto = tocbase.children('li#toc-'+ids[lvl-1]);
				tocto.append('<ol></ol>');
				tocto = tocto.children('ol');
			} else {
				tocto = $('li#toc-'+ids[lvl-1]+' ol');
			}
			tocto.append('<li value="'+cnts[lvl]+'" id="toc-'+ids[lvl]+'"><a href="#subh-'+ids[lvl]+'">'+txt+'</a></li>');
		});
	}
});

$(function () {
	var ibase = window.location.protocol + '//' + window.location.host + '/';
	$('a').each(function (index) {
		var href = $(this).attr('href');
		var rel = $(this).attr('rel');
		if (rel == undefined) rel = '';
		else if (rel != '') rel += ' ';
		if (href == undefined) {
		} else if (href.substr(0,7) === 'mailto:') {
			$(this).attr('rel', rel + 'email');
		} else if (href.substr(0,3) === 'ftp'
			|| (href.substr(0,4) === 'http' && href.substr(0, ibase.length) !== ibase)
			) {
			$(this).attr('rel', rel + 'external');
		} else {
			$(this).attr('rel', rel + 'internal');
		}
	});
	// Setup analytics tracking
	if (typeof pageTracker != "undefined") {
		$('a[rel*=external]').click(function () {
			pageTracker._trackPageview('/outgoing/' + $(this).attr('href'));
		});
	}
	$('a[rel*=external]').attr('target', '_blank');
});

