var timer;

function RotateFlash()
{  
  // if there are less than 4 banners, do not rotate
  if(parseInt(fCount) < 4) return;
  
  var loaded = 1;
  for(var i=0; i < 3; i++)
  {
      var objFlash = document.getElementById("flash" + i);
      if(objFlash == null) loaded = 0;
      if(objFlash == 'undefined') loaded = 0;
  }
      
  // we wait until all the three flash objects are loaded on the page    
  if(loaded == 0)     
  {   
     setTimeout( "RotateFlash()", 1000 );     // try again in a second
  }
  else
     timer = setTimeout("Refresh(" + nextIndex.valueOf() + "," + fCount.valueOf() + ")", fInterval.valueOf());
}

function Refresh(n, count)
{
     for(var i=0; i < 3; i++)
     {
         var objFlash = document.getElementById("flash" + i);
         if(objFlash == null) return;
         if(objFlash == 'undefined') return;
         
         var outerHTML = replaceSubstring(objFlash.outerHTML, objFlash.src, flashArray[parseInt(n)]);
         n = parseInt(n) + 1;
         if(parseInt(n) > parseInt(count) - 1)
            n = 0;
            
         objFlash.outerHTML = outerHTML;    
     }
     
     nextIndex = parseInt(n);
     timer = setTimeout("Refresh(" + nextIndex.valueOf() + "," + count.valueOf() + ")", fInterval.valueOf());
  
}

//show prev set of banners
function ShowPrev()
{
   //debugger;
   clearTimeout(timer);
   nextIndex = parseInt(nextIndex) - 6;
   while(parseInt(nextIndex) < 0) 
       nextIndex = parseInt(nextIndex) + parseInt(fCount);
   
   Refresh(nextIndex, fCount);

}

//show next set of banners
function ShowNext()
{
   clearTimeout(timer);

/*
   nextIndex = parseInt(nextIndex) + 3;
   if(parseInt(nextIndex) > parseInt(fCount) - 1) 
       nextIndex = parseInt(nextIndex) - parseInt(fCount);
*/   
   Refresh(nextIndex, fCount);
}

function replaceSubstring(inputString, fromString, toString) 
{
   
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { 
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { 
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
            
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } 
      
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } 
   return temp; 
} 