// JavaScript Document

var homePageURL = "http://192.168.2.22/jepp/";  
var recentHash = "";
var iframemin = 400;
var frameid = "mainContent";
var mainContent = null; 
var mainContentContainer = null;
var categories = new Array("whatIs", "education", "db", 
								"links", "news", "about");
var links = new Array( 	homePageURL + "WhatIsEcovillage.html",
					   	homePageURL + "EcovillageEducation.html", 
					  	homePageURL + "browse/", 
						homePageURL + "Links.html", 
					  	homePageURL + "wordpress", 
						homePageURL + "AboutJEPP.html"  );
var currentFrame = null;	

// extend functionality of Array objects
Array.prototype.contains = function (element) 
  { // from http://www.go4expert.com/forums/showthread.php?t=606
          for (var i = 0; i < this.length; i++) 
       {
              if (this[i] == element) 
          {
                      return true;
              }
          }
          return false;
  };

Array.prototype.indexOf = function (element)  
{ // from: http://www.devsource.com/article2/0,1895,2180067,00.asp
    for (var i = 0; i < this.length; i++)  {
        if (this[i] == element)  {
            return i;
        }
    }
    return -1;
};

function initializeState() {
	alert("Hi there" + window.location);
	if (window.location != homePageURL)
		window.location == homePageURL;
	mainContent = document.getElementById(frameid);
	if (mainContent == null) {
		alert("jepp.js initializeState() Error: couldn't get an instance of mainContent");
		return false;		
	}
	mainContentContainer = document.getElementById("mainContentContainer");
	if (mainContentContainer == null) {
		alert("jepp.js initializeState() Error: couldn't get an instance of mainContentContainer");
		return false;
	}
	
	var state = window.location.hash;
	if (state == null || state == "" || state == "#" || state=="#home" || state=="#Home") {
		showTab('home');
	}
	else {
		state = state.substring(1); // remove the hash mark
		showTab(state);
	}
	
	addListener(mainContent, "load", resizeIframe);	
	setInterval("pollHash()", 750);	// check URL's hash for changes every .75 seconds
}

function pollHash() {
  	// from http://ajaxpatterns.org/Unique_URLs
     if (window.location.hash==recentHash) {
       return; // Nothing's changed since last polled.
     }
     recentHash = window.location.hash;
     // URL has changed, update the UI accordingly.
     showTab(recentHash.substr(1));
   }

function showTab(objectId) {
	var tab = document.getElementById('mainContent');
	if (tab == null) { 
		alert("jepp.js showTab() error: could not locate element \'mainContent\'");
		return false;
	}
	var btn = null;

	if (categories.contains(objectId) == false) {
		tab.src = "Home.html"; // send the user to the Home page on '#Home' or anything not listed.
		objectId = '#home';
	}
	else {
		// set the selected content
		tab.src = links[categories.indexOf(objectId)];	
	}
	updateButtons(objectId);	
	updateHash(objectId);
//	if(objectId == "news") {
		resizeIframe(); // }
	return false; // don't follow the clicked elements associated href
}

function updateHash(objectId) {
	// set the document hash for bookmarking
	currentFrame = objectId;
	document.location.hash = objectId;
	recentHash = document.location.hash;
}

function updateButtons(objectId) { 
	// highlight selected button, shade others
	for (var e = 0; e < categories.length; e++) {
		btn = document.getElementById(categories[e] + 'Btn');
		if (btn == null) {
			alert("jepp.js showTab() error: could not find element with id: " + categories[e] + "Btn");
		}
		else if (objectId == categories[e])  {
			// highlight its relevant button
			btn.className='lightBG';
		}
		else { 
			// return other buttons to normal style
			btn.className='darkBG';
		}
	}		
}

function testIFrame() {
	mainContent.src = "http://www.cnn.com";
}
function testIFrame2() {
}

/** Dynamically Resize IFrame functions
 *  from http://www.frontpagewebmaster.com/m-89000/tm.htm#89000
 */
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 3 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

function resizeIframe(){

	var currentfr=document.getElementById(frameid)
	if (currentfr && !window.opera){
		currentfr.style.display="block"
		if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) {//ns6 syntax
			currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight + 30; // +judah offset 
		}
		else if (currentfr.Document && currentfr.Document.body.scrollHeight) {//ie5+ syntax
			currentfr.height = currentfr.Document.body.scrollHeight + 30; // +judah offset
		}
	}
	else {
		alert("no currentfr or Using Opera...");
		return;
	}
	
	alert (currentfr.height);
// DOESN'T WORK, IE HOLDS THE LAST CLICKED ON SRC, NOT THE BACKUP UP ONES	
//	if(links.contains(currentfr.src)) {
//		updateButtons(categories[links.indexOf(currentfr.src)]);
//		updateHash(categories[links.indexOf(currentfr.src)]);
//	}
}


// Cross-browser implementation of element.addEventListener()
// from http://snipplr.com/view/561/add-event-listener/
function addListener(element, type, expression, bubbling)
{
	bubbling = bubbling || false;
	
	if(window.addEventListener)	{ // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else return false;
}


// verfies that the calling page is in a frame, if not,
// replaces the URL with variable homePageURL plus the provided hash (provided hash
// must include the # sign. From: http://www.webmasterworld.com/forum21/12046.htm
function checkInFrame(hash) { 
	if (top==self) { 
		if (hash != null && hash.charAt(0) == '#')
			location.replace(homePageURL + hash);	
		else
			location.replace(homePageURL);
	}
}