/* 
What the accordion js needs to do: 
1. wire up html so to create accordion functionality on mouseover
2. generate metrics event on 1st panel view for each panel except default panel you see on page load 
*/

/*
widget-accordion.js
$Revision: 1.2 $
(c) 1992-2006 Cisco Systems, Inc. All rights reserved.
Terms and Conditions: http://cisco.com/en/US/swassets/sw293/sitewide_important_notices.html
*/

//
// ** {{{object creation }}} **
//
// Helper function for namespace.
cdc.util.ensureNamespace('cdc.cl.c19v1');

// accordion slide controls
cdc.cl.c19v1.active = "";
cdc.cl.c19v1.sliding = 0;
cdc.cl.c19v1.oneTimers = 0;
cdc.cl.c19v1.firstHover = 0;

// slide speed controls
cdc.cl.c19v1.slideDuration = .5;   // seconds
cdc.cl.c19v1.fps = 36;                // frames per second

cdc.cl.c19v1.tf = function(){

   if (jQuery(this).find('.acc-overlay').css('display') != 'block' && cdc.cl.c19v1.sliding == 0){

      // who we're working with this round
      cdc.cl.c19v1.active = this;

      // which panel in this accordion was previously activated
      jQuery(cdc.cl.c19v1.active).siblings().each(function(){
         if (jQuery(this).attr('class').indexOf('acc-active') != -1) {
            cdc.cl.c19v1.prev = this;
            return false;
         }
      });

      // set/reset starting increase and decrease factors
      // jquery's .height() function caused blipping
      cdc.cl.c19v1.targetHeight = jQuery(cdc.cl.c19v1.prev).find('.acc-overlay').css('height');
      cdc.cl.c19v1.targetHeight = parseInt(cdc.cl.c19v1.targetHeight.replace('px', ''));
      cdc.cl.c19v1.increase = 0;
      cdc.cl.c19v1.decrease = cdc.cl.c19v1.targetHeight - 2;

      // slide speed control
      cdc.cl.c19v1.pixFactor = Math.round(cdc.cl.c19v1.targetHeight/(cdc.cl.c19v1.slideDuration*cdc.cl.c19v1.fps));

      cdc.cl.c19v1.oneTimers = 1;

      cdc.cl.c19v1.slider = function(){
         if (cdc.cl.c19v1.prev) {
            cdc.cl.c19v1.sliding = 1;
            cdc.cl.c19v1.currCrease = cdc.cl.c19v1.increase;
            cdc.cl.c19v1.currDease = cdc.cl.c19v1.decrease;
            setTimeout(function(){
               if (cdc.cl.c19v1.increase < cdc.cl.c19v1.targetHeight) {
                  if (cdc.cl.c19v1.oneTimers == 1) {
                     if (jQuery(cdc.cl.c19v1.prev).find('.acc-header').attr('class').indexOf('acc-arrow-active') != -1) {
                        jQuery(cdc.cl.c19v1.active).find('.acc-header').removeClass('acc-arrow');
                        jQuery(cdc.cl.c19v1.active).find('.acc-header').addClass('acc-arrow-active');
                        jQuery(cdc.cl.c19v1.prev).find('.acc-header').removeClass('acc-arrow-active');
                        jQuery(cdc.cl.c19v1.prev).find('.acc-header').addClass('acc-arrow');
                     }
                     jQuery(cdc.cl.c19v1.active).find('.acc-overlay').css('overflow', 'hidden');
                     jQuery(cdc.cl.c19v1.prev).find('.acc-overlay').css('overflow', 'hidden');
                     jQuery(cdc.cl.c19v1.active).addClass("acc-active");
                     jQuery(cdc.cl.c19v1.prev).removeClass("acc-active");
                     if (navigator.userAgent.indexOf('MSIE') != -1) {
                        jQuery(cdc.cl.c19v1.active).find('.acc-content').height(cdc.cl.c19v1.targetHeight - 1); // prevents ie micro-bounce
                     }
                  }
                  // slides
                  jQuery(cdc.cl.c19v1.active).find('.acc-overlay').css('height', cdc.cl.c19v1.currCrease);
                  jQuery(cdc.cl.c19v1.prev).find('.acc-overlay').css('height', cdc.cl.c19v1.currDease);
                  jQuery(cdc.cl.c19v1.active).find('.acc-overlay').css('display', 'block');
                  cdc.cl.c19v1.slider();
                  cdc.cl.c19v1.oneTimers = 0;
               } else {
                  jQuery(cdc.cl.c19v1.prev).find('.acc-overlay').css('display', 'none');
                  jQuery(cdc.cl.c19v1.active).find('.acc-overlay').height(cdc.cl.c19v1.targetHeight);
                  jQuery(cdc.cl.c19v1.active).find('.acc-overlay').css('overflow', 'auto');
                  jQuery(cdc.cl.c19v1.active).find('.acc-content').height('auto'); // reset for ie
                  cdc.cl.c19v1.sliding = 0;
                  // complete slides before updating url to avoid ie6 slide blip
                  cdc.cl.c19v1.hasher = function() {
                     setTimeout(function(){
                        if (cdc.cl.c19v1.sliding == 1) {
                           cdc.cl.c19v1.hasher();
                        } else {
                           // update the hash
                           cdc.util.setToHash('acc', jQuery(cdc.cl.c19v1.active).find('.acc-header').attr('id'));
                           // init metrics call
                           cdc.cl.c19v1.vs(jQuery(cdc.cl.c19v1.active).find('.acc-header'));
                        }
                     }, 10);
                  }
                  cdc.cl.c19v1.hasher();
               }
            }, 10);
            cdc.cl.c19v1.increase = cdc.cl.c19v1.increase + cdc.cl.c19v1.pixFactor;
            cdc.cl.c19v1.decrease = cdc.cl.c19v1.decrease - cdc.cl.c19v1.pixFactor;
         } else {
            // first run set up
            jQuery(cdc.cl.c19v1.active).find('.acc-overlay').css('display', 'block');
            jQuery(cdc.cl.c19v1.active).addClass("acc-active");
            if (jQuery(cdc.cl.c19v1.active).find('.acc-header').attr('class').indexOf('acc-arrow') != -1) {
               jQuery(cdc.cl.c19v1.active).find('.acc-header').removeClass("acc-arrow");
               jQuery(cdc.cl.c19v1.active).find('.acc-header').addClass("acc-arrow-active");
            }
            cdc.cl.c19v1.firstHover = 1;
         }
      } // end cdc.cl.c19v1.slider()

      cdc.cl.c19v1.slider();

   } else if (cdc.cl.c19v1.firstHover == 1) {
      
      // metrics call for first hovered
      cdc.cl.c19v1.vs(jQuery(cdc.cl.c19v1.active).find('.acc-header'));
      cdc.cl.c19v1.firstHover = 0;

   }

}; // end cdc.cl.c19v1.tf


