var slideshow_timer;
var timeout;
var slideshow_status = 'off';
var gallery = new Array();
var count = -1;

/** Starts the slideshow **/
function slideshowToggle()
{
	if(slideshow_status == 'off') 
	{
		document.getElementById('slide_start').innerHTML = 'Stop slideshow';
		slideshow_status = 'on'
		startSlideshow();
	}
	else 
	{
		document.getElementById('slide_start').innerHTML = 'Start slideshow';
		slideshow_status = 'off'
		stopSlideshow();
	}
}

function startSlideshow()
{
	var timer = 0;
	var radioChecked = '';
	if(document.getElementById('radio_1').checked == true) {
		radioChecked = 'radio_1';
	}
	if(document.getElementById('radio_2').checked == true) {
		radioChecked = 'radio_2';
	}
	if(document.getElementById('radio_5').checked == true) {
		radioChecked = 'radio_5';
	}

	timeout = document.getElementById(radioChecked).value*1000;
	clearTimeout(slideshow_timer);
	slideshow_timer = setTimeout("swapPicLoad(\'forward\')", timeout);
	
}

function stopSlideshow()
{
	clearTimeout(slideshow_timer);
	document.getElementById('slide_start').innerHTML = 'Start slideshow';
	slideshow_status = 'off'
}

