
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);
  }
});

FadingBanner = Class.create();
Object.extend(FadingBanner.prototype, {
  initialize: function(container, elements, interval, slice_size, container_static, sub_container_prefix, divider_div_class){
	this.halt = this.stop.bindAsEventListener(this);
	this.go = this.start.bindAsEventListener(this);

	this.container_elements = new Array();
	this.interval = interval;
	this.container = $(container);
	this.elements = $A(elements);
	this.elements.sort(function() {return 0.5 - Math.random();});
	this.current = 0;
	var idx = 0;
	this.elements.eachSlice(slice_size, function(sl){
		var d = Element.extend(document.createElement('div'));
		d.style.display = 'none'
		d.id = sub_container_prefix+"_"+idx;
		var cnt = 0;
		sl.each(function(el){
			d.appendChild(el);
			if (divider_div_class && cnt<(slice_size-1)) {
				new Insertion.Bottom(d, "<div class='"+divider_div_class+"'> </div>");
			}
			cnt = cnt+1;
		});
		this.container.appendChild(d);
		this.container_elements[idx] = d;
		idx = idx+1;
	}.bind(this));

	//adding the hover effect
	this.container.observe('mouseover', this.halt)
	this.container.observe('mouseout', this.go)

	$(container_static).hide();
	// this.container_elements[this.current].show();
	new Effect.Appear(this.container_elements[this.current], { queue: 'end', duration: 0.4 });
  },
  start: function(){
	if (this.container_elements.size()>1 && !this.pe) {
		this.pe = new PeriodicalExecuter(function(pe) {
		  	current_el = this.container_elements[this.current];
			this.current = (this.current+1)%this.container_elements.size();
		  	next_el = this.container_elements[this.current];
		  	new Effect.Fade(current_el, { queue: 'end', duration: 0.6 });
			new Effect.Appear(next_el, { queue: 'end', duration: 0.6 });
		}.bind(this), this.interval);		
	}
  },
  stop: function(){
	if(this.pe) {
		this.pe.stop();
		this.pe = null;		
	}
  }
});


