﻿var JavaScript = JavaScript || {};
JavaScript.Forms = JavaScript.Forms || {};

JavaScript.Forms.SlideShow = SlideShow = function(base) {
  var base = base;
  base.Parent = this;
  this.Background = "#f0f0f0";
  this.Container = document.createElement("div");
  this.Interval = null;
  this.Items = new SlideCollection(this);
  this.Speed = 50;
  this.Width = 0;
  
  this.Ending = function() {
    var slide = this.Container.childNodes[0].cloneNode(false);
    this.Container.removeChild(this.Container.childNodes[0]);
    this.Container.appendChild(slide);
    base.scrollLeft = 0;
  };
  this.Preload = function() {
    for (var i = 0; i < this.Items.Count; i++) {
      var slide = this.Items[i].cloneNode(false);
      slide.style.display = "none";
      document.body.appendChild(slide);
    };
    this.Container.style.width = "3000px";
  };
  this.Scroll = function() {
    if (base.scrollLeft < base.scrollWidth) {
      base.scrollLeft++;
      if (base.scrollLeft > base.Parent.Container.childNodes[0].clientWidth) {
        base.Parent.Ending();
      };
    };
  };
  this.Start = function() {
    base.scrollLeft = 0;
    this.Interval = setInterval(this.Scroll, this.Speed);
  };
  this.Stop = function() {
    clearInterval(this.Interval);
  };
  this.Update = function() {
    if (base.childNodes.length == 0) base.appendChild(this.Container);
    for (var i = 0; i < this.Items.Count; i++) {
      this.Container.appendChild(this.Items[i]);
      this.Width += this.Items[i].clientWidth;
    };    
    this.Container.style.width = this.Width + "px";
  };
};

JavaScript.Forms.SlideCollection = SlideCollection = function(owner) {
  var owner = owner;
  
  this.Add = function(slide) {
    this[this.Count] = slide;
    this.Count++;
    owner.Update();
  };
  this.AddRange = function(slides) {
    for (var i = 0; i < slides.length; i++) {
      this[this.Count] = slides[i];
      this.Count++;
    };
    owner.Update();
  };
  this.Count = 0;
};

JavaScript.Forms.Slide = Slide = function(alt, src) {
  var base = document.createElement("img");
  base.setAttribute("alt", alt);
  base.setAttribute("src", src);
  base.Type = "Slide";
  
  return base;
};
