var imgheight;
var imgwidth;
var img_src;
var position;
var gallery_length;
var current_array_name;

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(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();
		showElement();
	}
	if(key == 37) // LEFT ARROW
	{ 
//		document.getElementById("btn_back").focus();	
		loadImage(0, "back");
	}
	if(key == 39) // RIGHT ARROW
	{ 
//		document.getElementById("btn_forward").focus();	
		loadImage(0, "forward");
	}

}

function loadImage(id, direction, array_name)
{
	if(array_name == undefined)
	{
		array_name = current_array_name;
	}
	
	if(array_name == "a_gallery_images_frontpage")
	{
		array = a_gallery_images_frontpage;
	}
	else
	{
		array = a_gallery_images;
	}
	
	current_array_name = array_name;
	gallery_length = array.length;
	
	if(direction == undefined)
	{
		/** Direction was undefined, thus we use the provided src **/
		img_src = id.src.replace("_small", "");	
		/** We need to set the position **/
		for(var i = 0;i<array.length;i++)
		{
			if(id.src == (documentroot+image_path+array[i][2]))
			{
				position = i;
			}
		}
	}
	else
	{
		/** Direction was defined, thus we move a step in that direction **/
		if(direction == "forward" && array[position+1] != undefined)
		{
			position++;
			
		}
		else if(direction == "back" && array[position-1] != undefined)
		{
			position--;
		}
		else
		{
			return false;
		}
		img_src = documentroot+image_path+array[position][2].replace("_small", "");
	}
	
	imgwidth = array[position][0]*1;
	imgheight = array[position][1]*1;
	swapPic();
}

function swapPic() 
{
	var imgnum = gallery_length;
	var count = position;
	
	if(count < imgnum && count > -1) 
	{
		var gris = getScrollY() + (windowSizeY()/2);
		
		if(windowSizeY() < 700) 
		{
			var size = windowSizeY()*10/100;
			
			var maxHeight = 500; 
			if(imgheight > maxHeight) 
			{
				var tmp = imgheight / maxHeight;
				
				imgwidth = imgwidth / tmp
				imgheight = maxHeight;
				imgtext = "This image have been resized due to small screen resolution";
			}
		}
		else 
		{
			var size = windowSizeY()*10/100;
		}
		
		if(count == 0) 
		{
			document.getElementById("btn_bck_img").src = "images/icon_back_faded.gif";
			document.getElementById("btn_back").href = "javascript:void(0);";
			document.getElementById("btn_back").style.cursor = "default";
			document.getElementById("btn_back").title = "";
			document.getElementById("btn_back").alt = "";
		}
		else 
		{
			document.getElementById("btn_bck_img").src = "images/icon_back.gif";
			document.getElementById("btn_back").href = "javascript:loadImage(0,\'back\');";
			document.getElementById("btn_back").style.cursor = "pointer";
			document.getElementById("btn_back").title = "Tilbage";
			document.getElementById("btn_back").alt = "Tilbage";
		}
		
		if(count == gallery_length-1) 
		{
			document.getElementById("btn_fwd_img").src = "images/icon_forward_faded.gif";
			document.getElementById("btn_forward").href = "javascript:void(0);";
			document.getElementById("btn_forward").style.cursor = "default";
			document.getElementById("btn_forward").title = "";
			document.getElementById("btn_forward").alt = "";
		}
		else 
		{
			document.getElementById("btn_fwd_img").src = "images/icon_forward.gif";
			document.getElementById("btn_forward").href = "javascript:loadImage(0,\'forward\');";
			document.getElementById("btn_forward").style.cursor = "pointer";
			document.getElementById("btn_forward").title = "Nęste";
			document.getElementById("btn_forward").alt = "Nęste";
		}
		
		document.getElementById("gallery_outerPicture").style.display = "inline";	
		document.getElementById("gallery_innerPicture").innerHTML = "<img width=\'" + imgwidth + "\' height=\'" + imgheight + "\' id=\'gallery_picture\' src=\'" + img_src + "\' />";
		document.getElementById("gallery_innerPicture").style.width = imgwidth +"px";
		document.getElementById("gallery_innerPicture").style.height = imgheight +"px";
		
		document.getElementById("gallery_outerPicture").style.width = imgwidth +"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";
	}
}
