var ppsGaleries = new Array();

function switchGalerie ( link, gid, pid ) {
	galerieBox = link.parentNode.parentNode;
	spans = galerieBox.getElementsByTagName('span');
	divs = galerieBox.getElementsByTagName('div');
	anchors = galerieBox.getElementsByTagName('a');
	imgs = galerieBox.getElementsByTagName('img');
	spans[2].innerHTML = 'Bild '+(pid+1)+' / '+ppsGaleries[gid].length+' &nbsp;';
	divs[(divs.length-1)].innerHTML = '<p>'+ppsGaleries[gid][pid]['comment']+'</p>';
	divs[0].innerHTML = ppsGaleries[gid][pid]['img_div'];
	if ( pid == (ppsGaleries[gid].length-1) ) {
		next = 0;
	} else {
		next = pid+1;
	}
	if ( pid == 0 ) {
		prev = (ppsGaleries[gid].length-1);
	} else {
		prev = pid-1;
	}
	anchors[2].href = ppsGaleries[gid][pid]['href_prev'];
	anchors[2].onclick = function () {switchGalerie(this, gid, prev); return false;};
	anchors[3].href = ppsGaleries[gid][pid]['href_next'];
	anchors[3].onclick = function () {switchGalerie(this, gid, next); return false;};
}

function makeGalerie(link, gid, pid) {
	div = link.parentNode.parentNode.parentNode;
	ppsGaleries[gid]['preview'] = div.parentNode.parentNode.parentNode.parentNode.cloneNode(true);
	captions = div.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('caption');
	captions[0].removeChild(captions[0].firstChild);
	captions[0].removeChild(captions[0].firstChild);
	caption = captions[0].innerHTML;
	newBox = document.createElement('div');
	widths = div.style.width;
	width = widths.substring(0, widths.length-2) * div.parentNode.parentNode.getElementsByTagName('td').length;
	newBox.className = 'galeriebox';
	newBox.style.width = width+'px';
	quicklink = document.createElement('a');
	quicklink.id = 'galerie'+gid;
	newBox.appendChild(quicklink);
	captionspan = document.createElement('span');
	captionspan.innerHTML = '<a onclick="showPreview(this,'+gid+');" style="cursor: pointer;" title="Minimieren"><img alt="minimize" src="'+wgScriptPath+'/extensions/imagegallery/icons/minimize_inactive.png"/></a>&nbsp;'+caption;
	captionspan.style.cssText = 'float: left;';
	newBox.appendChild(captionspan);
	navspan = document.createElement('span');
	navspan.style.cssText = 'float: right;';
	navspan.appendChild(document.createElement('span'));
	nextlink = document.createElement('a');
	prevlink = document.createElement('a');
	nextlink.innerHTML = '<img alt="Next" src="'+wgScriptPath+'/extensions/imagegallery/icons/next_inactive.png"/>';
	nextlink.setAttribute('title', 'Weiter');
	prevlink.innerHTML = '<img alt="Prev" src="'+wgScriptPath+'/extensions/imagegallery/icons/prev_inactive.png"/>';
	prevlink.setAttribute('title', 'Zurück');
	navspan.appendChild(prevlink);
	navspan.innerHTML = navspan.innerHTML+'&nbsp;';
	navspan.appendChild(nextlink);
	newBox.appendChild(navspan);
	newBox.appendChild(document.createElement('div'));
	textbox = document.createElement('div');
	textbox.className = 'galerietext';
	newBox.appendChild(textbox);
	div.parentNode.parentNode.parentNode.parentNode.parentNode.replaceChild(newBox ,div.parentNode.parentNode.parentNode.parentNode);
	switchGalerie ( nextlink, gid, pid );
}

function makeGalerie2(link, gid, pid) {
	links = link.parentNode.parentNode.parentNode.getElementsByTagName('a');
	makeGalerie(links[2], gid, pid);
}

function showPreview(link, gid) {
	link.parentNode.parentNode.parentNode.replaceChild(ppsGaleries[gid]['preview'], link.parentNode.parentNode);
}
