RotatingBanner = Class.create({
   cl: null, // currentLeft
   cr: null, // currentRight
   nr: null, // nextLeft
   nl: null, // nextRight
   ctl: null,  // count (left)
   ctr: null,  // count (right)
   d: null,  // delay, milliseconds

   initialize: function(seconds) {
      //this.d = seconds*1000;
      this.d = 5000;

      // show a random large banner
      var largeCount = $('rotatingBanner').down('div.largeWrapper').select('div.largeArticle').length;
      if (largeCount > 0) {
         var randomNumber = Math.floor(Math.random()*largeCount);
         $('rotatingBanner').down('div.largeWrapper').down('div.largeArticle', randomNumber).removeClassName("hidden");
      }


      // count small banners (left)
      this.ctl = $('rotatingBanner').select('div.smallBanner.left').length;
      // count small banners (right)
      this.ctr = $('rotatingBanner').select('div.smallBanner.right').length;

      this.cl = 0;
      this.cr = 0;
      this.nl = (this.cl+1)%this.ctl;
      this.nr = (this.cr+1)%this.ctr;
      
      // set up interval
      setTimeout("rotatingBanner.startInterval('left')", 1);
      setTimeout("rotatingBanner.startInterval('right')", 2501);

      setTimeout("rotatingBanner.changeBanner('left')", 10);
      setTimeout("rotatingBanner.changeBanner('right')", 2510);
        

      $$('div.smallBanner').each(function (element) {
    	  element.setOpacity(0);
      });  
      
      $('rotatingBanner').down("div.smallBanner.left", this.cl).setOpacity(1);
      $('rotatingBanner').down("div.smallBanner.right", this.cr).setOpacity(1);      
            
   },
   
   startInterval: function(position) {
	  setInterval("rotatingBanner.changeBanner('"+position+"')", this.d); 	   	   
   },
  
   changeBanner: function(position) {	  	  
	  
      // refresh values current/next
	  
	   if(position == "left") {
			  var fromBanner = $('rotatingBanner').down("div.smallBanner.left", this.cl);
			  var toBanner = $('rotatingBanner').down("div.smallBanner.left", this.nl);
			  
		      new Effect.Opacity(fromBanner, {
		          to: 0.0,
		          from: 1.0,
		          duration: 0.5,		          
		          afterSetup: function() { // Etter oppsett, men før selve animasjonen settes i gang
		        	  //fromBanner.show();
		          },          
		          afterFinish: function() { // Etter at hele animasjonen er gjennomført.
		        	  fromBanner.hide();
		        	  //fromBanner.setOpacity(1.0);
		          }
		          
		      })
		      
        	  new Effect.Opacity(toBanner, {
		          to: 1.0,
		          from: 0.0,        		  
        		  duration: 0.5,
		          afterSetup: function() { // Etter oppsett, men før selve animasjonen settes i gang
	        	  	  toBanner.show();
	          	  },          
	              afterFinish: function() { // Etter at hele animasjonen er gjennomført.
	        	      //fromBanner.hide();
	        	      //fromBanner.setOpacity(1.0);
	              }
        	  })
		      
		      
		      
		      this.cl = this.nl;
			  this.nl = (this.cl+1)%this.ctl;      
		   
	   } else if (position == "right") {
		   
		   
			  var fromBanner = $('rotatingBanner').down("div.smallBanner.right", this.cr);
			  var toBanner = $('rotatingBanner').down("div.smallBanner.right", this.nr);
			  
		      new Effect.Opacity(fromBanner, {
		          to: 0.0,
		          from: 1.0,
		          duration: 0.5,		          
		          afterSetup: function() { // Etter oppsett, men før selve animasjonen settes i gang
		        	  //fromBanner.show();
		          },          
		          afterFinish: function() { // Etter at hele animasjonen er gjennomført.
		        	  fromBanner.hide();
		        	  //fromBanner.setOpacity(1.0);
		          }
		          
		      })
		      
        	  new Effect.Opacity(toBanner, {
		          to: 1.0,
		          from: 0.0,        		  
        		  duration: 0.5,
		          afterSetup: function() { // Etter oppsett, men før selve animasjonen settes i gang
	        	  	  toBanner.show();
	          	  },          
	              afterFinish: function() { // Etter at hele animasjonen er gjennomført.
	        	      //fromBanner.hide();
	        	      //fromBanner.setOpacity(1.0);
	              }
        	  })
		      
		      this.cr = this.nr;
			  this.nr = (this.cr+1)%this.ctr;      
		   
	   }  	
	  
   }

});
var rotatingBanner;
