(function(window, document, undefined){
    var oldOnload = window.onload;
    var base = document.getElementsByTagName('base')[0].href;
    var url = splitURL();
    
    function isEmpty(t){
	return t==undefined || t == '' || (t.length != undefined && t.length ==0);
    }

    function splitURL(u){
	var anchor, url, params, items, tmp, tmpa, i;
	if (u==undefined)
	    u = document.URL;
	tmp = u.split('#');
	anchor = tmp[1] || undefined;
	tmp = tmp[0].split('?');
	url = tmp[0];params=tmp[1];
	items = url.split('/');
	if (params != undefined){
	    tmp = params.split('&');
	    params = {};
	    for (i=0; i < tmp.length; ++i){
		tmpa = tmp[i].split('=');
		params[tmpa[0]] = decodeURIComponent(tmpa[1]);
	    }
	} else params = {};
	return {protocol: items[0],base: items[2],
		scheme: items[3],module: items[4],
		urlparams:items.slice(5,items.length),
		params: params,anchor:anchor
		};
    }
    function isBilderPage(){
	return url.module == 'bilder';
    }
    function isGalPage(){
	return isBilderPage() && url.urlparams!=undefined && url.urlparams[0]=='gal';
    }
    function isPicPage(){
	return isBilderPage() && url.urlparams!=undefined && url.urlparams[0]=='pic';
    }
    function isBilderLink(link){
	return link.search(/\/bilder\//)>-1;
    }
    function insertLink(url){
	var a = document.createElement('a');
	a.href = url;
	a.innerHTML = '↑ Zurück zum Artikel';
	if(isPicPage()){
	    var e = document.getElementById('galerienavigation');
	    var t = document.createTextNode(' | ');
	    e.insertBefore(a,e.childNodes[3]);
	    e.insertBefore(t,e.childNodes[3]);
	} else if (isGalPage()) {
	    var l = getBilderlisten(), p;
	    for (var i=0; i<l.length; ++i){
		p = document.createElement('p');
		p.appendChild(a);
		l[i].parentNode.insertBefore(p,l[i]);
	    }
	}
    }
    function hasArticleInfo(){
	return url.params !=undefined && url.params.art!=undefined;
    }
    function hasScrollInfo(){
	return url.params !=undefined && url.params.scr!=undefined;
    }
    function getBilderLinks(){
	var a = document.getElementsByTagName('a'), r, i;
	r = new Array();
	for(i=0;i<a.length;++i){
	    if(a[i].href != undefined){
		if(isBilderLink(a[i].href)) r[r.length] = a[i];
	    }
	}
	return r;
    }
    function getBilderlisten(){
	var uls = document.getElementsByTagName('ul'), r = new Array();
	for (var i=0; i<uls.length;++i){
	    var classes = uls[i].getAttribute('class').split(' ');
	    for (var j=0; j<classes.length;++j){
		if(classes[j] =='bilderliste'){
		    r[r.length] = uls[i];
		    break;
		}
	    }
	}
	return r;
    }
    function getArticleInfo(){
	if (isBilderPage()){
	    if (hasArticleInfo())
		return url.params.art;
	} else {
	    var s = isEmpty(url.scheme) ? '' : url.scheme;
	    if (!isEmpty(url.module))
		s+='/' + url.module;
	    if (!isEmpty(url.urlparams))
		s+='/' + url.urlparams.join('/');
	    return s;
	}
    }
    function getScrollPos(){
	var scrY = 0;
	if(typeof(window.pageYOffset ) == 'number'){
	    scrY = window.pageYOffset;
	} else if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
	    scrY = document.body.scrollTop;
	} else if(document.documentElement && 
		  (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
	    scrY = document.documentElement.scrollTop;
	}
	return scrY;
    }
    function makeURL(u){
	var s=u.protocol+'//'+u.base;
	if(!isEmpty(u.scheme))
	    s+='/'+u.scheme;
	if(!isEmpty(u.module))
	    s+='/'+u.module;
	if(u.urlparams != undefined && u.urlparams!=''){
	    s+='/'+u.urlparams.join('/');
	}
	var params= new Array();
	for (prop in u.params){
	    params[params.length]=prop+'='+encodeURIComponent(u.params[prop]);
	}
	if (params.length>0) s+='?'+params.join('&');
	if (u.anchor != undefined){
	    s+='#'+u.anchor;
	}
	return s;
    }
    function populateWithInfo(){
	var a = getBilderLinks();
	for(var i=0; i< a.length; ++i){
	    a[i].onclick = function(e){
		var targ;
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3)
		    targ = targ.parentNode;
		while(targ.tagName != 'A')
		    targ=targ.parentNode;
		var u = splitURL(targ.href);
		u.params.art= getArticleInfo();
		if(isBilderPage() && hasScrollInfo())
		    u.params.scr= url.params.scr;
		else u.params.scr=getScrollPos();
		document.location.href = makeURL(u);
		return false;
	    }
	}
    }
    function doScroll(){
	window.scrollTo(0,url.params.scr);
    }
    window.onload = function(){
	oldOnload();
	if (!isBilderPage() && hasScrollInfo())
	    doScroll();
	else if(isBilderPage && hasArticleInfo()){
	    var s = splitURL(base+url.params.art);
	    if(hasScrollInfo())
		s.params.scr = url.params.scr;
	    insertLink(makeURL(s));
	}
	populateWithInfo();
    };
    

})(window,document); 



