function initGallery ( )
{
	var contentbox = document.getElementById ( 'BlestBoxContent' );
	contentbox.parentNode.style.background = '#000000';
		
	var arleft = document.createElement ( 'div' );
	arleft.id = 'ProjectImageNext';  
	arleft.style.width = '20px';
	arleft.style.height = '80px';
	arleft.style.background = 'url(upload/images-master/previmage.png)';
	arleft.style.position = 'absolute';
	arleft.style.left = '0px';
	arleft.style.bottom = '0px';
	contentbox.appendChild ( arleft );
	
	var arright = document.createElement ( 'div' );
	arright.id = 'ProjectImagePrev';
	arright.style.width = '20px';
	arright.style.height = '80px';
	arright.style.background = 'url(upload/images-master/nextimage.png)';
	arright.style.position = 'absolute';
	arright.style.right = '0px';
	arright.style.bottom = '0px';
	contentbox.appendChild ( arright );
	
	// Set contents to be the div that has the GalleryImages class
	var divs = document.getElementsByTagName ( 'div' );
	var contents; // <- inneholder bilde thumbnails
	for ( var a = 0; a < divs.length; a++ )
	{
		if ( hasClass ( divs[ a ], 'GalleryImages' ) )
			contents = divs[ a ];
	}
	if ( !contents )
	{
		return false;
	}
	else document._thumbs = contents;

	try
	{
		document._thumbs.parentNode.removeChild ( document.imgDiv );
		document._thumbs.parentNode.removeChild ( document.backDiv );
	}
	catch ( e ){};

	//  Marg rundt bilde serien
	var Margin = 24;

	document.imgblockChange = false;
	document.backDiv = false;
	document.imgDiv = false;

	contents.style.top = '3px';
	contents.style.left = Margin + 'px';

	var elementLeft = getElementLeft ( document._thumbs ) + Margin;
	var elementWidth = getElementWidth ( document._thumbs ) - ( Margin * 2 );
	var totalWidth = getElementWidth ( contents );
	var totalWidthCalced = totalWidth - elementWidth;

	document._thumbs.onmousemove = function ( )
	{
		if ( contents && elementWidth < totalWidth )
		{
			contents.style.left = ( Margin - ( ( ( mousex - elementLeft ) / elementWidth * totalWidthCalced ) ) ) + 'px';
		}
	}
 
	var imgs = document._thumbs.getElementsByTagName ( 'a' );
	document._totalimages = imgs.length;
	var width = 0;
	for ( var a = 0; a < imgs.length; a++ )
	{
		imgs[ a ].index = a;
		if ( !imgs[ a ].urlLocation )
		{
			imgs[ a ].urlLocation = imgs[ a ].href;
			imgs[ a ].href = 'javascript: void ( 0 )';
		}
		imgs[ a ].onmousedown = function ( ) { return false; }
		imgs[ a ].onclick = function ( )
		{
			if ( document.imgblockChange == true )
			{
				var curr = this;
				document.imgqueuefunc = function ( )
				{
					curr.onclick ( );
				}
				document._fadeMode = 'hide';
				hideImgDiv ( );
			}
		}
		width += getElementWidth ( imgs[ a ] );
	}
	contents.style.width = width + 'px';

	imgs[ 0 ].onclick ( );

	/*			setOpacity ( document.getElementById ( 'ProjectImageNumeration' ), 0.4 );*/
	setOpacity ( arleft , 0.4 );

	arleft.onclick = function ( )
	{
		var is = document._thumbs.getElementsByTagName ( 'a' );
		for ( var a = 0; a < is.length; a++ )
		{
			if ( is[ a ].index == document._selectedImage )
			{
				var b = a + 1;
				if ( b >= is.length ) b = 0;
				is[ b ].onclick ( );
				break;
			}
		}
	}

	setOpacity ( arright , 0.4 );

	arright.onclick = function ( )
	{
		var is = document._thumbs.getElementsByTagName ( 'a' );
		for ( var a = 0; a < is.length; a++ )
		{
			if ( is[ a ].index == document._selectedImage )
			{
				var b = a - 1;
				if ( b < 0 ) b = is.length - 1;
				is[ b ].onclick ( );
				break;
			}
		}
	}

}


// Alle blestbox elementer får en vente funksjon
function initGallerySystem ( )
{
	if ( !document.getElementById ( 'Innhold' ) )
		setTimeout ( 'initGallerySystem()', 100 );
	else
	{
		var eles = document.getElementsByTagName ( 'a' );
		var boxes = 0;
		for ( var a = 0; a < eles.length; a++ )
		{
			if ( hasClass ( eles[ a ], 'BoxPopup' ) )
			{
				eles[ a ].onmouseup = waitForBox;
				boxes++;
			}
		}
	}
}
initGallerySystem();

function waitForBox ( )
{
	if ( document.getElementById ( 'BlestBoxContent' ) )
	{
		if (isIE) setTimeout ( 'initGallery ( )', 150 );
		else initGallery();
	}
	else setTimeout ( 'waitForBox ( )', 100 );
}



