var currentscrolltop;

var gallery = new Array();

function addtogallery(description, size, img){
	
	gallery[gallery.length] = new Array(description, size, img);
	
}
function getprevious(img){
	
	for (var i = 0; i < gallery.length; i++){
		
		if (gallery[i][2] == img){
			
			switch (i){
				
				case 0:
				
					var j = gallery.length - 1;
					
				break;
				default:
				
					var j = i - 1;
					
				break;
				
			}
			
		}
		
	}
	
	showimage(gallery[j][0], gallery[j][1], gallery[j][2]);
	
}

function getnext(img){
	
	for (var i = 0; i < gallery.length; i++){
		
		if (gallery[i][2] == img){
			
			switch (i){
				
				case (gallery.length - 1):
				
					var j = 0;
					
				break;
				default:
				
					var j = i + 1;
					
				break;
				
			}
			
		}
		
	}
	
	showimage(gallery[j][0], gallery[j][1], gallery[j][2]);
	
}

function showimage(description, size, image){

	if (!get('absolute')){ return; }
	if (image == ''){
		get('absolute').style.display = 'none'; undobody();
		return;
	}
	
	var galleryimage = get('galleryimage');
	if (galleryimage){

		var sizes = size.split('x');
		
		var theimage = document.createElement('img');
		theimage.setAttribute('src',image);
		theimage.setAttribute('id','gallerypic');
		theimage.style.opacity = '0';
		theimage.alt = '';


		var closelink = document.createElement('span');
		closelink.innerHTML = '(close)';
		closelink.setAttribute('id','closelink');
		closelink.className = 'closelink';
		if (browser.isIE){
			closelink.onclick = function(){ showimage('','','') }
		}
		else {
			closelink.setAttribute('onclick','showimage(\'\',\'\',\'\')');
		}
		
		var thegallerydescription = document.createElement('div');
		thegallerydescription.style.opacity = '0';
		thegallerydescription.setAttribute('id','gallerydescription');
		
		var thedescription = document.createElement('p');
		thedescription.className = 'gallerydescription';
		thedescription.innerHTML = description;
		
		var popupnavigation = document.createElement('div');
		popupnavigation.className = 'popupnavigation';
		popupnavigation.setAttribute('id','gallerynavigation');
		popupnavigation.style.width = sizes[0]+'px';
		popupnavigation.style.height = sizes[1]+'px';
		
		//var vorige = document.createElement('a');
		var vorige = document.createElement('div');
		vorige.className = 'popupprevious';
		vorige.style.height = sizes[1]+'px';
		//vorige.setAttribute('href', 'javascript:getprevious(\''+image+'\')');
		if (browser.isIE){
			vorige.onclick = function(){ getprevious(image); }
			vorige.onmouseover = function(){ previous('block'); }
			vorige.onmouseout = function(){ previous('none'); }
		}
		else {
			vorige.setAttribute('onclick', 'getprevious(\''+image+'\')');
			vorige.setAttribute('onmouseover','previous(\'block\')');
			vorige.setAttribute('onmouseout','previous(\'none\')');
		}
		
		var vorigespancontainer = document.createElement('span');
		vorigespancontainer.style.height = sizes[1]+'px';
		
		var vorigespan = document.createElement('span');
		vorigespan.innerHTML = 'vorige';
		vorigespan.setAttribute('id','vorigespan');
		
		vorigespancontainer.appendChild(vorigespan);
		vorige.appendChild(vorigespancontainer);
		
		//var volgende = document.createElement('a');
		var volgende = document.createElement('div');
		volgende.className = 'popupnext';
		volgende.style.height = sizes[1]+'px';
		//volgende.setAttribute('href', 'javascript:getnext(\''+image+'\')');
		if (browser.isIE){
			volgende.onclick = function(){ getnext(image); }
			volgende.onmouseover = function(){ next('block') }
			volgende.onmouseout = function(){ next('none') }
		}
		else {
			volgende.setAttribute('onclick', 'javascript:getnext(\''+image+'\')');
			volgende.setAttribute('onmouseover','next(\'block\')');
			volgende.setAttribute('onmouseout','next(\'none\')');
		}
		
		var volgendespancontainer = document.createElement('span');
		volgendespancontainer.style.height = sizes[1]+'px';
				
		var volgendespan = document.createElement('span');
		volgendespan.innerHTML = 'volgende';
		volgendespan.setAttribute('id','volgendespan');
		
		volgendespancontainer.appendChild(volgendespan);
		volgende.appendChild(volgendespancontainer);
		
		popupnavigation.appendChild(vorige);
		popupnavigation.appendChild(volgende);
		
		if (browser.isIE){
			theimage.style.filter = 'alpha(opacity=0)';
			thegallerydescription.style.filter = 'alpha(opacity=0)';
		}
		
		galleryimage.innerHTML = '';
		galleryimage.appendChild(theimage);
		galleryimage.appendChild(popupnavigation);
		thegallerydescription.appendChild(thedescription);
		thegallerydescription.appendChild(closelink);
		galleryimage.appendChild(thegallerydescription);

		get('absolute').style.display = 'block'; 
		galleryimage.style.height = '10px';
		galleryimage.style.width = '10px';
		
		dobody(); 
		
		get('gallerycontainer').style.width = (parseInt(sizes[0])+20)+'px';
		resize(parseInt(sizes[0])+20,parseInt(sizes[1])+45);
		
	}

}


