var slideshow = (function() { return function(containerId, slideContainerId, slideCount, options) {
  var ret = {
    currentSlide : 1,
    slideContainer : document.getElementById(slideContainerId),
    slideCount : slideCount,
    options : options,
    width : 0,
    timer : null,
    next : function() {
      if (this.timer) return;

      if (this.currentSlide == this.slideCount) {
        if (!this.options.onLastNextFirst) return;
        this.currentSlide=1;
        this.timer = setTimeout(
          (function(that, dir, startTime, num) { return function() {
            that.move(dir, startTime, num);
          };})(this, 1, new Date().getTime(), this.slideCount-1),
          1
        );
      } else {
        this.currentSlide+=1;
        this.timer = setTimeout(
          (function(that, dir, startTime, num) { return function() {
            that.move(dir, startTime, num);
          };})(this, -1, new Date().getTime(), 1),
          1
        );
      }
      
    },
    prev : function() {
      if (this.currentSlide == 1 || this.timer) return;
      this.currentSlide-=1;
      this.timer = setTimeout(
        (function(that, dir, startTime, num) { return function() {
          that.move(dir, startTime, num);
        };})(this, 1, new Date().getTime(), 1),
        1
      );
    },
    move : function(dir, startTime, num) {
      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
      
      var curTime = (new Date().getTime() - startTime) / this.options.slideTime;
      if (curTime > 1) curTime = 1;

      var left = this.options.slideCurve.getPoint(curTime).y * this.width * num;
      this.slideContainer.style.left = Math.round((-(this.currentSlide-1+dir)*this.width*num)+(left*dir))+"px";

      if (curTime != 1) {
        this.timer = setTimeout(
          (function(that, dir, startTime, num) { return function() {
            that.move(dir, startTime, num);
          };})(this, dir, startTime, num),
          1
        );
      } else {
        this.timer = null;
      }
    }
  };
  
  var x, y, elems, elem, rel;
  elems = document.getElementById(containerId).getElementsByTagName('*');
  for (y=0; y<elems.length; y++) {
    ele = elems[y];
    rel = ele.getAttribute("rel")
    if (rel == "prev") {
      if (ele.addEventListener) {
        ele.addEventListener(
          'click',
          (function(that) { return function() {
            that.prev();
          };})(ret),
          false
        );
      } else {
        ele.attachEvent(
          'onclick',
          (function(that) { return function() {
            that.prev();
          };})(ret)
        );
      }
    } else if (rel == "next") {
      if (ele.addEventListener) {
        ele.addEventListener(
          'click',
          (function(that) { return function() {
            that.next();
          };})(ret),
          false
        );
      } else {
        ele.attachEvent(
          'onclick',
          (function(that) { return function() {
            that.next();
          };})(ret)
        );
      }
    }
  }
  
  var func = (function(ret) { return function() {
    if (document.getElementById(slideContainerId).parentNode.currentStyle) {
      var width = document.getElementById(slideContainerId).parentNode.currentStyle.width;
    } else if (window.getComputedStyle) {
      var width = document.defaultView.getComputedStyle(document.getElementById(slideContainerId).parentNode, null).getPropertyValue("width");
    }
    ret.width = width.replace(/\D/g,'');
    ret.slideContainer.style.left = 0;

    if (typeof ret.options == 'undefined')
      ret.options = {};
    if (typeof ret.options.onLastNextFirst == 'undefined')
      ret.options.onLastNextFirst = 1;
    if (typeof ret.options.slideTime == 'undefined')
      ret.options.slideTime = 500;
    if (typeof ret.options.slideCurve == 'undefined') {
      ret.options.slideCurve = new bezier();
      ret.options.slideCurve.addPoint(ret.options.slideCurve.point(0,0));
      ret.options.slideCurve.addPoint(ret.options.slideCurve.point(0.4,0));
      ret.options.slideCurve.addPoint(ret.options.slideCurve.point(0.6,1));
      ret.options.slideCurve.addPoint(ret.options.slideCurve.point(1,1));
    }
  };})(ret);

  var oldonload = window.onload;
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } else { 
    window.onload = function() { 
      if (oldonload) { 
        oldonload(); 
      } 
      func(); 
    } 
  }   
  
};})();

function isInt(x) {
  var y=parseInt(x);
  if (isNaN(y)) return false;
  return x==y && x.toString()==y.toString();
}