var Widget = new Class({
	setOptions: function(options) {
		this.options = $extend({
      onComplete: Class.empty,
			onStart: Class.empty}, options || {});
	},
	initialize: function(el, ID, options){
		this.setOptions(options);
		this.elem = el;
    this.el = $(el);
		this.ID = ID;
		this.scroller = new Scroller(this.el, this.ID, {speed : this.options.speed, delay : this.options.delay, updown : this.options.updown});
		var scr = this.scroller;
    $(this.el).addEvent('mouseenter', function(event) {
		  this.scroller.pause();
	  }.bind(this));
    $(this.el).addEvent('mouseleave', function(event) {
		  this.scroller.resume();
	  }.bind(this));
	},
	
  updateContainer: function(feed) {
	    this.scroller.pause();
	    var myAjax = new Request.HTML({url: 'index.php/widgets', method: 'get', data:feed, onComplete: function(html){
      this.el.set('text', '');
      this.el.adopt(html);format();
      if (this.el.firstChild.innerHTML == '') {
          this.el.set('text', 'sorry, no results');
      } else 
      {
          if (feed.indexOf('searchterm=')<0)   {  
              $('navmenu'+this.ID).getFirst().getFirst().set('text', feed.split("=")[2]);
          }
          this.scroller.fx.cancel();
          this.scroller.initialize(this.el, this.ID, {speed : this.options.speed, delay : this.options.delay, updown : this.options.updown});
          this.el.getParent().setStyle("cursor", "auto");
      }
      }.bind(this)});
      myAjax.send();
      this.el.getParent().setStyle("cursor", "progress");
      return false;
  }
});//end class

var Scroller = new Class({
	setOptions: function(options) {
		this.options = $extend({
			speed: 1500,
			delay: 5000,
      onComplete: Class.empty,
			onStart: Class.empty
		}, options || {});
	},
	initialize: function(el,ID, options){
		this.setOptions(options);		
		this.elem = el;
		this.ul = this.elem.firstChild;
		this.myTimer = null;
		this.items = null;
		//this.items = this.ul.getElements('li');
		this.items = this.elem.getElements('li');
		var w = 0;
		var h = 0;
		w = this.elem.getSize().x;
		this.items.each(function(li,index) {
		     h += li.getSize().y;
		});
		var el1 = this.ul;
		var sp = this.options.speed
		this.ul.style.position = "absolute";
		this.ul.style.height = h;
		this.ul.style.top = '0';
		//this.ul.setStyles({position: 'absolute', top: 0, left: 0, width: w, height: h}); (did not work in IE)
		this.fx = new Fx.Morph(el1,{duration:sp, onComplete: function() {
       if (this.options.updown == 'up') {
          var fc = this.ul.firstChild.clone();
          this.ul.removeChild(this.ul.firstChild);
          fc.injectInside(this.ul);
          this.ul.setStyles({position: 'absolute', top: 0, left: 0, width: w, height: h});
       }
       this.working = false;
      }.bind(this)
    });
		
	if($('up'+ID)) {$('up'+ID).addEvent('click', function(event) {
		  event = new Event(event).stop();
		  this.options.updown = 'up';
		  this.resume();
     }.bind(this));
  }
  if($('down'+ID)) {$('down'+ID).addEvent('click', function(event) {
		  event = new Event(event).stop();
		  this.options.updown = 'down';
		  this.resume();
	  }.bind(this));
  };
	this.resume();
 },//end initialize

	pause: function() {
	    $clear(this.mytimer);
	    this.mytimer = null;
	},
	resume: function() {
	    if (!this.working) {
	    this.pause();
	        if (this.options.updown == 'up') {
              this.moveup();
          }
          else {
              this.movedown();          
	        }
	    }
	},
  moveup: function() {
		if (this.ul.firstChild.nextSibling) {
      this.working = true;
		  this.fx.start({
			  top: [ -this.ul.firstChild.nextSibling.offsetTop]
		  });
		  this.mytimer = this.moveup.bind(this).delay(this.options.delay+this.options.speed);
		}
	},
  movedown: function() {
		var lc = this.ul.lastChild;		
		  this.ul.removeChild(lc);
		  lc.injectBefore(this.ul.firstChild);
      this.ul.setStyles({top: -this.ul.firstChild.nextSibling.offsetTop});
      this.working = true;
			this.fx.start({
			  top: [-this.ul.firstChild.nextSibling.offsetTop, 0]
		  });
		  this.mytimer = this.movedown.bind(this).delay(this.options.delay+this.options.speed);
	}
});

