var carousel = {
  slideFx: [],
  callout: null,
  calloutOffsets: {x: 30, y: 25, x2: -360},

  prepareCarousels: function(targetsArray, navClasses, calloutIdClasses) {
    // Test DOM
    if (!api.testDOM()) {
      return;
    }

    // Make callout container
    this.calloutIdClasses = calloutIdClasses;
    this.callout = document.body.appendChild(document.createElement("div"));
    this.callout.id = this.calloutIdClasses.id;

    // Make carousels
    this.targetsArray = targetsArray;
    this.navClasses = navClasses;
    this.makeCarousels();

    // Assign methods
    this.CarouselFx.prototype.startLeft = carousel.startLeft;
    this.CarouselFx.prototype.slideLeft = carousel.slideLeft;
    this.CarouselFx.prototype.startRight = carousel.startRight;
    this.CarouselFx.prototype.slideRight = carousel.slideRight;
    this.CarouselFx.prototype.linear = carousel.linear;
    this.CarouselFx.prototype.reset = carousel.reset;
  },

  CarouselFx: function(a) {
    this.offsetW = a[1];
    this.distance = a[2];
    this.xdistance = 0;
    this.margin = 0;
    this.interval = null;
    this.child = null;
    this.container = document.getElementById(a[0]);
    this.container.style.width = 2*this.offsetW+"px";
  },

  startLeft: function() {
    if (this.interval != null) { return; }

    this.child = api.getLastChild(this.container);
    this.child.style.marginLeft = (this.margin = -1*this.offsetW)+"px";
    this.child = this.container.insertBefore(this.child, this.container.firstChild);

    var p = this;
    this.interval = setInterval(function() { p.slideLeft(); }, 50);
  },
  slideLeft: function() {
    if (this.margin+this.distance < 0) {
      this.child.style.marginLeft = (((this.margin += this.linear()) > 0) ? 0 : this.margin)+"px";
    } else {
      this.reset();
      pageTracker._trackPageview('/homepage/carousel-arrow-left');            
    }
  },

  startRight: function() {
    if (this.interval != null) { return; }

    this.child = api.getFirstChild(this.container);
    this.child.style.marginLeft = (this.margin = 0)+"px";

    var p = this;
    this.interval = setInterval(function() { p.slideRight(); }, 50);
  },
  slideRight: function() {
    if (this.margin >= -1*this.offsetW) {
      this.child.style.marginLeft = (this.margin -= this.linear())+"px";
    } else {
      this.child.style.marginLeft = -1*this.offsetW+"px";
      this.child.parentNode.appendChild(this.child);
      this.reset();
      pageTracker._trackPageview('/homepage/carousel-arrow-right');       
    }
  },

  reset: function() {
    clearInterval(this.interval);
    this.xdistance = 0;
    this.interval = null;
    this.child.style.marginLeft = "0px";
  },

  linear: function() {
    return Math.ceil(this.xdistance += this.distance/2);
  },

  // Make carousels
  makeCarousels: function() {
    var p = this;
    api.map(function(a) {
      // Create carousel object
      var obj = p.slideFx[p.slideFx.length] = new p.CarouselFx(a);

      // Add carousel navigation
      p.makeCarouselNav(obj, "left");
      p.makeCarouselNav(obj, "right");

      // Add mouse events to display callout
//   p.applyCalloutEvents(document.getElementById(a[0]).getElementsByTagName("dl"));
    }, this.targetsArray);
  },

  // Make carousel navigation
  makeCarouselNav: function(obj, direction) {
    var div = obj.container.parentNode.appendChild(document.createElement("div"));
    div.className = this.navClasses[direction][0];

    var hoverClass = this.navClasses[direction][1];
    div.onmouseover = function() { this.className += " "+hoverClass; }
    div.onmouseout = function() { this.className = api.removeLastClassName(this); }

    var p = this;
    var fn = (direction == "left") ?
      function() { obj.startLeft(); } :
      function() { obj.startRight(); }
    api.addEvent(div, "click", fn);
  },

 
  // Position callout
  positionCallout: function(event) {
    var callout = this.callout;
    var rightClass = this.calloutIdClasses.classes[1];
    var offsets = this.calloutOffsets;
    var offsetX = (callout.className == rightClass) ? offsets.x2 : offsets.x;
    var offsetY = offsets.y;
    var coords = api.getCursorPosition(event);
    callout.style.top = coords.y-offsetY+"px";
    callout.style.left = coords.x+offsetX+"px";
  }
}

$(function() {
//api.addEvent(window, "load", function() {
    carousel.prepareCarousels(
      // object ID, object width, distance iteration
      [["t_imageShow", 380, 25]],
      // nav classes
      {left: ["scrollbtn-left", "over-left"],
       right: ["scrollbtn-right", "over-right"]},
      // callout ID and classes
      {id: "carousel-callout",
       classes: ["leftside", "rightside"]}
    );
  });
   
 

