// Blog Image Mover script
//
// This code is specific to the id values used in the blog template
//
// 2008-12-28T12:06:54PST -- A couple of minor fixes to eliminate warnings in Firefox
//   error console. Stuff like adding 'var' in front of a variable.

var com=com||{};
com.YourFriendPaul=com.YourFriendPaul||{};
//com.YourFriendPaul.blogImgMover=com.YourFriendPaul.blogImgMover||{};
com.YourFriendPaul.blogLogoChanger=com.YourFriendPaul.blogLogoChanger||{};

(function( $ ){

   // new int that is between min and max, inclusive.
   function RandomIntIncl( minInt, maxInt )
   {
      var offset  = 0;
      if ( minInt < maxInt ){
         var amt = maxInt - minInt + 1;
         offset = Math.floor( Math.random() * ( amt ));
      }
      minInt += offset;
      return( minInt );
   }
   
   /* ************************************************************ */
   /* ************************************************************ */
   // Functions for BlakeBlog Logo mover 
   /* ************************************************************ */
   /* ************************************************************ */
   var logoTimer = {};     // timer variables
   var imgNames = [];      // location of all possible images
   var imgObjs = [];       // image objects for the imgNames
   var imgId = "logoImg";  // default image to change
   var timeoutId = null;
   var runTimer = true;    // don't allow timeoutId to change if we're trying to stop the timer
      
   function buildImgObjs( names ){
   
      var iCount = 0;
      var obs = [];
      var nameLen = names.length;
      
      for( iCount = 0; iCount < nameLen; iCount++ ){
      
         obs[ iCount ] = new Image();
         obs[ iCount ].src = names[ iCount ];
      }
      
      return obs;
   }
   
   // make an array of image names based on the current image
   function defaultImageNames( divId ){
   
      var imgNames = [];
      divId = divId || "logoImg";
      var logoImg = document.getElementById( divId );
      
      if ( logoImg !== null ){
      
         // try to build a path based on the original image
         // (needed for wordpress varying page)
         var baseImage = logoImg.src;
         var pos = baseImage.lastIndexOf( '/' );
         
         if( -1 !== pos ){
            baseImage = baseImage.slice( 0, pos ) + '/';
         }

         else {
         
            baseImage = 'image/';
         }

         imgNames[0] = baseImage + "BB-BG only.gif";         // just the background image
         imgNames[1] = logoImg.src;
         imgNames[2] = baseImage + "BlakeBlog2.gif";
         imgNames[3] = baseImage + "BlakeBlog3.gif";
         imgNames[4] = baseImage + "BlakeBlog4.gif";
         imgNames[5] = baseImage + "BlakeBlog5.gif";
         imgNames[6] = baseImage + "BlakeBlog6.gif";
         imgNames[7] = baseImage + "BlakeBlog7.gif";
         imgNames[8] = baseImage + "BlakeBlog8.gif";
         imgNames[9] = baseImage + "BlakeBlog9.gif";
      }
      
      return imgNames;
   }

   $.loop = function(  ){
   
      var logoImg = document.getElementById( imgId );
      var imgNum = RandomIntIncl( 0, imgNames.length - 1 );      // select image number

      logoImg.src = imgObjs[ imgNum ].src;

      if ( 0 === imgNum ) { 

         logoTimer.nextTime = RandomIntIncl( 5, 20 ); // display blank screen for shorter time
      }

      else {

         logoTimer.nextTime = RandomIntIncl( logoTimer.minDelay, logoTimer.maxDelay );
      }

      // avoid changing timeoutId if we want to cancel
      if( runTimer === true ){
      
         timeoutId = setTimeout( $.loop, logoTimer.nextTime * 100 );
      }
   };
   
   
   $.begin = function( inImgNames, divId ){
   
      imgId = divId || imgId;
      
      // make sure it is an array with elements
      if( typeof inImgNames === 'object' && inImgNames.length > 1 ){
      
         imgNames = inImgNames;
      }
      
      else{
      
         imgNames = defaultImageNames();
      }

      var logoImg = document.getElementById( imgId );
      
      if ( logoImg !== null ){
      
         imgObjs = buildImgObjs( imgNames );
         logoTimer.minDelay = 20; // 2 sec
         logoTimer.maxDelay = 120; // 12 sec
         logoTimer.nextTime = RandomIntIncl( 10, 50 );
         runTimer = true;
         timeoutId = setTimeout( $.loop, logoTimer.nextTime * 100 );
      }
   };
   
   $.end = function(){
   
      runTimer = false; // no race condition for timeout

      if( timeoutId !== null ){

         clearTimeout( timeoutId );
         timeoutId = null;
         //alert( 'timer stopped' );
      }
   };
   
   
   

})( com.YourFriendPaul.blogLogoChanger );


