
window.onload = function()
{
	setEmailAddresses();
	setFooter();
	setSelectedImg();
	slideshow();
	var slide = new kvSlide();
	slide.Run();
}


var imgInfo;
function setThumbNails()
{
	if( imgInfo != null )
	{
		var thumbsdiv = document.getElementById( "gallerythumbs" );
		var s = "";
		for( var i in imgInfo )
		{
			s += "<div class=\"gallerythumb\" onclick=\"setSelectedImg(" + imgInfo[i]["id"] + ")\" id=\"gallerythumb_" + i + "\"><a href=\"#foto\"></a></div>";
		}
		thumbsdiv.innerHTML = s;
		for( var i in imgInfo )
		{
			var thumbdiv = document.getElementById( "gallerythumb_" + i );
			var im = new Image();
			im.src = imgInfo[ i ][ "thumb" ];
			im.alt = imgInfo[ i ][ "title" ];
			im.border = "0";
			im.style.border = "0px solid";
			thumbdiv.childNodes[0].appendChild( im );
		}
	
	}
}

function setSelectedImg( selId )
{
	if( selId && selId != selImgId )
	{
		selImgId = selId;
		var selIndex = -1;
		for( var i in imgInfo )
		{
			var thumbdiv = document.getElementById( "gallerythumb_" + i );
			if(thumbdiv)
			{
				thumbdiv.className = "gallerythumb";
				if( imgInfo[ i ][ "id" ] == selImgId )
				{
					selIndex = i;
				}
			}		
		}
	
		if( selIndex >= 0 )
		{
	
	
			var thumbdiv = document.getElementById( "gallerythumb_" + i );
			
			if(thumbdiv)
			{
				thumbdiv.className = "gallerythumbsel";
			}		
	
			setLabel( "imgTitle"	, imgInfo[ selIndex ][ "title" ]	);
			setLabel( "imgSize" 	, imgInfo[ selIndex ][ "size" ]	);
			setLabel( "imgWidth" 	, imgInfo[ selIndex ][ "width" ]	);
			setLabel( "imgHeight" 	, imgInfo[ selIndex ][ "height" ]	);
			setLabel( "imgDate" 	, imgInfo[ selIndex ][ "date" ]	);
			setLabel( "imgPlace" 	, imgInfo[ selIndex ][ "place" ]	);
	
			if(document.getElementById( "galleryfoto" ))
			{
				document.getElementById( "galleryfoto" ).onload = function(){ document.getElementById( "loadinghint" ).style.display = "none"; document.getElementById( "galleryfoto" ).style.display = "inline"; };
				document.getElementById( "loadinghint" ).style.display = "inline";
				document.getElementById( "galleryfoto" ).style.display = "none";
				document.getElementById( "galleryfoto" ).src = imgInfo[ selIndex ][ "url" ];
				if(maxImgHeight)
					document.getElementById( "galleryfoto" ).style.marginTop = (5+(maxImgHeight/2-imgInfo[ selIndex ][ "height" ]/2)) + "px";
				document.getElementById( "galleryfoto" ).alt = imgInfo[ selIndex ][ "title" ];	
			}
	
		}
	}
}

function selectNextImg()
{
	if(imgInfo)
	{
		for( var i = 0; i < imgInfo.length; i++ )
		{
			if( imgInfo[ i ][ "id" ] == selImgId )
			{
				if( i < imgInfo.length - 1 )
				{
					setSelectedImg( imgInfo[ i+1 ][ "id" ] );
				}
				else
				{
					setSelectedImg( imgInfo[ 0 ][ "id" ] );
				}
				break;
			}
		}
	}	
}

function selectPrevImg()
{
	if(imgInfo)
	{
		for( var i = 0; i < imgInfo.length; i++ )
		{
			if( imgInfo[ i ][ "id" ] == selImgId )
			{
				if( i > 0 )
				{
					setSelectedImg( imgInfo[ i-1 ][ "id" ] );
				}
				else
				{
					setSelectedImg( imgInfo[ imgInfo.length-1 ][ "id" ] );
				}
				break;
			}
		}	
	}
}

function setLabel( ElementId, content )
{
	var el = document.getElementById( ElementId );
	if( el )
	{
		if(el.innerHTML)
			el.innerHTML = content;

	}

}

function loadPictureListAsync(sUri) 
{
   var xmlHttp = XmlHttp.create();
   var async = true;
   xmlHttp.open("GET", sUri, async);
   xmlHttp.onreadystatechange = function () 
   {
      if (xmlHttp.readyState == 4)
      {
			eval(xmlHttp.responseText);
			setThumbNails();
			setSelectedImg();
      }
   }
   xmlHttp.send(null);
}


