//************ Carousel ************//
function Carousel (xc, paneArray)
{
  this.centerX           = xc;                   // coordinates calc by trig functions will be positive and neg
  this.positionInfo      = null;                 // this will be initialized in initPositions
  this.panes             = paneArray;
  this.nPanes            = this.panes.length;
  this.speed             = 450;

  this.initPositions     ();                     // builds array of coorindates where each panel will be displayed
  this.initPanes         ();                     // display panes in their initial position
}


Carousel.prototype.initPositions = function ()
{
  this.positionInfo = new Array({left: (this.centerX - 408) + "px", top: "0px",  height: "401px", zIndex: 105, opacity: 1.0 },
                                {left: (this.centerX - 461) + "px", top: "9px",  height: "383px", zIndex: 104, opacity: 0.75},
                                {left: (this.centerX - 494) + "px", top: "31px", height: "339px", zIndex: 103, opacity: 0.45},
                                {left: (this.centerX - 494) + "px", top: "31px", height: "339px", zIndex: 102, opacity: 0.30},
                                {left: (this.centerX - 461) + "px", top: "31px", height: "339px", zIndex: 101, opacity: 0.30},
                                {left: (this.centerX - 408) + "px", top: "31px", height: "339px", zIndex: 100, opacity: 0.30},
                                {left: (this.centerX - 355) + "px", top: "31px", height: "339px", zIndex: 101, opacity: 0.30},
                                {left: (this.centerX - 322) + "px", top: "31px", height: "339px", zIndex: 102, opacity: 0.30},
                                {left: (this.centerX - 322) + "px", top: "31px", height: "339px", zIndex: 103, opacity: 0.45},
                                {left: (this.centerX - 355) + "px", top: "9px",  height: "383px", zIndex: 104, opacity: 0.75});
}


Carousel.prototype.canHide = function (pane)
{
  if ((pane.hide == false) && ((pane.positionNdx >= 3) && (pane.positionNdx <= 7)))
  {
    pane.hide = true;
    jQuery("#" + pane.id).css("display", "none");
  }
}


Carousel.prototype.canUnhide = function (pane, newPosition)
{
  if ((pane.hide) && ((newPosition == 0) || (newPosition == 1) || (newPosition == 2) || (newPosition == 8) || (newPosition == 9)))
  {
    pane.hide = false;
    jQuery("#" + pane.id).css("display", "block");
  }
}


Carousel.prototype.getPaneNdxByName = function (paneName)
{
  var bWorkToDo = true;
  var ndx       = 0;

  while (bWorkToDo)
  {
    if (this.panes[ndx].id == paneName)
      bWorkToDo = false;
    else
    {
      ndx++;
      if (ndx >= this.nPanes)
      {
        ndx = -1;
        bWorkToDo = false;
      }
    }
  }
  return (ndx);
}


Carousel.prototype.getPaneNdxByPosition = function (positionNdx)
{
  var bWorkToDo = true;
  var ndx       = 0;

  while (bWorkToDo)
  {
    if (this.panes[ndx].positionNdx == positionNdx)
      bWorkToDo = false;
    else
    {
      ndx++;
      if (ndx >= this.panes.length)
      {
        ndx = -1;
        bWorkToDo = false;
      }
    }
  }
  return (ndx);
}


// goToPaneNdx
// ndx      = pane to rotate to
Carousel.prototype.goToPaneNdx = function (ndx)
{
  var nCount   = this.nPanes - this.panes[ndx].positionNdx;

  // do a simple check to rotate backwards if thats the shortest distance
  if (nCount > parseInt(this.nPanes / 2))
    nCount -= this.nPanes;
  this.rotate (nCount, false);
}


