//finalsiteCf global.js
//v. 2.4

// 2.3.1 Added support for drop "up" menus, format: "menu_name,up"
// 2.3.2 Added support for "center" aligned menus, format: "menu_name,center"
// 2.4 Added support for otherBtn, format: fsBtn('btn_name','state','otherBtn_name,other/btn/src.jpg');
//      -- Replacing basic alt_onSrc function with otherBtn function
//		-- Adding addtional pre-load image function which will pull a list from buttons.js

//-- Functions --//
/*

fsBtn('btn_name','state','alt_onSrc') or fsBtn('btn_name','state','otherBtn_name,other/btn/src.jpg');
-- state: on/off (on is default)

swapStyle(title,reset)
*/


// Browser & Platform Check
var isIE = (document.all) ? true : false;
var isNS4 = (document.layers) ? true : false;
var isNS6 = (document.getElementById && !isIE) ? true : false;
var isSafari = (navigator.userAgent.indexOf("Safari") >= 0) ? true : false;

if (navigator.appVersion.toLowerCase().indexOf("mac")!=-1) {
	var isMAC = true;
} else {
	var isMAC = false;
}

//--------------------------------------------------------//

// Button & Menu Options ---------------------------------//

//Time before menus auto-hide
var menu_pause = 250; 

//Show sub-menus for current section( 0=no, 1=yes)
var section_subs = 0;

//Use auto position find for the menus( 0=no, 1=yes)
//Used for relatively positioned/centered layouts
//This option can be turned on for individual layouts by setting it in the <head> tag
var pos_find = 1;


// Set offsets for the menus
    // x/y offset from top left corner of parent button
    var pos_x = 0; 
    var pos_y = 0;

/* ---------

This part has been removed as adding these options in the top margin attribute in a css file is easier. The Safari specific code has been added to address the issues with that browser.

    // browser specific offsets(added to offsets above) for netscape
    // base browser is IE
    var ns6_xoffset = 0;
    var ns6_yoffset = 0;

-------------*/


	// Safari seems to ignore margins on dynamically(?) positioned elements
	var safari_xoffset = 0;
	var safari_yoffset = -15;

//--------------------------------------------------------//



// Style Sheet Options -----------------------------------//

var sheets = new Array(
	//Define stylesheets: "title,href"
	// MUST Have "Default,some/stylesheet.css" defined! 
	"default,uploaded/code/small.css",
    "large,uploaded/code/large.css"
);


// Auto Color link colors
var link_color = "#fff";
var bg_color = "#7B7EAE";


//Browser Specific CSS Code
//if(isIE){ document.write( "<style>#ql_menu a, #sitetools_menu a{ width:100%; }</style>" ); }

//--------------------------------------------------------//
//onLoad functions
function pageLoad() { preLoad(); colorLink(); if(pageid==1){ document.getElementsByTagName('body')[0].className = "home"; } }

var sectionId = "";
var sectionBtn = "";
var thisMenu = "";
var xBtn = "";
var xBtnImg = "";
var wait;


// Button Object Constructor
function btnObj(btnName,menuName,pageId,offSrc,onSrc,selectSrc) {
	this.btnName = btnName;
	if( menuName.indexOf(",") != -1 ){
		this.menuName = menuName.split(",")[0];
		this.menuDirection = menuName.split(",")[1];
	}else{
		this.menuName = menuName;
	}
	this.pageId = pageId;
	this.offSrc = offSrc;
	this.onSrc = onSrc;
	this.selectSrc = selectSrc;
}

function preLoad(){
	//preLoad loops through the buttons array and pre-loads all of the onSrc's
	//Also defines sectionID & sectionBtn & turns on section button

	if(document.images){
		var myImages = new Array();
		var i = 0;
		for(this.btnName in buttons){
			myImages[i] = new Image(); myImages[i].src = buttons[this.btnName].onSrc;
			i++;
			var n=0;
			for(n in pagearray){ // loop through pagearray to match pageid
				if( pagearray[n] == buttons[this.btnName].pageId){
					//turn button on if pageid matches
					document.images[this.btnName].src = buttons[this.btnName].selectSrc ? buttons[this.btnName].selectSrc : buttons[this.btnName].onSrc;
					sectionId = buttons[this.btnName].pageId;
					sectionBtn = buttons[this.btnName].btnName;
				}
				n++;
			}
		}
	
	if(otherImages){
		var oi = new Array();
		for(i=0; i < otherImages.length; i++){
			oi[i] = new Image(); oi[i].src = otherImages[i];
		}
	}
	
	}

} //end preLoad


function fsBtn(btn,state,alt_onSrc){
	
	if(alt_onSrc){
		if(alt_onSrc.indexOf(",") != -1){ //check if there is a comma list with btn_name and src
			ob = alt_onSrc.split(",");
			xBtn = ob[0];
			xBtnImg = ob[1];
			swapImage(btn);
		}else{
			// if there is just an alt src then only that button will be changed
				// this function may no longer be needed but is included for backwards compatiblity
			document.images[btn].src = alt_onSrc;
		}
	}else if(state == "off"){
		imgRestore(btn);
	}else{ swapImage(btn); }
}

