// JavaScript Document

var servId="";
var albumId="";
var albumCnt="";
var currentStart=1;
var currentStop=20;
var currentImage=0;
var imagesPerPage=20;
var slideShow=false;
var flagLockNavigation=false;


function downloadCurrentImage(){
	if ( $('td_filename').innerHTML != "&nbsp;" ){
		window.location = 'http://fotoalbums.server8.eu/image_downloader.php?serv_id='+servId+'&album_id='+albumId+'&filename='+$('td_filename').innerHTML;	
	}
}


function fotoAlbum(servId, albumId, albumCnt){
	this.servId=servId;
	this.albumId=albumId;
	this.albumCnt=albumCnt;
	
	showImageButtons(1,albumCnt);
	setFadeImage();
}

// initially set all thumbs to opacity .85
function setFadeImage(){
	var i=1;
	
	for( i; i<imagesPerPage+1; i++){
//		new Effect.Opacity($('thumb_'+i), {duration:0.0, from:1.0, to:0.85});	
	}
}

function onMouseOutKeepHighlight(id){
	if( id == currentImage ){
		$('div_link_'+id).className = 'hover_nummering';
	}
}


function showImageButtons(start,stop){
	currentStart = start;
	
	if ( start <= stop ){
		var strImageButtonHTML='<div id="div_link_%i%" class="nummering" onMouseOver="this.className=\'hover_nummering\';" onMouseOut="this.className=\'nummering\';onMouseOutKeepHighlight(%i%);" style="float:left;"><a href="%i%" class="link_nummering" onclick="imageButtonClicked(%i%);return false;" onFocus="this.blur();">%i%</a></div>'
		
		
		imageButtonsHolder = $('menuImageButtons');
		imageButtonsHolder.innerHTML="";
		
		Element.hide(imageButtonsHolder);
	
		var stopAt = 0;
		
		if ( (start+imagesPerPage) > stop){
			stopAt = parseInt(stop)+1;
		}else{
			stopAt = start+imagesPerPage;
		}
		
		for (var i=start;i<stopAt; i++){
			new Insertion.Bottom(imageButtonsHolder, strImageButtonHTML.replace(/%i%/g,i));
		}
		
		currentStop = i-1;
		new Effect.Appear(imageButtonsHolder, {duration:.3});
		
		showImages(start,stop);
	}else{
		
	}
}


function showFirstRange(){
	showImageButtons(1,1+imagesPerPage);
}

function showLastRange(){
	var m = Math.floor(albumCnt / imagesPerPage);

	var to = ((m*imagesPerPage) + 1) + imagesPerPage; 
		
	if (to >= albumCnt ){
		to = albumCnt;
	}
	
	showImageButtons((m*imagesPerPage)+1,to);
}

function showImages(start,stop){
	for( var i=1; i<=imagesPerPage+1; i++ ){
		if ( start+i-1 <= stop){
			if( isUndefined($('thumb_'+i)) == false){
				Element.show($('thumb_'+i));
				Element.show($('dia_'+i));
			
				var current_image = $('thumb_'+i);
					current_image.src = imgObject[start+i-1].src;
					current_image.filename = imgObject[start+i-1].filename;
							
					//new Effect.Opacity(current_image, {duration:0.0, from:1.0, to:0.75});
						
					current_image.onmouseover = mouseGoesOver;
					current_image.onmouseout = mouseGoesOut;
					current_image.style.border='1px solid #999999';
			}
		}else{

			if ( isUndefined($('thumb_'+i)) == false ){
				Element.hide($('thumb_'+i));
				Element.hide($('dia_'+i));
			}
			//if ( isUndefined($('dia_'+1)) == false ){			
				
			//}
		}
	}
}


function imageButtonClicked(picture_id){
//	while(picture_id > imagesPerPage){
//		picture_id -= imagesPerPage;
//	}
	
	if ( statusImageCanvaseVisible == false ){
		showCanvas();
	}
	
	highlightImageLink(picture_id);
	
	loadSiteImage("http://fotoalbums.server8.eu/" + servId + "/" + albumId + "/site/" + images[picture_id]['filename']);
	$('td_filename').innerHTML = images[picture_id]['filename'];
	
	currentImage=picture_id;
	
	return false;
}


function showNextRange(){
	if ( currentStop < albumCnt ){
		showImageButtons(currentStop+1,albumCnt);	
	}
}

function showPrevRange(){
	var from=1;
	
	if ( currentStart < imagesPerPage ){
		from = 1;	
	}else{
		from = currentStart-imagesPerPage;	
	}
	
	showImageButtons(from,albumCnt);	
}

var statusImageCanvaseVisible=false;

function thumbClickHandler(filename,thumb_id){
	
	if ( statusImageCanvaseVisible == false ){
		showCanvas();
				
		$('bottom_img_canvas').src='images/arrows_00000_f0f0f0.gif';
		
		loadSiteImage('http://fotoalbums.server8.eu/'+servId+'/'+albumId+'/site/'+filename);
		$('td_filename').innerHTML = filename; 
		
		currentImage=currentStart+thumb_id-1;
		
		highlightImageLink(currentImage);
	}
}

function backToIndex(){
	showIndex();
	highlightImageLink(-1);

	$('img_canvas').src='http://fotoalbum.infoweb.be/dyn/images//arrows_00000_f0f0f0.gif';
	$('bottom_img_canvas').	src='http://fotoalbum.infoweb.be/dyn/images//arrows_00000_f0f0f0.gif';
/*
	$('img_canvas').src='images/arrows_00000_f0f0f0.gif';
	$('bottom_img_canvas').src='images/arrows_00000_f0f0f0.gif';
*/
	$('slideshow_play').style.display='none';
	$('slideshow_pause').style.display='none';
	$('slideshow_normal').style.display='block';
}

