var dom = (document.getElementById) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;
var mac = (navigator.userAgent.indexOf("Mac") != -1) ? true : false;

function getSection() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split(",");
	//alert(pairs);
	for(var i=0; i<pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	if (args.section==null) args.section='welcome';
	return args.section;
}

function getName() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split(",");
	//alert(pairs);
	for(var i=0; i<pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	//alert(args.section);
	//alert(value);
	//if (value==null) value='welcome';
	return args.name;
}
/*function listPropertyNames(obj) {
	var names = "";
	for(var i in obj) names += i + "\n";
	alert (names);
}*/

function msieversion()
// Return Microsoft Internet Explorer (major) version number, or 0 for others.
// This function works by finding the "MSIE " string and extracting the version number
// following the space, up to the semicolon
{
    var ua = window.navigator.userAgent
    var msie = ua.indexOf ( "MSIE " )
    
    if ( msie > 0 )        // is Microsoft Internet Explorer; return version number
        return parseFloat ( ua.substring ( msie+5, ua.indexOf ( ";", msie ) ) )
    else
        return 0    // is other browser
}

// resize fix for ns4
var origWidth, origHeight;
if (document.layers && !document.getElementById) {
	origWidth = window.innerWidth; origHeight = window.innerHeight;
	window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }
}

// create array of sources for images to preload
var fadeimages=new Array()
fadeimages[0]="images/patch.jpg"
fadeimages[1]="images/crew.jpg"

// create array of preloaded images
var preloadedimages=new Array()
for (p=0;p<fadeimages.length;p++)
{
  preloadedimages[p]=new Image()
  preloadedimages[p].src=fadeimages[p]
}

//set opacity of initial image for mozilla browsers
function setOpacity(id) { 
  if ((navigator.userAgent.indexOf("Gecko")>-1) && (document.getElementById)) {
    document.getElementById(id).style.setProperty('-moz-opacity','0','');
  }
}

function changeImage(id,src) { //switch to second image
   id="fadeImage";
   if (document.getElementById && !document.layers) {
	document.getElementById(id).src=src
  }
}

var counter=0;

// fade out intial image and fade in second image
function fade(vector)
{
  var curOpacity;
  //set id = image id to fade
  id="fadeImage"; 
  //switch to second image for second fade in
  if (vector=="in" && counter==1) changeImage(id,preloadedimages[1].src)
  
  if (msieversion() >= 5.5) 
  //browser supports filters (ie 5.5 or greater)
  {
	// set curOpacity to current opacity of image to fade
	curOpacity = document.getElementById(id).filters.item("DXImageTransform.Microsoft.Alpha").opacity;
	if (vector=="out") {
	  // reduce opacity by 10%
	  document.getElementById(id).filters.item("DXImageTransform.Microsoft.Alpha").opacity -= 10;			
	} else {
	  // increase opacity by 10%
	  document.getElementById(id).filters.item("DXImageTransform.Microsoft.Alpha").opacity += 10;			
	}		
	// reset current opacity to new level
	curOpacity = document.getElementById(id).filters.item("DXImageTransform.Microsoft.Alpha").opacity;
  } else if ((navigator.userAgent.indexOf("Gecko")>-1) && document.getElementById) { 
    // some variant of mozilla
	// set curOpacity to current opacity of image to fade
	curOpacity = document.getElementById(id).style.getPropertyValue('-moz-opacity') * 100;
	if (vector=="out") {
	  // reduce opacity by 10%
	  document.getElementById(id).style.setProperty('-moz-opacity', (curOpacity - 5) / 100, '');
	} else {
	  // increase opacity by 10%
	  document.getElementById(id).style.setProperty('-moz-opacity', (curOpacity + 5) / 100, '');
	}
	// reset current opacity to new level
	curOpacity = document.getElementById(id).style.getPropertyValue('-moz-opacity') * 100;
  }
  
  if (vector=="out") {
	if (curOpacity > 0) {
	  //repeat fade interval after .1 second
	  setTimeout('fade("out")',100);
	} else {
	  //stop fading out when opacity = zero
	  clearTimeout('fade("out")');
	  fade('in');
	}
  } else {
	if (curOpacity < 100) {
	  //repeat fade interval after .1 second
	  setTimeout('fade("in")',100);
	} else {
	  //stop fading in when opacity = 100%
	  clearTimeout('fade("in")');
      counter++; // count fade-ins to know when to switch images
	}			  
  }
}

// create array and populate with navigation button IDs
var btnArray = new Array('welcome_button','news_button','meetings_button','projects_button','events_button','essays_button','members_button','resources_button','gallery_button','community_button');
// associate names with IDs
btnArray["welcome_button"]="welcome_button";
btnArray["news_button"]="news_button";
btnArray["meetings_button"]="meetings_button";
btnArray["projects_button"]="projects_button";
btnArray["events_button"]="events_button";
btnArray["essays_button"]="essays_button";
btnArray["members_button"]="members_button";
btnArray["resources_button"]="resources_button";
btnArray["gallery_button"]="gallery_button";
btnArray["community_button"]="community_button";

