var pollPDF_ID = 0;	// needed to handle background synchronisation of 'content' and 'pdf' frames
var pollPDFcounter = 0	// do it twice, to override any entanglements with the back button fixing routine!
var backButtonFIX = 0;	// needed to know of any discrepancies between content and URL in address bar
var countFourOFour = 0;	// if it's up to 3, it's definitely obsolete content...

function openFrame(currentHash) {	// adapt state according to hash in URL of main window
 var queryHash = currentHash.substring(currentHash.lastIndexOf('#')+1, currentHash.length);
 var pdf_bottom = getObjRef("pdf", "bottom");

 switch(queryHash) {
  case "list":
   top.content.location.href = "./content/courses/courses.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "list_animated":
   top.content.location.href = "./presentation/list/list_v31.html";
   startPollingPDF_BG("pdf_bottom.jpg");
   break;
  case "contact1":
   top.content.location.href = "./content/contact/address.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "contact2":
   top.content.location.href = "./content/contact/accomodation.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "contact3":
   top.content.location.href = "./content/contact/jobs.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "contact4":
   top.content.location.href = "./content/contact/imprint.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "contact5":
   top.content.location.href = "./content/contact/links.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "about1":
   top.content.location.href = "./content/about/philosophy.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "about2":
   top.content.location.href = "./content/about/team.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "about3":
   top.content.location.href = "./content/about/surroundings.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "about4":
   top.content.location.href = "./content/about/history.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "philo1":
   top.content.location.href = "./content/philosoforum/healing.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "philo2":
   top.content.location.href = "./content/philosoforum/humanity.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "philo3":
   top.content.location.href = "./content/philosoforum/poetry.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info1":
   top.content.location.href = "./content/info/info1/info1.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info2":
   top.content.location.href = "./content/info/info2/info2.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info3":
   top.content.location.href = "./content/info/info3/info3.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info4":
   top.content.location.href = "./content/info/info4/info4.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info5":
   top.content.location.href = "./content/info/info5/info5.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info6":
   top.content.location.href = "./content/info/info6/info6.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info7":
   top.content.location.href = "./content/info/info7/info7.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info8":
   top.content.location.href = "./content/info/info8/info8.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "info9":
   top.content.location.href = "./content/info/info9/info9.html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "animation":	
   top.content.location.href = "./presentation/projection/projection_v31.html";
   startPollingPDF_BG("pdf_bottom.jpg");
   break;
  case "katalog":	
   top.content.location.href = "./presentation/forms/request_catalogue.php";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  case "anmeldung":	
   top.content.location.href = "./presentation/forms/book_courses.php";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  default:	// it's a course (at least it should be)...
   // ...so we accept it - if it's bogus (doc title "404"), we'll deal with it in pollHash...
   top.content.location.href = "./content/courses/" + queryHash + "/" + queryHash + ".html";
   startPollingPDF_BG("pdf_bottom_v2.jpg");
   break;
  }
 }

function content2hash() {	// convert file name of source in content frame into hash definition (see "openFrame" above)

// again, we have to take into account, that IE6 might deny access to "404" page displayed in content frame!
try {
//

 var contentPath = top.content.location.href;

//
} catch(error) {
//

 var contentPath = "./presentation/background/dummy.html";

//
} finally {
//

 var contentFile = contentPath.substr(contentPath.lastIndexOf('\/') + 1)
 var contentName = contentFile.substr(0, contentFile.lastIndexOf('.'));

 switch(contentName) {
  case "address":	// general page
   return("contact1");
   break;
  case "accomodation":	// general page
   return("contact2");
   break;
  case "jobs":	// general page
   return("contact3");
   break;
  case "imprint":	// general page
   return("contact4");
   break;
  case "links":	// general page
   return("contact5");
   break;
  case "philosophy":	// general page
   return("about1");
   break;
  case "team":	// general page
   return("about2");
   break;
  case "surroundings":	// general page
   return("about3");
   break;
  case "history":	// general page
   return("about4");
   break;
  case "healing":	// general page
   return("philo1");
   break;
  case "humanity":	// general page
   return("philo2");
   break;
  case "poetry":	// general page
   return("philo3");
   break;
  case "projection_v31":	// animation
   return("animation");
   break;
  case "request_catalogue":	// form1
   return("katalog");
   break;
  case "book_courses":	// form2
   return("anmeldung");
   break;
  case "courses":	// simple list
   return("list");
   break;
  case "list_v31":	// animated list
   return("list_animated");
   break;
  default:	// it's a course or info page, (or it's 'dummy', which indicates missing content)...
   return(contentName);
   break;
  }

//
}
//

 }

