

/* USAGE:
var arrAds = new Array();
arrAds[0] = new Ad(ad_id, file_url, click_url);
arrAds[1] = new Ad(ad_id, file_url, click_url);
arrAds[2] = new Ad(ad_id, file_url, click_url);

var objPosition = new Position(code, timeout, width, height, arrAds);
objPosition.init();
*/

/************************************************************************/
/* Object Definitions */

// ad object stores the following properties
// ad_id
// ad_type: image or flash
// file_url
// click_url
// target

function Ad(ad_id, file_url, real_file_url, click_url, ad_type) {
	this._ad_id = ad_id;
	this._file_url = file_url;
	this._real_file_url = real_file_url;
	this._click_url = click_url;
	this._target = "_blank";
	this._ad_type = ad_type;
	this._record_impression=true;
}

Ad.prototype._ad_id;
Ad.prototype._ad_type;
Ad.prototype._file_url;
Ad.prototype._real_file_url;
Ad.prototype._click_url;
Ad.prototype._target;
Ad.prototype._record_impression;

Ad.prototype.getAdId = function() {
	return this._ad_id;
}
Ad.prototype.getAdType = function() {
	return this._ad_type;
}
Ad.prototype.getFileUrl = function() {
	return this._file_url;
}
Ad.prototype.getRealFileUrl = function() {
	return this._real_file_url;
}
Ad.prototype.getClickUrl = function() {
	return this._click_url;
}
Ad.prototype.getTarget = function() {
	return this._target;
}
Ad.prototype.getRecordImpression = function() {
	return this._record_impression;
}
Ad.prototype.setRecordImpression = function(recordImpression) {
	this._record_impression=recordImpression;
}

// ad position object stores the following properties
// position id
// position code
// rotate timespan (seconds)
// ads (array of ads)
// current_ad
// width
// height
function Position(code, timeout, width, height, ads) {
	this._code = code;
	this._timeout = (timeout*1000);
	this._width = width;
	this._height = height;
	this._ads = ads;
	this._current_ad = 0;
}

Position.prototype._code;
Position.prototype._timeout;
Position.prototype._width;
Position.prototype._height;
Position.prototype._ads;

Position.prototype.getCode = function() {
	return this._code;
}
Position.prototype.getTimeout = function() {
	return this._timeout;
}
Position.prototype.getWidth = function() {
	return this._width;
}
Position.prototype.getHeight = function() {
	return this._height;
}
Position.prototype.init = function() {
	var positionObject=this;
	this._ads[0].setRecordImpression(false);
	if (this._ads.length>1) {
		setTimeout(function () { positionObject.rotate() },this._timeout);
	}
}

Position.prototype.rotate = function() {

	if (this._current_ad >= this._ads.length-1) {
		this._current_ad=0;
	} else {
		this._current_ad++;
	}

	var _positionContainer = document.getElementById(this._code);
	var _objAd = this._ads[this._current_ad];

	var _fileUrl = "";
	if (_objAd.getRecordImpression()) {
		_fileUrl=_objAd.getFileUrl()+"&recordImpression="+_objAd.getRecordImpression();
	} else {
		_fileUrl=_objAd.getRealFileUrl();
	}
	_objAd.setRecordImpression(false);

	if (_objAd.getAdType()=="flash") {
		//alert("flash");
		_positionContainer.innerHTML = "";

		var _so = new SWFObject('/common_scripts/flash/flashBannerLoader/flashBannerLoader.swf', 'flashBanner', this._width, this._height, '7', '#FFFFFF', false, 'best');
		_so.addParam("FlashVars", "&clickURL="+escape(_objAd.getClickUrl())+"&flashBannerURL="+escape(_fileUrl)+"&target="+_objAd.getTarget()+"&width="+this._width+"&height="+this._height);
		_so.addParam("allowScriptAccess", "always");
		_so.addParam("movie", "/common_scripts/flash/flashBannerLoader/flashBannerLoader.swf");
		_so.addParam("wmode", "transparent");
		_so.write(this._code);

	} else {
		_positionContainer.innerHTML = "<a href='" + _objAd.getClickUrl() + "' target='" + _objAd.getTarget() + "'><img src='" + _fileUrl + "' /></a>";
	}

	var positionObject=this;
	setTimeout(function () { positionObject.rotate() },this._timeout);

}