//
// I made this! // KSO 26/01/2009
//
function ImageSlider(iwidth,imargin,itemc,frequency,jumpduration,sid,cb) {
  this.itemWidth = iwidth;
  this.itemMargin = imargin;
  this.itemMWidth = this.itemWidth + this.itemMargin*2;
  this.sliderItemCount = itemc;
  this.sliderItems = new Array();
  this.sliderCurrent = new Array();  
  this.sliderId = sid;
  this.callBackF = cb;
  this.jumpingTo = 0;
  this.jumptime = jumpduration;
  this.freq = frequency;
  this.speed = 0;
  this.step = 0;
  this.target = 0;
  this.sliderPosition = 0;
}

ImageSlider.prototype.addSliderItem = function(newItem) {
  this.sliderItems[this.sliderItems.length] = newItem;
  var imagepreload = new Image();
  imagepreload.src = newItem[0];
};

ImageSlider.prototype.applyOpacity = function(d,op) {
  d.style.filter = "alpha(opacity = "+parseInt(op*100)+")";
  d.style.MozOpacity = op;
  d.style.opacity = op;
};

ImageSlider.prototype.getSliderItemPosition = function(item) {
  return item * this.itemMWidth;
};

ImageSlider.prototype.sliderItemCorrection = function(item) {
  if (item > this.sliderItems.length - this.sliderItemCount) {
    return this.sliderItems.length - this.sliderItemCount;
  } else if (item < 0) {
    return 0;
  } else {
    return item;
  }
};

ImageSlider.prototype.getElementsByClassNameIE = function(elem,classname) {
  if (!elem.getElementsByClassName) { // bloody IE
    var ret = new Array();
    var e = elem.firstChild;
    while (e) {
      if (e.className == classname) {
        ret[ret.length] = e;
      }
      e = e.nextSibling;
    }
    return ret;
  } else {
    return elem.getElementsByClassName(classname);
  }
};

ImageSlider.prototype.drawSliderItems = function(offset) {
  var i;
  var ex;
  var found;
  var iw,mrg,mrg2;
  
  ex = this.getElementsByClassNameIE(this.slider,"imageslideritem");
  for (i=ex.length-1;i>=0;i--) {
    found = false;
    for (j=this.sliderCurrent.length-1;j>=0;j--) {
      if (ex[i].id == "imgsl_" + this.sliderCurrent[j]) {
        found = true;
        break;
      }
    }
    if (!found) {
      this.slider.removeChild(ex[i]);
    }
  }

  for (j=0;j<this.sliderCurrent.length;j++) {
    found = false;
    if (!(d = document.getElementById("imgsl_" + this.sliderCurrent[j]))) {
      d = document.createElement("div");
      d.className = "imageslideritem";
      d.onclick = new Function(this.callBackF+"(iso.sliderItems["+this.sliderCurrent[j]+"],false);");
      d.id = "imgsl_" + this.sliderCurrent[j];
      d.style.margin = this.itemMargin + "px";
      d.style.backgroundImage = "url('"+this.sliderItems[this.sliderCurrent[j]][0]+"')";
      if (j == 0) {
        this.slider.insertBefore(d,this.slider.firstChild);
      } else {
        this.slider.appendChild(d);
      }
    }
    if (j == 0) {
      iw = this.itemMWidth - offset;
      if (iw >= this.itemMargin) {
        mrg = this.itemMargin;
        iw -= this.itemMargin;
      } else {
        mrg = 0;
      }
      if (iw > this.itemWidth) {
        mrg2 = iw - this.itemWidth;
      } else {
        mrg2 = 0;
      }
      iw = iw - mrg2;
      d.style.width = iw + "px";
      d.style.marginRight = mrg;
      d.style.marginLeft = mrg2;
      d.style.backgroundPosition = "top right";
    } else {
      if (j == this.sliderCurrent.length-1 && this.sliderCurrent.length > this.sliderItemCount) {
        iw = offset;
      } else {
        iw = this.itemMWidth;
      }
      if (iw >= this.itemMargin) {
        mrg = this.itemMargin;
        iw -= this.itemMargin;
      } else {
        mrg = 0;
      }
      if (iw > this.itemWidth) {
        mrg2 = iw - this.itemWidth;
      } else {
        mrg2 = 0;
      }
      iw = iw - mrg2;
      d.style.width = iw + "px";
      d.style.marginLeft = mrg;
      d.style.marginRight = mrg2;
      d.style.backgroundPosition = "top left";
    }
    if (d.clientWidth == this.itemWidth) {
      this.applyOpacity(d,1);
      d.innerHTML = this.sliderItems[this.sliderCurrent[j]][4];
    } else {
      d.innerHTML = "";
      this.applyOpacity(d,d.clientWidth/this.itemWidth);
    }
  }  
};

// ********** testing

