var lightbox;
var myDiv_g;
var loading_g;


function setDivs_gallery()
{
	lightbox = document.getElementById("galleryBack");
	loading_g = document.getElementById("galleryLoad");
	myDiv_g = document.getElementById("galleryWrapper");	

	

}

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--) {
            setTimeout("changeOpac(" + i + ",'" + id + "', false)",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "', true)",(timer * speed));
            timer++;
        }
    }
	
}

//change the opacity for different browsers
function changeOpac(opacity, id, up) 
{
 	var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
	

	if (opacity<=0 && up == false)
		object.zIndex = 0;
} 


// holds an instance of XMLHttpRequest
var xmlHttp_gallery = createXmlHttpRequestObject_gallery();
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject_gallery()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

function closePicture()
{
	
	opacity('galleryBack', 90, 0, 400);
	
	
	myDiv_g.style.visibility = 'hidden';
	
	myDiv_g.innerHTML = '';
	
	window.scrollTo(0,0);

	
}

function showPicture(user, picture)
{
	
	
	window.scrollTo(0,0);
	setDivs_gallery();
	lightbox.style.zIndex = 10;
	opacity('galleryBack', 0, 90, 400);
	
	
	myDiv_g.style.visibility = 'visible';
	windowHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.documentElement.clientHeight);
	lightbox.style.height = windowHeight + 'px';

	getPicture(user, picture);
	
	
	
}

// called to read a file from the server
function getPicture(user, picture)
{
		// only continue if xmlHttp isn't void
		if (xmlHttp_gallery)
		{
			// try to connect to the server
			try
			{
				/*var params = "user=" + escape(user) + "&picture=" + escape(picture);
				xmlHttp_gallery.open("POST", "../include/getPicture.php", true);
				
				//Send the proper header information along with the request
				xmlHttp_gallery.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp_gallery.setRequestHeader("Content-length", params.length);
				xmlHttp_gallery.setRequestHeader("Connection", "close");			
				
				xmlHttp_gallery.onreadystatechange = handleRequestStateChange_gallery;
				
				xmlHttp_gallery.send(params);
				*/
				xmlHttp_gallery.open("GET", "../include/getPicture.php?"+"user=" + escape(user) + "&picture=" + escape(picture), true);
				xmlHttp_gallery.onreadystatechange = handleRequestStateChange_gallery;
				xmlHttp_gallery.send(null);
				
				setDivs_gallery();
				
				
				
			
				
			}
			// display the error in case of failure
			catch (e)
			{
				alert("Can't connect to server:\n" + e.toString());
				closePicture();
			}
		}
}	
// function that handles the HTTP response
function handleRequestStateChange_gallery()
{
	
	// display the status of the request
	if (xmlHttp_gallery.readyState == 1)
	{
		//loading
		myDiv_g.innerHTML = '';

		loading_g.style.visibility = "visible";
		window.scrollTo(0,0);
		
	}
	
	// when readyState is 4, we also read the server response
	else if (xmlHttp_gallery.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if (xmlHttp_gallery.status == 200)
		{
			try
			{
				loading_g.style.visibility = 'hidden';
				
				// read the message from the server
				response = xmlHttp_gallery.responseText;
			
				myDiv_g.innerHTML = response;
		
				
				
			}
			catch(e)
			{
				// display error message
				alert("Error reading the response: " + e.toString());
				closePicture();
			}
		}
		else
		{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp_gallery.statusText);
			closePicture();
		}
	}
}


function setGalleryDivs()
{
	image   = document.getElementById("galleryImage");		
	imgWrapper = document.getElementById("galleryImageWrapper");
	desc = document.getElementById("galleryDesc");
	
	imgWrapper.style.background = 'white';
	
	if (image.width >=200)			
		imgWrapper.style.width = (image.width) + 'px';
	else
		imgWrapper.style.width = 200 + 'px';
	
	
	
	if (desc != null)
		imgWrapper.style.height = (image.height +  desc.offsetHeight + 10) + 'px';
	else
		imgWrapper.style.height = (image.height) + 'px';
				
	nav    = document.getElementById("galleryNav");
	
	if (image.width >=200)	
		nav.style.width = (image.width) + 'px';	
	else
		nav.style.width = 200 + 'px';	

	
	if (parseInt(imgWrapper.style.height) + 200 > parseInt(lightbox.style.height))
	{
		lightbox.style.height = (parseInt(imgWrapper.style.height) + 200) + 'px';
	}
	

}