function undobody(){
	document.body.style.height = '';
	document.body.style.overflow = '';
	document.documentElement.scrollTop = currentscrolltop;
}
function dobody(){
	currentscrolltop = document.documentElement.scrollTop;
	//document.documentElement.scrollTop = 0;
	if (!browser.isIE){
		get('absolute').style.top = currentscrolltop+'px';
	}
	document.body.style.height = '100.1%';
	document.body.style.overflow = 'hidden';
}
function get(elementID){
	if (document.getElementById(elementID)){ return document.getElementById(elementID); }
	else { return false; }
}

function fadein(){
	
	var theblock = get('gallerypic');
	if (!theblock){ return; }
	
	var opacity = parseFloat(theblock.style.opacity);
	if (opacity < 1){
		switch (opacity){
			case 0:		theblock.style.opacity = 0.3;	break;
			case 0.3:	theblock.style.opacity = 0.5;	break;
			case 0.5:	theblock.style.opacity = 0.7;	break;
			case 0.7:	theblock.style.opacity = 0.8;	break;
			case 0.8:	theblock.style.opacity = 0.9;	break;
			case 0.9:	theblock.style.opacity = 1;		break;		
			
		}
		setTimeout('fadein()','100');
		return;
	}
	
	var theblock = get('gallerydescription');
	if (!theblock){ return; }
	
	var opacity = parseFloat(theblock.style.opacity);
	if (opacity < 1){
		switch (opacity){
			case 0:		theblock.style.opacity = 0.3;	break;
			case 0.3:	theblock.style.opacity = 0.5;	break;
			case 0.5:	theblock.style.opacity = 0.7;	break;
			case 0.7:	theblock.style.opacity = 0.8;	break;
			case 0.8:	theblock.style.opacity = 0.9;	break;
			case 0.9:	theblock.style.opacity = 1;		break;		
			
		}
		
		setTimeout('fadein()','100');
		return;
	}
	
}

function fadeinIE(){
	
	var theblock = get('gallerypic');
	if (!theblock){ return; }
	
	var opacity = parseInt(theblock.style.filter.replace('alpha(opacity=',''));
	if (opacity < 100){
		switch (opacity){
			case 0:			theblock.style.filter = 'alpha(opacity=30)';	break;
			case 30:		theblock.style.filter = 'alpha(opacity=50)';	break;
			case 50:		theblock.style.filter = 'alpha(opacity=70)';	break;
			case 70:		theblock.style.filter = 'alpha(opacity=80)';	break;
			case 80:		theblock.style.filter = 'alpha(opacity=90)';	break;
			case 90:		theblock.style.filter = 'alpha(opacity=1000)';	break;
		}
		setTimeout('fadeinIE()','100');
		return;
	}
	
	var theblock = get('gallerydescription');
	if (!theblock){ return; }
	
	var opacity = parseInt(theblock.style.filter.replace('alpha(opacity=',''));
	if (opacity < 1){
		switch (opacity){
			case 0:			theblock.style.filter = 'alpha(opacity=30)';	break;
			case 30:		theblock.style.filter = 'alpha(opacity=50)';	break;
			case 50:		theblock.style.filter = 'alpha(opacity=70)';	break;
			case 70:		theblock.style.filter = 'alpha(opacity=80)';	break;
			case 80:		theblock.style.filter = 'alpha(opacity=90)';	break;
			case 90:		theblock.style.filter = 'alpha(opacity=100)';	break;
		}
		
		setTimeout('fadeinIE()','100');
		return;
	}
	
}

