/** _autoindex/autoindex.js
	Copyright (C) Lee Goddard. All Rights Reserved.
	You may use with the GPL Licence.

	This is an ugly turd of a script
	that works well
	and needs a day or so to turn OO
	Shame on me, but it wasn't meant to
	get this interesting!

	Updated: 2008-11-18

**/

var version = 2.1;
var linked_items = 0;
var my_dir = '/_autoindex';
var header_page_name = '_header.html';
var footer_page_name = '_footer.html';
var dir_intro_page_name  = 'DIRINTRO.html';
var local_icons_dir = my_dir + '/_icons_local/';
var headlines_without_bodies = true;
var sub_dirs = new Array();
var root_dirs = new Array();
// Include paragraphs of file size/modified in HTML?
var include_stats = 'inline';

var loader_img = new Image;
loader_img.src = my_dir + '/ajax-loader-small.gif';

// Use CGI to create images of headline text?
var images_for_headlines = true;

// Include icons in headlines:
var icons_in_headlines = false;

// Adds this to URIs of pages it embeds to prevent html-header/extras:
var embed_flag_for_uri = 'embed';

// Add no headers, links, etc?
var embedded_in_iframe = document.location.search.match( embed_flag_for_uri )? true : false;
var do_nothing = false;
var other_items_label; // may become an object

// sss?
var this_location = document.location.href.toLowerCase();
if (this_location.indexOf('server-sidesystems.ltd.uk') > -1
 && this_location.indexOf('/sss') == -1){
	document.location.replace('http://www.server-sidesystems.ltd.uk/sss/');
	do_nothing = true;
}

// two hosts, two headers and footers
var uri_root = "";
if (this_location.indexOf('/sss')>-1){
	uri_root = "/sss";
}

var is_inline = false;
var body_el;

var frontpage = false;
if ( this_location.match(
	/(localhost|\.net|\.uk|\.com)(\/?|\/index\.s?html?)?$/
)){
	frontpage = true;
}

var is_index = document.location.href.match(/([^\/]+)$/i);
if (is_index){
	if (!is_index[0] || is_index[0].match(/index/i)){
		is_index = true;
	} else {
		is_index = false;
	}
} else {
	is_index = true;
}

