/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Scragar | Licensed under: Public Domain
 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Banner 1

var myBanner1 = new BannerRotator();

myBanner1.add('http://www.rostrasport.com/', '/images/expo/left_baner_1/left_image_00.gif');
myBanner1.add('http://www.vestipb.ru/', '/images/expo/left_baner_1/left_image_15.gif');
myBanner1.add('http://www.portnews.ru/', '/images/expo/left_baner_1/left_image_17.jpg');
myBanner1.add('http://www.transteka.ru/', '/images/expo/left_baner_1/left_image_18.jpg');
myBanner1.add('http://www.mirpress.ru/mirpress_wor.php', '/images/expo/left_baner_1/left_image_19.gif');
myBanner1.add('http://www.mirbez.ru/', '/images/expo/left_baner_1/left_image_16.gif');
myBanner1.add('http://www.rzd-partner.ru/', '/images/expo/left_baner_1/left_image_01.gif');
myBanner1.add('http://www.rostrasport.com/', '/images/expo/left_baner_1/left_image_02.gif');
myBanner1.add('http://www.neftegaz.ru/', '/images/expo/left_baner_1/left_image_03.gif');
myBanner1.add('http://morspb.ru/', '/images/expo/left_baner_1/left_image_04.gif');
myBanner1.add('http://www.transportweekly.com/', '/images/expo/left_baner_1/left_image_05.jpg');
myBanner1.add('http://www.mbsz.ru/', '/images/expo/left_baner_1/left_image_06.gif');
myBanner1.add('http://www.infomirspb.ru/', '/images/expo/left_baner_1/left_image_07.gif');
myBanner1.add('http://www.bdi.spb.ru/', '/images/expo/left_baner_1/left_image_08.gif');
myBanner1.add('http://www.algoritm.org/', '/images/expo/left_baner_1/left_image_09.jpg');
myBanner1.add('http://www.proectv.ru/', '/images/expo/left_baner_1/left_image_10.gif');
myBanner1.add('http://transteka.ru/', '/images/expo/left_baner_1/left_image11.jpg');
myBanner1.add('http://www.sitr.ru/', '/images/expo/left_baner_1/left_image_12.gif');
myBanner1.add('http://www.transportrussia.ru/', '/images/expo/left_baner_1/left_image_13.jpg');
myBanner1.add('http://www.s-ng.ru/', '/images/expo/left_baner_1/left_image_14.gif');

myBanner1.timer = 3;// 5 secs between images

// Banner 2

var myBanner2 = new BannerRotator();

myBanner2.add('http://www.groteck.ru/', '/images/expo/left_baner_2/left_image_101.gif');
myBanner2.add('http://http://www.abnews.ru/?p=novosti91/', '/images/expo/left_baner_2/left_image_111.gif');
myBanner2.add('http://www.safeprom.ru/', '/images/expo/left_baner_2/left_image_103.gif');
myBanner2.add('http://www.ati.su/', '/images/expo/left_baner_2/left_image_104.gif');
myBanner2.add('http://www.transport.ru/', '/images/expo/left_baner_2/left_image_105.gif');
myBanner2.add('http://comtrans-expo.ru/', '/images/expo/left_baner_2/left_image_106.gif');
myBanner2.add('http://www.transserver.ru/', '/images/expo/left_baner_2/left_image_107.jpg');
myBanner2.add('http://www.specserver.com/', '/images/expo/left_baner_2/left_image_108.gif');
myBanner2.add('http://www.gruzit.ru/', '/images/expo/left_baner_2/left_image_109.gif');
myBanner2.add('http://www.ekskovator.ru/', '/images/expo/left_baner_2/left_image_110.jpg');
myBanner2.add('', '/images/expo/left_baner_2/left_image_102.gif');

myBanner2.timer = 4;// 6 secs between images


// Set-up display

addLoadEvent(function(){
  myBanner1.bind('banner1');// match to ID of element.
  myBanner1.startTimer();

  myBanner2.bind('banner2');// match to ID of element.
  myBanner2.startTimer();
});


function BannerRotator(){
// first, defaults:
  this.timer = 3;
  this.bannerNum = 0;// -1 = random
// then normal init work
  this.banners = [];
  this.binding = null;
  this.timeout = null;

  this.add = function(){// add a banner to the array
    this.banners[this.banners.length] = [arguments[0], arguments[1]];
  }

  this.bind = function(){// bind to an element
    if(typeof arguments[0] == 'string')
      this.binding = document.getElementById(arguments[0]);
    else
      this.binding = arguments[0];
    this.rotate();
  }

  this.rotate = function(){// the actual image rotator
    if( ! this.empty())
      return;
    var showNum, tmpA = document.createElement('a'), tmpImg = document.createElement('img');

    if(this.bannerNum < 0)// random
      showNum = Math.floor(Math.random()*this.banners.length);
    else// syncronous
      showNum = this.bannerNum=(++this.bannerNum >= this.banners.length)?0:this.bannerNum;

    tmpA.href = this.banners[showNum][0];
    tmpImg.src = this.banners[showNum][1];
    tmpA.appendChild(tmpImg);
    this.binding.appendChild(tmpA);
  }

  this.empty = function(){// empty the element if possible
    if(this.binding == null)
      return false;
    while(this.binding.hasChildNodes())
      this.binding.removeChild(this.binding.firstChild);
    return true;
  }

  this.startTimer = function(){// start the loop, normaly done during page load.
    this.stopTimer();
    this.timeout = window.setInterval(
      (function(x){
        return function(){
          x.rotate();
        }
      })(this), this.timer*1000);
  }

  this.stopTimer = function(){// stop the loop, nice to make available via a button.
    if(this.timeout != null)
      window.clearInterval(this.timeout);
    this.timeout = null;
  }
}