// loop through buttons in array
function setButtons(activeBtn)
{
  var i=0;
  do {    
	  setButton(btnArray[i],activeBtn);	
	} while (++i < btnArray.length);
}

// highlight or unhighlight each button when one is clicked
function setButton(btnId,activeBtn) 
{  
  
  var btnObj= null;
  var bannerObj= null;
  if (document.getElementById(btnId)!=null)
  { 
    btnObj=document.getElementById(btnId);
    bannerObj=document.getElementById('logo_banner');	
  } else {
    btnObj=parent.document.getElementById(btnId);
    bannerObj=parent.document.getElementById('logo_banner');	
  }
  
  var thisBtnTextColor= btnObj.style.color;
  if (thisBtnTextColor=='#000000' || thisBtnTextColor=='rgb(0, 0, 0)' || thisBtnTextColor=='rgb(0,0,0)') thisBtnTextColor = 'black'
  if (thisBtnTextColor == 'black' && btnId==activeBtn) // highlight active button
  {
   	btnObj.style.backgroundColor="#993300"; // make background dk brown
	btnObj.style.color="#FFCC33"; // make text gold
	btnObj.style.borderTopColor="#FFCC33"; // make border top gold
	btnObj.style.borderBottomColor="#FFCC33"; // make border bottom gold
	btnObj.style.cursor="default"; // make it not look clickable
  } 
  else if (thisBtnTextColor != 'black' && btnId!=activeBtn) // remove highlight from formerly active button
  {	
    btnObj.style.backgroundColor="#FFCC33";  // make background gold
	btnObj.style.color="#000000"; // make text black
	btnObj.style.borderTopColor="#000000"; // make border top black
	btnObj.style.borderBottomColor="#000000"; // make border bottom black 
    if (navigator.userAgent.indexOf("Gecko")>-1) {	
	  btnObj.style.cursor="pointer"; // make it look clickable
	} else {	
  	  btnObj.style.cursor="hand"; // make it look clickable
	}	
  }
  if (activeBtn=="welcome_button")
  {
    bannerObj.style.cursor="default";
  } else {
    if (navigator.userAgent.indexOf("Gecko")>-1) {	
	  bannerObj.style.cursor="pointer"; // make it look clickable
	} else {	
  	  bannerObj.style.cursor="hand"; // make it look clickable
	}	
  }  
}

function setContent(sectionId,name)
{
  //alert(sectionId,name);
  var activeBtn =  sectionId + '_button';
  var contentSrc = '';
  var highlightsSrc = '';
  var currentContentSrc = parent.document.getElementById('content_frame').src; ;
  var currentHighlightsSrc = parent.document.getElementById('highlights_frame').src;
  
  if (name != '') name= '#' + name;
 
  contentSrc = sectionId + '/' + sectionId + '_content.php' + name;
  highlightsSrc = sectionId + '/' + sectionId + '_highlights.php';
   
   if (currentContentSrc.indexOf(contentSrc)== -1) // if content needs changed
  {
    if (mac && ie5)	{
      parent.document.getElementById('content_frame').src= contentSrc; //change content
    } else {
      document.getElementById('content_frame').src= contentSrc; //change content
	}
  }
  
  if (currentHighlightsSrc.indexOf(highlightsSrc)== -1) // if highlights need changed
 {
    if (mac && ie5)	{
      parent.document.getElementById('highlights_frame').src= highlightsSrc; //change content
    } else {
      document.getElementById('highlights_frame').src= highlightsSrc; //change content
	}
  }
    
  // check to see if highlight button needs to be switched
  var btnTextColor= document.getElementById(activeBtn).style.color; 
  if (btnTextColor=='#000000' || btnTextColor=='rgb(0, 0, 0)' || btnTextColor=='rgb(0,0,0)') {
	setButtons(activeBtn);
  }
}

function setContentFromButtons(sectionId,name)
{
  var activeBtn =  sectionId + '_button';
  var contentSrc = '';
  var highlightsSrc = '';
  var currentContentSrc = parent.document.getElementById('content_frame').src; ;
  var currentHighlightsSrc = parent.document.getElementById('highlights_frame').src;
  
  if (name != '') name= '#' + name;
 
  if (mac && ie5)	{
    contentSrc = sectionId + '/' + sectionId + '_content.php' + name;
    highlightsSrc = sectionId + '/' + sectionId + '_highlights.php';
  } else {
    contentSrc = sectionId + '/' + sectionId + '_content.php' + name;
    highlightsSrc = sectionId + '/' + sectionId + '_highlights.php';
  }    
   
   if (currentContentSrc.indexOf(contentSrc)== -1) // if content needs changed
      parent.document.getElementById('content_frame').src= contentSrc; //change content
  
  if (currentHighlightsSrc.indexOf(highlightsSrc)== -1) // if highlights need changed
     parent.document.getElementById('highlights_frame').src= highlightsSrc; //change content
  
  // check to see if highlight button needs to be switched
  var btnTextColor= parent.document.getElementById(activeBtn).style.color; 
  if (btnTextColor=='#000000' || btnTextColor=='rgb(0, 0, 0)' || btnTextColor=='rgb(0,0,0)') setButtons(activeBtn)
}