var AdSponsorBlock = Class.create();

AdSponsorBlock.prototype =
{
    initialize: function(opt)
    {
        for (i in opt) {
            this[i] = opt[i];
        }
        this.current_ids = [0, 0];
        this.load();
    },

    load: function()
    {
        var obj = this;
        //alert( BASE_URL );
        $.ajax({
            url: BASE_URL+'/ad_sponsor_block',
            type: 'POST',
            data: {
                action: 'get_banner',
                'current_ids[0]': obj.current_ids[0],
                'current_ids[1]': obj.current_ids[1],
                id_type: obj.id_type,
                markets: obj.markets
            },
            beforeSend: function() {
            },
            dataType: 'json',
            error: function (request, textStatus, errorThrown) {
                //alert(textStatus +' ' + errorThrown);
            },
            success: function(data) {
                if (typeof data.list == 'object') {
                    obj.render(data.list, data.num_rows);
                    setTimeout('banner_block['+obj.index+'].load()', obj.timeInterval);
                }
            },
            complete: function(request, textStatus) {
            }
        });
    },

    render: function(list, num_rows)
    {
        this.container.empty();
        var itemContainerStart = '';
        var itemContainerEnd = '';
        var html = '';
        if (num_rows > 0) {
            html +='<table width="100%" height="100%"><tr>';
            itemContainerStart = '<td style="vertical-align:middle; text-align:center;">';
            itemContainerEnd = '</td>';
        }
        var item;
        var notifyUrl;
        for (var i in list) {
            item = list[i];
            notifyUrl = BASE_URL + '/sponsor_click?id=' + item.id;
            this.current_ids[i] = item.id;

            html += itemContainerStart
                  + '<a title="' + item.company + '" ';
            if (item.isUrl) {
                html += 'target="_blank" href="' + notifyUrl + '"';
            } else {
                html += 'href="mailto:' + item.url + '" onclick = "return qs.touchUrl(\'' + notifyUrl + '\');" ';
            }
            html += '>';
            html += '<img src="' + item.img + '"';
            if (typeof this.width == 'number') {
                html += ' width="' + this.width + '" ';
            }
            if (typeof this.height == 'number') {
                html += ' height="' + this.height + '" ';
            }
            html += '>'
                  + '</a>'
                  + itemContainerEnd;
        }
        if (num_rows > 0) {
            html +='</tr></table>';
        }
        this.container.html(html);
    }
};

$(document).ready(initAdSponsorBlock);

var banner_block = [];
function initAdSponsorBlock()
{

    $('div[sponsor_block]').each(function(){
        var index = banner_block.length;
        var opt = {
            index: index,
            timeInterval: 30000,
            container:$(this),
            id_type:$(this).attr('sponsor_block'),
            markets:$(this).attr('markets')
        };
        if (opt.id_type == '2') {
            opt.width = AD_BANNER_WIDTH;
            opt.height = AD_BANNER_HEIGHT;
        }
        banner_block[index] = new AdSponsorBlock(opt);
    });
}

