/************************************************************************* 
   Easy Accordion version 1.2, by // William Bisbee, 6/6/2011
   Instructions for use may be found on the TNGWiki, "Easy Accordion"
   user configurable options defaults -- do not change them here, but in
   the <head> section of your document immediatly after loading this script
**************************************************************************/
var firstSectionOpenAtStart = false;
var persistentSections=false;
var onlyOneSectionOpen=false; 
var sectionOpenIcon='';
var sectionClosedIcon='';
var sectionTitleUnderline='';
var sectionTitleFontSize='';
var sectionTitleFontStyle='';
var sectionTitleOpenColor='';
var sectionTitleOpenFontWeight='';
var sectionTitleClosedColor='';
var sectionTitleClosedFontWeight='';
var pageRef="ezacc"; 

// cookie functions
function createCookie(statestring) {
// need to create cookie name from this file -- oops, this is the javascript file?
   document.cookie=pageRef + '=' + statestring;
}
function readCookie() {
	var nameEQ = pageRef + "=";
	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;
}
function eraseCookie() {  // not used.. yet
	document.cookie=pageRef + "=none; expires=3/23/2003 00:00:00;";
}
function openPersistent() {
   if (persistentSections) {
      var statestring = readCookie();
      if (statestring) {
         arrStates = statestring.split(':');
         arrStates.each(function(v, i) {
            if (v==1) 
               fastOpen('d'+i)
            else if (v==0) 
               fastClose ('d'+i);
         });
         return true;
      }
   }
   return false;
}
function saveState(hid, state) {
   if (persistentSections) {
      var i = parseInt(hid.substring(1));
      var statestring=readCookie();
      if (!statestring) statestring = saveAllStates(0);
      var arrStates=statestring.split(':');
      arrStates[i]=state;
      statestring=arrStates.join(':');
      createCookie(statestring);
   }
}
function saveAllStates(state) {
   if (persistentSections) {
      arrState=new Array();
      $$('div.ezheader').each(function(d, i) {
         arrState[i]=state;
      });
      var statestring=arrState.join(':');
      createCookie(statestring);
      return statestring;
   }
   return null;
}
function setHeaderStyles (hid, state) {
   if (state == 'open') {
      if (sectionOpenIcon.length) 
         $(hid+'im').src=sectionOpenIcon;
      $(hid).setStyle( {
         color: sectionTitleOpenColor, 
         fontWeight: sectionTitleOpenFontWeight
      } );
   }
   else {
      if (sectionClosedIcon.length)
         $(hid+'im').src=sectionClosedIcon;   
      $(hid).setStyle( {
         color: sectionTitleClosedColor, 
         fontWeight: sectionTitleClosedFontWeight
      } );
   }
}
// OPEN SECTIONS
function openSection(hid) { // with effects
var sid = hid+'x';
   setHeaderStyles(hid, 'open');
   Effect.Appear(sid, { duration: .3} );
   saveState(hid, 1);
}
function fastOpen(hid) { // no effects
   setHeaderStyles(hid, 'open');
   $(hid+'x').show();
   saveState(hid, 1);
}
function openAllSections () {
   $$('div.ezheader').each(function(d, i) {
      fastOpen('d'+i);
   });
}
// CLOSE SECTIONS
function closeSection(hid) { // with effects
var sid=hid+'x';
   setHeaderStyles(hid, 'closed');
   Element.hide($(sid));
   saveState(hid, 0);
}
function fastClose(hid) { // no effects
   setHeaderStyles(hid, 'close');
   $(hid+'x').hide();
   saveState(hid, 0);
}
function closeAllSections () {
   $$('div.ezheader').each(function(d, i) {
      fastClose('d'+i);
   });
}
function loadSections() {
var linktitle;
   $$('div.ezsection').invoke('hide');
   $$('div.ezsection').each(function(d, i) {
      d.writeAttribute('id', 'd'+i+'x');
   });
   $$('div.ezheader').each(function(d, i) {
      var linktitle=d.innerHTML;
      d.innerHTML='';
      d. setStyle({marginTop: '1em'});
      if (sectionClosedIcon.length) {
         d.insert( { top: new Element('img', {id: 'd'+i+'im', src: sectionClosedIcon, border: '0',  alt: '', title: 'expand section'}) } );
         $('d'+i+'im').setStyle ( { marginRight: '5px' } ); 
      }
      d.insert( { bottom: new Element('a', {id: 'd'+i, href: '#'}) });
      $('d'+i).insert(linktitle);
      d.setStyle ({
         fontSize: sectionTitleFontSize,
         fontStyle: sectionTitleFontStyle,   
         textDecoration:sectionTitleUnderline,          
         color: sectionTitleClosedColor, 
         fontWeight: sectionTitleClosedFontWeight
      });
      Event.observe($('d'+i), 'click', toggleSection);
   });
   if (!openPersistent()) { 
      if (firstSectionOpenAtStart) {
         fastOpen('d0');
      }
   }
} // load sections
function toggleSection() { 
var sid = this.id + 'x';
   if ($(sid).style.display=='none') { // open section
      if (onlyOneSectionOpen) 
         closeAllSections();
      openSection(this.id);
   }
   else { // close section
      if (this.id == 'd0' && onlyOneSectionOpen)
         return;
      closeSection (this.id);
      if (onlyOneSectionOpen) {
         openSection('d0');
      }
   }
   return;
}
onload=loadSections;


