﻿/*****************************************************************************************************
            Script File Name  :   slideshow.js
            Developer         :   Gary Woodfine
            Date              :   01/03/07
            Purpose           :   To Rotate display of images in a slideshow effect.
            Notes             :   This script is dependent on an AJAX call that will rendered from the page that
                                  calls the method. i.e CallServer           

*/
   var c_interval = 3000;
   var currentIndex;
   var bPlay = true;
   var timeOutID =  window.setTimeout("getNextImage()", c_interval);
   //var safari = navigator.userAgent.search(/safari/i) + 1;
   //alert('Safari ' + safari);
   
   // As firefox has issues with .innerText these two routines cater for get and setting the  values in the Label
   function getIndex(control)
   {
         //alert('Agent ' + navigator.userAgent);
         if ((navigator.userAgent).indexOf("Firefox")!=-1) 
         {
             var myspan = document.getElementById(control);
              var span_textnode = myspan.firstChild;
             i =  span_textnode.data;
         }
         else if((navigator.userAgent).indexOf("Safari")!=-1) 
         {
            var myspan = document.getElementById(control);
              var span_textnode = myspan.firstChild;
             i =  span_textnode.data;
         } 
         else
         {
            i = document.getElementById(control).innerText;
          }
            
        return i;
     }
   function setIndex(control,value)
   {
         //alert('Agent ' + navigator.userAgent);
         if ((navigator.userAgent).indexOf("Firefox")!=-1) 
         {
             var myspan = document.getElementById(control);
             var span_textnode = myspan.firstChild;
             span_textnode.data = value;
         }
         else if((navigator.userAgent).indexOf("Safari")!=-1) 
         {
             var myspan = document.getElementById(control);
             var span_textnode = myspan.firstChild;
             span_textnode.data = value;
         }
         else
         {
           document.getElementById(control).innerText = value;
         }
            
        return i;
        
   }
  //   End get set
    function slide(text)
    {
          doShow();
         document.aspnetForm.slidebutton.value = (text == "Stop") ? "Start" : "Stop";
        
     }
    function doShow()
    {
        if (document.aspnetForm.slidebutton.value == "Stop") 
        {
            bPlay = false;
            var i = getIndex("ctl00_cphSearchGrid_lblCurrent");
            currentIndex = currentIndex == Number(i)-1 ? 0 : Number(currentIndex)+1;
          window.clearTimeout(timeOutID);
        }
        else
        {
             bPlay = true;
             timeOutID =  window.setTimeout("getNextImage()", c_interval);
         }
    }
    function first() 
    {
       setIndex("ctl00_cphSearchGrid_lblCurrent",0);
       window.clearTimeout(timeOutID);
       CallServer(getIndex("ctl00_cphSearchGrid_lblCurrent") , "");
    }
    function last() 
    {
      setIndex("ctl00_cphSearchGrid_lblCurrent", Number(getIndex("ctl00_cphSearchGrid_lblCount")) - 1);
        window.clearTimeout(timeOutID);
      CallServer(Number(getIndex("ctl00_cphSearchGrid_lblCount")) - 1 , "");
    }
    function next() 
    {
         var i = getIndex("ctl00_cphSearchGrid_lblCurrent");
        if (Number(i) < Number(getIndex("ctl00_cphSearchGrid_lblCount"))) 
        {
            setIndex("ctl00_cphSearchGrid_lblCurrent",Number(i) + 1);
             window.clearTimeout(timeOutID);
             CallServer(Number(i)   , "");
        }
      else first();
    }
    function previous() 
    {
         var i = getIndex("ctl00_cphSearchGrid_lblCurrent");
         if (Number(i) > 1) 
          {
            setIndex("ctl00_cphSearchGrid_lblCurrent", Number(i) -2);
              window.clearTimeout(timeOutID);
             CallServer( Number(i)-2, "");
          }
         else last();
    }

    function getNextImage()
      {
         var i = getIndex("ctl00_cphSearchGrid_lblCurrent");

          if(i == "") i =1;
          
          //Send the request to server with the current image url as the argument
           CallServer( i, "");
      }
    function ReceiveServerData(rValue)
     {
       try
        {
           var wds = rValue.split(":");
           currentIndex = wds[1];
           setIndex("ctl00_cphSearchGrid_lblCurrent",wds[1]);
         
           document.getElementById("ctl00_cphSearchGrid_imgThumb").style.filter ="alpha(opacity=100)";
           var img  = new Image();
           img.onload = function(){ imageLoaded(this); }
           img.onerror = function(){ imageError(this); }
           img.onabort = function(){ imageError(this); }
           //alert('Get Image Source' + wds[0]);
           img.src = wds[0]; 
        }
        catch(err)
        {
           alert(err.description);
        }
       }   
   
       function imageError(img)
         {
            //If image download errors occur, this function will be called.
             window.setTimeout("getNextImage()", 1000);
         }
     function imageLoaded(img)
      {
              
               var photo = document.getElementById("ctl00_cphSearchGrid_imgThumb");   //Find the image control object
              //This line is needed as Non IE browsers do not play transition effects.
               if(navigator.appName == "Microsoft Internet Explorer")
                    {
                        photo.filters[0].apply(); //Apply the transition effect
                        photo.filters[0].play(); //Play the effect and display the new image
                    }  
                   photo.src = img.src; 
                   
                  if(bPlay)timeOutID = window.setTimeout("getNextImage()", c_interval); ;//Initiate the next request
                
        }
        
   
