// global.js: shared JavaScript for Africa Action 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.ie55 = (navigator.userAgent.indexOf('MSIE 5.5')>0)
	this.dom = (this.ie || (this.b=="ns" && this.v>=5));
	if (this.ie5) this.v = 5
	this.min = (this.ns||this.ie);
}

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

// -----------------------------------------------------------------------------------
// Functions from Fireworks to handle rollovers

// function that displays status bar message
var showMsg = navigator.userAgent != "Mozilla/4.0 (compatible; MSIE 4.0; Mac_PowerPC)";

function di(id,name){
  if (document.images) {document.images[id].src=eval(name+".src"); }
}

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_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_nbGroup(event, grpName) { //v3.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : args[i+1];
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    if ((nbArr = document[grpName]) != null)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = args[i+1];
      nbArr[nbArr.length] = img;
  } }
}

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_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 MM_swapImagePerm() { //v3.0
  var i,x,a=MM_swapImagePerm.arguments; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){ x.src=a[i+2];}
}

// -----------------------------------------------------------------------------------
// Function from Fireworks to build menus

function fwLoadMenus() {
  var path = "location='" + getRootPath();
  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",120,16,font,fs);
  fw_menu_0.addMenuItem("History &amp; Mission",path+"about/index.php'");
  fw_menu_0.addMenuItem("Programs",path+"about/programs.php'");
  fw_menu_0.addMenuItem("Baraza",path+"events/baraza/index.php'");
  fw_menu_0.addMenuItem("Staff &amp; Board",path+"about/staff.php'");
  fw_menu_0.addMenuItem("Jobs &amp; Internships",path+"about/jobs.php'");
  fw_menu_0.addMenuItem("Annual Reports",path+"about/reports.php'");
  fw_menu_0.addMenuItem("Contact Us",path+"about/contact.php'");
  fw_menu_0.addMenuItem("Archives",path+"resources/archives.php'");
//  fw_menu_0.addMenuItem("Africa's Right to Health Campaign",path+"whatwedo/index.php#campaign'");
//  fw_menu_0.addMenuItem("Policy Analysis &amp; Information for Action",path+"whatwedo/index.php#policy'");
//  fw_menu_0.addMenuItem("Media Relations",path+"whatwedo/index.php#media'");
//  fw_menu_0.addMenuItem("Public Education &amp; Outreach",path+"whatwedo/index.php#education'");
   fw_menu_0.hideOnMouseOut=true;
  window.fw_menu_1 = new Menu("root",150,16,font,fs);
  fw_menu_1.addMenuItem("Action Center Home",path+"campaign_new/index.php'");
  fw_menu_1.addMenuItem("Activist Toolkit",path+"campaign_new/toolkit.php'");
  fw_menu_1.addMenuItem("Take Action Now",path+"campaign_new/action.php'");
  fw_menu_1.addMenuItem("Campaign Updates",path+"campaign_new/campaign_updates.php'");
  fw_menu_1.addMenuItem("Religious Action Network",path+"campaign_new/ran.php'");
  fw_menu_1.addMenuItem("Student Network",path+"campaign_new/student.php'");
  fw_menu_1.addMenuItem("Stop Genocide in Darfur",path+"campaign_new/darfur.php'");
  fw_menu_1.addMenuItem("End HIV/AIDS in Africa",path+"campaign_new/arth.php'");
  fw_menu_1.addMenuItem("Cancel Africa's Debt",path+"campaign_new/debt.php'");
  fw_menu_1.addMenuItem("Sign Me Up",path+"campaign_new/signup.php'");
   fw_menu_1.hideOnMouseOut=true;
  window.fw_menu_2 = new Menu("root",200,16,font,fs);
  fw_menu_2.addMenuItem("Resources Home",path+"resources/index.php'");
  fw_menu_2.addMenuItem("Africa Policy Outlook",path+"resources/page.php?type=18'");
  fw_menu_2.addMenuItem("Africa Action on Darfur",path+"campaign_new/resources_analysis.php?issues=1024'");
  fw_menu_2.addMenuItem("Africa Action on HIV/AIDS",path+"campaign_new/resources_analysis.php?issues=1'");
  fw_menu_2.addMenuItem("Africa Action on Debt",path+"campaign_new/resources_analysis.php?issues=2'");
  fw_menu_2.addMenuItem("Resources on Global Apartheid",path+"resources/globalapartheid.php'");
  fw_menu_2.addMenuItem("Resources on Other Issues",path+"resources/issues/index.php'");
  fw_menu_2.addMenuItem("Resources on Key Countries",path+"resources/countries/index.php'");
  fw_menu_2.addMenuItem("Position Papers, Reports &amp; Articles",path+"resources/papers.php'");
  fw_menu_2.addMenuItem("Presentations",path+"resources/page.php?type=9'");
  fw_menu_2.addMenuItem("News &amp; Information on Africa",path+"resources/news.php'");
  fw_menu_2.addMenuItem("Map of Africa",path+"resources/map.php'");
  fw_menu_2.addMenuItem("Links",path+"resources/links.php'");
  
//  fw_menu_2.addMenuItem("Press Commentaries &amp; Opinion Pieces",path+"resources/page.php?type=15'");
//  fw_menu_2.addMenuItem("Activist Toolkit",path+"campaign/toolkit.php'");
//  fw_menu_2.addMenuItem("Africa Policy E-Journal",path+"resources/ejournal.php'");
//  fw_menu_2.addMenuItem("Archives",path+"resources/archives.php'");
   fw_menu_2.hideOnMouseOut=true;
  window.fw_menu_3 = new Menu("root",200,16,font,fs);
  fw_menu_3.addMenuItem("Newsroom Home",path+"newsroom/index.php'");
  fw_menu_3.addMenuItem("Press Releases &amp Advisories",path+"newsroom/index.php?type=2'");
  fw_menu_3.addMenuItem("Opinion &amp; Commentary",path+"newsroom/index.php?type=15'");
  fw_menu_3.addMenuItem("Talking Points",path+"newsroom/index.php?type=14'");
  fw_menu_3.addMenuItem("Position Papers, Reports &amp; Articles",path+"newsroom/index.php?type=6'");
  fw_menu_3.addMenuItem("Interviews",path+"newsroom/index.php?type=16'");
  fw_menu_3.addMenuItem("Headlines from Africa",path+"newsroom/headlines.php'");
  fw_menu_3.addMenuItem("Presentations",path+"newsroom/index.php?type=9'");
   fw_menu_3.hideOnMouseOut=true;

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


// for handling popup menus
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  var url=selObj.options[selObj.selectedIndex].value;
  if (url.length > 0) eval(targ+".location='"+url+"'");
  if (restore) selObj.selectedIndex=0;
}

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