var in_root_dir = false;
var oblq = document.location.href.match( /^http:\/\/[\w\d.-]+\/+(.*)$/ );
if ( oblq.length > 1){
	in_root_dir = ! oblq[1].match( /\// )
}

// #navmenu = top nav menu
// #dirs_index_div = sub nav menu
// .article .readme .standfirst

var files_index_div;
var dirs_to_add    = new Array();
var divs_to_remove = new Array();

var text_from_ext = {
	"odt": "Open Documenet Text",
	"rtf": "Rich Text Format",
	"doc": "Microsoft Word",
	'pdf': "Portable Document (Acrobat)",
	'xml': "XML",
	'tif': "Uncompressed image",
	"zip": "Compressed file"
};

var types_from_ext = {
	"odt": "bin",
	"rtf": "bin",
	"doc": "bin",
	'pdf': "bin",
	'xml': "markup",
	'tif': "bin",
	"zip": "bin",
	'html': 'markup',
	'htm': 'markup',
	'txt': 'markup'
};

var types_ext_ignore = {
	'db'	: 1,
	'ico'	: 1
};

var months = {
	'Jan' : '01',
	'Feb' : '02',
	'Mar' : '03',
	'Apr' : '04',
	'May' : '05',
	'Jun' : '06',
	'Jul' : '07',
	'Aug' : '08',
	'Sep' : '09',
	'Oct' : 10,
	'Nov' : 11,
	'Dec' : 12
}

Element.extend({
	/*
	Property: adoptTop
		Inserts the passed elements inside the Element.

	Arguments:
		accepts elements references, element ids as string, selectors ($$('stuff')) / array of elements, array of ids as strings and collections.
	*/

	adoptTop: function(){
		var elements = [];
		$each(arguments, function(argument){
			elements = elements.concat(argument);
		});
		$$(elements).injectTop(this);
		return this;
	}
});


var Page = new Class({
    initialize: function(args){
		if (!args) args = {};
		this.index		= args.index	|| -1;
        this.icon		= args.icon 	|| '';
        this.extension	= args.extension || '';
        this.filename 	= args.filename || '';
        this.href		= args.href		|| '';
		this.modfified	= args.modified	|| "";
		this.size		= args.size		|| "";
        this.text		= args.text		|| '';
		this.no_text	= args.no_text	|| false;
		this.type		= args.type		|| '';
		this.up			= args.up		|| false; // Is a link up a level?

		// ISO the date for sorting
		if (typeof this.modified != 'undefined'){
			this.modify_modified();
		}
	},

	modify_modified: function(){
		var d = this.modified.match( /^\s*(\d{2})-(\w{3})-(\d{4})\s(\d\d:\d\d)\s*$/ );
		this.modified = d[3] +"-"+ months[d[2]] +"-"+ d[1] +" "+ d[4];
	},

	set_extension: function(){
		if (! this.href) alert( "Missing HREF");
		this.extension = this.href.match(/\.(\w+)$/ );
		if (this.extension) this.extension = page.extension[1];
	},

	get_icon: function(src){
		if (!src){
			if (this.extension.match(/^(txt|html?|pdf|rss)$/i)){
				src = this.extension + '.png';
			}
			else if (this.extension.match(/^(tif|gif)$/i)){
				src = 'image.png';
			}
			else if (this.extension.match(/^(png|jpg)$/i)){
				src = 'image2.png';
			}
			else if (this.extension == 'xml'){
				src = 'layout.png';
			}
			else if (this.extension.match(/^(odt|doc|rtf)$/i)){
				src = 'ball.gray.png';
			}
			else if (this.icon){
				src = src == this.icon? this.icon : local_icons_dir + 'unknown.png';
			}
		}
		if (src.indexOf('/')==-1){
			src = local_icons_dir + src
		} else {
			src = src.replace(/icons(_local)?\//g, local_icons_dir );
		}
		src = src.replace(/gif/g, 'png');
		this.icon = src;

		var i = new Element("img",{
			"class": "icon",
			"src":   this.icon,
			"title": this.icon,
			"alt": this.icon
		});
		return i;
	},

	more_link: function( href ){
		var a = new Element('a', {
			'title' : 'View this...',
			'href'  : href,
			'class' : 'more'
		});
		a.appendText('Read »>');
		return a;
	},

	entity: function (str, mode) {
		str = (str) ? str : "";
		mode = (mode) ? mode : "string";

		var e = document.createElement("div");
		e.innerHTML = str;

		if (mode == "numeric") {
			return "&#" + e.innerHTML.charCodeAt(0) + ";";
		}
		else if (mode == "utf16") {
			var un = e.innerHTML.charCodeAt(0).toString(16);
			while (un.length < 4) un = "0" + un;
			return "\\u" + un;
		}
		else return e.innerHTML;
	},

	add_dir: function(){
		sub_dirs.push( this );
		var li = new Element('li');
		var a  = new Element("a",{
			"title": this.filename,
			"href":  this.href
		});
		var t = this.text.replace(/_+/, ' ');
		a.appendText( this.entity('&#9698;') + '  ' + t ); // Arrow in text
		li.adopt(a);
		dirs_to_add.push( li ); // Store to render when header loaded
	},


	add_binary: function( into ){
		if ( this.filename == 'favicon.ico' ) return;
		if (!into) into = files_index_div;

		this.text = text_from_ext[this.extension] || '';
		var src = "";
		// thumb nail links to larger image,which is not displayed
		if (this.href.substr( this.href.length -6).match(/_t\.(jpg|png)$/) ) {
			this.type = 'thumb';
			src = this.href;
			// Link to the large image
			this.href = this.href.substr(0, this.href.length -6)
				+ this.href.substr( this.href.length -4,4);
			var f = this.href.match(/([^\/]+)\.(jpg|png)$/);
			this.filename = f[1];
			this.size = '';
			divs_to_remove.push( this.filename + "." + this.extension );
		}

		var i = this.get_icon(src);

		var h3 = new Element('h3');
		h3.adopt(i);
		var a  = new Element("a",{
			"title": this.filename + ( this.text?  "("+this.text+")" : "" ),
			"href":  this.href,
			"target": '_blank'
		});
		a.appendText( this.filename );
		h3.adopt(a);
		var binfile = new Element('div', {
			"class": "binfile",
			"id": this.type=='thumb'? 'thumb_'+this.filename : this.filename
		});
		$(into).adopt(binfile);
		binfile.adopt(h3);

		if (include_stats && (this.modified || this.size )){
			var p = new Element("p", {"class":"stats"});
			if (this.text){
				var type = new Element("span", { 'class': 'filetype' });
				p.appendText( this.text +". ");
			}
			if (this.modified){
				var mod	 = new Element('span', { "class": "modified" });
				mod.appendText( this.modified );
				p.adopt(mod);
			}
			if (this.size){
				var size = new Element('span', { "class": "size" });
				size.appendText( this.size);
				p.adopt(size);
			}
			binfile.adopt(p);
		}
	},

	add_simple_headline_from_title: function () {
		var title_only = new Element('div', {
			"class": "title_only",
			"id":    "title_only" + this.index
		});
		files_index_div.adopt(title_only);
		this.add_headline_from_title( title_only );
		var bod = new Element('div', {
			"class": "text",
			"id":    "text" + this.index
		});
		title_only.adopt(bod);
	},

	add_headline_from_title: function( into ){
		if (!into) into = files_index_div;
		var a  = new Element("a",{
			"title": this.filename,
			"href":  this.href
		});

		if (images_for_headlines){
			var dim,size;
			if (is_inline){
				dim  = "220x100";
				size = "24";
			} else {
				size = "16";
				dim = "300x300";
			}

			var img = new Element('img',{
				"border": 0,
				"src":	"/cgi-bin/title.cgi?"
					+ escape( this.text )
					+ "&" + dim
					+ "&" + size
					+ "&" + 'orange',
				"title": this.text,
				"alt": this.text
			});
			a.adopt( img );
			$(into).adopt( a );
		}
		else {
			a.appendText( this.text );
			var h3 = new Element('h3');
			if (icons_in_headlines){
				var icon = this.get_icon();
				h3.adopt(icon);
			}
			h3.adopt(a);
			$(into).adopt(h3);
		}

		if (include_stats){
			var p	 = new Element("p", {"class":"stats"});
			var mod	 = new Element('span', { "class": "modified" });
			mod.appendText( this.modified );
			p.adopt(mod);
			var size = new Element('span', { "class": "size" });
			size.appendText( this.size);
			p.adopt(size);
			$(into).adopt(p);
		}
	},


	add_headline_and_standfirst: function( ){
		if ( this.filename.match(/^(sitemap|google).*?(ml|>)$/) ) return;
		if ( this.filename.match(/^robots\.txt$/) ) return;

		var into = files_index_div;
		var standfirst = new Element('div', {
			"class": "standfirst",
			"id":    "standfirst" + this.index
		});
		$(into).adopt(standfirst);
		this.add_headline_from_title( standfirst );
		var bod = new Element('div', {
			"class": "text",
			"id":    "text" + this.index
		});
		$(standfirst).adopt(bod);

		var elem = this.more_link( this.href );
		add_loader_to(bod);
		new Ajax( this.href, {
			method: 'get',
			onComplete: function(){
				bod.setHTML( this.response.text );
				if (standfirst.getText()){
					bod.setHTML( standfirst.getText().substring(0,200) + "..." );
				}
				bod.adopt( elem );
			}
		}).request();
	},

	// Load a complete article into an iframe: the article
	// loads this JS and onLoad, checks the URI for embed_flag_for_uri
	add_whole_article : function(){
		if (this.filename == dir_intro_page_name) return;

		var article = new Element('div', {
			"class"			: "embed_article",
			"id"			: "article" + this.index
		});
		/**
		var article = new Element('iframe', {
			"class"			: "embed_article",
			"id"			: "article" + this.index,
			"frameborder"	: 0,
			"style"			: "background: red"
		});
		**/
		$("content").adoptTop(article);
		add_loader_to(article);
		new Ajax( this.href, {
			method: 'get',
			onComplete: function(){
				article.setHTML( this.response.text );
			}
		}).request();

		var label = new Element('div', {
			"class"			: "embed_article_label",
			"id"			: "label" + this.index
		});
		label.setHTML("<a title='Latest content' href='"+this.href+"'>Latest content</a>");
		$("content").adoptTop(label);
	},

	add_article_frag : function( ){
		if (this.filename == dir_intro_page_name) return;

		var into = files_index_div;
		var article = new Element('div', {
			"class": "article",
			"id":    "article" + this.index
		});
		$(into).adopt(article);
		this.add_headline_from_title( article );
		var bod = new Element('div', {
			"class": "text",
			"id":    "text" + this.index
		});
		$(article).adopt(bod);
		var elem = this.more_link( this.href );
		add_loader_to(bod);
		new Ajax( this.href, {
			method: 'get',
			onComplete: function(){
				// bod.setHTML( this.response.text );
				// bod.setHTML( article.getText().substring(0,1000) + "..." );
				// bod.adopt( elem );
				bod.setHTML( this.response.text );
				var txt = "";
				if (bod.getText()){
					txt = trim_txt(bod.getText().substring(0,2000));
					bod.setHTML( txt );
					bod.adopt( elem );
				}
			}
		}).request();
	}
});


function main_layout(){
	other_items_label = new Element('div', {
		"class"			: "other_items_label",
		"id"			: "other_items_label"
	});
	other_items_label.setHTML("Other items in this section");
	$("newcontent").adopt(other_items_label);

	files_index_div = new Element('div', {
		"id": "files_index_div",
		"class": "files_index_div"
	});
	$("newcontent").adopt( files_index_div );
}


/** Process autoindex in src
	return a collection of Pages
**/
function pages_from_index( src, src_href, src_dir ){
	// if (!$$('autoindex')) return;

	// Don't process our own (private) dir
	if (src_href.indexOf(my_dir) > -1) return;

	var idx			= 0;
	var pages		= new Array();
	var re_file 	= /\s*(\d+-\w+-\d+\s+\d+:\d+)\s+([\dK.]+)\s+(.*?)\s*$/mi;
	var re_dir  	= /\s*(\d+-\w+-\d+\s+\d+:\d+)\s+(-)\s*$/mi;
	var re_up   	= /<a href="\/([^"]+)">([^<]+)<\/a>/gi; // "
	var re_page_name = new RegExp( /([^\/]+)\/*$/ ); /* */
	var f;

	// Iterate over index table cells
	src.getElements('img').each( function(i){

		page = new Page;
		page.get_icon(i.src);
		page.no_text	= (i.alt != '[TXT]');
		if (i.alt == '[DIR]') page.type	= 'dir'

		var a 		  = i.getNext();
		// Allow us to specify the dir of this href
		// since stringifying it seems to attempt to make it
		// absolute based upon the URI of the code being executed,
		// rather than the URI which originated the text being processed
		if (src_dir){
			var href 	= re_page_name.exec(a.href);
			// alert( href );
			page.href	= src_dir + '/' + href[1];
		} else {
			page.href 	  = a.href;
		}

		page.filename = a.getText();

		page.set_extension();

		if (src_href != page.href
			&& ! page.filename.match(/parent directory/i)
			&& page.filename != dir_intro_page_name
			&& ! page.filename.match( /^(google8c.*|sitemap.xml|robots.txt)$/ )
		){

			var t = a.nextSibling;
			if ( $type(t) == 'whitespace') t = t.nextSibling;
			if ( $type(t) == 'textnode'){
				if (page.type == 'dir' , f = re_dir.exec( t.nodeValue) ){
					if (!in_root_dir){
						if (f){
							// No dirs from root, these are coded literally in header file so I can store shite on the site
							page.text = page.filename; // .substring(0, page.filename.length);
							page.text = page.text.replace(/\//g,' ');
							pages.push( page );
						}
					}
				}
				else if( f = re_file.exec( t.nodeValue) ){
					if (page.size.toLowerCase() != '0k' ){
						page.type = types_from_ext[page.extension] || 'unknown';
						if (! types_ext_ignore[page.extension]
						&& !(in_root_dir && page.filename.match(/\d+\.s?html?$/) ) // ignore err pages in root
						){
							page.modified = f[1];
							page.modify_modified();
							page.size = f[2];
							page.text = f[3] || page.filename;
							page.index = idx++;
							pages.push( page );
						}
					}
					page = new Page();
				}
			}
		}
	});

	// Order
	var sorted = new Array;
	pages.each( function(j){
		if (j.type == 'markup') sorted.push(j)
	});

	pages.each( function(j){
		if (j.type == 'dir') sorted.push(j)
	});

	pages.each( function(j){
		if (j.type != 'dir' && j.type != 'markup') sorted.push(j)
	});

	pages = '';
	return sorted;
}


// Render the new, updated index
function render_my_index(sorted, expand_first){
	// Render
	var i=0;

	sorted.each( function(j){
		if (document.location.href != j.href ){
			if (j.type == 'markup'){
				i++;
				if (i==1 && expand_first){
					// j.add_article_frag()
					j.add_whole_article();
				}
				else {
					++ linked_items;
					if (headlines_without_bodies){
						j.add_simple_headline_from_title()
					} else {
						j.add_headline_and_standfirst()
					}
				}
			}
			else if (j.type == 'dir'){
				j.add_dir();
			}
			else {
				j.type = 'binary';
				j.add_binary();
			}
		}
	});

	divs_to_remove.each( function(i){
		$(i).setStyle('display','none');
		-- linked_items;
	});

	// No files in dir? Remove label etc
	if (sorted.length == 0){
		other_items_label.remove();
		files_index_div.remove();
		// 	other_items_label.style.display = 'none';
	}

} // Ends render_my_index


/** Sets active link in #header #navmenu
	and populates root_dirs
**/
function process_navmenu(){
	var navmenu;
	// Render sub-dirs to the div
	if (dirs_to_add.length){
		dirs_to_add.each( function(i){
			$('dirs_index_div').adopt( i );
		});
	} else if ($('dirs_index_div')){
		$('dirs_index_div').remove();
		// $('dirs_index_div').setHTML("");
	}

	if (navmenu = $('navmenu')){

		var h = document.location.href + "/";
		h = h.replace(/[\/]+$/, '/?');
		h = h.match(/(http:\/\/[^\/]+\/[^\/]+)(.*)$/);

		if (!h) return;

		h = new RegExp( h[1] +'$' );

		navmenu.getChildren().each( function(i){
			var a = i.getChildren()[0];
			root_dirs.push(
				new Page({
					"type"		: "dir",
					// "filename"	: "/" + f[1],
					"href"		: a.href
				})
			);
			if ( a.href.match( h ) ) a.setProperty("class",'active');
		});
	}
}




function process_subdirs(){
	/** Create a hidden div into which to load the pages
		so we can DOM parse them
		Then, foreach each sub-dir, get index, get latest file in index, load latest of all ...?
	**/
	var pages = new Array();
	var into = new Element('div', {
		'id'	: 'hidden',
		'style' : 'display:none;visability:none;width:1px;height:1px'
	});
	body_el.adopt( into );

	var dirs = frontpage? root_dirs : sub_dirs;
	dirs.each( function(dir){
		var html = SJAX.load( dir.href );
		into.setHTML( html );
		pages.push( pages_from_index(into, dir.href) );
	});
}


function add_loader_to( to ){
	if (!to) return;
	var img = new Element('img',{
		src: loader_img.src,
		alt: "Loading...",
		title: "Loading..."
	});
	to.adopt(img);
}


// Adopted from Mootools forum:
var SJAX = {
	request: null,
	loaded: {},
	load: function() {
		for (var i = 0, len = arguments.length; i < len; i++) {
			var filename = arguments[i];
			if (!this.loaded[filename]) {
				if (!this.request) {
					if (window.XMLHttpRequest) this.request = new XMLHttpRequest;
					else if (window.ActiveXObject) {
						try {
							this.request = new ActiveXObject('MSXML2.XMLHTTP');
						} catch (e) {
							this.request = new ActiveXObject('Microsoft.XMLHTTP');
						}
					}
				}
				if (this.request) {
					this.request.open('GET', filename, false); // synchronous request!
					this.request.send(null);
					if (this.request.status == 200) {
						return this.request.responseText
					}
				}
			}
		}
	}
}



window.addEvent('domready', function(){
	var footer;
	if (do_nothing) return 1;

	new Asset.css( uri_root + my_dir + '/autoindex.css');

	body_el = $$(document.getElementsByTagName('body'));
	if (body_el.length) body_el = body_el[0]

	if (embedded_in_iframe) {
		// No chance unless evalScripts is on
		body_el.id = 'embed';
	}

	else {
		if (!$('header')){
			body_el.adoptTop(
				new Element('div', {'id':'header'})
			);
		}

		if (!$('centre')){
			var existing_cont = new Array;
			var node;
			while ( node = $('header').getNext() ){
				if (node.id && node.id == 'footer') break;
				existing_cont.push(node);
				node.remove();
			}
			var centre = new Element('div', {'id':'centre'});
			centre.injectAfter( 'header' );
			var content = new Element('div', {'id':'content'});
			content.adopt( existing_cont );
			centre.adopt( content );
		}

		// Load the pre-defined header page that contains the site-wide navmenu
		add_loader_to($('header'));
		new Ajax(uri_root + my_dir + '/'+header_page_name, {
			method: 'get',
			update: $('header'),
			onComplete: function(){
				process_navmenu();
				// process_subdirs();
			}
		}).request();

		// Embedded in a normal article page - call the dir index in a new div
		if (!$('autoindex') ){
			// Don't add to existing indexes
			if (!document.location.href.match(/(\/+index\.s?html?)$/) ){
				is_inline = true;
				body_el.id = 'article';
				// Add it
				var inline_index = new Element('div', {
					"id": "inline_index",
					"class": "inline_index"
				});
				// inline_index.injectAfter( $('header') );
				$('content').adoptTop(inline_index);
				add_loader_to($('inline_index'));

				var newcontent = new Element('div', {
					"id": "newcontent",
					"class": "newcontent"
				});
				inline_index.adopt( newcontent );

				new Ajax('./', {
					method: 'get',
					update: $('inline_index'),
					onComplete: function(){
						inline_index.empty;
						inline_index.setHTML = this.response.text;
						main_layout();
						var pages = pages_from_index( $('autoindex'), document.location.href );
						render_my_index(pages, false);
					}
				}).request();

				footer = new Element('div', {
					'id'	: 'footer'
				});
				body_el.adopt( footer );
			}
		}

		// Has autoindex element added by mod_autoindex
		else {
			main_layout();
			var pages = pages_from_index( $('autoindex'), document.location.href );
			render_my_index(pages, true);
		}

		// Load the pre-defined footer page that contains the site-wide copyright & links
		if (typeof footer == 'undefined') {
			footer = $('footer');
		}
		add_loader_to( footer );
		new Ajax( uri_root + my_dir + '/'+footer_page_name, {
			method: 'get',
			update: footer
			//, onComplete: function(){ alert(this.response.text) }
		}).request();

		if ( is_index ){
			// May include a dir summary file?
			new Ajax( dir_intro_page_name, {
				method: 'get',
				// evalScripts: true,
				onComplete: function(){
					if (!$('readme')){
						var readme = new Element('div', {
							"id": "readme",
							"class": "readme"
						});
						readme.setHTML( this.response.text );
					} else {
						$('readme').adopt( this.response.text );
					}
					$('content').adoptTop(readme);
					process_latest();
				}
			}).request();
		} else {
			process_latest();
		}
	}
});


function process_latest(){
	/** PROCESS
		<ul class='latest'>
			<li title='Latest reading' id='/reading'></span></li>
			<li title='Latest news' id='/news'></span></li>
			<li title='Latest writing' id='/essays'></span></li>
		</ul>
	**/

	// Temp place to store index for sub-dirs
	var temp = new Element('div');

	// Iterate through <div class='latest'>
	$$('.latest').each( function( div_latest ){

		if( typeof div_latest.children == 'undefined'){
			div_latest.children = div_latest.getChildren();
		}

		// For every element under class='latest'
		for (var i=0; i < div_latest.children.length; i++){

			// Get the index for directory div_latest.children[i].id;
			var uri = div_latest.children[i].id;
			var title = div_latest.children[i].title;
			if (uri){
				new Ajax( uri, {
					uri: uri,
					title: title,
					method: 'get',
					onComplete: function(){
						// Populate div and process
						temp.setHTML( this.response.text );

						// var w = window.open('');
						// w.document.write( "<xmp>"+this.response.text );

						var pages = pages_from_index( temp, this.options.uri, this.options.uri );
						var into_id = this.options.uri;
						var get_doc_uri =  pages[0].href;
						var section_title = this.options.title;

						new Ajax( pages[0].href, {
							method: 'get',
							onComplete: function(){
								// Populate div and process
								temp.setHTML( this.response.text );
								var text = temp.getText().substring(0,500);
								text = trim_txt(text);
								text = '<h3><a title="'+ section_title + '" href="' +get_doc_uri + '">'
									+ section_title
									+ '</a></h3>'
									+ text
									+ '<a style="text-decoration:none" title="'+ section_title + '" href="' +get_doc_uri + '">...&#8594;</a>';
								$(into_id).setHTML( text );
							}
						}).request();
					}
				}).request();
			}
		}
	});
}

function trim_txt( txt ){
	txt = txt.replace(/^(.*?[\n\r\f]+){2}/,'');		// cut the title
	txt = txt.replace(/[\n\r\f]+/g, '<br/>');
	txt = txt.replace(/[^ ]+$/,'');
	txt = txt.replace(/(\s*<br\/>\s*){2,}/,'<br/>');
	return txt;
}

function over( it ){
	if ( it.src.match(/-opaque.png$/) ){
		it.src = it.src.replace(/-opaque.png$/, '-norm.png');
	} else {
		it.src = it.src.replace(/-norm.png$/, '-opaque.png');
	}
}

