function FullScreenImage()
{
	this.displayHelpTextNode = true;

	this.display = function display( imagePath, imageWidth, imageHeight, title, titleURL, displayHelpText )
	{
		if( body = getBodyNode() )
		{
			var div = document.createElement( "div" );
								
			div.style.width = getFullBodyWidth() + "px";
			div.style.height = getFullBodyHeight() + "px";
			div.style.backgroundColor = "black";
			div.style.position = "absolute";
			div.style.top = "0px";
			div.style.left = "0px";

			div.setAttribute( "id", "BigImageBackground" );
			div.setAttribute( "onclick", "FullScreenImage.close()" );
			div.setAttribute( "title", "Klik for at lukke billedet." );
		
			// For IE
			div.onclick = function() { FullScreenImage.close(); }
			
			setOpacity( div, 84 );
			
			body.appendChild( div );
			
			this.backgroundNode = div;
			
			{
				var div = document.createElement( "div" );
				
				var verticalCenter = getScrollTop() + ( getWindowHeight() / 2 );
				
				var top = parseInt( verticalCenter - ( ( imageHeight + 30 ) / 1.5 ) );
				
				if( top <= getScrollTop() )
				{
					top = getScrollTop() + 15;
				}
				
				div.style.top = top + "px";
				
				div.setAttribute( "id", "BigImage" );
				div.style.backgroundColor = "#FFF";
				div.style.position = "absolute";
				div.style.left = parseInt( ( getWindowWidth() - ( imageWidth + 30 ) ) / 2 ) + "px";
				div.style.padding = "15px";
									
				var img = document.createElement( "img" );
						
				img.setAttribute( "src", imagePath );
				img.setAttribute( "width", imageWidth + "px" );
				img.setAttribute( "height", imageHeight + "px" );
				
				img.style.display = "block";
				
				div.appendChild( img );
				
				if( title )
				{
					var p = document.createElement( "p" );
					
					if( titleURL )
					{
						var a = document.createElement( "a" );
						
						a.setAttribute( "href", titleURL );
						
						a.appendChild( document.createTextNode( title ) );
						
						p.appendChild( a );
					}
					else
					{
						p.appendChild( document.createTextNode( title ) );
					}
					
					p.setAttribute( "style", "width: " + imageWidth + "px" );
					
					div.appendChild( p );
				}
				
				if( displayHelpText && this.displayHelpTextNode )
				{
					var p = document.createElement( "p" );
					
					p.style.width = imageWidth + "px";
					p.style.borderTop = "1px solid #CCC";
					p.style.paddingTop = "10px";
					
					var em = document.createElement( "em" );
					
					em.appendChild( document.createTextNode( "Klik i det mørke felt uden for billedet for at lukke det." ) );
					
					p.appendChild( em );
					
					p.appendChild( document.createElement( "br" ) );
					
					var a = document.createElement( "a" );
					
					a.setAttribute( classAttributeName, "hideNotification" );
					a.setAttribute( "onclick", "javascript:FullScreenImage.hideHelpTextNode()" );

					// For IE
					a.onclick = function() { FullScreenImage.hideHelpTextNode(); }
					
					a.appendChild( document.createTextNode( "Skjul besked og vis ikke igen." ) );
					
					p.appendChild( a );
					
					div.appendChild( p );
					
					this.helpTextNode = p;
				}
				
				body.appendChild( div );
				
				this.hiresImageNode = div;
				this.hiresImageNodeWidth = imageWidth + 30;
				this.hiresImageNodeHeight = imageHeight + 30;
			}
		}
	}
	
	this.hideHelpTextNode = function hideHelpTextNode()
	{
		if( this.helpTextNode )
		{
			var httpRequest = new XMLHttpRequest();

			httpRequest.open( "GET", "/services/set_hide_image_help_text.php", false );
			httpRequest.send( "" );
						
			if( httpRequest.status == 200 )
			{
				var node = this.helpTextNode.parentNode;
				
				if( node )
				{
					node.removeChild( this.helpTextNode );
					
					this.displayHelpTextNode = false;
					this.helpTextNode = null;
				}
			}
		}
	}
	
	this.resize = function resize()
	{
		if( this.backgroundNode )
		{
			this.backgroundNode.style.width = getFullBodyWidth() + "px";
			this.backgroundNode.style.height = getFullBodyHeight() + "px";
		}

		if( this.hiresImageNode )
		{
			var verticalCenter = getScrollTop() + ( getWindowHeight() / 2 );
				
			var top = parseInt( verticalCenter - ( this.hiresImageNodeHeight / 1.5 ) );
			
			if( top <= getScrollTop() )
			{
				top = getScrollTop() + 15;
			}
		
			this.hiresImageNode.style.top = top + "px";
			this.hiresImageNode.style.left = parseInt( ( getWindowWidth() - this.hiresImageNodeWidth ) / 2 ) + "px";
		}
	}
	
	this.close = function close()
	{
		this.hiresImageNode = null;
		
		if( body = getBodyNode() )
		{
			var div = document.getElementById( "BigImageBackground" );
			
			if( div )
			{
				body.removeChild( div );
			}
			
			if( div = document.getElementById( "BigImage" ) )
			{
				body.removeChild( div );
			}
		}
	}
}

