var pics=new Array(); 
var placeholder = 0;
pics[0]="images/internet/cherry-large.jpg";
pics[1]="images/internet/DOE-large.jpg";   
pics[2]="images/internet/genetics-large.jpg";
pics[3]="images/internet/habitat-large.jpg";
pics[4]="images/internet/imageimpact-large.jpg";   
pics[5]="images/internet/innova-large.jpg";
pics[6]="images/internet/veterans-large.jpg";	

function hidediv( id ) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function accordionVisible() {
   var allAccordions = Core.getElementsByClass("accordion");
   var myAccordion = allAccordions[0];
   if ( myAccordion.style.display == "none" )
      return false;
   else
      return true;	  
}

function hideAccordion() {
   var allAccordions = Core.getElementsByClass("accordion");
   var myAccordion = allAccordions[0];
   myAccordion.style.display = "none";
}

function rotate( direction ) {   
   placeholder += direction;
      
   if ( placeholder < 0 )
      placeholder = pics.length - 1;
   else if ( placeholder > pics.length - 1 )
      placeholder = 0;
   
   
   if ( accordionVisible() ) {
      hideAccordion();
	  placeholder = 0;
   }
   
   myCycle = document.getElementById("cycle");
   myCycle.src = pics[placeholder];
   	  
   var myPicture =  document.getElementById("picture");
   myPicture.style.display = "block";
   return false;
}

function restore() { 
   var allAccordions = Core.getElementsByClass("accordion");
   var myAccordion = allAccordions[0];
   var myPicture =  document.getElementById("picture");
   
   myPic = document.getElementById("cycle");
   myPic.src = "images/spacer.gif";   
   
   myPicture.style.display = "none";
   myAccordion.style.display = "block";
   
   return false;
}

function hideAccordion() {
   var allAccordions = Core.getElementsByClass("accordion");
   var myAccordion = allAccordions[0];
   myAccordion.style.display = "none";
}


function cycle_image( image ) { 
   hideAccordion();
   placeholder += image;
   myPic = document.getElementById("cycle");
   myPic.src = pics[image];   
   
   elem = document.getElementById("picture");
   elem.style.display = "block";



   return false;
}


var Accordion =
{
  init: function()
  {
    Accordion.frameRate = 30;
    Accordion.duration = 0.5;
    
    var accordions = Core.getElementsByClass("accordion");

    for (var i = 0; i < accordions.length; i++)
    {
      var folds = accordions[i].childNodes;
      for (var j = 0; j < folds.length; j++)
      {
        if (folds[j].nodeType == 1)
        {
          var accordionContent = document.createElement("div");
          accordionContent.className = "accordionContent";
          
          for (var k = 0; k < folds[j].childNodes.length; k++)
          {
            if (folds[j].childNodes[k].nodeName.toLowerCase() != "h2")
            {
              accordionContent.appendChild(folds[j].childNodes[k]);
              k--;
            }
          }
            
          folds[j].appendChild(accordionContent);
          folds[j]._accordionContent = accordionContent;
          
		  var getHref = folds[j].childNodes[0].childNodes[0].getAttribute( "href" );
		  
		  if ( getHref != "#range_of_services" && getHref != "http://www.jennifergonzalez.com/rgmgraphics/html/internet.html#range_of_services") 		  
		     Accordion.collapse(folds[j]);
			 
          var foldLinks = folds[j].getElementsByTagName("a");
          var foldTitleLink = foldLinks[0];
          Core.addEventListener(foldTitleLink, "click", Accordion.clickListener);
          
          for (var k = 1; k < foldLinks.length; k++)
          {
            Core.addEventListener(foldLinks[k], "focus", Accordion.focusListener);
          }
        }
      }
      
      if (location.hash.length > 1)
      {
        var activeFold = document.getElementById(location.hash.substring(1));
        if (activeFold && activeFold.parentNode == accordions[i])
        {
          Accordion.expand(activeFold);
        }
      }
    }
  },

  collapse: function(fold)
  {
    var content = fold._accordionContent;
    content._height = parseInt(content.style.height, 10);
    content._increment = content._height / (Accordion.frameRate * Accordion.duration);
    
    if (Core.hasClass(fold, "expanded"))
    {
      clearTimeout(content._timer);
      Accordion.collapseAnimate(content);
    }
    else
    {
      Core.addClass(fold, "collapsed");
    }
  },
  
  collapseAnimate: function(content)
  {
    var newHeight = content._height - content._increment;
    
    if (newHeight < 0)
    {
      newHeight = 0;
      Core.removeClass(content.parentNode, "expanded");
      Core.addClass(content.parentNode, "collapsed");
    }
    else
    {    
      content._timer = setTimeout(function()
        {
          Accordion.collapseAnimate(content);
        }, 1000 / Accordion.frameRate);
    }
    
    content._height = newHeight;
    content.style.height = Math.round(newHeight) + "px";
  },

  collapseAll: function(accordion)
  {
    var folds = accordion.childNodes;
    for (var i = 0; i < folds.length; i++)
    {
      if (folds[i].nodeType == 1)
      {
        Accordion.collapse(folds[i]);
      }
    }
  },

  expand: function(fold)
  {
    var content = fold._accordionContent;
    // picture.style.display = "none";
    Accordion.collapseAll(fold.parentNode);
    
    if (!Core.hasClass(fold, "expanded"))
    {
      content.style.height = "0";
      content._height = 0;
      Core.removeClass(fold, "collapsed");
      Core.addClass(fold, "expanded");
      content._increment = content.scrollHeight / (Accordion.frameRate * Accordion.duration);
      Accordion.expandAnimate(content);
    }
  },
  
  expandAnimate: function(content)
  {
    var newHeight = content._height + content._increment;
    
    if (newHeight > content.scrollHeight)
    {
      newHeight = content.scrollHeight;
    }
    else
    {
      content._timer = setTimeout(function()
          {
            Accordion.expandAnimate(content);
          }, 1000 / Accordion.frameRate);
    }
    
    content._height = newHeight;
    content.style.height = Math.round(newHeight) + "px";
    content.scrollTop = 0;
  },
  
  clickListener: function(event)
  {
    var fold = this.parentNode.parentNode;
    if (Core.hasClass(fold, "collapsed"))
    {
      Accordion.expand(fold);
    }
    else
    {
      Accordion.collapse(fold);
    }  
    Core.preventDefault(event);
  },
  
  focusListener: function(event)
  {
    var element = this;
    while (element.parentNode)
    {
      if (element.parentNode.className == "accordion")
      {
        Accordion.expand(element);
        return;
      }
      element = element.parentNode;
    }
  }
};

Core.start(Accordion);

