﻿var slideInterval = 250; //default slide interval (actual calulated below)
var slideAnimInverval = 100; // in ms
var slideAnimMoveBy = 50; // in px
var filmStartLeft = filmEndLeft = 0;
var bouncesAtEnd = 14; //number of bounces * 2 -- must be even number!
var bounceMultiplier = 3; //multiplication factor for bounce-out px at end
var bounceSpeedMultiplier = 10; //multiplication factor for bounce-out ms at end


function setupAdSlider(){
var numcars = document.getElementById('WSsliderFilm').getElementsByTagName('TD').length;
if(numcars > 0){document.getElementById('sliderContainer').style.display = 'block';}

   var WSsliderFilm = document.getElementById('WSsliderFilm');
   var WSsliderWindow = document.getElementById('WSsliderWindow');
   var windowWidth = WSsliderWindow.offsetWidth;
   var slideFilmWidth = document.getElementById("sliderFilmTable").offsetWidth;
                    
   WSsliderFilm.style.left = '0px';
   WSsliderFilm.style.top = '0px';
   WSsliderFilm.style.width = slideFilmWidth + 'px';
   
    slideInterval = Math.floor(WSsliderFilm.scrollWidth / numcars);
    
    if(numcars > 3){document.getElementById('navArrowRight').style.display = 'block';}
    
   filmStartLeft = 0; // left pos of film when at start (update this if not starting at 0)
   filmEndLeft = WSsliderFilm.scrollWidth - windowWidth + 10; // left pos if film when at end

} // end setupAdSlider


function showBothNavArrows(){
   var navArrowLeft = document.getElementById('navArrowLeft');
   var navArrowRight = document.getElementById('navArrowRight');
   navArrowLeft.style.color="#000";
   navArrowRight.style.color="#000";
   
   navArrowLeft.style.display = 'block';
   navArrowRight.style.display = 'block';
} // end showBothNavArrows


function adSlide(direction){
   //determines new position for film slider, calls sliderFilmAnim to animate move

   var WSsliderFilm = document.getElementById('WSsliderFilm');
   var currentFilmLeft = 0;

   //get film slider left; convert to integer px values
      currentFilmLeft = WSsliderFilm.style.left;
      currentFilmLeft = currentFilmLeft.substring(0,currentFilmLeft.indexOf('px'));

   //determine the new left px for film slider
      if(direction == 'right'){
         newFilmLeft = currentFilmLeft * 1 - slideInterval;
      } // end if direction == right  
      else{
         newFilmLeft = currentFilmLeft * 1 + slideInterval;
      } // end else (direction: left);


   //determine if we should animate, or indicate to user that we're at the end of the film
  // if((newFilmLeft > 0) || (newFilmLeft < (-filmEndLeft))){
   //   alert('your at the end');
      //endOfFilmAnim(bouncesAtEnd);
   //   return;
   //}

    //call anim function with new position
      sliderFilmAnim(newFilmLeft);

    //update Navigation arrows
       var navArrowLeft = document.getElementById('navArrowLeft');
       var navArrowRight = document.getElementById('navArrowRight');
       if(newFilmLeft >= -1){ navArrowLeft.style.display = 'none';}
       if(newFilmLeft <= (-filmEndLeft) + 30){ navArrowRight.style.display = 'none';}
       
     //call anim function with new position
      sliderFilmAnim(newFilmLeft);     
         
         
} // end adSlide




function sliderFilmAnim(targetLeftPos){
   var WSsliderFilm = document.getElementById('WSsliderFilm');
   var goingUp = stay =  0;

   //get film slider left and top; convert to integer px values
      currentFilmLeft = WSsliderFilm.style.left;
      currentFilmLeft = currentFilmLeft.substring(0,currentFilmLeft.indexOf('px'));

   //determine which direction we're going
      if(currentFilmLeft < targetLeftPos) { goingUp = 1; }
      if(currentFilmLeft == targetLeftPos) { stay = 1;} // Stay!
      

   //determine next position (if not staying)
      if(!stay){
         newLeftPos = currentFilmLeft;
         if(goingUp){newLeftPos = newLeftPos * 1 +  slideAnimMoveBy;}
         else{newLeftPos = newLeftPos * 1 - slideAnimMoveBy;}
      }
     

   //determine if we're over target
      if((goingUp) && (newLeftPos > targetLeftPos)){  
         newLeftPos = targetLeftPos;
      }
      if((!goingUp) && (newLeftPos < targetLeftPos)){  
         newLeftPos = targetLeftPos;
       } 

   //assign new position to slider
      WSsliderFilm.style.overflow = 'hidden';
      newFilmLeftPx = newLeftPos + 'px';
      WSsliderFilm.style.left = newFilmLeftPx; 
//alert('setting to: ' + newFilmLeftPx);

    //determine if we're done, and if we're at the end of the film
      var doneWithAnim = 0;
      if (newLeftPos == targetLeftPos){ doneWithAnim=1; } // Are we done with this anim?

      if(doneWithAnim){ //if we're done with the animation, determine if we're at the end of the film & call end anim function
      //   if(newLeftPos == filmStartLeft){ endOfFilmAnim(bouncesAtEnd); }
      //   if(newLeftPos <= (-filmEndLeft)){ endOfFilmAnim(bouncesAtEnd); }
      ////   if(newLeftPos == filmStartLeft){  document.getElementById('arrowLeft').style.display = 'none'; }
      ////   if(newLeftPos <= (-filmEndLeft)){ document.getElementById('arrowRight').style.display = 'none'; }
  
      //if(navArrowLeft.style.display == 'none') { alert('bounce1'); }
      //if(navArrowRight.style.display == 'none') { alert('bounce2'); }
         return; //return regardless, becuase we're done with this animation
       }

   //recursive call for animation
     eval('   setTimeout("sliderFilmAnim(' + targetLeftPos + ')", ' + slideAnimInverval + ')   ');

} // end sliderFilmAnim