// initPanes
// Set up some initial values in each Pane object and
//  position each of the panes in their correct spots
Carousel.prototype.initPanes = function ()
{
  var positionInfo;
  var pane;
  var ndx;

  for (ndx = 0; ndx < this.nPanes; ndx++)
  {
    //logDebug ('<div style="position:absolute; left:' + this.positionInfo[ndx].left + "; top:" + this.positionInfo[ndx].top + '">' + ndx + '</div>');

    positionInfo     = this.positionInfo[ndx];
    pane             = this.panes[ndx];
    pane.positionNdx = ndx;

    jQuery("#" + pane.id)     .css({left: positionInfo.left, top: positionInfo.top, height: positionInfo.height, zIndex: positionInfo.zIndex});
    jQuery("#" + pane.idImage).css({height: positionInfo.height, opacity: positionInfo.opacity});
  }

  this.panes[3].hide = true;
  this.panes[4].hide = true;
  this.panes[5].hide = true;
  this.panes[6].hide = true;
  this.panes[7].hide = true;
}


Carousel.prototype.opacityIncrease = function (divName)
{
  var ndx        = this.getPaneNdxByName (divName);
  var pane       = this.panes[ndx];
  jQuery("#" + pane.idImage).css({opacity: 1.0});
}


Carousel.prototype.opacityRestore = function (divName)
{
  var ndx        = this.getPaneNdxByName (divName);
  var pane       = this.panes[ndx];
  var opacity    = this.positionInfo[pane.positionNdx].opacity;
  jQuery("#" + pane.idImage).css({opacity: opacity});
}


// reinitPanePositions
// xc         = center of browser window
Carousel.prototype.reinitPanePositions = function (xc)
{
  this.centerX           = xc;                  // center window
  this.initPositions     ();
  this.initPanes         ();                    // display panes in their initial position
}


// reinitPanes
// xc         = center of browser window
Carousel.prototype.reinitPanes = function (paneArray)
{
  // carouselMgr changed the paneArray, so the carousel must reinitialize everything
  this.panes             = paneArray;
  this.initPanes         ();                    // display panes in their initial position
}


// rotate
// nPositions = number of position to move - can be positive or negative
Carousel.prototype.rotate = function (nPositions, bOpacityDisabled)
{
//  if (bOpacityDisabled === undefined)
//    bOpacityDisabled = false;

  var ndx;
  var pane;
  var next;

  startDate = new Date();           // Grab new copy of date
  startTime = startDate.getTime();  // Grab current millisecond #

  ndx    = this.getPaneNdxByPosition (0);
  pane   = this.panes[ndx];
  jQuery ("#" + pane.idContent).fadeOut();

  for (ndx = 0; ndx < this.nPanes; ndx++)
  {
    pane             = this.panes[ndx];
    next             = (pane.positionNdx + nPositions + this.nPanes) % 10;
    positionInfo     = this.positionInfo[next];
    pane.positionNdx = next;

    // check if any pane is hidden and will rotate so that it should become visible
    this.canUnhide (pane, next);

    if (ndx == this.nPanes - 1)
    {
      var tmp = "#" + pane.id;
      setTimeout ('jQuery("' + tmp + '").css({zIndex:' + positionInfo.zIndex + '})', this.speed / 2);
      jQuery("#" + pane.id)     .animate({left: positionInfo.left, top: positionInfo.top, height: positionInfo.height}, this.speed, function (){carouselMgr.rotateComplete()});
      if (bOpacityDisabled == true)
        jQuery("#" + pane.idImage).animate({height: positionInfo.height}, this.speed);
      else
        jQuery("#" + pane.idImage).css({opacity: positionInfo.opacity}).animate({height: positionInfo.height}, this.speed);
    }
    else
    {
      if (pane.hide == false)
      {
        var tmp = "#" + pane.id;
        setTimeout ('jQuery("' + tmp + '").css({zIndex:' + positionInfo.zIndex + '})', 250);
        jQuery("#" + pane.id)     .animate({left: positionInfo.left, top: positionInfo.top, height: positionInfo.height}, this.speed);
        if (bOpacityDisabled == true)
          jQuery("#" + pane.idImage).animate({height: positionInfo.height}, this.speed);
        else
          jQuery("#" + pane.idImage).css({opacity: positionInfo.opacity}).animate({height: positionInfo.height}, this.speed);
      }
    }

    // determine if can hide this pane because its now hidden behind another pane --
    this.canHide  (pane);
  }
}