// returns value of specified cookie
function getCookieValue(cName)
{
	var result = null;
	var pValue;
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + cName + "=";
	var cStart = myCookie.indexOf(searchName);
	var cEnd;

	if (cStart != -1)
	{
		cStart += searchName.length;
		cEnd = myCookie.indexOf(";", cStart);
		result = unescape(myCookie.substring(cStart, cEnd));

		return result;
	}

	return "";
}

function getIntCookie(cName, defaultValue)
{
	var value = getCookieValue(cName);
	return (value.length > 0 ? parseInt(value) : defaultValue);
}

// sets value of the specified cookie
function setCookieValue(cName, value)
{
	var cookieStr = value;
	var expDate = new Date();
	var oneYear = 12 * 31 * 24 * 60 * 60 * 1000;

	expDate.setTime(expDate.getTime() + oneYear);

	cookieStr += "; expires=" + expDate.toGMTString();
	cookieStr += "; path=/";

	document.cookie = cName + "=" + cookieStr;
}



// returns top coordinate of an HTML element
function aaGetTop(htmlObj)
{	
	if (htmlObj)
	{
		if (is.ie)
			return htmlObj.style.pixelTop;
		else if (is.ns5)
			return parseInt(htmlObj.style.top);
		else if (htmlObj.element)
			return htmlObj.element.top; 
	}
	

	return 0;
}

// selects or deselects a set of checkboxes based on the current value of the "select all" checkbox
function selectAll(formname, key, numvalues, newvalue)
{
	for (i = 0; i < numvalues; ++i)
	{
		eval("document." + formname + "." + key + i + ".checked = " + (newvalue ? "true": "false"));
	}
	return false;
}

// submits the named form
function submitForm(formname)
{
	eval("document." + formname + ".submit()");
	return false;
}


// returns the path to the root folder of the web site
var hasCustomRootPath = 0;
function getRootPath()
{
	if (hasCustomRootPath)
		return customRootPath;
		
//  return "/africaaction/www/";
//  return "/africaaction/work/";
    return "/";
}

// should be called by the onLoad handler
function AfricaActionInit()
{
	// preload nav bar rollover images
	var imagepath = getRootPath() + "images/nav/";
    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');

	// setNav(rootpath);
}
