/**
 * Function to map trigger elements with target elements for hover effect.
 * @param hover          - jQuery object - hover data for hoverIntent
 * @param triggers       - jQuery object - selection of trigger elements which to bind the mouseover event
 * @param target_content - jQuery object - selection of content elements to display upon the mouseover event, respective of the trigger elements
 * @param first_active   - boolean       - if true, initially show the first target element
 * @returns hover        - jQuery object - with the triggers selection added to any pre-existing selection
 */
function map_selection(hover, triggers, target_content, first_active) {
  hover.selection = hover.selection.add(triggers);
  target_content.hide();
  if (first_active) {
    triggers.filter(':first').addClass('selected');
    target_content.filter(':first').show();
  }
  jQuery(triggers).each(function(index) {
    jQuery(this).data('rollover_content', target_content.filter(':eq(' + index +')'));
    jQuery(this).data('trigger_set', triggers);
    jQuery(this).data('target_set', target_content);
  });
  return hover;
}

/**
 * Event handler for mouseover event.  Used for hoverIntent.
 */
function hover_over() {
  jQuery(this).data('trigger_set').removeClass('selected');
  jQuery(this).data('target_set').not(jQuery(this).data('rollover_content')).hide();
  jQuery(this).addClass('selected').data('rollover_content').filter(':hidden').fadeIn(400);
}

/**
 * Event handler for mouseout event.  Used for hoverIntent.
 */
function hover_out() {
}

function mycarousel_initCallback(carousel) {
  // Disable autoscrolling if the user clicks the prev or next button.
  carousel.buttonNext.bind('click', function() {
    carousel.stopAuto();
  });

  carousel.buttonPrev.bind('click', function() {
    carousel.stopAuto();
  });

  // Pause autoscrolling if the user moves with the cursor over the clip.
  carousel.clip.hover(function() {
      carousel.stopAuto();
    }, function() {
      carousel.startAuto();
  });
}

/**
 * For example: http://www.northps.com/content_solutions/product-expertise
 *   path         = '/content_solutions/product-expertise'
 *   path_args[0] = 'content_solutions'
 *   path_args[1] = 'product-expertise'
 */
function markActiveLink() {
  var path = location.pathname;
  if (path && path != '/') {
    var path_args = path.substring(1, path.length).split('/');
    if (path_args.length > 0) {
      jQuery('.links li a[href$="' + path_args[0] + '"]').addClass('active');
      if (path_args.length > 1 && path_args[1] != "") {
        jQuery('.sub_links li a[href$="'+ path_args[1] +'"]').addClass('active');
      }
    }
  }
}

jQuery(document).ready(function(){
  var hover = {                  // set up hover configuration object for hoverIntent
    'selection': jQuery([]),     // initialize an empty selection
    'config':    {
      'sensitivity': 2,          // number = sensitivity threshold (must be 1 or higher)
      'interval':    100,        // number = milliseconds for onMouseOver polling interval
      'over':        hover_over, // function = onMouseOver callback (REQUIRED)
      'timeout':     400,        // number = milliseconds delay before onMouseOut
      'out':         hover_out   // function = onMouseOut callback (REQUIRED)
    }
  };
  hover = map_selection(hover, jQuery('#cs-channel-info-wrapper .top-ul li a'), jQuery('#cs-channel-info-wrapper .bottom-ul li'), true);
  hover = map_selection(hover, jQuery('#sub-channel-info-wrapper .top-ul li.question .link'), jQuery('#sub-channel-info-wrapper .top-ul li ul'));
  $(hover.selection).hoverIntent(hover.config);
   
    //added a new-window class to all links with in the body of a article.
   jQuery("#content-area .node-inner .content area, #content-area .node-inner .content p a, #content-area .node-inner .content ul li a").addClass('new-window');
/*    jQuery("#content-area .content .more_link a").removeClass('new-window'); */
   
   // SEMANTICALLY CORRECT _blank_target (REQUIRES JQUERY)
    $('.new-window').click(function(){
      window.open(this.href);
      return false;
    });
    
  jQuery('#mycarousel').jcarousel({
    //these values can be changed, removed if need be.
    auto: 4,
    visible: 7,
    //size: 7,
    scroll: 3,
    animation: 'slow',
    easing: 'linear',
    wrap: 'circular',
    initCallback: mycarousel_initCallback
  });
  markActiveLink();
  jQuery('#nps-employee .item-list li').addClass('clear-block');
});

