/* LivePower.org Scripts */

// section buttons

function swapSectionNav(sectionName, switchOn) {
	if  (document.body.id == sectionName) return false;
	var navImage = "/img/page_elements/nav-btn_"+sectionName;
	if (switchOn ) navImage = navImage + "-on.gif";
	else navImage = navImage + ".gif";
	document.getElementById("nav-" + sectionName).src = navImage;
}

function Sections_preloadImages() {
	MM_preloadImages(
		 '/img/page_elements/nav-btn_About.gif',
		 '/img/page_elements/nav-btn_Apprentice.gif',
		 '/img/page_elements/nav-btn_School.gif',
		 '/img/page_elements/nav-btn_CSA.gif',
		 '/img/page_elements/nav-btn_About-on.gif',
		 '/img/page_elements/nav-btn_Apprentice-on.gif',
		 '/img/page_elements/nav-btn_School-on.gif',
		 '/img/page_elements/nav-btn_CSA-on.gif'
	);
}

Sections_preloadImages();

// open a large (600 x 400) window
function openLargeWindow(url) {
	if ( window.name == 'largeWindow' ) {
		window.name = "";
		}
	legalWindow = window.open(url, "largeWindow", "toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=yes,width=600,height=420");
	}

// open a small (340 x 320) window
function openSmallWindow(url) {
 smallWindow = window.open(url,"smallWindow","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=340,height=320");
}

// picture window
function pictPopUp(url,height,width) {
	if (height === undefined) height = '604';
	if (width === undefined) width = '530';
	winOpts = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=" + width + ",height=" + height ;
	//winOpts = "toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=" + width + ",height=" + height ;
	largeWindow = window.open(url,"largeWindow",winOpts);
	largeWindow.resizeTo(width,height) ;
	largeWindow.focus() ;
}

// set the main window to the new URL and send that window to the front
function returnToOpener(URLToOpen) {
  if ((window.focus)&&(URLToOpen != "")&&(URLToOpen != null)) { 
    window.opener.location = URLToOpen; 
    }
  if (window.focus) { 
    window.opener.focus(); 
    }
  }
	
// set the main window to the new URL and send that window to the front and then close the calling window
function returnToOpenerCloseSelf(URLToOpen) {
  if ((window.focus)&&(URLToOpen != "")&&(URLToOpen != null)) { 
    window.opener.location = URLToOpen; 
    }
  if (window.focus) { 
    window.opener.focus(); 
		self.close();
    }
  }

// called by a window to move to the front when loaded with new content
function setToFront() {
    if( window.focus ) {
	    self.focus();
    }
}

function PS_preloadImages() {
	MM_preloadImages('/img/interface/utl-calendar-on.gif','/img/interface/utl-contact-on.gif','/img/interface/utl-site_map-on.gif','/img/interface/utl-directory-on.gif','/img/interface/admissions-on.gif','/img/interface/alumni-on.gif','/img/interface/inside-on.gif','/img/interface/parents-on.gif','/img/interface/supporting-on.gif')
}

/* General Macromedia functions, pretty much take as is */

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* Image Rotation Script Modification of code 
   by Sharon Paine (http://www.dyn-web.com/) 
   The initImgRotation() function and rotateImgObj.imagesPath variable are on the page 
   that uses the image rotation 
*/

rotateImgObjs = []; // holds all rotating image objects defined
// constructor 
function rotateImgObj(objectReference,speed) {
  this.speed=speed; this.ctr=0; this.timer=0;  
  this.imgObj = document.images[objectReference]; // get reference to the image object
  this.index = rotateImgObjs.length; rotateImgObjs[this.index] = this;
  this.animString = "rotateImgObjs[" + this.index + "]";
}

rotateImgObj.prototype = {
  addImages: function() { // preloads images
    this.imgObj.imgs = [];
    for (var i=0; arguments[i]; i++) {
      this.imgObj.imgs[i] = new Image();
      this.imgObj.imgs[i].src = rotateImgObj.imagesPath + arguments[i];
    }
  },

  rotate: function() {
    if (this.ctr < this.imgObj.imgs.length-1) this.ctr++;
    else this.ctr = 0;
    this.imgObj.src = this.imgObj.imgs[this.ctr].src;
  }
}

// sets up rotation for all defined rotateImgObjs
rotateImgObj.start = function() {
  for (var i=0; i<rotateImgObjs.length; i++) 
    rotateImgObjs[i].timer = setInterval(rotateImgObjs[i].animString + ".rotate()", rotateImgObjs[i].speed);                     
}

function browserSafeDocHeight() {
	docHeight = Math.max(document.body.scrollHeight,document.body.offsetHeight) ;
	if (window.innerHeight) {  // all except Explorer
		winHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		winHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		winHeight =  document.body.clientHeight;
	}
	return Math.max(winHeight,docHeight); 
}

// Event handling scripts

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};

fixEvent.preventDefault = function() {
	this.returnValue = false;
};

fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

// trims white space from the beginning and the end of a string
function trim(aString) {
	return String(aString).replace(/^\s*/, "").replace(/\s*$/, "") ;
}

/* this is here to extend the navigation bar to the bottom of the screen */
addEvent(window, 'load', initPage) ;

function initPage() {
	if (document.getElementById('nav')) setElementHeightsMax() ;
}

function setElementHeightsMax() {
	navHeight = document.getElementById("nav").offsetHeight;
	contentHeight = document.getElementById("content").offsetHeight;
	if (navHeight < contentHeight) {
		document.getElementById("nav").style.height = contentHeight + 'px';
	} else {
		document.getElementById("content").style.height = navHeight + 'px';
	}
}
