/**
 * @note Changes the height of all adjacent containers based on the tallest one
 */

/*function equalHeights(section) {
  tallboy = 0;
  section.each(function() {
    thisHeight = $(this).height();
    if(thisHeight > tallboy) {
      tallboy = thisHeight;
    }
  });
  section.height(tallboy);
}*/

/**
 * @note Generate our pageTracker URL string based on the supplied target
 */
function getTrackerURL(target) {
  var fullURL    = document.location.toString();
  var URLParts   = fullURL.split('/');
  var trackerURL = '/'+URLParts[3]+'/'+target;
  return trackerURL;
}

$(document).ready(function(){
  
  // set the pricing container boxes to equal heights
  $('#pricing-boxes .box').equalHeights();
  // equalHeights($("#pricing-boxes .box"));
  
  // set the tour section container boxes to equal heights
  $('#tour-sections .sub-section').equalHeights();
  // equalHeights($("#tour-sections .sub-section"));
  
  // equal heights: integration sections
  $('body#integration #article .halfscreen').equalHeights('66.833333333333333em');
  // equalHeights($("body#integration #article .halfscreen"));
  
  // wrap our top section with the parallax style
  $('#top').wrapInner('<div class="parallax"></div>');
  
  // wrap our content section with the parallax style
  /*$('#content').wrapInner('<div class="parallax-content"></div>');*/
  
  // wrap our bottom section with the parallax style
  $('#bottom').wrapInner('<div class="parallax-footer"></div>');
  
/*  Slide-down login box is disabled
  
  // set up the login form CSS w/ jQuery to override the defaults set in /css/master.marketing.css
  $('#heads-up #login').css({
    'background-color'              : 'rgba(255, 255, 255, 0.75)',
    'border'                        : '0.083333333em solid #A8A1B8',
    'border-radius'                 : '12px',
    '-moz-border-radius'            : '12px',
    '-webkit-border-radius'         : '12px',
    'border-radius-top-right'       : '0',
    'border-radius-top-left'        : '0',
    '-moz-border-radius-topright'   : '0',
    '-moz-border-radius-topleft'    : '0',
    '-webkit-border-top-right'      : '0',
    '-webkit-border-top-left'       : '0',
    'box-shadow'                    : '2px 2px 3px rgba(0, 0, 0, 0.24)',
    '-moz-box-shadow'               : '2px 2px 3px rgba(0, 0, 0, 0.24)',
    '-webkit-box-shadow'            : '2px 2px 3px rgba(0, 0, 0, 0.24)',
    'float'                         : 'none !important',
    'opacity'                       : 1,
    'padding'                       : '0.75em 1em 0.75em 1em',
    'position'                      : 'absolute',
    'top'                           : '-6.5em',
    'width'                         : 'auto',
    'z-index'                       : '100'
  });
  
  // append the login toggle button
  $('#heads-up #signup-top').append(' <strong><a class="button" href="#login" id="toggle-login">Login</a></strong>');
  
  // kind of a kludgey stuff here, but it works OK :-/
  var $orig_label = $('#heads-up #signup-top #toggle-login').text();//original text label for the button (Login)
  
  // show/hide the login form
  $('#heads-up #signup-top #toggle-login').toggle(
    function() {
      $(this).addClass('subdued');// add the subdued class to the link for that classy look
      $(this).text('Cancel');// change the text of the link
      // animate the form
      $('#heads-up #login').animate({
        top       : '-1.5em',
        opacity   : 1
      }, 'fast', 'easeInCirc');
     },
    function() {
      $(this).removeClass('subdued');// remove our class
      $(this).text($orig_label);// return to our previous program, already in progress
      // and hide the form
      $('#heads-up #login').animate({
        opacity   : 1,
        top       : '-6.5em'
      }, 1000, 'easeOutCirc');
  });
  
*/
  
  /**
   * show/hide the tour/features sections
   */
  // first we need to hide all the sections, excluding the first
  $('#tour-sections .sub-section:not(:first-child)').each(function() {
    $(this).hide();
  });
  
  // global animation speed for scrolling the page
  $animSpeedScroll = 1200;
  
  // and add the 'selected' class to our first link
  $('#overview ul.overview-list:first li:first-child a').addClass('selected');
  
  var selected         = $('#overview ul.overview-list:first li:first-child a').attr('id');// currently selected tab
  var sectionCount     = $('#tour-sections .sub-section').size();// number of tour sections on this page
  var sectionIDs       = new Array();
  var linkIDs          = new Array();
  var sectionsAndLinks = new Array();// empty array to hold our link and section IDs
  
  var i = 0;// tmeporary counter
  
  // grab the ID of each tour section
  $('#tour-sections .sub-section').each(function() {
    sectionIDs[i] = $(this).attr('id');
    i++;
  });
  
  i = 0;// reset the counter
  
  // grab the ID of each link
  $('#overview ul.overview-list li a').each(function() {
    linkIDs[i] = $(this).attr('id');
    i++;
  });
  
  // supply our sectionsAndLinks multi-dimensional array with the contents of both the sectionIDs and the linkIDs arrays
  sectionsAndLinks[0] = sectionIDs;
  sectionsAndLinks[1] = linkIDs;
  
  /* now sectionsAndLinks have matching key/value pairs for sections and links: 
     sectionsAndLinks[0][0] is the section ID and sectionsAndLinks[1][0] is the link ID */
  // alert('Section ID '+ sectionsAndLinks[0][0]+' is linked with '+sectionsAndLinks[1][0]);
  
  // now set up the toggles on each link for their corresponding sections
  $('#overview ul.overview-list li a').each(function() {
    // onClick
    $(this).click(function() {
      var thisURL   = $(this).attr('href').split('#');// split out the URL into an array
      var target    = thisURL[1];// our target is the last array member
      var shown     = $('#tour-sections .sub-section:visible').attr('id');// our target is the last array member
      
      var trackerURL = getTrackerURL(target);// generate our pageTracker._trackPageview() URL
      pageTracker._trackPageview(trackerURL);// track the link
      
      // remove the 'selected' class from the old link and apply it to our new link
      $('#'+selected).removeClass('selected');
      $(this).addClass('selected');
      selected = $(this).attr('id');
      
      // toggle the content
      if($('#'+target).is(':hidden')) {
        $('#'+shown).hide();
        $('#'+target).fadeIn('slow');
      }
      
      /* I turned off the scrolling page because it was causing a flash (not to mention it was annoying) -- WVD */
      /*var offset = $('#tour-sections').offset();// what's our offset from the top of the #tour-sections container
  		$('html').animate({scrollTop:offset.top}, $animSpeedScroll, 'easeOutCirc');// scroll the page to the top of the #tour-sections section*/
      return false;// kill the link targets
    });// end this.click
  });// end nav.each
  
  // now set up the "Next ->" links for their corresponding sections
  $('#tour-sections .sub-section a.next').each(function() {
    // onClick
    $(this).click(function() {
      var thisURL           = $(this).attr('href').split('#');// split out the URL into an array
      var target            = thisURL[1];// our target is the last array member
      
      var sectionIndex      = sectionsAndLinks[0].indexOf(target); // the index number of the array member
      var link              = sectionsAndLinks[1][sectionIndex]; // the value of link we want to activate
      
      var trackerURL = getTrackerURL(target);// generate our pageTracker._trackPageview() URL
      pageTracker._trackPageview(trackerURL);// track the link
      
      $('#'+link).click(); // activate the link to move the story forward
      
      return false;// kill the link targets
    });// end this.click
  });// end nav.each
});