function setEmailAddresses()
{
	var links = document.getElementsByTagName( "a" );
	//Trace( "links: " + links );
	//Trace( "links.length: " + links.length );
	for( var i = 0; i < links.length; i++ )
	{
		var className = links[i].className;
		//Trace( className + ": " + className.match( /.*email.*/ ) );
		if( className && className.match( /.*email.*/ ) )
		{
			className = className.substr( className.indexOf( "email" ) + 6 );
			var email = className.split( " " );
			rlcMail( links[i], email[0], email[1], email[2] );
		}
	}
}


function setFooter()
{

	var main	= getObj( "main" );
	var submenu	= getObj( "menu_sub" );

	if( main && submenu ){

		var mainHeight		= main.offsetHeight;
		var subMenuHeight	= submenu.offsetHeight;

		if( subMenuHeight > mainHeight )
		{
			main.style.height = subMenuHeight + "px";
		}
		else if ( subMenuHeight < mainHeight )
		{
			submenu.style.height = mainHeight + "px";
		}
		
	}

	setTimeout( "setFooter()", 200 );

}

function Trace( line )
{
	var debug = getObj( "debug" );
	if( debug )
	{
		debug.innerHTML = line + "<br />" + debug.innerHTML;
	}
}

function rlcMail( o, t1, t2, t3 )
{  
	var m = getMailStr( t1, t2, t3 );
	o.innerHTML =  m;
	o.title = m;
	o.href = "mailto:" + m;
}

function getMailStr( t1, t2, t3 )
{
	return t1 + "@" + t2 + "." + t3;
}

function slideshow(){

	var d=getObj("doslide");
	var s=getObj("slt");
	if( s != null )
	{
		var to=parseInt(s.value);
		if( s )
		{
			if(d.checked)
			{
				//var f=getObj("f1");
				//f.submit();
				selectNextImg();
			}
		}
		setTimeout("slideshow()",to*1000);
	}
	
}
function getObj(n){
	if(document.getElementById)
		return document.getElementById(n)
	if(document.all)
		return eval("document.all."+n);
	return false;
}


function appendRand( url )
{
	var now = new Date();
	if( url.indexOf( "?rand=" ) > 0 )
	{
		url = url.substr( 0, url.indexOf( "?rand=" ) ); 
	}
	if( url.indexOf( "&rand=" ) > 0 )
	{
		url = isrc.substr( 0, url.indexOf( "&rand=" ) ); 
	}
	if( url.indexOf( "?" ) > 0 )
	{
		url += "&rand=" + now.getTime();
	}
	else
	{
		url += "?rand=" + now.getTime();
	}
	
	return url;
	
}



function kvSlide()
{
}

kvSlide.prototype.ImagesPath = "img/layout/";
kvSlide.prototype.Images = [ 
	"kv16.jpg",
	"kv0.jpg",
	"kv1.jpg",
	"kv2.jpg",
	"kv3.jpg",
	"kv4.jpg",
	"kv5.jpg",
	"kv6.jpg",
	"kv7.jpg",
	"kv8.jpg",
	"kv9.jpg",
	"kv10.jpg",
	"kv11.jpg",
	"kv12.jpg",
	"kv13.jpg",
	"kv14.jpg",
	"kv15.jpg"
];
	
kvSlide.prototype.Timeout = 5000;
kvSlide.prototype.CurrentIndex = 0;
kvSlide.prototype.ElementId = "kv";
	
kvSlide.prototype.Run = function()
{

	var preLoad = new Array()
	for (var i = 0; i < this.Images.length; i++){
	   	preLoad[i] = new Image();
	   	preLoad[i].src = "../../" + this.ImagesPath + this.Images[i];
		//document.body.appendChild(preLoad[i]);
	}
	
	this.BeginSlide( Math.floor(Math.random() * this.Images.length) );

};

kvSlide.prototype.BeginSlide = function(newIndex)
{
	setTimeout('new kvSlide().SlideOne(' + newIndex + ')', this.Timeout);
};

kvSlide.prototype.SlideOne = function(newIndex)
{
   	if (newIndex != undefined)
		this.CurrentIndex = parseInt(newIndex);
	if (this.CurrentIndex > this.Images.length - 1 || this.CurrentIndex < 0)
		this.CurrentIndex = 0;
		
	if (document.getElementById(this.ElementId))
	{
		document.getElementById(this.ElementId).style.background = "url(../../" + this.ImagesPath + this.Images[this.CurrentIndex] + ")";
		this.BeginSlide(this.CurrentIndex + 1);
	}
};

