// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// takes care of setting the correct styles on the top menu items.
// invoke it with the partial identifier of the item that should be
// active, as in "blog" to activate the "menu_blog" item.
function p2pSetMenu(id) {
  // start by setting all the menu items to inactive; the template doesn't do this.
  $('menurow').childElements().each(function(elt) { elt.addClassName('menuInactive'); });
  
  // Now change the class on the active one to "menuActive".  If it doesn't exist, do nothing.
  var elt = $('menu_' + id);
  if(elt)
    elt.removeClassName('menuInactive').addClassName('menuActive');
  }

// Takes care of adjusting the widths of the top-level table columns so that when there
// is no sidebar, the main content column expands to 100% width while the sidebar column
// shrinks to an explicit 0%.
function p2pAdjustMaindiv() {
  // If there's no sidebar...
  if( $('sidebar') == null ) {
    // Then walk the DOM, starting from the 'contents' div, and reset the widths of the main (only) div.
    $('contents').down('div').style.width = "100%";
  }
} 

// takes an array of anonymous objects with :id and :target properties, and sets
// an event handler for the $(id) item that performs the rollover effect on the $(target) item.
function p2pInitRolloverHandlers(items) {
  items.each(function(item) {
    $(item.id).observe('mouseover', function() {
      // First, make sure the proper tab is underlined
      $$("div.tabActive").each(function(activeItem) { activeItem.removeClassName("tabActive").addClassName("tabInactive"); });
      $(item.id).removeClassName("tabInactive").addClassName("tabActive");
      // Next, make sure the proper target is displayed:
      $$("div.targetShown").each(function(shownItem) { shownItem.removeClassName("targetShown").addClassName("targetHidden"); });
      $(item.target).removeClassName("targetHidden").addClassName("targetShown");
    });
  });
}

// takes an array of anonymous objects with :id and :target properties, and sets
// the CSS classes such that the first $(target) item is shown and the rest are hidden
function p2pInitRolloverClasses(items) {
  items.each(function(item, index) {     // Set the classes on all the targets
    if(index == 0) {
      $(item.id).addClassName("tabActive");        // First item is activated by default,
      $(item.target).addClassName("targetShown");
    } else {
      $(item.id).addClassName("tabInactive");      // while the rest are unactivated.
      $(item.target).addClassName("targetHidden"); 
    }
  });
}

function p2pInitFictionRollovers() {
  var items = $A([
    { id: "overview",    target: "overviewTarget" },
    { id: "copy",        target: "copyTarget"},
    { id: "sentence",    target: "sentenceTarget"},
    { id: "paragraph",   target: "paragraphTarget"},
    { id: "writing",     target: "writingTarget"},
    { id: "story",       target: "storyTarget"},
    { id: "character",   target: "characterTarget"},
    { id: "readthrough", target: "readthroughTarget"}
  ]);
  p2pInitRolloverHandlers( items ); // Set the event handlers
  p2pInitRolloverClasses( items );  // Set the Hidden / Shown classes for them.
}

function p2pInitNonFictionRollovers() {
  var items = $A([
    { id: "overview",     target: "overviewTarget" },
    { id: "copy",         target: "copyTarget"},
    { id: "sentence",     target: "sentenceTarget"},
    { id: "paragraph",    target: "paragraphTarget"},
    { id: "writing",      target: "writingTarget"},
    { id: "organization", target: "organizationTarget"},
    { id: "research",     target: "researchTarget"},
    { id: "readthrough",  target: "readthroughTarget"}
  ]);
  p2pInitRolloverHandlers( items ); // Set the event handlers
  p2pInitRolloverClasses( items );  // Set the Hidden / Shown classes for them.
}

function p2pInitTechnicalRollovers() {
  var items = $A([
    { id: "overview",     target: "overviewTarget" },
    { id: "copy",         target: "copyTarget"},
    { id: "sentence",     target: "sentenceTarget"},
    { id: "paragraph",    target: "paragraphTarget"},
    { id: "writing",      target: "writingTarget"},
    { id: "organization", target: "organizationTarget"},
    { id: "argument",     target: "argumentTarget"},
    { id: "readthrough",  target: "readthroughTarget"}
  ]);
  p2pInitRolloverHandlers( items ); // Set the event handlers
  p2pInitRolloverClasses( items );  // Set the Hidden / Shown classes for them.
}

function p2pInitAboutRollovers() {
  var items = $A([
    { id: "overview",   target: "overviewTarget" },
    { id: "fiction",    target: "fictionTarget" },
    { id: "nonfiction", target: "nonfictionTarget" },
    { id: "technical",  target: "technicalTarget" }
  ]);
  p2pInitRolloverHandlers( items );
  p2pInitRolloverClasses( items );
}

function p2pInitServicesRollovers() {
  var items = $A([
    { id: "overview",    target: "overviewTarget" },
    { id: "readthrough", target: "readthroughTarget" },
    { id: "copyedit",    target: "copyeditTarget" },
    { id: "lineedit",    target: "lineeditTarget" },
//    { id: "p2p",         target: "p2pTarget" },
    { id: "guarantees",  target: "guaranteesTarget" }
  ]);
  p2pInitRolloverHandlers( items );
  p2pInitRolloverClasses( items );
}