function showCanvas(){
	Element.hide('image_table');
	
	Element.show('div_img_canvas');
	Element.hide('div_bottom_img_canvas');
	Element.show('div_backToIndex');
	Element.show('div_slideShowControl');
	
	statusImageCanvaseVisible=true;
}

function showIndex(){
	slideShow=false;
	
	Element.show('image_table');
		
	Element.hide('div_img_canvas');
	Element.hide('div_bottom_img_canvas');
	Element.hide('div_backToIndex');
	Element.hide('div_slideShowControl');
	
	$('img_canvas').src='images/arrows_00000_f0f0f0.gif';
	$('td_filename').innerHTML = '&nbsp;';
	
	statusImageCanvaseVisible=false;
	slideShow=false;
}

function loadSiteImage(src){
	var loadImg = new Image()
		loadImg.src = src;
		loadImg.onLoad=showImage(loadImg);
}

function showNextImage(){
	if(flagLockNavigation==false){
		flagLockNavigation=true;
		
		if ( currentImage <= albumCnt-1 ){
			currentImage+=1;
			
			if (currentImage > currentStop){
				setTimeout("showNextRange()", 10);
			}
		}else{
			currentImage=1;
			currentStart=1;
			currentStop=20;
			setTimeout("showPrevRange()",10);
			//slideShow=false;
		}
	
		var filename = images[currentImage]['filename'];
		
		$('td_filename').innerHTML = images[currentImage]['filename'];
		
		setTimeout("highlightImageLink(currentImage)",150);
		
		loadSiteImage('http://fotoalbums.server8.eu/'+servId+'/'+albumId+'/site/'+filename);
		
		if ( slideShow == true ){
			setTimeout("showNextImage()", 5000);
		}
	}
}

function highlightImageLink(id){
	for(var i=0; i<=albumCnt; i++){
		if ( $('div_link_'+i) ){
			$('div_link_'+i).className="nummering";
		}
	}
	
	if ( id != -1){
		$('div_link_'+id).className="hover_nummering";
	}
}

function showPrevImage(){
	
		if ( currentImage > 1 ){
			currentImage-=1;
			
			if( currentImage < currentStart){
				setTimeout("showPrevRange()",10);
			}
		}
	
		var filename = images[currentImage]['filename'];
		
		setTimeout("highlightImageLink(currentImage)",150);
		
		$('td_filename').innerHTML = images[currentImage]['filename'];
		
		loadSiteImage('http://fotoalbums.server8.eu/'+servId+'/'+albumId+'/site/'+filename);	

}

function showImage(img){
	if ( (Element.getStyle('div_img_canvas','display')=='none') && (Element.getStyle('div_bottom_img_canvas','display')=='none') ){
	}else{
	
		if ( Element.getStyle('div_img_canvas','display') == 'block' ){
			$('bottom_img_canvas').src=img.src;
	
			new Effect.Fade('div_img_canvas',{duration:1,from:1.0, to:0.0});
			new Effect.Appear('div_bottom_img_canvas',{duration:1,from:0.0, to:1.0});
	
		}else{
			$('img_canvas').src=img.src;
	
			new Effect.Appear('div_img_canvas',{duration:1,from:0.0, to:1.0});	
			new Effect.Fade('div_bottom_img_canvas',{duration:1,from:1.0, to:0.0});
		}	
		
		setTimeout("setLockFlag(false)",1250);
	}
}


function setLockFlag(bool){
	flagLockNavigation=bool;	
}

function slideShowStart(){
	if ( Element.getStyle('div_img_canvas','display') == 'none') {
		showCanvas();
		showNextImage();
	}else{
	
	}
	
	$('slideshow_normal').style.display='none';
	$('slideshow_play').style.display='none';
	$('slideshow_pause').style.display='block';
	
	
	slideShow=true;

	setTimeout("showNextImage()", 5000);
}

function slideShowPause(){
	if( slideShow==true){
		currentImage = currentImage - 1;
		slideShow=false;

		$('slideshow_pause').style.display='none';
		$('slideshow_play').style.display='block';
		$('slideshow_pause').style.display='none';
	}else{
		slideShowStart();
	}
}

function myLoad(){	
	for(var i=1; i<images.length; i++){
		var myImg = new Image();	
			myImg.src=images[i]['url'];	
			myImg.filename=images[i]['filename'];
			myImg.onLoad=complete(i,myImg);
		}
}			

	
function complete(id,img){
	if ( id < imagesPerPage+1){
		var current_image = $('thumb_'+id);
			current_image.src = img.src;
			current_image.filename = img.filename;
			
			//new Effect.Opacity(current_image, {duration:0.0, from:1.0, to:0.75});
		
			
			current_image.onmouseover = mouseGoesOver;
			current_image.onmouseout = mouseGoesOut;
			current_image.style.border='1px solid #999999';
	}
	
	imgObject[id] = img;
}


function mouseGoesOver()
{
	//new Effect.Opacity(this, {duration:0.1, from:0.75, to:1.0});
}

function mouseGoesOut()
{
	//new Effect.Opacity(this, {duration:0.8, from:1.0, to:0.75});
}

function isUndefined(a) {
	return typeof a == 'undefined';
}
