     //
     // Global variables (for shared use between function calls)
     //
     pageName = "";
     toggleloading_var = 0;
     toggleloading_hide = false;
     ajax_photoNavArray = new Array();
     ajax_photoNavArrayIndex = 0;
     ajax_photoNav_Active = 0;


     // _____________________________________________________________


     //
     // Base history functions
     //

     window.dhtmlHistory.create({});
   
     var existingOnload = window.onload;
     window.onload = function() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);
        
        if (existingOnload)
          existingOnload();

       // Bookmarking
       if (window.location.hash != '' && window.location.indexOf('/photos/') != -1) {
          ajax_photoNav(window.location.hash.substr(1,window.location.hash.length-1));
       }
     }

     // page id was passed as the target; therefore, the target passed here contains the id
     function ajax_history(data, key) {
	ajaxgeneric(data,"body_Main");
	dhtmlHistory.add(key,data);
     }

     // Adds page to history list
     function historyChange(newLocation, historyData) {
	var history = (typeof historyData == "object" && historyData != null ? historyStorage.toJSON(historyData) : historyData);
	if (history)
           ajaxgeneric(history, 'body_Main');
        if (newLocation)
           pageName = newLocation;
     }


     // _____________________________________________________________


     //
     // Base navigation functions
     //

     function ajax_nav(page) {
	// passes page id as the target; ajax_history accepts the target variable as the page id
	ajax('ajax_nav.php','id='+page,ajax_history,page);

        pageName = page;
     }

     // ajax navigation without triggering a history event
     function ajax_navnh(page){
	ajax('ajax_nav.php','id='+page,ajaxgeneric,'body_Main');
        pageName = page;
     }

     function ajax_photoNav(page){
//alert("ajax_photoNav: " + ajax_photoNav_Active);
        if (ajax_photoNav_Active == 0) { // If there is no active call out, set as active and perform ajax
           ajax_photoNav_Active = 1;
           ajax('/ajax_photo.php','url='+prepare(page),ajax_photoNavWrapper,page);
        } else {
           ajax_photoNavArray[ajax_photoNavArrayIndex++] = page; // else, push call on end of array
        }
     }

     function ajax_photoNavWrapper(data, key) {
//alert("Start of ajax_photoNavWrapper: " + ajax_photoNav_Active + ", " + ajax_photoNavArray + ", index: " + ajax_photoNavArrayIndex);
        ajax_history(data, key); // put ajax response in history list
        pageName = key;
        ajax_photoNav_Active = 0; // set as inactive
//alert("ajax_photoNavArray[0]: " + ajax_photoNavArray[0]);
        if (ajax_photoNavArray[0] && ajax_photoNavArray[0] != "") { // if there is still actions to perform...
           var page = ajax_photoNavArray[0]; // pop off first element, and shift all others
           for (var i = 1; i <= ajax_photoNavArrayIndex; i++) { 
              ajax_photoNavArray[i - 1] = ajax_photoNavArray[i];
           }
           ajax_photoNavArrayIndex--;
//alert("Start of ajax_photoNavWrapper: " + ajax_photoNav_Active + ", " + ajax_photoNavArray + ", index: " + ajax_photoNavArrayIndex);
//alert("page: " + page);
           ajax_photoNav(page); // perform call
        }
     }

     // _____________________________________________________________


     // 
     // Base AJAX functions
     //

     // url: page to request
     // params: args to post in request
     // callbackFunction: function to call when the request is complete - must receive two arguments
     // target: argument passed on to the callback function (usually target element to display response)
     function ajax(url, params, callbackFunction, target)
     {
	var request = new XMLHttpRequest();
  	request.open("POST", url, true);
  	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	request.setRequestHeader("Connection", "close");

        toggleloading("start");

  	request.onreadystatechange = function()
  	{
    	  if (request.readyState == 4 && request.status == 200)
    	  {
	    toggleloading("done");
	    if (request.responseText) { callbackFunction(request.responseText, target); }
	  }
	};

  	request.send(params);
     }

     // url: page to request
     // params: args to post in request
     // callbackFunction: function to call when the request is complete - must receive two arguments
     // target: argument passed on to the callback function (usually target element to display response)
     function ajaxdie(url, params, callbackFunction, target)
     {
	var request = new XMLHttpRequest();
  	request.open("POST", url, false);
  	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	request.setRequestHeader("Connection", "close");

  	request.send(params);
     }

     // Get an XMLHttpRequest object
     if( !window.XMLHttpRequest ) XMLHttpRequest = function()
     {
	try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
	try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
	throw new Error("Could not find an XMLHttpRequest alternative.")
     };

     // Used to display the results of an ajax request
     function ajaxgeneric(response, targetElement)
     {
        var target = document.getElementById(targetElement);
        if (target && target.innerHTML != response)
        {
           if (targetElement == "body_Main")
           {
             var hold = target.style.visibility;
             target.style.visibility = "hidden";
             target.innerHTML = response;
             fixPageSize();
             target.style.visibility = hold;
           }
           else
           {
             target.innerHTML = response;
           }
        }
     }  

     // Displays the loading box while waiting for an ajax call (unless caller sets toggleloading_hide = true)
     function toggleloading(arg)
     {
        if (toggleloading_hide)
          return;

	if (arg == "start") { toggleloading_var += 1; }
	if (arg == "done") { toggleloading_var -= 1; }
        if (toggleloading_var < 0) toggleloading_var = 0;

        if (toggleloading_var > 0)
           document.getElementById("ajax_loading").style.display = "block";
        else
           document.getElementById("ajax_loading").style.display = "none";
     }


     // _____________________________________________________________


     // 
     // Global functions
     //

     function getBrowserSize()
     {
           // Gets page width & height and window width & height
           // Core code from - quirksmode.org
           var xScroll, yScroll;
	   if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	   } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	   } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	   }
	
	   var windowWidth, windowHeight;
	   if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	   } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	   } else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	   }	
	
	   // for small pages with total height less then height of the viewport
	   if(yScroll < windowHeight){
		pageHeight = windowHeight;
           } else { 
		pageHeight = yScroll;
	   }

 	   // for small pages with total width less then width of the viewport
	   if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	   } else {
		pageWidth = xScroll;
	   }

           return new Array(windowWidth, windowHeight, pageWidth, pageHeight);
     }

     function fixPageSize()
     {
           var bodyHeight = document.getElementById("body_Main").offsetHeight + 210;
           if (bodyHeight > 600)
              document.getElementById("pageContainer").style.height = bodyHeight + "px";
           else 
              document.getElementById("pageContainer").style.height = "600px";
     }

     // escapes the & symbol to be sent through an ajax request
     function prepare(arg)
     {
        arg = arg.replace(/\+/g, "%2B");
        return arg.replace(/&/g, "%26");
     }

     function toggleDisplay(arg)
     {
	arg=document.getElementById(arg);
        
        if (!arg) return;

	if (arg.style.display=="none") arg.style.display = "block"; else arg.style.display="none";
     }


      function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
      	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
      	var arrReturnElements = new Array();
      	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)", "i") : null;
      	var oCurrent;
      	var oAttribute;
      	for(var i=0; i<arrElements.length; i++){
      		oCurrent = arrElements[i];
      		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
      		if(typeof oAttribute == "string" && oAttribute.length > 0){
      			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
      				arrReturnElements.push(oCurrent);
      			}
      		}
      	}
      	return arrReturnElements;
      }