var FullScreenImage = new FullScreenImage();

function ImageViewImage( node )
{
	this.rootNode = node;

	this.hiresImagePath = null;
	this.hiresImageWidth = 0;
	this.hiresImageHeight = 0;
	
	setOpacity( this.rootNode, 0 );
	
	this.hasHiresImage = function hasHiresImage()
	{
		return this.getHiresImagePath() != null;
	}
	
	this.getWidth = function getWidth()
	{
		var nodes = this.rootNode.getElementsByTagName( "img" );
				
		if( nodes && nodes.length > 0 )
		{
			var width = parseInt( nodes[0].getAttribute( "width" ) );
			
			if( width > 0 )
			{
				return width;
			}
			else // For IE 6
			{
				var attribute = this.rootNode.getAttribute( classAttributeName );
				
				if( attribute )
				{
					var result = attribute.match(/(^| )width::([0-9]+)/);
				
					if( result && result.length > 0 )
					{
						return parseInt( result[2] );
					}
				}
			}
		}
	}
	
	this.getImageNode = function getImageNode()
	{
		var nodes = this.rootNode.getElementsByTagName( "img" );
		
		if( nodes && nodes.length > 0 )
		{
			return nodes[0];
		}
	}
	
	this.getTextNode = function getTextNode()
	{
		var nodes = this.rootNode.getElementsByTagName( "p" );
		
		if( nodes && nodes.length > 0 )
		{
			return nodes[0];
		}
	}
	
	this.getHiresImagePath = function getHiresImagePath()
	{
		if( this.hiresImagePath == null )
		{
			var attribute = this.rootNode.getAttribute( classAttributeName );
			
			if( attribute )
			{
				var result;
				
				result = attribute.match(/(^| )hires::([^ ]+)(\[([0-9]+)x([0-9]+)\])/);
			
				if( result && result.length > 0 )
				{
					this.hiresImagePath = result[2];
					this.hiresImageWidth = parseInt( result[4] );
					this.hiresImageHeight = parseInt( result[5] );
				}
			}
		}
		
		return this.hiresImagePath;
	}
	
	this.getTextPlacement = function getTextPlacement()
	{
		var attribute = this.rootNode.getAttribute( classAttributeName );
		
		var result;

		if( attribute )
		{
			result = attribute.match(/(^| )text::([^ ]+)\.([^ ]+)/);
			
			if( result && result.length > 0 )
			{
				return result[2];
			}
		}
	}
	
	this.getTextColor = function getTextColor()
	{
		var attribute = this.rootNode.getAttribute( classAttributeName );
		
		var result;
		
		if( attribute )
		{
			result = attribute.match(/(^| )text::([^ ]+)\.([^ ]+)/);
			
			if( result && result.length > 0 )
			{
				return result[3];
			}
		}
	}
	
	this.getTextBackgroundColor = function getTextBackgroundColor()
	{
		var attribute = this.rootNode.getAttribute( classAttributeName );
		
		var result;
		
		if( attribute )
		{
			result = attribute.match(/ background::([0-9]+)/);
			
			if( result && result.length > 0 )
			{
				return result[1];
			}
		}
	}
	
	this.getTextLength = function getTextLength()
	{
		var attribute = this.rootNode.getAttribute( classAttributeName );
		
		var result;
		
		if( attribute )
		{
			result = attribute.match(/ textLength::([0-9]+)/);
			
			if( result && result.length > 0 )
			{
				return parseInt( result[1] );
			}
		}
		
		return 0;
	}
}

