﻿// JScript File
 /*
    The BIS(BetterImageSlideshow) javascript object is not documented anywhere so here's a short list of fields and methods:
    ---Fields---
    this.A : Array with the image names.
    this.B : Base of the images there url
    this.bF : Boolean telling if we use filters
    this.C : Counter keep track of which image is on.
    this.I : The html image element where to show the images.
    this.M : Ammount of millisecond between slides.
    this.P : Params added to the images there url.
    this.T : Timer object.

    ---Methods---
    this.GE(el) : Short version of document.getElementById(el) returns an Element
    this.IF(iT, iTD) : Initialize filter iT with a duration of iTD. Returns a boolean telling whether or not a filter is applied.
    this.SS : Sets the image to this.C
    this.Ne : Goto next image
    this.Pr : Goto previous image
    this.Pl : Play. Set's the timer (this.T) to do this.NE after this.M seconds.
    this.St : Stop. Clears this.T
    this.PF : Preload all the images in this.A
    */


    var fileSelected = "";
    
    var tblDataList;

   
    function adjustBehaviour()
    {
    
       setText("count", this.A.length);

        writeImgProp(this.I);
        setText("slidenum", this.C + 1);
       
        this.I['onload'] = function()
         {  
            writeImgProp(this);
         }
        
        var oldStopFunc = this.St;
        this.St = function(){
          oldStopFunc.call(this);     
        }
      
        var oldPlayFunc = this.Pl;
        this.Pl = function(){
          oldPlayFunc.call(this);
          selectCurrThumbnail(this.C);
          autoScroll(this.C);
        }

        var oldSSFunc = this.SS;
        this.SS = function(){
            oldSSFunc.call(this);
            setText("slidenum", this.C + 1);
        }
  }
  
    function writeImgProp(img)
        {
            var imgName = stripFileName(img.src);
            var frags;
            for(var i = 0; i<fileArr.length;i++)
            {
           

               if(fileArr[i].indexOf(imgName) > -1)
               {
                frags = fileArr[i].split('|');
                setText("imgprop", frags[1]); 
                break;
                }
            }
         }

        function stripFileName(src)
        {
            var indexofSlash = src.lastIndexOf("%2f");
            
            if(indexofSlash > 0)
                indexofSlash += 3;
            
            var indexofHash = src.indexOf("\&",indexofSlash);
            return src.substring(indexofSlash,indexofHash);
        }

        function setText(objId, text)
        {
            var obj = document.getElementById(objId);
            var textNode = document.createTextNode(text);
            
            (obj.firstChild)?obj.replaceChild(textNode, obj.firstChild):obj.appendChild(textNode);
        }
        
        
    function resetSlide()
    {
      for(var j=0;j<this.A.length;j++)
	        {
	          if(this.A[j].indexOf(fileSelected) > -1)
		           {
		             this.C = j;
		             this.SS();
		             adjustBehaviour.call(this);
		             break;
		           }
	        }
    }
        

 function selectNext()
 {
   var slideNumber;
   var fileName;
   if(this.C + 1 >= this.A.length)
      slideNumber = 1;
   else 
      slideNumber = this.C + 2;

   selectCurrThumbnail(slideNumber - 1);
   autoScroll(slideNumber - 1);

 }
 
 function selectPrevious()
 {
   var slideNumber;
   var fileName;
   if(this.C == 0)
      slideNumber = this.A.length;
   else 
      slideNumber = this.C;
   selectCurrThumbnail(slideNumber - 1);
   autoScroll(slideNumber - 1);

 }
 
    
 function stopSlideshow()
 {   
   selectCurrThumbnail(this.C);
   autoScroll(this.C);
    }

//auto-scrolling ...
 function autoScroll(ind) //args: x
 {
   var posX= document.getElementById("__thumbnailsScrollPosX");
   var posY= document.getElementById("__thumbnailsScrollPosY");
   posY.value=0;
   if((ind + 1)> 3)
      posX.value = 18 + (102.5 * (ind - 3));
   else
      posX.value = 0;
   SaveElementScroll1.RS();
 }
 
  
 function selectCurrThumbnail(selectedIndex)
 {
    var parentEle;
    var count = 0;
   
    if(tblDataList != null)
    {
      parentEle = tblDataList;
      while(parentEle.childNodes[0].tagName != "TR")
         parentEle = parentEle.childNodes[0];
        

      if(parentEle.childNodes[0].tagName == "TR")
      {
          var trEle = parentEle.childNodes[0];
          for(var i =0; i < trEle.childNodes.length;i++)
          {
             if(i==selectedIndex)
               {
                 trEle.childNodes[i].style.borderWidth="2px";
                 trEle.childNodes[i].style.borderColor="#000000";
                 trEle.childNodes[i].style.borderStyle="solid";
                } 
             else
               {
                 trEle.childNodes[i].style.borderWidth="0";
                 trEle.childNodes[i].style.borderColor="#FFFFFF";
                 trEle.childNodes[i].style.borderStyle="none";
               } 
          }
       }
   
    } 
    
 }
