SlideShow = function() {
    this.bigDiv = null;
    this.smallDiv = null;
    this.images = null;
    this.imageStates = new Object();
    this.bigImages = new Array();
    this.smallImages = new Array();
    this.nbSmallImages = 14;
    this.animate = false;
    this.preload = false; 
}

SlideShow.prototype =
          {
              Load: function(images, bigDiv, smallDiv, nbSmall, animate) {
                  this.bigDiv = document.getElementById(bigDiv);
                  this.smallDiv = document.getElementById(smallDiv);
                  this.images = images;
                  //alert(this.images.length);
                  this.nbSmallImages = nbSmall;
                  this.animate = animate;
                  this.CheckImages();
                  this.ResetImageStates();
              },
              ResetImageStates: function() {
                  this.imageStates.states = new Array();
                  this.imageStates.length = this.smallImages.length;
                  this.imageStates.shown = 0;

                  for (var i = 0; i < this.smallImages.length; i++) {
                      this.imageStates.states[i] = 0;
                  }
              },
              GetRandomImageIndex: function() {
                  if (this.imageStates.shown == this.imageStates.length) {
                      this.ResetImageStates();
                  }
                  rn = Math.floor(Math.random() * this.imageStates.length);
                  while (this.imageStates.states[rn] == 1) {
                      rn++;
                      if (rn >= this.imageStates.length) rn = 0;
                  }

                  this.imageStates.shown++;
                  this.imageStates.states[rn] = 1;
                  return rn;
              },
              ContinueLoad: function() {
                  for (i = 0; i < this.images.length; i++) {
                      if (this.images[i].fixed == 1) {
                          this.bigImages.push(this.images[i]);
                      } else {
                          this.smallImages.push(this.images[i]);
                      }
                  }

                  this.CreateFixedImages();
                  this.CreateSmallImages();
              },
              CreateFixedImages: function() {
                  html = "";
                  for (i = 0; i < this.bigImages.length; i++) {
                      im = this.bigImages[i];
				if (im.url.length > 0) {
                      		html += "<A href='" + im.url + "'><IMG style='Image' class='large' src='" + im.image + "'></a>";
				} else
				{
					html += "<A href='#'><IMG style='Image' class='large' src='" + im.image + "'></a>";
				}

                  }
		if (this.bigDiv != null)
		{
                  		this.bigDiv.innerHTML = html;
		}
              },
              CreateSmallImages: function() {
                  html = "";
                  for (var i = 0; i < this.smallImages.length && i < this.nbSmallImages; i++) {
                      rn = this.GetRandomImageIndex();
                      im = this.smallImages[rn];
				if (im.url.length > 0) {
		                      html += "<A href='" + im.url + "'><IMG style='Image' class='small' src='" + im.image + "'></a>";
				} else {
					html += "<A href='#'><IMG style='Image' class='small' src='" + im.image + "'></a>";
				}

		    if (im.img.complete != true)
		    {
			var self = this;
                      	var CreateSmallImagesFunctionContinue = function() {
                          		self.CreateSmallImages();
	                   }
                      	setTimeout(CreateSmallImagesFunctionContinue, 1000);
			return; 
		     }
                  }
	
		if (this.smallDiv != null)
		{
                  		this.smallDiv.innerHTML = html;
		}

                  var self = this;
                  if (this.animate) {
                      var CreateSmallImagesFunctionContinue = function() {
                          self.CreateSmallImages();
                          $("#" + self.smallDiv.id).animate({ opacity: 1 }, 'fast'); //fadeIn('slow');
                      }

                      var CreateSmallImagesFunction = function() {
                          $("#" + self.smallDiv.id).animate({ opacity: 0.0 }, 'slow', CreateSmallImagesFunctionContinue); // fadeOut('slow',CreateSmallImagesFunctionContinue );
                      }
                      setTimeout(CreateSmallImagesFunction, 5000);
                  } else {
                      var CreateSmallImagesFunctionContinue = function() {
                          self.CreateSmallImages();
                      }

                      setTimeout(CreateSmallImagesFunctionContinue, 5000);

                  }

              },
              CheckImages: function() {

                  var i = 0;
                  var okCheckImages = false;
                  okCheckImages = true;
                  var self = this;
                  for (var i = 0; i < this.images.length; i++) {
                      var icon = this.images[i];
                      if (icon != null && icon.url != null) {
                          if (icon.img == null) {
                              var image = new Image();
                              image.src = icon.image;
                              icon.img = image;
                              okCheckImages = false;
                          } else {
                              if (icon.img.complete != true) {
                                  okCheckImages = false;
                              }
                          }
                      }
                  }

                  var self = this;
                  var CheckImageFunction = function() {
                      self.CheckImages();
                  };

                  if (this.preload == true && okCheckImages == false) {
                      setTimeout(CheckImageFunction, 100);
                  } else {
                      this.ContinueLoad();
                  }
              }
          }

             