ImageSlider.prototype.drawSliderItems2 = function(offset) {
  var i,d,d2;
  var ex;
  var found;
  var iw,mrg,mrg2;
  
  ex = this.getElementsByClassNameIE(this.slider,"imageslideritem");
  for (i=ex.length-1;i>=0;i--) {
    found = false;
    for (j=this.sliderCurrent.length-1;j>=0;j--) {
      if (ex[i].id == "imgsl_" + this.sliderCurrent[j]) {
        found = true;
        break;
      }
    }
    if (!found) {
      this.slider.removeChild(ex[i]);
    }
  }

  for (j=0;j<this.sliderCurrent.length;j++) {
    found = false;
    if (!(d = document.getElementById("imgsl_" + this.sliderCurrent[j]))) {
      d = document.createElement("div");
      d2 = document.createElement("div");
      d.className = "imageslideritem";
      d.onclick = new Function(this.callBackF+"(iso.sliderItems["+this.sliderCurrent[j]+"],false);");
      d.id = "imgsl_" + this.sliderCurrent[j];
      d.style.margin = this.itemMargin + "px";
      d2.className = "picdiv";
      d2.style.backgroundImage = "url('"+this.sliderItems[this.sliderCurrent[j]][0]+"')";
      d.appendChild(d2);
      if (j == 0) {
        this.slider.insertBefore(d,this.slider.firstChild);
      } else {
        this.slider.appendChild(d);
      }
    }
    if (j == 0) {
      iw = this.itemMWidth - offset;
      if (iw >= this.itemMargin) {
        mrg = this.itemMargin;
        iw -= this.itemMargin;
      } else {
        mrg = 0;
      }
      if (iw > this.itemWidth) {
        mrg2 = iw - this.itemWidth;
      } else {
        mrg2 = 0;
      }
      iw = iw - mrg2;
      d.style.width = iw + "px";
      d.style.marginRight = mrg;
      d.style.marginLeft = mrg2;
      d.style.backgroundPosition = "right";
    } else {
      if (j == this.sliderCurrent.length-1 && this.sliderCurrent.length > this.sliderItemCount) {
        iw = offset;
      } else {
        iw = this.itemMWidth;
      }
      if (iw >= this.itemMargin) {
        mrg = this.itemMargin;
        iw -= this.itemMargin;
      } else {
        mrg = 0;
      }
      if (iw > this.itemWidth) {
        mrg2 = iw - this.itemWidth;
      } else {
        mrg2 = 0;
      }
      iw = iw - mrg2;
      d.style.width = iw + "px";
      d.style.marginLeft = mrg;
      d.style.marginRight = mrg2;
      d.style.backgroundPosition = "left";
    }
    if (d.clientWidth == this.itemWidth) {
      this.applyOpacity(d,1);
//      d.firstChild.innerHTML = this.sliderItems[this.sliderCurrent[j]][4];
    } else {
      d.firstChild.innerHTML = "";
      this.applyOpacity(d,d.clientWidth/this.itemWidth);
    }
  }  
};

// ********** testing

ImageSlider.prototype.setSliderPosition = function(pos) {
  var i,j,ic;

  ic = 0;
  j = parseInt(pos);
  while (j >= this.itemMWidth) {
    j -= this.itemMWidth;
    ic++;
  }
  
  for (i=0;i<this.sliderItemCount;i++) {
    if (this.sliderItems[ic+i]) {
      this.sliderCurrent[i] = ic + i;
    }
  }
  
  if (j > 0) {
    this.sliderCurrent[this.sliderItemCount] = this.sliderCurrent[this.sliderItemCount-1]+1;
  } else {
    if (this.sliderCurrent.length > this.sliderItemCount) {
      this.sliderCurrent.pop();
    }
  }
  
  this.drawSliderItems(j);
  
  this.sliderPosition = pos;
};

ImageSlider.prototype.timerLooper = function() {
    if (this.step < (this.jumptime/this.freq)) {
    this.step++;
    if (this.step == (this.jumptime/this.freq)) {
      this.sliderPosition = this.target - this.speed;
    }
    this.setSliderPosition(this.sliderPosition + this.speed);
    var self = this;
    setTimeout(function(){self.timerLooper();},this.freq);
  }
};


ImageSlider.prototype.generateSlider = function() {

  this.slider = document.getElementById(this.sliderId);
  //this.slider.style.width = (this.sliderItemCount * this.itemMWidth) + "px";
  this.setSliderPosition(0);

};

ImageSlider.prototype.jumpToPosition = function(pos) {
  this.target = this.getSliderItemPosition(this.sliderItemCorrection(pos));
  this.jumpingTo = this.sliderItemCorrection(pos);
  var distance = this.target - this.sliderPosition;
  
  this.speed = distance / (this.jumptime / this.freq);
  this.step = 0;
  this.timerLooper();
}

ImageSlider.prototype.changePos = function(ch) {
  this.jumpToPosition(this.jumpingTo + ch);
}

