// crpe.js: shared JavaScript for CRPE web site

// -----------------------------------------------------------------------------------
// BrowserCheck Object
// provides most commonly needed browser checking variables
// 19990326
// Copyright (C) 1999 Dan Steinman
// Distributed under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynapi/
function BrowserCheck() {
    var agt=navigator.userAgent.toLowerCase(); 
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.v = parseInt(navigator.appVersion)
    this.mac = (agt.indexOf("mac")!=-1);
	this.win = (agt.indexOf("win")!=-1);
    this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0)
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0)
	this.ie6 = (navigator.userAgent.indexOf('MSIE 6')>0)
	if (this.ie5) this.v = 5
	this.min = (this.ns||this.ie); // this.ie && this.win;
}

// automatically create the "is" object
var is = new BrowserCheck()
// end BrowserCheck Object;
// -----------------------------------------------------------------------------------

// Functions from Fireworks to handle rollovers
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_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_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_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];}
}

function fwLoadMenus() {
  var home = document.links[0].href;
  var path = "location='" + (home ? home.substring(0, home.length - 10) : "");
  var font = "Verdana, Arial, Helvetica, sans-serif";
  var fs = (is.win && is.ns ? 10 : 9);
  if (window.fw_menu_0) return;
  window.fw_menu_0 = new Menu("root",160,16,font,fs);
  fw_menu_0.addMenuItem("What We Do",path+"about/index.html'");
  fw_menu_0.addMenuItem("Staff",path+"about/staff/index.html'");
  fw_menu_0.addMenuItem("Board of Directors",path+"about/board/index.html'");
  fw_menu_0.addMenuItem("Delano Advisory Board",path+"about/advisory/index.html'");
  fw_menu_0.addMenuItem("Employment Opportunities",path+"about/employment.html'");
  fw_menu_0.addMenuItem("Internships",path+"about/interns/index.html'");
  fw_menu_0.hideOnMouseOut=true;
  window.fw_menu_1 = new Menu("root",140,16,font,fs);
  fw_menu_1.addMenuItem("Introduction",path+"campaigns/index.html'");
  fw_menu_1.addMenuItem("Civil Rights",path+"campaigns/civilrights/index.html'");
  fw_menu_1.addMenuItem("Clean Air in the Valley",path+"campaigns/cleanair/index.html'");
  fw_menu_1.addMenuItem("Clean Water",path+"campaigns/safewater/index.html'");
  fw_menu_1.addMenuItem("Stop Mega-Dairies",path+"campaigns/dairy/index.html'");
  fw_menu_1.addMenuItem("Responsible Power",path+"campaigns/energy/index.html'");
   fw_menu_1.hideOnMouseOut=true;
  window.fw_menu_2 = new Menu("root",100,16,font,fs);
  fw_menu_2.addMenuItem("Introduction",path+"cases/index.html'");
  fw_menu_2.addMenuItem("California",path+"cases/ca.html'");
  fw_menu_2.addMenuItem("National",path+"cases/us.html'");
  fw_menu_2.hideOnMouseOut=true;
  window.fw_menu_3 = new Menu("root",100,16,font,fs);
  fw_menu_3.addMenuItem("Introduction",path+"clients/index.html'");
  fw_menu_3.addMenuItem("California",path+"clients/ca.html'");
  fw_menu_3.addMenuItem("National",path+"clients/us.html'");
  fw_menu_3.hideOnMouseOut=true;
  window.fw_menu_4 = new Menu("root",100,16,font,fs);
//  fw_menu_4.addMenuItem("Features",path+"resources/index.html'");
  fw_menu_4.addMenuItem("Publications",path+"resources/publications.html'");
  fw_menu_4.addMenuItem("Factsheets",path+"resources/factsheets.html'");
//  fw_menu_4.addMenuItem("Affiliated Organizations",path+"resources/index.html'");
//  fw_menu_4.addMenuItem("Educational Resources",path+"resources/index.html'");
//  fw_menu_4.addMenuItem("Links",path+"resources/index.html'");
   fw_menu_4.hideOnMouseOut=true;
  window.fw_menu_5 = new Menu("root",120,16,font,fs);
//  fw_menu_5.addMenuItem("Introduction",path+"press/index.html'");
  fw_menu_5.addMenuItem("Press Releases",path+"press/pressreleases.html'");
  fw_menu_5.addMenuItem("CRPE in the News",path+"press/news.html'");
   fw_menu_5.hideOnMouseOut=true;
  window.fw_menu_6 = new Menu("root",100,16,font,fs);
//  fw_menu_6.addMenuItem("Upcoming",path+"events/index.html'");
  fw_menu_6.addMenuItem("Calendar",path+"events/index.html'");
//  fw_menu_6.addMenuItem("Past Events",path+"events/index.html'");
   fw_menu_6.hideOnMouseOut=true;

  fw_menu_6.writeMenus();
} // fwLoadMenus()

function crpe_popup(url, name, w, h)
{
	window.open(url, name, "height=" + h + ",width=" + w + ",screenX=8,left=8,screenY=8,top=8,directories=0,menubar=1,location=0,resizable=1,scrollbars=1,status=1,titlebar=1,hotkeys=1,toolbar=0");
    return false;
}


// returns the path to the root folder of the web site
var hasCustomRootPath = 0;
function getRootPath()
{
  	var home = document.links[0].href;
  	var path = (home ? home.substring(0, home.length - 10) : "");
	
	if (hasCustomRootPath)
		return customRootPath;

    return path;
}

// simple script to modify navigation bar to show arrow above section name in nav bar

var sections = ["about", "campaigns", "cases", "clients", "resources", "press", "events"];

function checkSection(i, section)
{
	var imagepath = getRootPath() + "images/";
	if (document.location.href.indexOf("/" + section + "/") > -1)
  	{ 
 		MM_swapImage(section, imagepath + section + "_on.gif", imagepath + section + "_on.gif");
		return true;
	}
	return false;
}

function setNav(imagepath)
{
	for (var i = 0; i < sections.length; ++i)
	{
		if (checkSection(i+1, sections[i]))
			return;
	}
}

// should be called by the onLoad handler
// individual pages can set the pageInit variable to execute a custom onLoad function.
var pageInit = 0;
function crpeInit()
{
	// preload nav bar rollover images
	var imagepath = getRootPath() + "images/";
/*    MM_preloadImages(imagepath + 'home_f2.gif', imagepath + 'about_f2.gif', imagepath + 'whatwedo_f2.gif', imagepath + 'campaign_f2.gif', imagepath + 'resources_f2.gif', imagepath + 'newsroom_f2.gif'); */

	if (pageInit)
		eval(pageInit);

	setNav(imagepath);
}

