   var gadget = new function() {
    var boss = null;
    var last = null;
    var list = null;
    var skip = 0;
    var pager = null;
    var current = null;
    var img = null;
    var datasource = '';
    this.showloading = function() {
     list.setHTML('<img id="gadgetloading" src="' + img + '" />');
    }
    this.parse = function(text) {
     var resp = Json.evaluate(text);
     var ul = new Element('ul').addClass('videolist');
     if (!resp.count) return;
     for (var n = 0; n < resp.count; n++) {
      var item = resp.rows[n];
      var anc = new Element('a').setProperties({'href': item.href, 'tabindex': 0})
       .adopt(new Element('span').addClass('img').setHTML('<img src="' + item.headpic + '" />'))
       .appendText(' ')
       .adopt(new Element('span').addClass('text').setHTML('<strong>' + item.title + '<' + '/strong> ' + item.headline));
      anc.innerHTML += ' ' + item.ranking;
      ul.adopt(new Element('li').adopt(anc));
     };
     list.setHTML('').adopt(ul);
    }
    this.show = function(e) {
     gadget.showloading();
     var anc = wu.target(e);
     if (!anc || !anc.tagName || (anc.tagName.toLowerCase() != 'a')) anc = last;
     last.removeClass('current');
     last = $(anc);
     last.addClass('current');
     $(pager.getElementsByTagName('a').item(skip)).removeClass('current');
     $(pager.getElementsByTagName('a').item(0)).addClass('current');
     skip = 0;
     new Ajax(datasource + '?source=' + anc.source + '&skip=' + skip, {method: 'get', onComplete: gadget.parse}).request();
     return wu.stop(e);
    }
    this.prev = function(e) {
     if (skip < 1) return wu.stop(e);
     gadget.showloading();
     $(pager.getElementsByTagName('a').item(skip)).removeClass('current');
     skip--;
     $(pager.getElementsByTagName('a').item(skip)).addClass('current');
     new Ajax(datasource + '?source=' + last.source + '&skip=' + skip, {method: 'get', onComplete: gadget.parse}).request();
     return wu.stop(e);
    }
    this.next = function(e) {
     if (skip > 7) return wu.stop(e);
     gadget.showloading();
     $(pager.getElementsByTagName('a').item(skip)).removeClass('current');
     skip++;
     $(pager.getElementsByTagName('a').item(skip)).addClass('current');
     new Ajax(datasource + '?source=' + last.source + '&skip=' + skip, {method: 'get', onComplete: gadget.parse}).request();
     return wu.stop(e);
    }
    this.jump = function(e) {
     var anc = wu.target(e);
     if (!anc) return wu.stop(e);
     gadget.showloading();
     $(pager.getElementsByTagName('a').item(skip)).removeClass('current');
     skip = anc.skip;
     anc.addClass('current');
     new Ajax(datasource + '?source=' + last.source + '&skip=' + skip, {method: 'get', onComplete: gadget.parse}).request();
     return wu.stop(e);
    }
    this.createcontrol = function(label, psource) {
     var gadgetclass = 'gadget_'+psource;
     var anc = new Element('a').setProperties({'href': '#', 'tabindex': 0, 'class': gadgetclass}).addEvent('click', gadget.show);
     anc.adopt(new Element('span').appendText(label));
     if (psource == current) {
      anc.addClass('current');
      last = anc;
     };
     $extend(anc, { source: psource });
     return new Element('li').adopt(anc);
    }
    this.init = function(pcurrent, pimg, pdatasource) {
     current = pcurrent;
     datasource = pdatasource;
     img = pimg;
     boss = $('gadget');
     boss.adopt(new Element('ul').setProperty('id', 'gadgetcontrols')
      .adopt(gadget.createcontrol('friss', 'freshest'))
      .adopt(gadget.createcontrol('népszerű', 'mostpopular'))
      .adopt(gadget.createcontrol('legjobb', 'toprated'))
      .adopt(gadget.createcontrol('vitatott', 'mostdiscussed'))
     );
     list = new Element('div').setProperty('id', 'gadgetitems');
     boss.adopt(list);
     pager = new Element('ul').setProperty('id', 'gadgetpager');
     for (var n = 0; n < 9; n++) {
      var anc = new Element('a').setProperties({'href': '#', 'tabindex': 0}).appendText(parseInt(n + 1)).addEvent('click', gadget.jump);
      $extend(anc, { skip: n });
      pager.adopt(new Element('li').adopt(anc));
     };
     boss.adopt(new Element('a').addClass('pagerleft').setProperties({'href': '#', 'tabindex': 0}).appendText('előző').addEvent('click', gadget.prev));
     boss.adopt(new Element('a').addClass('pagerright').setProperties({'href': '#', 'tabindex': 0}).appendText('következő').addEvent('click', gadget.next));
     boss.adopt(pager);
     last.fireEvent('click');
    }
   }
   
  var wcs_scroller = new Class({
   moveleft: function(e) {
    var marginleft = this.container.getStyle('margin-left').toInt();
    if (marginleft > (0 - this.container.getStyle('width').toInt())) {
     var len = (0 - this.container.getStyle('width').toInt() + this.mainwidth + this.step);
     if ((len - marginleft) * - 1 < this.step) this.eff.start(marginleft, parseInt(len - this.step)); else this.eff.start(marginleft, parseInt(marginleft - this.step));
    };
    return wu.stop(e);
   },
   moveright: function(e) {
    var marginleft = this.container.getStyle('margin-left').toInt();
    if (marginleft < 0) this.eff.start(marginleft, Math.min(0, parseInt(marginleft + this.step)));
    return wu.stop(e);
   },
   initialize: function(mainobj) {
    this.ul = mainobj.getElements('.videolist')[0];
    this.container = mainobj.getElements('.scroll')[0];
    this.mainwidth = mainobj.getStyle('width').toInt();
    this.step = this.mainwidth / 2;
    var width = parseInt(parseInt(this.ul.getElementsByTagName('li').length) * 114);
    this.container.setStyle('width', width + 'px');
    if (width > this.mainwidth) {
     this.eff = new Fx.Style(this.container, 'margin-left', {duration: 100});
     mainobj
      .adopt(new Element('a').addClass('fleft').setProperties({'href': '#', 'tabindex': '0'}).appendText('jobbra').addEvent('click', this.moveright.bind(this)))
      .adopt(new Element('a').addClass('fright').setProperties({'href': '#', 'tabindex': '0'}).appendText('balra').addEvent('click', this.moveleft.bind(this)))
     ;
    };
   }
  });