function hash2menuReset(hash) {	// map hash to available menu reset values
 switch(hash) {
   case "contact1":
   case "contact2":
   case "contact3":
   case "contact4":
   case "contact5":
   case "about1":
   case "about2":
   case "about3":
   case "about4":
   case "philo1":
   case "philo2":
   case "philo3":
   case "list":	// simple list
     return(hash);
     break;
   case "list_animated":	// animated list
     return("list");
     break;
   default:
     return("animation");	// that is, completely reset the menu...
     break;
   }
 }

function setHash(hashValue) {		// set the hash cache in the menu frame
 var hashCacheElement = getObjRef("menu", "hash");

 hashCacheElement.value = hashValue;
 }

function setHash2(hashValue) {	// set the hash cache in the content frame
 var hashCacheElement = getObjRef("content", "hash");

 hashCacheElement.value = hashValue;
 }

function setHash3(hashValue) {	// set the hash cache in the pdf frame
 var hashCacheElement = getObjRef("pdf", "hash");

 hashCacheElement.value = hashValue;
 }

// for animated flash menu to signal it's "READY." (like in good ole commodore times ;)
function signalReady() {
 var readyCacheElement = getObjRef("menu", "menu_ready");

 readyCacheElement.value = "READY.";
 }

function pollHash() {	// if any changes in state or URL, adjust URL & state accordingly
 var baseURL = "http://www.schloss-schule.at/";
 var hashCacheElement = getObjRef("menu", "hash");		// state changes in menu frame
 var hashCacheElement2 = getObjRef("content", "hash");	// state changes in content frame
 var hashCacheElement3 = getObjRef("pdf", "hash");	// state changes in pdf frame
 var menuResetElement = getObjRef("menu", "menu_reset");
 var menuReadyElement = getObjRef("menu", "menu_ready");
 var theMenu = null;
 var hashCache = hashCacheElement.value;
 var hashCache2 = "";
 var hashCache3 = "";
 var currentURLhash = "";
 var currentURLhashTrim = "";
 var contentHash = "";
 var pdf_bottom = getObjRef("pdf", "bottom");
 var contentTitle = "";

 // one of the last (a nasty one to get right) fix to this website's ("schloss-schule 3.0") code:
 // check if the course/hash we set didn't exist and redirect to homepage instead...

 //
 try {	// unfortunately, for IE6 we have to catch this...
 //

 contentTitle = top.content.document.title;

 //
 } catch(error) {	// OK, so it's an unaccessible "404" page alright, fabricated by IE6
 //

 contentTitle = "404";

 //
 } finally {
 //

 if(contentTitle.indexOf("404") > -1) {
   countFourOFour = countFourOFour + 1;
   if (countFourOFour == 3)	// OK, that does it - relaunch!!
     top.location.href = "http://www.schloss-schule.at/";
   }
 else countFourOFour = 0;

 //
 // check for changes in state caused by menu clicks
 //

 // consider link functionality in pdf frame first
 if (hashCacheElement3) {	// if there is any cache in pdf...
   hashCache3 = hashCacheElement3.value;
   if (hashCache3 != "unused") {
     hashCache = hashCache3;
     hashCacheElement.value = hashCache;
     menuResetElement.value = "animation";
     hashCacheElement3.value = "unused";
     }
  }

 // consider link functionality in content frame first
 if (hashCacheElement2) {	// if there is any cache in content at all (e.g. could be a form or too early...)
   hashCache2 = hashCacheElement2.value;
   if (hashCache2 != "unused") {
     hashCache = hashCache2;
     hashCacheElement.value = hashCache;
     menuResetElement.value = "animation";
     hashCacheElement2.value = "unused";
     }
   }

 if (hashCache != lastHash) {	// If anything has changed at all...

   lastHash = hashCache;		// Yes, something's been going on, let's store it...
   window.location.replace(baseURL + "#" + lastHash);	//... and apply this to URL...
   openFrame(lastHash);						//... and adjust content!

   backButtonFIX = 0;
   }

 else {

   // so everything seems to be just fine and peachy, but first let's fix back button in IE...
   
   currentURLhash = window.location.hash
   if (currentURLhash.length)
     currentURLhashTrim = currentURLhash.substring(currentURLhash.lastIndexOf('#')+1, currentURLhash.length);
   
   contentHash = content2hash();

   if (currentURLhashTrim.length==0) {
     if (contentHash!="animation") {
       if(backButtonFIX) {	// that's one dissimilarity too many!
         window.location.replace(baseURL + "#" + contentHash); // reflect back button content in URL
         menuResetElement.value = hash2menuReset(contentHash); // reset menu too
         // now reset everything else too - nothing should stir after that... ;)
         hashCache = contentHash;
         hashCacheElement.value = hashCache;
         lastHash = hashCache;
         backButtonFIX = 0;
         }
       else {
         // change bg in pdf frame immediately, even when in doubt, to avoid visual flicker!
         pdf_bottom.src="pdf_bottom.jpg";
         backButtonFIX = backButtonFIX + 1;	// count occurrences
         }
       }
     }
   else {
     if (contentHash!=currentURLhashTrim) {	// ... if necessary!!!
       if(backButtonFIX) {	// that's one dissimilarity too many!
         window.location.replace(baseURL + "#" + contentHash); // reflect back button content in URL
         menuResetElement.value = hash2menuReset(contentHash); // reset menu too
         // now reset everything else too - nothing should stir after that... ;)
         hashCache = contentHash;
         hashCacheElement.value = hashCache;
         lastHash = hashCache;
         backButtonFIX = 0;
         }
       else {
         // change bg in pdf frame immediately, even when in doubt, to avoid visual flicker!
         if ((contentHash=="list_animated") || (contentHash=="animation"))	// reset background in pdf frame too
           pdf_bottom.src="pdf_bottom.jpg";
         else
           pdf_bottom.src="pdf_bottom_v2.jpg";
         backButtonFIX = backButtonFIX + 1;	// count occurrences
         }
       }
     }
   }

 // now check for changes in URL caused by bookmarks or addresses input by user

 currentURLhash = window.location.hash
 if (currentURLhash.length)
  currentURLhashTrim = currentURLhash.substring(currentURLhash.lastIndexOf('#')+1, currentURLhash.length);

 if ((currentURLhashTrim.length==0) && (lastHash!="animation")) {
  // URL is set back to animation/homepage!
  lastHash = "animation";
  menuResetElement.value = "animation";
  }
 else if (currentURLhashTrim.length) {
   if (currentURLhashTrim!=lastHash) {
    // URL is set differently...
    // ...prepare for adjustment in next call to pollHash()!
    lastHash = "";
    hashCacheElement.value = currentURLhashTrim;
    menuResetElement.value = hash2menuReset(currentURLhashTrim);
    }
   }

 // there's one important thing left to do:
 // in case of the menu getting stuck (e.g. after using the forward button to enter the site again), we have to do something...
 if (menuReadyElement != null) {
   if (menuReadyElement.value == "READY.") { // menu signalled ready, but it's stuck!!
     // so get it going again...
     theMenu=getFlashMovieObject("menu", "ssmenu");
     theMenu.GotoFrame(14);	// proper init
     theMenu.Play();
     menuReadyElement.value = "RESETPOLLINGREADY.";
     menuResetElement.value = contentHash;
     }
   }

 //
 }
 //

 }

function startPolling() {			// start checking periodically...
 setInterval("pollHash()", 150);
 }

function pollPDF_BG(pic_path) {
 var frame_loaded = getObjRef("content", "frame_loaded");
 var pdf_bottom = getObjRef("pdf", "bottom");

 if(frame_loaded != null) {
  if(frame_loaded.value == "done") {
   pdf_bottom.src=pic_path;
   pollPDFcounter = pollPDFcounter + 1;
   if (pollPDFcounter==2) clearInterval(pollPDF_ID);	// been there, done that...
   }
  }
 }

function startPollingPDF_BG(pic_path) {
 pollPDFcounter = 0;
 pollPDF_ID = setInterval("pollPDF_BG('" + pic_path + "')", 100);
 }