function ImageView( id, autostart, timeBetweenImages )
{
	this.magnifyImage = function magnifyImage()
	{
		var image = this.images[this.currentImageIndex];
	
		if( imagePath = image.getHiresImagePath() )
		{
			this.stopSlideshow();
		
			FullScreenImage.display( imagePath, image.hiresImageWidth, image.hiresImageHeight );
		}
	}
	
	this.hideBigImage = function hideBigImage()
	{
		FullScreenImage.close();
	}

	this.updateInfoNode = function updateInfoNode()
	{
		if( this.infoNode )
		{
			var textNode = document.createTextNode( "Viser billede " + ( this.currentImageIndex + 1 ) + " af " + this.images.length );
		
			this.infoNode.replaceChild( textNode, this.infoNode.firstChild );
		}
	}
	
	this.updateToggleSlideshowNode = function updateToggleSlideshowNode()
	{
		if( this.runSlideshow )
		{
			this.toggleSlideshowNode.setAttribute( classAttributeName, "button stopSlideshow" );
			this.toggleSlideshowNode.setAttribute( "onclick", /*this.id +*/ "ImageView.stopSlideshow()" );
			this.toggleSlideshowNode.setAttribute( "title", "Stop diasshow" );

			// For IE
			this.toggleSlideshowNode.onclick = function() { ImageView.stopSlideshow(); }
		}
		else
		{
			this.toggleSlideshowNode.setAttribute( classAttributeName, "button startSlideshow" );
			this.toggleSlideshowNode.setAttribute( "onclick", /*this.id +*/ "ImageView.startSlideshow()" );
			this.toggleSlideshowNode.setAttribute( "title", "Start diasshow" );

			// For IE
			this.toggleSlideshowNode.onclick = function() { ImageView.startSlideshow(); }
		}
	}
	
	this.stopSlideshow = function stopSlideshow()
	{
		if( this.runSlideshow )
		{
			this.runSlideshow = false;
			
			this.updateToggleSlideshowNode();
		}
	}
	
	this.startSlideshow = function startSlideshow()
	{
		if( !this.runSlideshow )
		{
			this.key++;
		
			this.runSlideshow = true;

			this.updateToggleSlideshowNode();
	
			window.setTimeout( /*this.id +*/ "ImageView.triggerShowImage(" + this.key + "," + this.getNextImageIndex() + ")", 500 );
		}
	}
	
	this.createControls = function createControls()
	{
		var controlsDiv = document.createElement( "div" );
		
		controlsDiv.setAttribute( classAttributeName, "ImageViewControls" );
		
		if( this.images.length > 1 )
		{
			var a = document.createElement( "a" );
			
			a.setAttribute( classAttributeName, "button showNextImage" );
			a.setAttribute( "onclick", /*this.id +*/ "ImageView.showNextImage()" );
			a.setAttribute( "title", "Vis næste billede" );
	
			// For IE
			a.onclick = function() { ImageView.showNextImage(); }
						
			controlsDiv.appendChild( a );
	
			var a = document.createElement( "a" );
			
			a.setAttribute( classAttributeName, "button showPreviousImage" );
			a.setAttribute( "onclick", /*this.id +*/ "ImageView.showPreviousImage()" );
			a.setAttribute( "title", "Vis forrige billede" );

			// For IE
			a.onclick = function() { ImageView.showPreviousImage(); }
	
			controlsDiv.appendChild( a );
	
	
			this.toggleSlideshowNode = document.createElement( "a" );
			
			this.updateToggleSlideshowNode();
	
			controlsDiv.appendChild( this.toggleSlideshowNode );
		}


		var a = document.createElement( "a" );

		this.magnifyButtonId = this.id + "MagnifyButton";
		
		a.setAttribute( classAttributeName, "button magnify" );
		a.setAttribute( "onclick", /*this.id +*/ "ImageView.magnifyImage()" );
		a.setAttribute( "title", "Vis stort billede" );
		a.setAttribute( "id", this.magnifyButtonId );
		a.setAttribute( "style", "display: none" );

		// For IE
		a.onclick = function() { ImageView.magnifyImage(); }

		controlsDiv.appendChild( a );
				
		this.magnifyButton = a;

		if( this.images.length > 1 )
		{
			this.infoNode = document.createElement( "div" );
			this.infoNode.setAttribute( classAttributeName, "info" );
			this.infoNode.appendChild( document.createTextNode( "Viser billede 1 af " + ( this.images.length + 1 ) ) );
			
			controlsDiv.appendChild( this.infoNode );
		}

		if( this.rootNode.nextSibling )
		{
			this.rootNode.parentNode.insertBefore( controlsDiv, this.rootNode.nextSibling );
		}
		else
		{
			this.rootNode.parentNode.appendChild( controlsDiv );
		}
	}
	
	this.setBackgroundColor = function setBackgroundColor( value )
	{
		this.rootNode.style.backgroundColor = value;
	}

	this.getNextImageIndex = function getNextImageIndex()
	{
		if( this.currentImageIndex == this.images.length - 1 )
		{
			return 0;
		}
		else
		{
			return this.currentImageIndex + 1;
		}
	}
	
	this.getPreviousImageIndex = function getPreviousImageIndex()
	{
		return this.currentImageIndex >= 1 ? this.currentImageIndex - 1 : this.images.length - 1;
	}
	
	this.showImage = function showImage( imageIndex )
	{
		if( !this.inFadeIn )
		{
			if( this.currentImageIndex >= 0 )
			{
				fadeOut( this.images[this.currentImageIndex].rootNode.getAttribute( "id" ), 0 );
			}
		
			this.key = 1;
	
			this.currentImageIndex = imageIndex;
			
			this.updateInfoNode();
			
			var image = this.images[this.currentImageIndex];
			
			var imageWidth = image.getWidth();
	
			var imageNode = image.getImageNode();
			var imageTextNode = image.getTextNode();
			
			if( imageWidth == 600 )
			{
				imageNode.style.marginLeft = "0";
				
				if( imageTextNode )
				{
					imageTextNode.style.width = "110px";
					imageTextNode.style.marginTop = "20px";
					
					if( image.getTextPlacement() == "Left" )
					{
						imageTextNode.style.marginLeft = "50px";
					}
					else
					{
						imageTextNode.style.marginLeft = "470px"; 
					}
					
					if( image.getTextColor() == "Dark" )
					{
						imageTextNode.style.color = "#222";
					}
					else
					{
						if( image.getTextBackgroundColor() < 80 )
						{						
							imageTextNode.style.backgroundImage = "url(/images/image_text_background.png)";
							imageTextNode.style.padding = "10px 10px 12px 10px";
							imageTextNode.style.height = "auto";
							
							imageTextNode.setAttribute( classAttributeName, "rounded" );
							
							imageTextNode.style.marginTop = "10px";
							
							if( image.getTextPlacement() == "Left" )
							{
								imageTextNode.style.marginLeft = "40px";
							}
							else
							{
								imageTextNode.style.marginLeft = "460px"; 
							}
							
							/*
							var bg = document.createElement( "div" );

							if( image.getTextPlacement() == "Left" )
							{
								imageTextNode.style.marginLeft = "30px";
								bg.style.left = "20px";
							}
							else
							{
								imageTextNode.style.marginLeft = "450px"; 
								bg.style.left = "440px";
							}
														
							bg.style.position = "absolute";
							bg.style.top = "10px";
							bg.style.backgroundColor = "#000";
							bg.style.width = "130px";
							bg.style.height = ( imageTextNode.clientHeight + 20 ) + "px";

							setOpacity( bg, 40 );
							
							imageTextNode.parentNode.insertBefore( bg, imageTextNode );
							*/
						}
						
						imageTextNode.style.color = "#FFF";
					}
				}
			}
			else
			{			
				if( this.alignRight )
				{
					imageNode.style.marginLeft = ( 600 - imageWidth ) + "px";
					
					if( imageTextNode )
					{
						imageTextNode.style.width = "110px";
						imageTextNode.style.marginTop = "20px";
						
						var marginLeft = 600 - imageWidth - 120;
						
						if( marginLeft > 50 && marginLeft - 50 <= 30 )
						{
							marginLeft = 50;	
						}
						
						imageTextNode.style.marginLeft = marginLeft + "px";
					}
				}
				else
				{
					// For IE 6
					imageNode.parentNode.style.width = "600px";
					
					imageNode.style.marginLeft = "0";
				
					if( imageTextNode )
					{
						imageTextNode.style.width = "110px";
						imageTextNode.style.marginTop = "20px";
						imageTextNode.style.marginLeft = ( imageWidth + 20 ) + "px";
					}
				}
			}
			
			this.alignRight = !this.alignRight;
			
			this.inFadeIn = true;
			
			this.timeBetweenImages = this.baseTimeBetweenImages;
			
			l = image.getTextLength();
			
			if( l > 250 )
			{
				this.timeBetweenImages += 5000;
			}
			else if( l > 200 )
			{
				this.timeBetweenImages += 3000;
			}
			else if( l > 150 )
			{
				this.timeBetweenImages += 1000;
			}
			
			if( !image.hasHiresImage() /*&& !this.magnifyButtonHidden*/ )
			{
				//fadeOut( this.magnifyButtonId, 40 );
				
				//this.magnifyButtonHidden = true;
				
				this.magnifyButton.style.display = "none";
			}
			else
			{
				//fadeIn( this.magnifyButtonId, 40, null );

				//this.magnifyButtonHidden = false;
				
				this.magnifyButton.style.display = "block";
			}
									
			fadeIn( image.rootNode.getAttribute( "id" ), 20, /*this.id +*/ "ImageView.imageDidFadeIn();" );
		}
	}
	
	this.imageDidFadeIn = function imageDidFadeIn()
	{
		this.inFadeIn = false;
	
		if( this.runSlideshow )
		{
			window.setTimeout( /*this.id +*/ "ImageView.triggerShowImage(" + this.key + "," + this.getNextImageIndex() + ")", this.timeBetweenImages );
		}
	}
	
	this.triggerShowImage = function triggerShowImage( key, imageIndex )
	{
		if( this.runSlideshow && ( key == this.key ) )
		{
			this.showImage( imageIndex );
		}
	}

	this.showNextImage = function showNextImage()
	{
		this.stopSlideshow();
		
		this.showImage( this.getNextImageIndex() );
	}
	
	this.showPreviousImage = function showPreviousImage()
	{
		this.stopSlideshow();
	
		this.showImage( this.getPreviousImageIndex() );
	}
	
	switch( timeBetweenImages )
	{
		case -1:
			this.baseTimeBetweenImages = 5000;
			break;
		
		case 1:
			this.baseTimeBetweenImages = 15000;
			break;
			
		default:
			this.baseTimeBetweenImages = 10000;
	}
	
	this.inFadeIn = false;
	
	this.id = id;
	
	this.rootNode = document.getElementById( id );
	
	if( !this.rootNode )
	{
		return;
	}
	
	this.images = new Array();
	
	var divs = this.rootNode.getElementsByTagName( "div" );
	
	if( divs && divs.length > 0 )
	{
		for( i = 0 ; i < divs.length ; i++ )
		{
			divs[i].setAttribute( "id", id + "Image" + ( i + 1 ) );
		
			this.images.push( new ImageViewImage( divs[i] ) );
		}
	}
	else
	{
		return;
	}

	if( !this.images || this.images.length == 0 )
	{
		return;
	}

	this.runSlideshow = autostart && this.images.length > 1;
	
	this.createControls();
		
	this.currentImageIndex = -1;
	this.alignRight = false;
	
	this.showImage( 0 );
}

function fadeIn( nodeId, opacity, callback )
{
	if( document.getElementById )
	{
		var node = document.getElementById( nodeId );
		
		if( opacity <= 100 )
		{
			if( opacity > 0 )
			{
				node.style.display = "block";
			}
			
			if( opacity == 100 && callback )
			{			
				eval( callback );
			}
			
			setOpacity( node, opacity );
			
			opacity += 10;
			
			window.setTimeout( "fadeIn('" + nodeId + "'," + opacity + ",'" + callback + "')", 40 );
		} 
	}
}

function fadeOut( nodeId, opacity )
{
	if( document.getElementById )
	{
		var node = document.getElementById( nodeId );
		
		if( opacity >= 0 )
		{
			if( opacity == 0 )
			{
				node.style.display = "none";
			}
			else
			{
				setOpacity( node, opacity );
				
				opacity -= 10;
				
				window.setTimeout( "fadeOut('" + nodeId + "'," + opacity + ")", 50 );
			}
		}
	}
}