function getScrollY() {

  var scrOfX = 0, scrOfY = 0;

  if( typeof( window.pageYOffset ) == 'number' ) {

    //Netscape compliant

    scrOfY = window.pageYOffset;

    scrOfX = window.pageXOffset;

  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {

    //DOM compliant

    scrOfY = document.body.scrollTop;

    scrOfX = document.body.scrollLeft;

  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {

    //IE6 standards compliant mode

    scrOfY = document.documentElement.scrollTop;

    scrOfX = document.documentElement.scrollLeft;

  }

  return scrOfY;

}


function windowSizeY() {

  var myWidth = 0, myHeight = 0;

  if( typeof( window.innerWidth ) == 'number' ) {

    //Non-IE

    myWidth = window.innerWidth;

    myHeight = window.innerHeight;

  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

    //IE 6+ in 'standards compliant mode'

    myWidth = document.documentElement.clientWidth;

    myHeight = document.documentElement.clientHeight;

  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

    //IE 4 compatible

    myWidth = document.body.clientWidth;

    myHeight = document.body.clientHeight;

  }

  return myHeight;

}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

function showElement()
{
	if(gallery.length == 0)
	{
		/** The gallery is empty, don't do anything **/
		return false;
	}
	
	/** Stops the slideshow **/
	stopSlideshow();
	
	if(document.getElementById("gallery_outerPicture").style.visibility=="visible") {
		opacity('veil', 70, 0, 700); 
		document.getElementById("gallery_outerPicture").style.visibility="hidden";
	}
	else 
	{  
		document.getElementById("veil").style.visibility="visible"; 
		document.getElementById("veil").style.display="inline"; 
		opacity('veil', 1, 70, 700);
		document.getElementById("gallery_outerPicture").style.visibility="visible"
	} 
}

function makeElementsVisible() {
	document.getElementById("veil").style.visibility="visible"; 
	document.getElementById("veil").style.display="inline"; 
	document.getElementById("gallery_outerPicture").style.visibility="visible"; 
	document.getElementById("gallery_outerPicture").style.position = 'absolute';	
	document.getElementById("gallery_outerPicture").style.left = ((getWindowWidth()/2)-(document.getElementById("gallery_outerPicture").style.width.replace('px', '')/2))+'px';
	document.getElementById("gallery_outerPicture").style.zIndex = '900';
}

function opacity(id, opacStart, opacEnd, millisec) 
{
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) 
    {
        for(i = opacStart; i >= opacEnd; i-=5) 
        {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) 
    {
        for(i = opacStart; i <= opacEnd; i+=5)
        {  	
        	if(i == 66) 
        	{		
        		setTimeout("changeOpac(" + i + ",'" + id + "',1)",(timer * speed));
        	}
        	else
        	{
            	setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
        	}
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id, goal) 
{
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    if (opacity == 0) 
    {
    	document.getElementById(id).style.visibility = 'hidden';
    }
    
    if(goal == 1) 
    {
		document.getElementById("gallery_outerPicture").style.visibility="visible"; 
		document.getElementById("btn_close").focus();
		document.getElementById("gallery_outerPicture").style.position = 'absolute';
		document.getElementById("gallery_outerPicture").style.left = ((getWindowWidth()/2)-(document.getElementById("gallery_outerPicture").style.width.replace('px', '')/2))+'px';
		document.getElementById("gallery_outerPicture").style.zIndex = '90000'; 		
    }
}



function navkeys(e) 
{
	if (!e) e = window.event;
	
	if(e.keyCode) 
		var key = e.keyCode;	
	if(e.which) 
		var key = e.which;
		
		
	if(key == 27) { // ESC key
		document.getElementById("btn_close").focus();
	}
	if(key == 37) { // LEFT ARROW
		document.getElementById("btn_back").focus();	
	}
	if(key == 39) { // RIGHT ARROW
		document.getElementById("btn_forward").focus();	
	}

}

function swapPicLoad(position, manual)
{
	/** Stops the slideshow if the user manually changed picture **/
	if(manual == "true")
	{
		stopSlideshow();
	}
	
	var imgnum = gallery.length;
	var this_image;
	
	if(position == "back") 
	{
		if(count > 0) 
		{
			this_image = count-1;
		}
		else
		{
			this_image = imgnum-1;
		}
	}
	
	if(position == "forward") 
	{
		if(count < imgnum-1) 
		{
			this_image = count+1;
		}
		else
		{
			this_image = 0;
		}
	}

	/** We throw up the loading image **/
	document.getElementById("gallery_innerPicture").innerHTML = "<table style='height:100%; width:100%'><tr><td align='center'><img align='center' width=\'48px\' height=\'48px\' src=\'images/ajax-loader.gif\' border=\'0\' alt=\'Activity indicator\' title=\'Image loading\' /><br /><p>Billedet er ved at blive loadet...</p></td></tr></table>";

	/** We start preloading the next image **/
	objImage = new Image();
	objImage.onLoad = imagesLoaded(position);
	objImage.src = gallery[this_image][5];
}

function imagesLoaded(position)
{    
    swapPic(position);
    if(slideshow_status == "on")
    {
		slideshow_timer = setTimeout("swapPicLoad(\'forward\')", timeout);
    }
    
}

function swapPic(position,id) 
{
	var imgnum = gallery.length;
	if(typeof(id) != "undefined") 
	{
		count = id;
	}
	
	if(position == "back") 
	{
		if(count > 0) 
		{
			count = count-1;
		}
		else
		{
			count = imgnum-1;
		}
	}
	
	if(position == "forward") 
	{
		if(count < imgnum-1) 
		{
			count = count+1;
		}
		else
		{
			count = 0;
		}
	}
	
	if(count < imgnum && count > -1) 
	{
		var gris = getScrollY() + (windowSizeY()/2);
		
		var imgheight = gallery[count][7];
		var imgwidth = gallery[count][6];
		var imgtext = gallery[count][8];
		
		if(windowSizeY() < 500) {
			var size = windowSizeY()*10/100;
			
			var maxHeight = 400; 
			if(gallery[count][7]*1 > maxHeight) {
				var tmp = gallery[count][7]*1 / maxHeight;
				
				imgwidth = gallery[count][6]*1 / tmp
				imgheight = maxHeight;
				imgtext = "This image have been resized due to small screen resolution";
			}
		}
		else 
		{
			var size = windowSizeY()*10/100;
		}
		
		/** Determining the size of the outerbox **/
		if(imgwidth < 300)
		{
			var outerwidth = 300;
		}
		else
		{
			var outerwidth = imgwidth*1 + 50;
		}
		
		document.getElementById("btn_bck_img").src = "images/icon_back.gif";
		document.getElementById("btn_back").href = "javascript:swapPicLoad(\'back\', \'true\');";
		document.getElementById("btn_back").style.cursor = "pointer";
		document.getElementById("btn_back").title = "Tilbage";
		document.getElementById("btn_back").alt = "Tilbage";

		document.getElementById("btn_fwd_img").src = "images/icon_forward.gif";
		document.getElementById("btn_forward").href = "javascript:swapPicLoad(\'forward\', \'true\');";
		document.getElementById("btn_forward").style.cursor = "pointer";
		document.getElementById("btn_forward").title = "Nęste";
		document.getElementById("btn_forward").alt = "Nęste";
		
		var current = count;
		current +=1;
		document.getElementById("gallery_outerPicture").style.display = "inline"			
		document.getElementById("gallery_innerPicture").innerHTML = "<img width=\'" + imgwidth + "\' height=\'" + imgheight + "\' id=\'gallery_picture\' src=\'" + gallery[count][5] + "\' border=\'0\' alt=\'" + imgtext + "\' title=\'" + imgtext + "\' >";
		document.getElementById("gallery_pictureText").style.width = imgwidth +"px";
		document.getElementById("gallery_pictureText").style.color = "#565656";
		document.getElementById("gallery_outerPicture").style.width = outerwidth +"px";
		document.getElementById("gallery_innerPicture").style.width = imgwidth +"px";
		document.getElementById("gallery_pictureText").innerHTML = "(" + current + "/" + imgnum + ") " + gallery[count][1];
		document.getElementById("gallery_innerPicture").style.height = imgheight +"px";
		document.getElementById("gallery_outerPicture").style.left = ((getWindowWidth()/2)-(document.getElementById("gallery_outerPicture").style.width.replace("px", "")/2))+"px"; 
		document.getElementById("gallery_outerPicture").style.zIndex = "99000";
		document.getElementById("gallery_outerPicture").style.top = (((gris)-(document.getElementById("gallery_innerPicture").style.height.replace("px", "")/2))-60*1)+"px";
	}
}

function createGallery() 
{
	var imgnum = gallery.length;
	var i = 0;
	var maxsize = 130;

	while(i < imgnum) 
	{
		var height = '';
		var width = '';
		
		if(gallery[i][4]*1 > gallery[i][3]*1) 
		{ 
			var tmp = gallery[i][4] / maxsize
			height = maxsize;
			width = (gallery[i][3]/tmp);
		}
		else 
		{
			var tmp = gallery[i][3] / maxsize
			width = maxsize;
			height = (gallery[i][4]/tmp);
		}
		
		document.getElementById('gallery_content').innerHTML += '<div class=\"gallery\"><div class=\"gallery_img\" style=\"left:-' + (width/2)*1 + 'px; top:-' + (height/2)*1 + 'px;\"><a class=\"gallery_img_link\" style=\"max-height:'+ height +'px; \" href=\"javascript:showElement();swapPic(\'new\', ' + i +'); \" ><img style=\"max-height:' + height + 'px; max-width:' + width + 'px;\" src=\"' + gallery[i][2] + '\"  /></a></div></div>'; 
		i++;
	}

}
