/* This class deals with the hiding/revealing of content on the site.
  To add a reveal/hide section add HTML like this to the page:

  <div class="revealWrap">
    <a href="javascript:;" class="revealToggler">Link text</a>
    <div class="revealContent">
      <p>your content here</p>
    </div>
  </div>
*/
var Reveal = {
  Classes: {
    wrap:     'revealWrap',
    link:     'revealToggler',
    content:  'revealContent'
  }
};
Reveal.init = function(showFirst) {
	if (showFirst!==false) {
		showFirst=true;
	}
	// check if one of our targets is in the URL
	curURI = window.location.href;
	if (curURI.indexOf("#")) {       
		element = curURI.substring(curURI.indexOf('#')+1);
	}
	initialElementToShow=false;
	if (document.getElementById(element) && (document.getElementById(element).parentNode.className.indexOf(Reveal.Classes.wrap)>-1)) {
		el1 = document.getElementById(element);
		initialElementToShow=el1;
		showFirst=false;
	}	

  togglerLinks = $$("."+Reveal.Classes.link); // get all the links
  len=togglerLinks.length;
  for (var i=0; i<len; i++) {
    $(togglerLinks[i]).observe('click',Reveal.revealClicked); // add click event
    Reveal.hideFromLink(togglerLinks[i]); // only hide the sections that have links associated with them
		if (i==0 && showFirst==true){
			Reveal.showFromLink(togglerLinks[i])
		}
  }
	if (initialElementToShow!=false) {
		Reveal.showFromLink(initialElementToShow);
	}
}

Reveal.getContentDivFromLink = function(linkEl) { //return the content element associated with the passed link
  return $(linkEl.parentNode).getElementsBySelector("div."+Reveal.Classes.content)[0];
}

Reveal.show = function(el) {
  //check if el exists
  if ($(el)!==null) {
    $(el).show();
  }
}
Reveal.hide = function(el) {
  //check if el exists
  if ($(el)!==null) {
    $(el).hide();
  }
}
Reveal.hideByClassName = function(cName) {
  elements = $$("."+cName);
  len = elements.length;
  for (var i=0;i<len;i++){
    Reveal.hide($(elements[i]));
  }
}
Reveal.showFromLink = function(linkEl) {
  contentEl = Reveal.getContentDivFromLink(linkEl);
  contentEl.show();
  $(linkEl).addClassName('active');
}
Reveal.hideFromLink = function(linkEl) {
  contentEl = Reveal.getContentDivFromLink(linkEl);
  if (contentEl) {
    contentEl.hide();
    $(linkEl).removeClassName('active');
  }
}
Reveal.isHidden = function(linkEl) {
  contentEl = Reveal.getContentDivFromLink(linkEl);
  if (contentEl && $(contentEl).getStyle('display') == 'none') {
    return true;
  }
  return false;
}
Reveal.toggle = function(el) {
  if (el && $(el).getStyle('display') == 'none') {
    $(el).show();
    return;
  }
  $(el).hide();
}
Reveal.revealClicked = function(e) {
	if (!e) { var e = window.event; }
	if (e.target) { targ = e.target; }
	else if (e.srcElement) { targ = e.srcElement; }
	if (targ.nodeType == 3) { targ = targ.parentNode; } // defeat Safari bug
  if (Reveal.isHidden($(targ))) {
    Reveal.showFromLink($(targ));
    return;
  }
  Reveal.hideFromLink($(targ));
  //event
}

Reveal.toggleManual = function (callingEl, toggleId) {
  //set callingEl active state.
  //toggle the element
  if ($(toggleId).getStyle('display') == 'none') {
    $(toggleId).show();
    $(callingEl).addClassName('active');
  }
  else {
    $(toggleId).hide();
    $(callingEl).removeClassName('active');
  }
}

addLoadListener(Reveal.init);
