﻿var SlideshowActive = false;

var Slideshow = new function ()

{

  var _Autostart = false;

  var _Started = false;

  var _Folder;

  var _Prefix;

  var _Count;

  var _Delay;

  var _Speed;

  var _Image;

  var _Lastload;

  var _Nextload;

  var _DivBackground;

  var _DivImage;

  var _DivCurrent;

  var _DivCount;

  var _Current;

  var _DivCurrent;

  var _DivCount;

  var _ImageName;



  this.Init = function()

  {

    var oParams = Page.getItems();

    _Slideshow = (new Boolean(Number(oParams.slideshow))).valueOf();

    if (_Slideshow)

    {

      _Autostart = (new Boolean(Number(oParams.slide_autostart))).valueOf();

      _Folder = oParams.slide_folder;

      _Prefix = oParams.slide_prefix;

      _Count = Number(oParams.slide_count);

      _Delay = Number(oParams.slide_delay);

      _Speed = Number(oParams.slide_speed);

      _Image = document.getElementById(oParams.slide_image);

      _Lastload = document.getElementById(oParams.slide_loadimagelast);

      _Nextload = document.getElementById(oParams.slide_loadimagenext);

      _DivBackground = document.getElementById(oParams.slide_backgrounddiv);

      _DivImage = document.getElementById(oParams.slide_imagediv);

      _DivCurrent = document.getElementById(oParams.slide_currentdiv);

      _DivCount = document.getElementById(oParams.slide_countdiv);

      _DivOn = document.getElementById(oParams.slide_ondiv);

      _DivOff = document.getElementById(oParams.slide_offdiv);

      _ImageName = _Folder + '/' + _Prefix;

      _Image.src = _ImageName + '01';

      _Current = 0;

      this.Next();

      _DivCurrent.innerHTML = _Current;

      _DivCount.innerHTML = _Count;

      if (_Autostart) this.Start();

    }

  }

  

  this.Next = function() {_Current=(_Current==_Count)?1:_Current+1;pTransition();};

  this.Last = function() {_Current=(_Current==1)?_Count:_Current-1;pTransition();};

  this.Start = function() {if (!_Started){SlideshowActive = setInterval("Slideshow.Next()", _Speed * 1000);_DivOn.className+=' active';_DivOff.className=_DivOff.className.replace(/active/g,'');}};

  this.Stop = function() {clearInterval(SlideshowActive);SlideshowActive=false;_Started=false;_DivOff.className+=' active';_DivOn.className=_DivOn.className.replace(/active/g,'');};

  this.Fade = function (opacity, id) {var o=document.getElementById(id).style;var op=(opacity/100);o.opacity=op;o.MozOpacity=op;o.KhtmlOpacity=op;o.filter='alpha(opacity='+opacity+')';}

  this.ResetBackground = function () {_DivBackground.style.backgroundImage='none';}



  function pTransition()

  {

    _DivBackground.style.backgroundImage='url('+_Image.src+')';

  	Slideshow.Fade(0, _DivImage.id);

  	var speed = Math.round(_Delay * 10);

    var last = (_Current == 1) ? _Count : _Current-1;

    var next = (_Current == _Count) ? 1 : _Current+1;

  	var current = (_Current < 10) ? '0'+_Current : _Current;

  	last = (last < 10) ? '0'+last : last;

    next = (next < 10) ? '0'+next : next;

    _Lastload.src = _ImageName + last + '.jpg';

    _Nextload.src = _ImageName + next + '.jpg';

    _Image.src = _ImageName + current + '.jpg';

	  for(var i = 0; i <= 100; i++) {setTimeout("Slideshow.Fade(" + i + ",'" + _DivImage.id + "')",(i * speed));}

    setTimeout("Slideshow.ResetBackground()",(100 * speed));

    _DivCurrent.innerHTML = _Current;

  }

}

