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()
{
	var location = document.location.href;
	
	// get the type to display....
	var viewMode = document.getElementById("view_mode");
	
	if ( viewMode != null )
	{
		var newMode = "?v=" + viewMode.value;
		var modeRegex = /(\?v=)(\w{2})/g;
		
		var settingsValues = "s";
		
		// See if we are going to settings
		if ( viewMode.value == settingsValues )
		{
			// reset the path to just the view
			document.location.href = newMode;
		}
		// If we have a view setting
		else if ( location.match(modeRegex) )
		{
			// Make sure &img isn't found... (preview display mode trickery)
			if ( location.indexOf("&img", 0) == -1 )
			{
				// change our page...
				var regex = /\?v=\w{2}(\&d=.*)/g
				
				document.location.href = location.replace(regex, newMode + "$1");
			}
			else
			{		
				var newMode = viewMode.value;
				var regex = /(\?v=)\w{2}(\&d=.*)(\&img=.*)/g;
				
				document.location.href = location.replace(regex, "$1" + newMode + "$2");
			}
		}
		// No view setting, must add it.
		else
		{
			// See if the directory is set
			var dirRegex = /\?d=.*/g;
			if ( location.match(dirRegex) != null )
			{
				// let's replace it
				var dirReplaceRegex = /\?d=(.*)/g;
				
				// found it, add the v setting at the begining of the path
				document.location.href = location.replace(dirReplaceRegex, newMode + "&d=$1");
			}
			else
			{
				// not found... we can set it
				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);
	}
}
