
// initialize ourselves when the page is finished loading
/** Our function that initializes when the page is finished loading. */
window.onload = function() {
  // Start preloading site elements.  Run this asynchronously so we don't stall other startup items.
  setTimeout(function() {
    PageLoader.initialize([
    'images/space.gif'
	]);
  }, 1);

}

/** Displays the given page. */
function displayLocation(newLocation, reload, subpage) {
  // if the location is invalid then display the default, 
  // which is the home page, and load the home page content
  if (newLocation == "") {
    //newLocation = "index.php";
  }
  // call swapPage for effects
  swapPage(newLocation, reload, subpage);
}

// swapPage: change page content
// - fade out current content
// - get new content
// - fade in new content
function swapPage(newLocation, reload, subpage) {

//alert(contentPage);
  var dur = 0.50; // fade/appear duration

  // must hack opacity setting for Safari
  // 0.99999 works in Safari and breaks IE6/7
  // 1.0 works in IE6/7 and breaks Safari
  var endOpacity = (/Konqueror|Safari|KHTML/.test(navigator.userAgent) ? 0.99999 : 1.0);
  
  var newContentPage = newLocation;
  var newSubPage = "";

  if (newContentPage == contentPage) {

  } else {
    //highlightMenu("menu", newContentPage);



    var contentPageURL = newContentPage;
    //var subPageURL = newSubPage + '_snippet.php';
    var subPageURL = "";
    
    if (reload) {
      // this is a page reload (omit fade/appear)
      if (subpage) {
		  Element.update('content_left', PageLoader.get(contentPageURL));
		
		  new Effect.Appear('content_left', {duration: dur});
          highlightMenu('content_right', contentPageURL.replace(/_snippet/, ""));

      } else {
	  
		  if (contentPageURL != 'index_snippet.php?workplaceID=2') {
	  
		  Element.update('content', PageLoader.get(contentPageURL));

		  new Effect.Appear('content', {duration: dur});
		  
		  } else {
			window.location = 'index.php?workplaceID=2&reloadPage=true';
		  
		  }
		  
	  }

    } else {

      // this is not a page reload
		  if (subpage) {
			highlightMenu('content_right', contentPageURL.replace(/_snippet/, ""));
			
			new Effect.Fade('content_left', {duration: dur,
			  afterFinish: function() {
				setTimeout(function() {
				  Element.update('content_left', PageLoader.get(contentPageURL));

				  new Effect.Appear('content_left', {duration: dur});
				}, 10);

			  }
			});

		  } else {

			new Effect.Fade('content', {duration: dur,
			  afterFinish: function() {
				setTimeout(function() {
				
				  if (contentPageURL != 'index_snippet.php?workplaceID=2') {
			  
				  Element.update('content', PageLoader.get(contentPageURL));

				  new Effect.Appear('content', {duration: dur});
				  
				  } else {
					window.location = 'index.php?workplaceID=2&reloadPage=true';
				  
				  }
				  
				}, 10);
			  }
			});
		  }

    }

    contentPage = newContentPage;
  }
}

function highlightMenu(menu, selected) {
  // clear out the old selected menu item
  var links = document.getElementsByClassName("pageLink", menu);
  for (var i = 0; i < links.length; i++) {
    Element.removeClassName(links[i], "selected");
  }
//alert(links);
  // cause the new selected menu item to appear differently in the UI
  Element.addClassName(selected, "selected");
}

var PageLoader = {
  pages: new Array(),

  initialize: function(preloadPages) {
    for (var i = 0; i < preloadPages.length; i++) {
      this.load(preloadPages[i]);
    }
  },
  
  load: function(page) {
//    window.status = "loading " + page;
    new Ajax.Request(page, {
      method: 'get',
      asynchronous: true,
      onComplete: this.onComplete.bind(this,page)
    });
  },
  
  loadNow: function(page) {
//    window.status = "loading " + page;
    var request = new Ajax.Request(page, {
      method: 'get',
      asynchronous: false
    });
    this.pages[page] = request.transport.responseText;
  },
    
  onComplete: function(page,request) {
    this.pages[page] = request.responseText;
//    alert (page + " = " + this.pages[page]);
  },
  
  get: function(page) {
    if (!this.pages[page]) {
      this.loadNow(page);
    }
    return this.pages[page];
  }
}

var contentPage = "";

