/////////////////////////////////////////////////////////
// START OF CHRIS ESLER FUNCTIONS
/////////////////////////////////////////////////////////

// QUICK TOGGLE FUNCTION
// objClass is the target flag className
function toggleDisplay(objClass)
{
  $S('.'+objClass).each(function(el)
  {
    if (el.className.indexOf("Hide") != -1) 
      el.className = el.className.replace("Hide","Show");
    else 
      el.className = el.className.replace("Show","Hide");
   });		
}

// SETUP KEYPRESS CONTROL VARS
var control = false;
var nextKey = false;



// DETERMINE WHICH KEYPRESS FUNCTIONS TO  
// USE BY BROWSER TYPE

if (!/MSIE (5\.5|6|7\.)/.test(navigator.userAgent)) 
{ 
  // MOZILLA-STYLE PROPAGATE DOWN EVENT ARCHITECTURE
  // Much better than IE's. Actually captures key combo's
  // and doesn't affect other key combo's
  function detectspecialkeys(e)
  {
    var evtobj= e
    var keyCode = e.which;
    if (evtobj.ctrlKey)
    {
      toggleAccordion(keyCode);
    }
  }
  document.onkeypress=detectspecialkeys;
}
else
{
  // MICROSOFT-STYLE BUBBLE UP EVENT ARCHITECTURE
  // retarded IE requires this funky capture system
  // checks if control key pressed, then marks true
  // then captures next key, then marks it true
  // if control key true and next key true, then it does
  // conditional statement
  document.onkeydown = function () 
  {
    var keyCode=event.keyCode
    // if its control key, mark it true 
    // and reset nextKey
    if(keyCode == 17)
    { 
      control = true;
      nextKey = false;
      // if control is true, then flag nextKey
    }
    else
    {
      nextKey = true;
    }
    // if both control and next key, then do our conditional statements
    if (control && nextKey) 
    {
      if (keyCode == 49 || keyCode == 50 || keyCode == 51 || keyCode == 52 || keyCode == 53 || keyCode == 54 || keyCode == 55 || keyCode == 56)
      {
	// alert ('control key pressed + '+keyCode);
	toggleAccordion(keyCode);
      }
      // reset control vars
      control = false;
      nextKey = false;
      // this is here last, so it nullifies IE beviours for these buttons
      // otherwise we don't want it to nullify other bbuttons such as Control-P (for print), etc
      if (keyCode == 49 || keyCode == 50 || keyCode == 51 || keyCode == 52 || keyCode == 53 || keyCode == 54 || keyCode == 55 || keyCode == 56)
      {
	return false;
      }
    } 
    else 
    {
      return true;
    }
  }
}

// SETUP ACCORDION ASSOCIATIVE ARRAY
// maps key presses to the accordion toggle
// 49 = number 1 key, 50 = number 2 key, and so on
var accArray = [0, 1, 2, 3, 4, 5, 6, 7]; 
var keyArray = [49, 50, 51, 52, 53, 54, 55, 56]; 
var newAccArray = accArray.associate(keyArray);
var currentArrow = 0;


// FUNCTION TO TOGGLE ACCORDION BASED ON KEYPRESS COMBO
// control key + number 1 key opens accordion[0]
function toggleAccordion(obj) 
{
  // check if the 1-5 keys are pressed in conjunction with the control key
  if (obj == 49 || obj == 50 || obj == 51 || obj == 52 || obj == 53 || obj == 54 || obj == 55 || obj == 56) 
  {
    // get the associated accordion to the key pressed
    var newAccObj = newAccArray[obj];
    // grab our accordion object
    var newArrow = $('arrow'+newAccArray[obj]);
    // set the new current Arrow
    currentArrow = newAccArray[obj];
    // toggle the accordion
    // myAccordion.showThisHideOpen(newAccArray[obj]);
    myAccordion.showThisHideOpen(newAccArray[obj]);
  }
}

// FUNCTIONS TO TOGGLE THE PILL IMAGE AND SET CURRENT TOGGLE
// change pill to ON
function toggleAccordionImageShow(el) 
{
  // grab our pill image
  var obj = el.getLast().getFirst();
  // get our arrow id / accordion id
  var newArrowId = obj.id.replace(/arrow/g,'');
  // set current Arrow
  currentArrow = newArrowId;
  // set the new source
  if (!obj.src.test('Lite'))
  {
    obj.setProperty('src',obj.src.replace(/.gif/g,'Lite.gif'));
  }
}

// change pill to off
function toggleAccordionImageHide(el) 
{
  // grab our pill image
  var obj = el.getLast().getFirst();
  // set the new source
  obj.setProperty('src',obj.src.replace(/Lite/g,''));
}

// ACCORDION VARS SETUP
var myAccordion;
var myStretch;
var myStretcher;

// WINDOW ONLOAD STUFF - onDomReady from Moottools
Window.onDomReady(function() 
{
  // get accordion elements
  myStretch = document.getElementsByClassName('toggler');
  myStretcher = document.getElementsByClassName('accordion');
  // setup the accordion elements by clearing display styles	
  myStretcher.each(function(el)
  {
    el.style.display = '';
  });
  // Create the accordion
  myAccordion = new fx.Accordion(myStretch, myStretcher, 
  {
    fixedWidth: 125,
    // fixedHeight: 125,
    opacity : true,
    // openClose : true,
    openClose : false,
    onActive : function(el){toggleAccordionImageShow(el)},
    onBackground : function(el){toggleAccordionImageHide(el)},
    // itemsOpen : [0,1,2],
    itemsOpen: [6],
    // start : 'first-open'
    start : 'none-open'
  });
  // SETUP THE TOOLTIPS
  // tooltip array
  var as = [];
  // grab all images that are supposed to have a tooltip
  $S('img.hasTooltip').each(function(a){ if (a.getAttribute('title')) as.push(a); });
  // create tooltip instance
  new Tips(as, {maxOpacity: 0.9, maxTitleChars: 300});	
});
