var origImgWidth = -1;
var origImgHeight = -1;
var XMLHttpRequestObject = false;
var isIE = (navigator.appName.indexOf("Internet Explorer") > 0);

if ( window.XMLHttpRequest )
	XMLHttpRequestObject = new XMLHttpRequest();
else if ( window.ActiveXObject )
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

function AjaxCall(dsource)
{
	AjaxCallWithCallback(dsource, null);
}
function AjaxCallWithCallback(dsource, callback)
{
	if ( XMLHttpRequestObject )
	{
		XMLHttpRequestObject.open("GET",dsource);
		if ( callback )
			XMLHttpRequestObject.onreadystatechange = callback;

		XMLHttpRequestObject.send(null);
	}
}
function AjaxPostCallWithCallback(dsource, params, callback)
{
	if ( XMLHttpRequestObject )
	{
		XMLHttpRequestObject.open("POST", dsource, true);
		
		// update our params
		params = encodeURI(params);

		if ( callback )
			XMLHttpRequestObject.onreadystatechange = callback;
		
		//Send the proper header information along with the request
		XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		XMLHttpRequestObject.setRequestHeader("Content-length", params.length);
		XMLHttpRequestObject.setRequestHeader("Connection", "close");
		
		XMLHttpRequestObject.send(params);
	}
}

// disable pressing of the 'down' arrow... it can cause display issues on some browsers
document.onkeydown = function MainKeyPress(e) 
{
	// Handle IE / Anything else...
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	switch( KeyID )
	{
		case 40: //  down arrow
			return false;
	}
	return true;
}
function HideDiv(div)
{
	var hideDiv = document.getElementById(div);
	if ( hideDiv )
		hideDiv.style.display = "none";
}
function ShowDiv(div)
{
	var hideDiv = document.getElementById(div);
	if ( hideDiv )
		hideDiv.style.display = "";
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}
// handle the change of view type mode
function changemode()
{
	// get the type to display....
	var viewMode = document.getElementById("view_mode");
	
	// change our page...
	if ( viewMode != "" )
		document.location.href = "?v=" + viewMode.value;
}


// This is a generic resize for the image browser whenever a resize of the body occurs
// This should be handled for each display class
function sizeBody()
{
	// Check to see if we are in the preview display Field
	if ( document.getElementById("main_div") && document.getElementById("main_div").innerHTML.indexOf("preview_display") >= 0 )
	{
		resizePD();
	}
}
function postOpenAlbum()
{
	if ( XMLHttpRequestObject.readyState == 4 && 
			XMLHttpRequestObject.status == 200 )
	{
		switch( XMLHttpRequestObject.responseText )
		{
			case "root": // return to home
				window.location.href = window.location.pathname;
				break;
			default:
				if ( XMLHttpRequestObject.responseText == "" )
					window.location.reload(); // make sure we reload
				else
					window.location.href = window.location.pathname + XMLHttpRequestObject.responseText;
				break;
		}
	}
}
function postSetDir()
{
	if ( XMLHttpRequestObject.readyState == 4 && 
			XMLHttpRequestObject.status == 200 )
	{
		// Reload the page with the new directory set in the $_SESSION['dir'] variable
		switch( XMLHttpRequestObject.responseText )
		{
			case "root": // return to root (HOME)
				window.location.href = window.location.pathname;
				break;
			default:
				// remove any img tag name so we see an album...
				var imgIndex = window.location.href.indexOf("&img");
				var changeLocation = false;
				if ( imgIndex > 0 )
				{
					window.location.href = window.location.href.substring(0, imgIndex);
					changeLocation = true;
				}				
				if ( XMLHttpRequestObject.responseText != "" )
				{
					window.location.href = window.location.pathname + XMLHttpRequestObject.responseText;
					changeLocation = true;
				}
				
				if ( !changeLocation )
					window.location.reload();
				break;
		}
	}
}
function OpenAlbum(dir)
{
	window.location.href = "?d=" + dir;
}
function SelectDir(dir)
{	
	if ( dir != "." )
		window.location.href = "?d=" + dir;
	else // we don't want ?dir=.  ...it's ugly
	{
		var dsource = "src/ajax.php?setdir=" + dir + window.location.search.replace("?","&");
		AjaxCallWithCallback(dsource, postSetDir);
	}
}