﻿// JScript File
//This scrips requires two copies of each image, the odd number be color and the even be black and white

var ColorTimer;
var BWTimer;
var Runs = 1;
var ColorSwitchingSpeed = 3000;
var BWSwitchingSpeed = 10000; 
var FadeSpeed = 2000;    

function PickAnyImage()
{
  //remove any active one
  var $active = $('#slider IMG.active');
  if ( $active.length == 0 )
      $active.removeClass('active');
      
  //find any image and find out how many siblings it has    
  var $sibs  = $('#slider IMG').siblings();

  //remove the last siblim, we will add it later
  var $Numb_sibs =$sibs.length -1; 

  //pick any of the siblings and set it to active
  var index = Math.floor(Math.random()*$Numb_sibs);
   
 //add a siblim if we have an even number
 //all color images are odd numbers
  if (index%2 == 0) 
  {
    index = index+1;
  }
    
   $('#slider IMG:nth-child('+index+')').addClass('active');
}


function slideBlackNWhiteSwitch() {
  try
  {
    var $active = $('#slider IMG.active');

    if ( $active.length == 0 )
    {
       PickAnyImage(); 
       $active = $('#slider IMG.active');
       //$active = $('#slider IMG:last');
    }

    // pull the next image which should be black and white
    var $next =  $active.next().length ? $active.next(): $('#slider IMG:first');

    $active.addClass('last-active');//.animate({opacity : 0.0}, 2000);

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, FadeSpeed, function() {
            $active.removeClass('active last-active');
        });


     clearInterval(BWTimer);
    ColorTimer= setInterval( "slideColorSwitch()", ColorSwitchingSpeed );     

  }
  catch(err)
  {
    alert(err.description);

  }
}

function slideColorSwitch() {
  try
  {
    var $active = $('#slider IMG.active');

    if ( $active.length == 0 )
    {
       PickAnyImage(); 
       $active = $('#slider IMG.active');
       //$active = $('#slider IMG:last');
    }

  //find any image and find out how many siblings it has    
  var $sibs  = $('#slider IMG').siblings();

  //remove the last siblim, we will add it later
  var $Numb_sibs =$sibs.length -1; 

  //pick any of the siblings and set it to active
  var index = Math.floor(Math.random()*$Numb_sibs);
   
 //add a siblim if we have an even number
 //all color images are odd numbers
  if (index%2 == 1) 
  {
    index = index+1;
  }
  
     var $next  = $( $sibs[ index ] );
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, FadeSpeed, function() {
            $active.removeClass('active last-active');
        });


     clearInterval(ColorTimer); 
    BWTimer= setInterval( "slideBlackNWhiteSwitch()", BWSwitchingSpeed );     

  }
  catch(err)
  {
    alert(err.description);
  }
}

//background-color: #75941f;
$(function () {

    PickAnyImage();
    ColorTimer = setInterval("slideColorSwitch()", ColorSwitchingSpeed);

});





