if (typeof xadv_styles == 'undefined')
{
  var xadv_styles = {};
  var xadv_slots_registred = {};
  var xadv_slots_shown = {};
}

function xadv_push(items, block_id)
  {
    var container = 'xadv_' + block_id;

    var div = document.createElement('div');
    document.getElementById(container).appendChild(div);

    var slot_id = container.split('_')[1];
    var slot = xadv_styles[slot_id];

    var html = '<table>';
    for (var row = 0; row != slot.ver; row++)
    {
      html += '<tr>';
        for (var cell=0; cell != slot.hor; cell++)
        {
          html += '<td>';
            var pos = (row*slot.hor) + (cell);
            if (items[pos]) {
                var item = items[pos];
                html += '<a class="img" href="' + item.link + '" target="_blank">';
                  if (slot.styles.resize_to)
                  {
                      html += '<img src="' + item.image + '" onmouseover="document.getElementById(\'xadv_tb_'+ slot_id + '_'+ pos +'\').style.display = \'\'">';
                      var big_img_src = item.image.replace(/_(50|75|100|150|200)\./, '_' + slot.styles.resize_to + '.');
                      html += '<img src="' + big_img_src + '" style="width: ' + slot.styles.resize_to + 'px; height: ' + slot.styles.resize_to + 'px; display: none" id="xadv_tb_'+ slot_id + '_'+ pos +'" class="img_big" onmouseout="this.style.display = \'none\'">';
                  }
                  else
                  {
                      html += '<img src="' + item.image + '">';
                  }
                html += '</a>';
                html += '<a href="' + item.link + '" target="_blank">' + item.title + '</a>';
                if (item.description) {
                    html += '<p>' + item.description + '</p>';
                }
            }
          html += '</td>';
        }
      html += '</tr>';
    }
    html += '</table>';
    html += '<style>';
    html +='#'+ container +' table{' + slot.styles.table + '}';
    html +='#'+ container +' tr{' + slot.styles.tr + '}';
    html +='#'+ container +' td{' + slot.styles.td + '}';
    html +='#'+ container +' .img{' + slot.styles.img + '}';
    html +='#'+ container +' a{' + slot.styles.a + '}';
    html +='#'+ container +' a:hover{' + slot.styles.a_hover + '}';
    html +='#'+ container +' p{' + slot.styles.p + '}';
    if (slot.styles.resize_to)
    {
        html +='#'+ container +' a.img {position: relative;}';
        html +='#'+ container +' a .img_big{position: absolute; z-index: 99999}';
        html +='#'+ container +' a .img_big{' + slot.styles.big_img + '}';
    }
      
    html += '</style>';

    div.innerHTML = html;

    xadv_slots_shown[block_id] = true;
  }

function xadv_prepare_slots()
{
//  if(xadv_slots_prepared)
//  {
//    return;
//  }

//  xadv_slots_prepared = true;

  if (typeof xadv_slots != 'undefined')
  {
    for(var i=0; i != xadv_slots.length; i++)
    {
      var settings = xadv_slots[i];
      var slot_id = 'xadv_' + settings.block_id;

      if (xadv_slots_shown[settings.block_id] || xadv_slots_registred[settings.block_id])
      {
          continue;
      }

      xadv_slots_registred[settings.block_id] = true;
        
      var s = document.createElement('script');
      s.type = 'text/javascript';
      s.src = 'http://adv.teaserboom.ru/blocks/' + settings.site_id + '/' + settings.block_id + '.js';
      document.getElementById(slot_id).appendChild(s);
    }
  }
}

function xadv_set_config(block_id, slot_config)
{
  xadv_styles[block_id] = slot_config;
  var slot_id = 'xadv_' + block_id;

  var banners_required = slot_config.hor * slot_config.ver;

  var s = document.createElement('script');
  s.type = 'text/javascript';
  s.src = 'http://adv.teaserboom.ru/1/?bc=' + banners_required + '&s=' + block_id + '&r=' + Math.random();
  document.getElementById(slot_id).appendChild(s);
}

xadv_prepare_slots();