
/**
 * @library RCounter
 */
function RCounter(counterId) {
	this.counterId = counterId;
}

RCounter.prototype.getUrl = function()
{
	var src = 'http://counter.rollad.ru/?cid=' + this.counterId + '&referrer=' + this.getReferrer() + '&location=' + this.getLocation();
	return src;
}

RCounter.prototype.getReferrer = function()
{
	return encodeURIComponent(document.referrer);
}

RCounter.prototype.getLocation = function()
{
	return encodeURIComponent(top.location.href);
}

RCounter.prototype.checked = function()
{
	var image = document.getElementById('rcounter-' + this.counterId)
	if(!image) {
		return true;
	}

	return false;
}

RCounter.prototype.load = function()
{
	if(this.checked()) {
		document.write('<img width="1" height="1" id="rcounter-' + this.counterId + '" src="' + this.getUrl() + '">');
		setTimeout('RCounter.reloadToTime(' + this.counterId + ')', 60000);
	}
}

RCounter.reloadToTime = function(counterId)
{
	var image = document.getElementById('rcounter-' + counterId);
	if(image.src.indexOf('action') != -1) {
		image.src = image.src + '&' + Math.random();
	} else {
		image.src += '&action=time';
	}

	setTimeout('RCounter.reloadToTime(' + counterId + ')', 60000);
}

RCounter.load = function(counterId) {
	var counter = new RCounter(counterId);
	counter.load();
}


/**
 * @library RRoller
 */
function RRoller(placeId, json)
{
	this.placeId = placeId;
	this.json     = json;
}

RRoller.prototype.getHTML = function()
{
	var html;
	function get_domain(url) {
       return url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/)[2];
    }

	if((typeof(this.json.banners) == 'object') && (this.json.banners.length>0)) {
		html = '<style>\
		         #rollad-ad-' + this.json.setting.id + ' \
		         {\
		           overflow:hidden;font-size:12px; border:1px solid #bbbbbb; background:transparent; margin:0; padding:0;\
		         }\
		         #rollad-ad-' + this.json.setting.id + ' td \
		         {\
		           cursor: pointer;padding:4px; background:transparent;line-height:15px;\
		         }\
		         #rollad-ad-' + this.json.setting.id + ' a \
		         {\
		           color:#000000;text-decoration:none;\
		         }\
		         #rollad-ad-' + this.json.setting.id + ' a u\
                 {\
                   color:blue;display:block;margin-bottom:5px;font-size: 14px; word-wrap: break-word;text-decoration:underline;\
                 }\
		         #rollad-ad-' + this.json.setting.id + ' a:hover u\
		         {\
		           color:red !important;\
		         }\
		         #rollad-ad-' + this.json.setting.id + ' img \
		         {\
		           padding:0px;margin:0 5px 0 0;border:1px solid #bbbbbb;\
		         }\
		         .rollad-ad-image-' + this.json.setting.id + ' \
		         {\
		           float: left ! important; width: 100px !important;height: 100px !important;margin-right: 10px !important;\
		         }\
		         </style>';

		if(this.json.banners.length == 1 && 'graphic' == this.json.banners[0].type) {
		    var banner = this.json.banners[0];
			html += '<a href="' + banner.url + '" class="rollad-ad-title-' + this.json.setting.id + '" target="_blank"><img width="' + banner.width + '" height="' + banner.height + '" src="' + banner.image + '" /></a>';
		} else {
			html += '<table border="0" width="'+this.json.setting.size.replace('x','" height="')+'" cellpadding="0" cellspacing="0" style="width:'+this.json.setting.size.replace('x','px;height:')+'px;" id="rollad-ad-' + this.json.setting.id + '">';
			if('horizontal' == this.json.setting.direction) html+='<tr>';

			for(i = 0; i < this.json.banners.length; i++) {
			    var banner = this.json.banners[i];

		        html += '<td width="236" valign="top" style="width:236px">\
                          <a href="'+banner.url+'" target="_blank">';
                if('teaser' == banner.type)
                     html += '<img src="'+banner.image+'" width="75" height="75" alt="" border="0" align="left" />';
                else
                     html += '<img width="1" height="1" src="' + banner.pixel + '" alt="" align="left" />';

                html += '<u>'+banner.title+'</u>\
                         '+banner.text+'\
                         <small style="color:#005500;display:block;font-size:11px;">'+get_domain(banner.url)+'</small>\
                         </a>\
                        </td>';

			    if ('vertical' == this.json.setting.direction)
			        html += '</tr><tr>';

			}

			if('horizontal' == this.json.setting.direction)
            {
              html+='<td width="20" align="center" valign="top" style="width:20px;overflow:hidden;background:#ffffff url(http://static.rollad.ru/i/add-here.png) top center no-repeat;border-left:1px solid #bbbbbb">\
                      <a href="http://rollad.ru/?place-id='+ this.json.setting.id +'" style="display:block;width:20px;height:90px"></a>\
                     </td></tr>';
			}
			else
			 html+='<td align="right"><a href="http://rollad.ru/?place-id='+ this.json.setting.id +'">Advertise here</a></td></tr>';


			html += '</table>';
		}
	} else {
		html = this.json.stub;
	}

	return html;
}

RRoller.prototype.display = function()
{
	document.write(this.getHTML());
}

RRoller.load = function(placeId) {
	var timestamp = Math.round(new Date().getTime() / 1000);
	var rand      = Math.round(new Date().getTime() / 1000);

	document.write('<script type="text/javascript" src="http://roller.rollad.ru/?aid=' + placeId + '&type=script&timestamp=' +  timestamp + '&referrer=' + encodeURIComponent(document.referrer) + '&location=' + encodeURIComponent(top.location.href) + '&rand=' + rand + '">' + '</sc' + 'ript>');
}

RRoller.display = function(placeId, json)
{
	var roller = new RRoller(placeId, json);
	roller.display();
}

