var Tj_Ad_Request = new Class({
	
	initialize: function(deliveryUrl, proxy) {
		this.proxy = proxy; 
		this.url = deliveryUrl;
		this.blocks = [];
	},

	add: function(block) {
		this.blocks[this.blocks.length] = block;
		return this;
	},
	
	
	send: function() {
		var trg;
		if(this.proxy != null) {
			data = 'data=' + JSON.encode(this.blocks) + '&url=' + this.url;
			trg = this.proxy;
		} else {
			data = 'data=' + JSON.encode(this.blocks);
			trg = this.url;
		}
		trg = trg + '?c=' + Math.floor(Math.random()*1100000);
		this.request = new Request.JSON({method: 'post', url: trg, onSuccess: function(blocksets) {
				for(var x = 0; x < blocksets.length; x++) {
					blocksets[x].each(function(ad, index) {
						this.setAd(ad);
					}, this._parentScope);
				}
		}});
		this.request._parentScope = this;
		this.request.send(data);
	},
	
	setAd: function(ad) {
		if(ad.type == 'static') {
			this.setStaticAd(ad);
		} else if(ad.type == 'iframe') {
			this.setIFrameAd_new(ad);
		}
	},	
		
	setStaticAd: function(ad) {
		var link = $(document.createElement('a'));
		link.setAttribute('rel', 'nofollow');
		link.setAttribute('href', ad.click_url);
		link.setAttribute('target', '_blank');
		var img = $(document.createElement('img'));
		img.setAttribute('src', ad.adUrl);
		img.setAttribute('border', '0');
		link.appendChild(img);
		$(ad.element_id).appendChild(link);
	},
	
	/**
	 * Work around for iframe ad. For some reason, creating element does not work well on old piece of shit IE
	 */
	setIFrameAd_new: function(ad) {
		var ref = $(ad.element_id);
		var iframecode = '<iframe frameborder="0" type="new" width="' + ref.getStyle('width') + '" height="' + ref.getStyle('height') + '" scrolling="no" allowtransparency="true" marginwidth="0" marginheight="0" src="' + ad.adUrl + '"></iframe>';
		ref.innerHTML = iframecode;
	},
	
	/**
	 * Original, the way it supposed to be done. Problems with IE.
	 */
	setIFrameAd: function(ad) {
		var ref = $(ad.element_id);				
		var iframe = $(document.createElement('iframe'));		
		iframe.setAttribute('width', ref.getStyle('width'));
		iframe.setAttribute('height', ref.getStyle('height'));
		iframe.setAttribute('scrolling', 'no');
		iframe.setAttribute('frameborder', '0');		
		iframe.setAttribute('allowtransparency', 'true');
		iframe.setAttribute('marginwidth', '0');
		iframe.setAttribute('marginheight', '0');
		iframe.src = ad.adUrl;
		ref.parentNode.replaceChild(iframe, ref);
	}
	
	
});


var Tj_Ad_Request_Block = new Class({
	
	initialize: function(unique) {
		this.unique = false;
		if(typeof(unique) == 'boolean') {
			this.unique = unique;
		}
		this.spots = []
	},

	add: function(spot) {
		this.spots[this.spots.length] = spot;
		return this;
	}		
});


var TJ_Ad_Request_Spot = new Class({
	
	initialize: function(site, zone, id, context) {
		this.site = site;
		this.zone = zone;
		this.element_id = id;
		if(context != undefined) {
			this.context = String(context).replace(/[^a-zA-Z1-9]+/, "").toString();
		} else {
			this.context = '';
		}
	}
	
});