function oldresize(desiredWidth, desiredHeight){
	
	var theblock = get('galleryimage');
	if (!theblock){ return; }
	
	var thewidth = parseInt(theblock.style.width);
	var theheight = parseInt(theblock.style.height);
	
	
	if (thewidth < desiredWidth){
		
		if (thewidth < (desiredWidth * 0.2) || thewidth > desiredWidth * 0.8){ 		rate = 8;	}
		else if (thewidth < (desiredWidth * 0.4) || thewidth > desiredWidth * 0.6){	rate = 16;	}
		else {																		rate = 32;	}
		
		if (thewidth+rate > desiredWidth){ thewidth = desiredWidth - rate; }
		theblock.style.width = (thewidth+rate)+'px';
		
	}
	else if (theheight < desiredHeight){
		
		if (theheight < (desiredHeight * 0.2) || theheight > desiredHeight * 0.8){		rate = 8;	}
		else if (theheight < (desiredHeight * 0.4) || theheight > desiredHeight * 0.6){	rate = 16;	}
		else {																			rate = 32;	}
		
		if (theheight+rate > desiredHeight){ theheight = desiredHeight - rate; }
		theblock.style.height = (theheight+rate)+'px';
		
	}
	else if (desiredWidth == thewidth && desiredHeight == theheight){
		get('gallerypic').style.display = 'block';
		if (browser.isIE){	fadeinIE();	}
		else {				fadein();	}
		return;
	}
	else {
		
		theblock.style.width = '0px';
		theblock.style.height = '0px';
		
	}
	
	setTimeout('resize('+desiredWidth+','+desiredHeight+')', 10);

}
var theblock = '';
function doresize(wid, hei){
	if (wid != ''){
		theblock.style.width = wid+'px';
	}
	else {
		theblock.style.height = hei+'px';
	}
}

function resize(desiredWidth, desiredHeight){
	
	var delay = 0;
	var speed = 15;
	theblock = get('galleryimage');
	if (!theblock){ return; }
	
	var thewidth = parseInt(theblock.style.width);
	var theheight = parseInt(theblock.style.height);

	while (thewidth < desiredWidth){
		delay += speed;
		if (thewidth < (desiredWidth * 0.2) || thewidth > desiredWidth * 0.8){ 		
			rate = 8;	
		}
		else if (thewidth < (desiredWidth * 0.4) || thewidth > desiredWidth * 0.6){	
			rate = 16;	
		}
		else {																		
			rate = 32;	
		}
		thewidth += rate;
		if (thewidth > desiredWidth){ thewidth = desiredWidth; }
		setTimeout('doresize(\''+thewidth+'\',\'\')',delay);
	}
	while (theheight < desiredHeight){
		delay += speed;		
		if (theheight < (desiredHeight * 0.2) || theheight > desiredHeight * 0.8){ 		
			rate = 8;	
		}
		else if (theheight < (desiredHeight * 0.4) || theheight > desiredHeight * 0.6){	
			rate = 16;	
		}
		else {																		
			rate = 32;	
		}
		theheight += rate;
		if (theheight > desiredHeight){ theheight = desiredHeight; }
		setTimeout('doresize(\'\',\''+theheight+'\')',delay);
	}
	delay += 100;
	setTimeout('doblocks()',delay);
	if (browser.isIE){	setTimeout('fadeinIE()',delay);	}
	else {				setTimeout('fadein()', delay);	}
	return;
	
}
function doblocks(){
	get('gallerypic').style.display = 'block';
	get('gallerynavigation').style.display = 'block';
	get('closelink').style.display = 'inline';
}
function previous(what){
	
	get('vorigespan').style.display = what;
	
}
function next(what){
	
	get('volgendespan').style.display = what;
	
}