function swapImage(btn){
	//swapImage creates a roll-over effect from onSrc when passed the name of the button
	//Also shows sub-menus if specified
	if(buttons[btn].pageId != sectionId){ // btn src will not change if it is in the section, this preserves the selectSrc
		document.images[btn].src = buttons[btn].onSrc; //Turn button on
	}
	
	//check btnID against sectionId and section_menu variable before turning on sub menus
	if( buttons[btn].menuName ){ //Turn on menus if section sub option is on or the button pageId does not equal the sectionId
		showSubs(btn);
	}
	
	if(xBtn){ document.images[xBtn].src = xBtnImg; }
	
} //end swapImage

function imgRestore(btn){
	// turns button off unless it is the section button or has a menu
	if(buttons[btn].menuName){
		mnuOut(btn);
	}else if(buttons[btn].pageId != sectionId){
		document.images[btn].src = buttons[btn].offSrc;
		if(xBtn){ document.images[xBtn].src = buttons[xBtn].offSrc; }
	}
}


//Sub Menu Functions
function showSubs(btn){
	if((thisMenu.length>0) && (thisMenu != buttons[btn].btnName)){
		clearSubs(thisMenu);
	}
	// set offsets based on browser			 
	
	var x_offset = (isSafari) ? safari_xoffset : 0;
	var y_offset = (isSafari) ? safari_yoffset : 0;
	
	if(section_subs == 1 || (buttons[btn].pageId != sectionId)){ //Test for section subs option and section ID
		if(pos_find==1){
			document.getElementById(buttons[btn].menuName).style.left = findPosX(document.getElementById(buttons[btn].btnName)) + (pos_x - x_offset);
			if( buttons[btn].menuDirection == "up" ){
				document.getElementById(buttons[btn].menuName).style.top = (findPosY(document.getElementById(buttons[btn].btnName)) - document.getElementById(buttons[btn].menuName).scrollHeight) + (pos_y - y_offset);
			}else if( buttons[btn].menuDirection == "center" ){
				document.getElementById(buttons[btn].menuName).style.top = (findPosY(document.getElementById(buttons[btn].btnName)) - (document.getElementById(buttons[btn].menuName).scrollHeight / 2))  + (pos_y - y_offset);
			}else{
				document.getElementById(buttons[btn].menuName).style.top = findPosY(document.getElementById(buttons[btn].btnName)) + (pos_y - y_offset);				 			 
			}		
		}
		document.getElementById(buttons[btn].menuName).style.visibility = 'visible';
		thisMenu = buttons[btn].btnName; //add menu to close later
	}
	
	clearTimeout(wait);
} //end showSubs

function mnuOut(btn){
	wait = setTimeout("clearSubs('"+ btn +"')", menu_pause);
}

function clearSubs(btn) { //clears open menu
	if(buttons[btn].menuName){
		document.getElementById(buttons[btn].menuName).style.visibility='hidden';
		thisMenu="";
	}

	if(buttons[btn].pageId != sectionId){
		document.images[btn].src = buttons[btn].offSrc; //turn button back off
		}else if(buttons[btn].selectSrc){
 			document.images[btn].src = buttons[btn].selectSrc;
		}
	if(xBtn){ document.images[xBtn].src = buttons[xBtn].offSrc; }
}


//Automatically find x,y positions of objects
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
//end Sub Menus

// ----------------------------------------------


// StyleSwitcher functions written by Paul Sowden
// http://www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - -
// For the details, visit ALA:
// http://www.alistapart.com/stories/alternate/
//
// nifty StyleSwitcher scripts modified by Aaron Jones 10/10/2002
//
// Updated for finalsite : Craig Edmond 7/8/2004,4/14/2005
// ----------------------------------------------


function swapStyle(title, reset) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (reset == 1) {
	  createCookie("cfstyle", title);
  }
}

function setStyle() {
	var style = readCookie("cfstyle");
	if (style != null) {
		swapStyle(style, 0);
	}
}

// ----------------------------------------------
// Cookie functions
// ----------------------------------------------

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = ";expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+";path=/;";
}

// ----------------------------------------------

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// Write out stylesheets

var style = readCookie("cfstyle");
if(style == null){ style = "default"; }

for(var i=0;i < sheets.length;i++){
  	var rel_str = "alternate stylesheet";
    var sheets_atr = sheets[i].split(',');
  	if(sheets_atr[0] == style){ rel_str = "stylesheet"; }
    document.write( "<link type=\"text/css\" title=\""+ sheets_atr[0] +"\" href=\""+ sheets_atr[1] +"\"  rel=\""+ rel_str +"\" >");
}


function colorLink(){	

	var links = new Array();
	
	if( document.getElementById('leftbanner') ){
		links = document.getElementById('leftbanner').getElementsByTagName('a');	
		for(i=0;i < links.length;i++){
			for(j=2; j<pagearray.length;j++){
				rExp = new RegExp( "p=" + pagearray[j] + "$" , "i" );
				if( links[i].href.search(rExp) > 0 ){ 
					links[i].style.color = link_color;				
					links[i].style.background = bg_color;
				}
			}
		}		
	}
}
