function diAd(block_id, ids_ar, instance_name, timeout)
{
  this.block_id = block_id;
  this.ar = [];
  this.instance_name = instance_name;
  this.selected_idx = -1;
  this.screen = _ge('ad_screen['+block_id+']');
  this.screen_size = {w: this.screen.offsetWidth, h: this.screen.offsetHeight};
  this.block = _ge('ad_block['+block_id+']');
  this.timer_id = 0;
  this.auto = true;

  this.settings = {
    timeout: 5000,
    border_width: 2,
    height_difference: 54,
    h1: 50,
    h2: 104,
    padding1: {top: 15, bottom: 10},
    padding2: {top: 17, bottom: 10},
    h1_last: 0,
    h2_last: 0
  };

  this.set_timeout = function(secs)
  {
    secs *= 1;

    if (secs)
    {
      this.settings.timeout = secs * 1000;
    }
  }

  this.calc_settings = function(ar_count)
  {
    //this.settings.h1 = Math.round((this.screen_size.h - (ar_count - 2) * this.settings.border_width) / (ar_count + 1));
    //this.settings.h2 = (this.settings.h1 + this.settings.border_width) * 2;
    //this.settings.height_difference = this.settings.h1 + this.settings.border_width * 2;

    switch (ar_count)
    {
      case 4:
        this.settings.h1 = 60;
        this.settings.h2 = 124;
        this.settings.height_difference = 64;
        //this.settings.h1_last = 64;
        //this.settings.h2_last = 118;
        break;
      case 3:
        this.settings.h1 = 75;
        this.settings.h2 = 154;
        this.settings.height_difference = 79;
        break;
      case 2:
        this.settings.h1 = 100;
        this.settings.h2 = 204;
        this.settings.height_difference = 104;
        break;
    }

    this.settings.h1_w_padding = is_ie4up ? this.settings.h1 : this.settings.h1 - this.settings.padding1.top - this.settings.padding1.bottom - this.settings.border_width;
    this.settings.h2_w_padding = is_ie4up ? this.settings.h2 : this.settings.h2 - this.settings.padding2.top - this.settings.padding2.bottom;
    this.settings.h1_last_w_padding = is_ie4up ? this.settings.h1_last : this.settings.h1_last - this.settings.padding2.top - this.settings.padding2.bottom;
    this.settings.h2_last_w_padding = is_ie4up ? this.settings.h2_last : this.settings.h2_last - this.settings.padding2.top - this.settings.padding2.bottom;

    return false;
  }

  this.set_auto = function(state)
  {
    if (state)
    {
      this.auto = true;
      this.set_timer_to_next();
    }
    else
    {
      this.auto = false;
      this.stop_timer();
    }
  }

  this.set_timer_to_next = function()
  {
    var idx = this.selected_idx + 1;
    if (idx > this.ar.length - 1) idx = 0;

    this.timer_id = setTimeout(this.instance_name+'.select('+idx+',true)', this.settings.timeout);
  }

  this.stop_timer = function()
  {
    clearTimeout(this.timer_id);
  }

  this.select = function(idx,auto_selected)
  {
    if (!this.auto && auto_selected)
    {
      this.stop_timer();
      return;
    }

    if (!auto_selected) this.set_auto(false);

    if (idx == this.selected_idx) return;

    //var old_selected_idx = this.selected_idx;

    this.unselect(this.selected_idx);

    this.ar[idx].e2.style.display = 'block';
    this.selected_idx = idx;

    // old
    //this.screen.style.backgroundImage = 'url('+this.ar[idx].pic+')';
    // new
    set_pic_smoothly(this.ar[idx].pic);

    this.screen.href = this.ar[idx].href;

    /*
    eval("delEvent(this.screen, 'click', function(event) { " + this.ar[old_selected_idx].onclick + " });");

    if (this.ar[idx].onclick)
    {
      eval("addEvent(this.screen, 'click', function(event) { " + this.ar[idx].onclick + " });");
    }
    */

    //this.ar[0].e.style.borderTopWidth = idx != 0 ? '0' : '2px';
    //this.ar[0].e.style.borderTopWidth = '0px';

    //var hd = this.settings.height_difference;

    this.calc_settings(this.ar.length);

    for (var i = 0; i < this.ar.length; i++)
    {
      this.ar[i].e.style.marginTop = i == idx + 1 ? this.settings.height_difference+'px' : '0';
    }

    if (auto_selected) this.set_auto(true);
  }

  this.unselect = function(idx)
  {
    this.ar[idx].e2.style.display = 'none';
  }

  this.set_height_difference = function(h)
  {
    this.settings.height_difference = h;
  }

  this.calc_settings(ids_ar.length);
  if (timeout) this.set_timeout(timeout);

  var d = document.images;
  if (d) d.diIMG = [];

  var total_h = 0;

  for (var i = 0; i < ids_ar.length; i++)
  {
    this.ar[i] = {
      id:ids_ar[i].id,
      e:_ge('ad_row['+ids_ar[i].id+']'),
      e2:_ge('ad_selected_row['+ids_ar[i].id+']'),
      pic:ids_ar[i].pic,
      href:ids_ar[i].href,
      onclick:false
    };

    if (ids_ar[i].onclick)
      eval('this.ar[i].onclick = function() {'+ids_ar[i].onclick+'};');

    if (i == 1)
      this.ar[i].e.style.marginTop = this.settings.height_difference+'px';

    var h1 = this.settings.h1_w_padding;
    var h2 = this.settings.h2_w_padding;

    if (i == ids_ar.length - 1 && this.settings.h1_last)
      h1 = this.settings.h1_last_w_padding;

    if (i == ids_ar.length - 1 && this.settings.h2_last)
      h2 = this.settings.h2_last_w_padding;
      //h1 = 43;
      //h1 = this.screen_size.h - total_h - (this.settings.h1 - this.settings.h1_w_padding) + this.settings.border_width;

    this.ar[i].e.style.height = h1+'px';
    this.ar[i].e2.style.height = h2+'px';

    total_h += i == 0 ? this.settings.h2 : this.settings.h1;

    if (this.selected_idx == -1)
      this.selected_idx = i;

    if (d)
    {
      d.diIMG[d.diIMG.length] = new Image();
      d.diIMG[d.diIMG.length - 1].src = this.ar[i].pic;
    }

    eval('addEvent(this.ar[i].e, "mouseover", function(){'+this.instance_name+'.select('+i+',false);});');
  }

  eval('addEvent(this.block, "mouseover", function(){'+this.instance_name+'.set_auto(false);});');
  eval('addEvent(this.block, "mouseout", function(){'+this.instance_name+'.set_auto(true);});');

  this.set_timer_to_next();
}

function diad_onclick(instance_name)
{
  var a = true;

  eval('if ('+instance_name+'.ar['+instance_name+'.selected_idx].onclick) a = '+instance_name+'.ar['+instance_name+'.selected_idx].onclick();');

  return a;
}

