var image;

function ajax(page, params, target){
	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;
			}
		}
	}
	
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			document.getElementById(target).innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("POST",page,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	//xmlHttp.overrideMimeType('text/html');
	xmlHttp.send(params);
}

function ajaxDownload(page, params, target){
	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;
			}
		}
	}
	
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			document.getElementById(target).innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("POST",page,true);
	xmlHttp.setRequestHeader("Content-type", "application/octet-stream");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	//xmlHttp.overrideMimeType('text/html');
	xmlHttp.send(params);
}

function refreshPhoto(s){
	clearTimeout();
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp2=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp2.onreadystatechange = function(){
		if(xmlHttp2.readyState == 4){
			image = xmlHttp2.responseText;
			oldHeight = document.getElementById('pgbox').offsetHeight;
			fadeOut(parseInt(s) + 1);
		}else{
			if (document.getElementById('some') != null){
				document.getElementById('some').innerHTML = "<img src=\"images/spinner.gif\" width=\"16\" height=\"16\">";
			}
		}
	}
	var params = 'start=' + parseInt(s);
	xmlHttp2.open("POST","ajaxPhotoGallery.php",true);
	xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp2.setRequestHeader("Content-length", params.length);
	xmlHttp2.setRequestHeader("Connection", "close");
	//xmlHttp.overrideMimeType('text/html');
	xmlHttp2.send(params);
}

var oldHeight = null;
var newHeight = null;
var timer1 = null;
var fadeTimer = null;
var op = 100;
var step = 50;
function fadeOut(np){
	setOpacity(document.getElementById('pgbox'),op);
	if (op > 0){
		step *= 0.9;
		op -= 5;
		fadeTimer = setTimeout("fadeOut('" + np + "')",step);
	}else{
		clearTimeout(fadeTimer);
		setOpacity(document.getElementById('pgbox'),0);
		op = 0;
		step = 25;
		document.getElementById('pgbox').innerHTML = image;
		fadeIn(np);
	}
}

function fadeIn(np){
	setOpacity(document.getElementById('pgbox'),op);
	if (op < 100){
		step *= 1.01;
		op += 5;
		fadeTimer = setTimeout("fadeIn('" + np + "')",step);
	}else{
		clearTimeout(fadeTimer);
		op = 100;
		step = 50;
		newHeight = document.getElementById('pgbox').offsetHeight;
		//resizeBox(np);
		setTimeout("refreshPhoto('" + np + "')",5000);
	}
}
/*
function resizeBox(np){
	if (oldHeight < newHeight){
		oldHeight += 1;
		document.getElementById('pgbox').offsetHeight = oldHeight + "px";
		alert()
		setTimeout("resizeBox('" + np + "')",50);
	}else if (oldHeight > newHeight){
		oldHeight -= 1;
		document.getElementById('pgbox').offsetHeight = oldHeight + "px";
		setTimeout("resizeBox('" + np + "')",50);
	}else{
		setTimeout("refreshPhoto('" + np + "')",5000);
	}
}
*/
function setOpacity(box, o){
	box.style.opacity = parseFloat(parseInt(o) / 100);
	box.style.filter = 'alpha(opacity=' + parseInt(o) + ')';
}