// vim: tabstop=4 shiftwidth=4 noexpandtab

function rebuildPage(){

	/* clean table */
	var rows = $A($$('table').first().rows);
	var header = rows.shift();
	
	if(rows[0].cells[1].firstChild.innerHTML.toLowerCase() == 'parent directory') {
		Element.remove(rows.shift())  /* remove parent link */
	}

	Element.remove(header.cells[0]);

	rows.each(function(e){
		var type = $(e.cells[0]).down().getAttribute('alt');
		if(type){
			// remove trailing slashes
			type = type.replace(/\[\s*(.*)\s*\]/,"$1").toLowerCase();
		}
		else {
			type = "ukn";
		}

		Element.remove(e.cells[4]);
		Element.remove(e.cells[0]);
		var a = $(e.cells[0]).down();
		
		if(type == 'img') a.setAttribute('rel','lightbox[image]');
		if(type == 'dir') {
			a.innerHTML = decodeURI(a.href.replace(/.*\/(.*)\/$/,'$1'));
		}
		else {
			a.innerHTML = decodeURI(a.href.replace(/.*\/(.*)$/,'$1'));
		}
		a.addClassName(type);

	});

	/* play with location */
	var h2 = $$('h2').first();
	var loc = document.location;
	document.title = loc.href;

	var fix = loc.protocol+"//"+loc.hostname+(loc.port?":"+loc.port:"")+"/";
	var parts = loc.pathname.split('/').without('');

	h2.appendChild(Builder.node('a',{href: fix},[fix]));

	if(parts.length) {
		var last = parts.pop();
		parts.each(function(e){
			fix += e+"/";
			h2.appendChild(Builder.node('a',{href: fix},[e]));
			h2.appendChild(Builder._text('/'));
		});
		h2.appendChild(Builder._text(last+"/"));
	}

}
Event.observe(document,'dom:loaded',rebuildPage);

