var light = new Array();

function initNavigation() {
	if (document.getElementById) {
		navRoot = document.getElementById("navigation");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className="";
				}
			}
		}
	}
	
	var imageBox = document.getElementById('imageBox');
	if( imageBox != null )
	{
		ipLoad = function (){
			Scroller = new ImageScroll();
			Scroller.init();
		}
		var iElm = imageBox.getElementsByTagName('IMG');
		var imgA = new Array();
		
		for(i in iElm){
			if(typeof(iElm[i]) == "object"){
				imgA[imgA.length] = iElm[i].src;
			}
		}
		
		var ip = new ImagePreloader(imgA, ipLoad);
	}
}

function initAgendaPaginering()	{
	if( paginering = document.getElementById('col1') )
	{
		html = '';
		e = paginering.getElementsByTagName('*');
		
		for(i = 0; i < e.length; i++ ){
			if( e[i].className == 'currentPage' ){
				html += '<option selected>'+e[i].innerHTML+'</option>';
			}
			else if( e[i].tagName == 'A' ){
				html += '<option value="'+e[i].href+'">'+e[i].innerHTML+'</option>';
			}
		}
		
		paginering.innerHTML = '<select onchange="window.location=this.value;">'+html+'</select>';
	}
	
	if( paginering = document.getElementById('col2') )
	{
		html = '';
		e = paginering.getElementsByTagName('*');
		
		for(i = 0; i < e.length; i++ ){
			if( e[i].className == 'currentPage' ){
				html += '<option selected>'+e[i].innerHTML+'</option>';
			}
			else if( e[i].tagName == 'A' ){
				html += '<option value="'+e[i].href+'">'+e[i].innerHTML+'</option>';
			}
		}
		
		paginering.innerHTML = '<select onchange="window.location=this.value;">'+html+'</select>';
	}
}

function ImageScroll() {
	ImageScroll.page		= 1;
	ImageScroll.totalPages	= 1;
}

ImageScroll.prototype.init = function(){
	var imageBox = document.getElementById('imageBox');
	if( imageBox != null )
	{
		ImageScroll.containerWidth = this.getElementWidth( imageBox );
		
		children = document.getElementById('imageBoxInside').childNodes;
		
		ImageScroll.width = 0;
		
		for( var i = 0; i < children.length; i++ ){
			if( children[i].tagName == "IMG" ){
				ImageScroll.width += this.getElementWidth( children[i] );
				if( ImageScroll.width > ImageScroll.containerWidth ){
					ImageScroll.width = 0;
					ImageScroll.totalPages++;
					i--;
				}
			}
		}
		this.changeNavStatus();
	}
}

ImageScroll.prototype.delta = function(direction){
	switch(direction){
		case -1:
			if( ImageScroll.page > 1 ){
				ImageScroll.page--;
			}
			break;
		case 1:
			if( ImageScroll.page < ImageScroll.totalPages ){
				ImageScroll.page++;
			}
			break;
	}
	
	page		= 1; 
	offset		= 0;
	pWidth		= 0;
	pOffset		= 0;
	children	= document.getElementById('imageBoxInside').childNodes;
	
	if( ImageScroll.page != 1 )
	{
		for( var i = 0; i < children.length; i++ ){
			if(children[i].tagName == "IMG" ){
				pWidth = this.getElementWidth( children[i] );
				if( pOffset + pWidth > ImageScroll.containerWidth ){
					page++;
					if( page == ImageScroll.page )
					{
						break;
					}
					pOffset = 0;
					i--;
				}
				else
				{
					pOffset	+= pWidth;
					offset	+= pWidth;
				}
			}
		}
	}
	this.changeNavStatus();
	return -offset;
}

ImageScroll.prototype.changeNavStatus = function(){
	if( ImageScroll.totalPages == 1 )
	{
		document.getElementById('upImg').style.visibility = 'hidden';
		document.getElementById('downImg').style.visibility = 'hidden';
	}
	else if( ImageScroll.page == 1 )
	{
		document.getElementById('upImg').style.visibility = 'visible';
		document.getElementById('downImg').style.visibility = 'hidden';
	}
	else if( ImageScroll.page == ImageScroll.totalPages )
	{
		document.getElementById('upImg').style.visibility = 'hidden';
		document.getElementById('downImg').style.visibility = 'visible';
	}
	else
	{
		document.getElementById('upImg').style.visibility = 'visible';
		document.getElementById('downImg').style.visibility = 'visible';
	}
}

ImageScroll.prototype.moveToNext = function(){
	new Effect.Morph('imageBoxInside',{style:'margin-left:'+this.delta(+1)+'px;',duration:0.8} );
}

ImageScroll.prototype.moveToPrevious = function(){
	new Effect.Morph('imageBoxInside',{style:'margin-left:'+this.delta(-1)+'px;',duration:0.8} );
}

ImageScroll.prototype.getElementWidth = function(obj){
	return obj.offsetWidth;
}

function ImagePreloader(images, callback){
	// store the call-back
	this.callback = callback;
	// initialize internal state.
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array;
	// record the number of images.
	this.nImages = images.length;
	
	// for each image, call preload()
	for(var i=0; i < images.length; i++)
		this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image){
	// create new Image object and add to array
	var oImage = new Image;
	this.aImages.push(oImage);
	// set up event handlers for the Image object
	oImage.onload = ImagePreloader.prototype.onload;
	oImage.onerror = ImagePreloader.prototype.onerror;
	oImage.onabort = ImagePreloader.prototype.onabort;
	// assign pointer back to this.
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;
	// assign the .src property of the Image object
	oImage.src = image;
}

ImagePreloader.prototype.onComplete = function(){
	this.nProcessed++;
	if(this.nProcessed == this.nImages){
		this.callback();
	}
}

ImagePreloader.prototype.onload = function(){
	this.bLoaded = true;
	this.oImagePreloader.nLoaded++;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function(){
	this.bError = true;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function(){
	this.bAbort = true;
	this.oImagePreloader.onComplete();
}