// gallery

function isLoading(doLoad){
	var loadingDiv = document.getElementById("loadingContentBlock");
	
	if(doLoad){
		loadingDiv.style.visibility = "visible";
		loadingDiv.style.display 	= "inline";
	}
	else{
		loadingDiv.style.visibility = "hidden";
		loadingDiv.style.display 	= "none";
	}
}

function setGalleryAlbum(){
	var gallerySelect = document.getElementById("gallerySelect");
	var selected = gallerySelect.selectedIndex;
	var galleryId = gallerySelect.options[selected].value;
	
	var albumSelect = document.getElementById("albumSelect");
	if(galleryId == -1){
		//empty albumSelect
		while(albumSelect.length > 0){
			albumSelect.remove(0);
		}
		var option = document.createElement('option');
		option.text = "Vælg album";
		option.value = "-1";
		
		try{
			albumSelect.add(option,null);
		}
		catch(exception){
			albumSelect.add(option); //IE
		}
		albumSelect.disabled = true;
	}
	else{
		//enable Loading
		isLoading(true);
	
		var params = "galleryid="+galleryId;
		
		var xmlHttp = getXmlHttpObject();
		
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				var response = eval(xmlHttp.responseText);				
				//empty albumSelect
				while(albumSelect.length > 0){
					albumSelect.remove(0);
				}
				//fill albumSelect
				for(i=0;i<response.length;i++){
					if(i%2==0){
						var newOption = document.createElement('option');
						newOption.value = String(response[i]);
					}
					else{
						newOption.text = String(response[i]);
						try{
							albumSelect.add(newOption,null);
						}
						catch(exception){
							albumSelect.add(newOption); //IE
						}
					}
				}
				//enable albumSelect
				albumSelect.disabled = false;
				//disable Loading
				isLoading(false);
			}
		}
		xmlHttp.open("POST","gallery.php?",true);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
		albumSelect.disabled = true;
	}
}

function showGalleryAlbum(){
	var gallerySelect 	= document.getElementById("gallerySelect");
	var selected 		= gallerySelect.selectedIndex;
	var galleryName 	= gallerySelect.options[selected].text;
	
	var albumSelect 	= document.getElementById("albumSelect");
	var albumSelected 	= albumSelect.selectedIndex;
	var albumName 		= albumSelect.options[albumSelected].text;
	var albumId 		= albumSelect.options[albumSelected].value;
	
	if(albumId != -1){
		//enable Loading
		isLoading(true);
		
		var params = "albumid="+albumId+"&album="+albumName+"&gallery="+galleryName;
		
		var xmlHttp = getXmlHttpObject();
		
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				var response = xmlHttp.responseText;
				
				var disPane = document.getElementById("displayPanel");
				disPane.innerHTML = response;
				disPane.style.visibility = "visible";
				disPane.style.display = "block";
				setGalleryDisplay();
				
				//disable Loading
				isLoading(false);
			}
		}
		xmlHttp.open("POST","gallery.php?",true);
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
}
var initTop,initLeft;
function setGalleryDisplay(){
	var display = document.getElementById("galleryDisplay");
	initLeft = findPosX(display);
	initTop = findPosY(display);
	
	display.style.top 	= initTop+"px";
	display.style.left 	= initLeft+"px";
	display.style.position = "static";
		
	if(window.addEventListener){
		window.addEventListener('scroll',positionDisplay,false);
	}
	else if(window.attachEvent){
		window.attachEvent('onscroll',positionDisplay);
	}
}
function positionDisplay(){
	var display = document.getElementById("galleryDisplay");
	var scrTop = document.documentElement.scrollTop;
	if(scrTop >= initTop-50){
		display.style.position = "fixed"
		display.style.top = "50px";
	}
	else if(scrTop <= initTop){
		display.style.position = "static";
	}
}
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function displayImg(path,filename,desc){
	var display = document.getElementById("galleryDisplay");
	var href = path.concat(filename);
	display.innerHTML =	"<a href=\""+href+"\" onclick=\"showLightbox(this);return false;\" title=\""+desc+"\"><img src=\""+path+"m_"+filename+"\"/></a><br />";
	display.innerHTML += "<div id=\"displayPanelDesc\">"+desc+"</div>";
	display.innerHTML += "<i>[Klik på billedet for at se det i fuld størrelse]</i>";
}

//AJAX
function getXmlHttpObject(){
	var xmlHttp = null;
	try{  // Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}
	catch(e){  // Internet Explorer  
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
			alert("Your browser does not support AJAX!");
			return false;
			}    
		}  
	}
	return xmlHttp;
}