/*
 * File: mog_swtchr.js
 * Description: image switcher, slider or rotator
 * Created: 2010.08.08
 * Author: Ed Moghis
 * Email: emoghis@yahoo.com
 * Business: naai.com@gmail.com
 * Cell : +91 925.890.1328
 * Tele:  +91 650.654.3590
*/

/* NOTE: This depends on AJAX routine in Utils.js */

/* 
   To use this image rotator, you will need to use the PHP AJAX routine that comes with it.
   Call  mog_swtchr() to start it running and mog_swtchr_kill() to stop it.
   For now only 1 switcher can be running at a time so a 2nd call to switcher will kill 1st one
*/

// Global vars used to keep track of 
var _mog_swtchr_tmr = 0;		// Timer reference
var _mog_swtchr_idx = 0;		// Current index into arrays (this will change to 1 when we start)
var _mog_swtchr_div = '';		// Div ID where images are being displayed
var _mog_swtchr_url = '';
var _mog_swtchr_max = 0;		// If max != 0 then when that many images have been show , it stops


// Start routine
function mog_swtchr($url, $target_DivS, $switch_rate, $swtchr_max) {
   if ( _mog_swtchr_tmr != 0) {
      clearTimeout(_mog_swtchr_tmr);
   }

   _mog_swtchr_div = $target_DivS;	// Save these for later
   _mog_swtchr_url = $url;
   _mog_swtchr_max = $swtchr_max;
   
   _mog_swtchr_idx = 0;			// Note this is both a flag (=0 means not init) & counter
   _mog_swtchr_tmr = setInterval("mog_swtchr_nxt()", $switch_rate);
   
   mog_swtchr_nxt();			// Start with 1st picture

   return _mog_swtchr_tmr;
}

// Timer call back routine
function mog_swtchr_nxt() {
   _mog_swtchr_idx++;			// Next image index
   
   if ( _mog_swtchr_max != 0 ) {       	// If max repetitions set (non zero)
      if ( _mog_swtchr_idx > _mog_swtchr_max ) {
         clearTimeout(_mog_swtchr_tmr);	// stop if > max pictures shown
         return;
      }
   }
   
   // Request next picture
   $url = _mog_swtchr_url + "?index=" +_mog_swtchr_idx 
   AJAXrequest($url, _mog_swtchr_div);
   
   return;
}

// This is utility for the use of the caller to stop mog_swtchr
function mog_swtchr_kill() {
   clearTimeout(_mog_swtchr_tmr);
   return;
}
   


