(function($){

$(document).ready(function() {

  /* spotlights */
  function showSpotlight(index) {
    $('#customer_spotlight ul li:visible').fadeOut(300, function() {
      $('#customer_spotlight ul li').eq(index).fadeIn(300, function() {
        $('#customer_spotlight p.spotlight_nav img').unbind('click');

        if (index == (spotlightCount - 1)) {
          $($next).attr({ 
            src: 'http://www.nortel.com/images/template2005/billboard/nav_forward_inactive.gif' 
          });
        } else {
          $next.click(function(e) { nextClick(e); });
          $($next).attr({ 
            src: 'http://www.nortel.com/images/template2005/billboard/nav_forward_active.gif' 
          });
        }

        if (index == 0) {
          $($prev).attr({ src: 'http://www.nortel.com/images/template2005/billboard/nav_back_inactive.gif' });
        } else {
          $prev.click(function(e) { prevClick(e); });
          $($prev).attr({ src: 'http://www.nortel.com/images/template2005/billboard/nav_back_active.gif' });
        }
      });
    });

  }

  function nextClick(e) {
    var newSpotlightIndex = spotlightIndex + 1;
    if (newSpotlightIndex >= spotlightCount) { newSpotlightIndex = 0; }
    showSpotlight(newSpotlightIndex);
    spotlightIndex = newSpotlightIndex;
    e.preventDefault();
  }

  function prevClick(e) {
    var newSpotlightIndex = spotlightIndex - 1;
    if (newSpotlightIndex < 0) { newSpotlightIndex = spotlightCount - 1; }
    showSpotlight(newSpotlightIndex);
    spotlightIndex = newSpotlightIndex;
    e.preventDefault();
  }

  // jq plugin to randomly reorder first-child elements of selected elements
  $.fn.reorder = function() {

    // http://javascript.about.com/library/blsort2.htm
    function randOrd() { return(Math.round(Math.random())-0.5); }

    return($(this).each(function() {
      var $this = $(this);
      var $children = $this.children();
      var childCount = $children.length;

      if (childCount > 1) {
        $children.remove();

        var indices = new Array();
        for (i=0;i<childCount;i++) { indices[indices.length] = i; }
        indices = indices.sort(randOrd);
        for (j=0;j<childCount;j++) {
          var k = indices[j];
          $this.append($children.eq(k));
        }

      }

    }));
  }

  $('#customer_spotlight').prepend('<div class="spotlight_header"></div>');
  $('#customer_spotlight h2:first').remove().prependTo('#customer_spotlight div.spotlight_header');

  var spotlightCount = $('#customer_spotlight ul li').length;

  if (spotlightCount > 1) {
    // rearrange spotlights randomly
    $('ul.spotlights').reorder();
    

    $('#customer_spotlight div.spotlight_header').append(
      '<p class="spotlight_nav">' +
      '<img src="http://www.nortel.com/images/template2005/billboard/nav_back_inactive.gif" alt="Previous" /> ' +
      '<img src="http://www.nortel.com/images/template2005/billboard/nav_forward_active.gif" alt="Next" />' +
      '</p>'
    );

    var $next = $('#customer_spotlight p.spotlight_nav img:last');
    var $prev = $('#customer_spotlight p.spotlight_nav img:first');

    var spotlightIndex = 0;

    $next.click(function(e) { nextClick(e); e.preventDefault(); });
    $('#customer_spotlight p.spotlight_nav img').css({cursor: 'pointer'});

  } 

  $('#customer_spotlight ul li').hide();
  $('#customer_spotlight ul li:first').show();

  /* tabs */
  $('#tabs').tabs('tabs-js',true,true,'30em');
});

})(jQuery);



