var currentphoto;

function xmlhttpPost(divName,file) {
	var xmlHttpReq = false;

	// Mozilla/Safari
	if (window.XMLHttpRequest) 
		xmlHttpReq = new XMLHttpRequest();
    	
	// IE
	else if (window.ActiveXObject) 
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    
//	if (divName == "photo")
//		xmlHttpReq.open('POST', "photo.php?photo&id=" + file, true);
	if (divName == "url")
		xmlHttpReq.open('POST', "photo.php?url&id=" + file, true);
	if (divName == "text")
		xmlHttpReq.open('POST', "photo.php?text&id=" + file, true);

	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			updatepage(divName, file, xmlHttpReq.responseText);
		}
	}
	xmlHttpReq.send("");
}

function refresh(file) {
	document.images['thisimage'].style.display = "none";
	document.images['thisimage'].src = "";
	document.images['thisimage'].width = 0;
	document.images['thisimage'].height = 0;
	
	//document.getElementById('photo').style.display = "none";
	document.getElementById('loading').style.display = "block";
	xmlhttpPost("url", file);
	xmlhttpPost("text", file);
}

function updatepage(divName, file, str){
	if (divName == "text")
		updateDiv(divName, str);
	else if (divName == "url")
		preLoadImage(str, file);
	else {
		document.getElementById('loading').style.display = "none";
		document.getElementById(divName).innerHTML = str;
		document.getElementById(divName).style.display = "block";	
	}
}

function updateDiv(divName, str) {
	document.getElementById(divName).innerHTML = str;
}

function preLoadImage(url, file) {
	currentphoto = new Image();
	currentphoto.onload = function(){ doneLoading(url, file); }
	currentphoto.src=url;
}

function doneLoading(url, file) {
	/* Resizing */
	var imageWidth = currentphoto.width;
	var imageHeight = currentphoto.height;
	if (imageWidth > 380) {
		imageHeight = imageHeight * (380 / imageWidth); 
		imageWidth = 380; 
		if (imageHeight > 380) { 
			imageWidth = imageWidth * (380 / imageHeight); 
			imageHeight = 380; 
		}
	} else if (imageHeight > 380) { 
		imageWidth = imageWidth * (380 / imageHeight); 
		imageHeight = 380; 
		if (imageWidth > 380) { 
			imageHeight = imageHeight * (380 / imageWidth); 
			imageWidth = 380;
		}
	}
	document.getElementById('loading').style.display = "none";
	document.images['thisimage'].src = currentphoto.src;
	document.images['thisimage'].width = imageWidth;
	document.images['thisimage'].height = imageHeight;
	document.images['thisimage'].style.display = "block";

//	document.images['photo']
}
	

