if (typeof xadv_styles == 'undefined')
{
  var xadv_styles = {};
  var xadv_slots_prepared = false;
}

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 href="' + item.link + '" target="blanck"><img src="' + item.image + '"></a>';
                html += '<a href="' + item.link + '" target="blanck">' + 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 + '}';
    html += '</style>';

    div.innerHTML = html;
  }

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;

      var s = document.createElement('script');
      s.type = 'text/javascript';
      s.src = 'http://ad.3fecta.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://ad.3fecta.ru/1/?bc=' + banners_required + '&s=' + block_id + '&r=' + Math.random();
  document.getElementById(slot_id).appendChild(s);
}

xadv_prepare_slots();