Effect.SlideRightOutOfView = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false, 
      scaleY: false, 
      restoreAfterFinish: true,
      afterFinishInternal: function(effect) {
        effect.element.remove().undoClipping();
      } 
    }, arguments[1] || {})
  );

}

SlidingBanner = Class.create();
Object.extend(SlidingBanner.prototype, {
  initialize: function(interval, pagesize, pagecount, containerelement, newpageurl, newpageparam, startpage, newstyle){
	this.interval = interval;
	this.page_size = pagesize;
    this.page_count = pagecount; 
	this.container_element = $(containerelement); 
	this.new_page_url = newpageurl; 
	this.new_page_param = newpageparam;
	this.current_page = startpage;
	this.newstyle = newstyle;
  },
  play: function(){
	new PeriodicalExecuter(function(pe) {
	  childs = $A(this.container_element.getElementsByClassName('slidingElement'));
	  if (childs.size()<(2*this.page_size)) {
		if (this.current_page==this.page_count) {
			this.current_page=0
		}
		this.current_page=this.current_page+1;
		req = this.new_page_url+"?"+this.new_page_param+"="+this.current_page;
		if (this.newstyle) {
			req = req+"&newstyle=1";
		}
		new Ajax.Request(req, {
		  method: 'get',
		  onSuccess: function(transport) {
			new Insertion.Bottom(this.container_element, transport.responseText);
		  }.bind(this)
		});		
	  }
	  var el = childs.find(function(e){
					return !e.hasClassName('fixed_banner');
			   });
	  new Effect.SlideRightOutOfView(el);
	}.bind(this), this.interval);
  }
});

SlidingStrip = Class.create();
Object.extend(SlidingStrip.prototype, {
  initialize: function(interval, containerelement, randomize, to_remove_on_start){
	
	// setup the callbacks
	this.pe_callback = this.append_to_container.bind(this);
	this.halt = this.stop.bindAsEventListener(this);
	this.go = this.start.bindAsEventListener(this);
	
	this.interval = interval;
	this.container = $(containerelement); 
	var childs = $A(this.container.getElementsByClassName('slidingElement'));
	
	//reordering the sliding elements
	if (randomize) {
		childs.sort(function() {return 0.5 - Math.random();});
		childs.each(function(e) {
			this.container.appendChild(e);			
		}.bind(this));
	}
	
	
	if ($(to_remove_on_start)) {
		new Effect.SlideRightOutOfView($(to_remove_on_start));
	}
  },
  animate: function(){
	//adding the hover effect
	this.container.observe('mouseover', this.halt)
	this.container.observe('mouseout', this.go)
	this.start();
  },
  start: function(){
	if (!this.pe) {
			this.pe = new PeriodicalExecuter(function(pe) {
			  var childs = $A(this.container.getElementsByClassName('slidingElement'))
			  new Effect.SlideRightOutOfView(childs[0], { afterFinishInternal: this.pe_callback });
			}.bind(this), this.interval);		
		}
  },
  stop: function(){
	if(this.pe) {
		this.pe.stop();
		this.pe = null;		
	}
  },
  append_to_container: function(effect) {
	this.container.appendChild(effect.element);
  }
});