cdc.cl.c19v1.init = function(){

   jQuery('.c19v1 .acc-panel').hoverIntent(cdc.cl.c19v1.tf,function(){}); //hover
   jQuery('.c19v1 .acc-header').focus(function(){
      cdc.cl.c19v1.currFocus = this;
      // delay helps the destination tab get triggered when tabbing through the accordion quickly
      setTimeout(function(){
         jQuery(cdc.cl.c19v1.currFocus).parent().each(cdc.cl.c19v1.tf);
      }, 500);
   });
   // clobber clicks on empty header links
   jQuery('.c19v1 .acc-header').click(function(){
      if (jQuery(this).attr('href') == "#" || jQuery(this).attr('href') == null) {
         return false;
      }
   });

   cdc.cl.c19v1.widgetHash = cdc.util.getFromHash();
   
   jQuery('.c19v1').each(function(){

      // look for arrow class in main div and add arrows to the current accordion's panel headers
      if (jQuery(this).attr('class').indexOf('acc-arrows-on') != -1) {
         jQuery(this).find('.acc-header').each(function(){
            jQuery(this).addClass("acc-arrow");
         });
      }

      // reset during init for multiple accordions
      cdc.cl.c19v1.active = "";

      var panelToActivate = '';
      if(jQuery(this).find('#' + cdc.cl.c19v1.widgetHash.acc).length){
         panelToActivate = jQuery(this).find('#' + cdc.cl.c19v1.widgetHash.acc).parent();
         //jQuery(this).find('.acc-active').removeClass('acc-active');
      }
      else if (jQuery(this).find('.acc-active').length){
         panelToActivate = jQuery(this).find('.acc-active');
      }
      else {
         panelToActivate = jQuery(jQuery(this).find('.acc-panel')[0]);
      }
      panelToActivate.each(cdc.cl.c19v1.tf);

   });

} // end cdc.cl.c19v1.init


//metrics setup
if (contenttype == "undefined") var contenttype="";
if (contentgroup == "undefined") var contentgroup="";
if (typeof(vs_pageview) == "undefined") var vs_pageview = function(){};

cdc.cl.c19v1.vs = function(currHeader) {
   var pagetype = "accordion";
   var action = "";
   if (contenttype == ""){
      contenttype = cdc.util.getSiteArea();
   }
   // still need the matrixd check
   if (!currHeader[0].hasBeenMetrixd){
      currHeader[0].hasBeenMetrixd = true;
      pagename =  currHeader.text();
      vs_pageview(pagename, contentgroup, pagetype, contenttype, action);
   } else {return;}
};

// launch accordion
cdc.cl.c19v1.init();


