// TII Ads - Version 1.0.2 - 2007.05.24

function TiiAdConfig(sitename) {
	this.sitename	= sitename;
	this.cmSitename	= sitename;

	this.setSitename	= TiiAdConfigSetSitename;
	this.setCmSitename	= TiiAdConfigSetCmSitename;
}

function TiiAdConfigSetSitename(value) {
	this.sitename = value;
}

function TiiAdConfigSetCmSitename(value) {
	this.cmSitename = value;
}

function TiiAdFactory() {
	// Detect if first agument is a string or an array
	var first = arguments[0];

	if (typeof(first) == "string") {
		this.sitename		= first;
		this.cmSitename		= first;

	} else if (typeof(first) == "object") {
		this.sitename		= first.sitename;
		this.cmSitename		= first.cmSitename;
	}

	if (TiiAd_isSecure()) {
		this.adServer		= "https://ad.doubleclick.net/";
	} else {
		this.adServer		= "http://ad.doubleclick.net/";
	}
	
	this.randomNumber	= TiiAdsGetRandomNumber();
	this.tileCounter	= 1;
	this.params		= new Array();
	this.zone		= "";
	this.dcopt		= false;
	
	if (arguments.length == 2) {
		this.zone = arguments[1];
	}
	
	this.createAd		= TiiAdFactoryCreateAd;
	this.getAd		= TiiAdFactoryGetAd;
	this.getCmAd		= TiiAdFactoryGetCmAd;
	this.getMultiAd		= TiiAdFactoryGetMultiAd;
	this.getTransitionalAd	= TiiAdFactoryGetTransitionalAd;
	this.setArticleId	= TiiAdFactorySetArticleId;
	this.setChannel		= TiiAdFactorySetChannel;
	this.setChannelPage	= TiiAdFactorySetChannelPage;
	this.setContentPage	= TiiAdFactorySetContentPage;
	this.setContentType	= TiiAdFactorySetContentType;
	this.setPackageId	= TiiAdFactorySetPackageId;
	this.setParam		= TiiAdFactorySetParam;
	this.setSubchannel	= TiiAdFactorySetSubchannel;
	this.setZone		= TiiAdFactorySetZone;
}

function TiiAdFactorySetParam(key, value) {
	if (typeof(value) == "object") {
		this.params[key] = value;
	} else {
		if (value.toString() != "") {
			this.params[key] = value;
		}
	}
}

function TiiAdFactoryCreateAd() {
	var width, height, zone, ad;

	if (arguments.length == 2) {
		width = arguments[0];
		height = arguments[1];

	} else {
		// Assume 3 arguments
		width = arguments[0];
		height = arguments[1];
		zone = arguments[2];

	}

	ad = new TiiAd(this, width, height, this.tileCounter);

	if (null != zone) {
		ad.setZone(zone);
	}

	// Copy Factory params to this specific ad
	for (var key in this.params) {
		ad.setParam(key, this.params[key]);	
	}

	ad.setParam("sz", width + "x" + height);

	var paths = window.location.pathname.split("/");
	paths = paths.slice(1, paths.length - 1);
	ad.setParam("path", paths);
	
	ad.setParam("dcove", "d");

	this.tileCounter++;

	return ad;
}

function TiiAdFactoryGetAd() {
	var width, height, zone, ad;
	
	if (arguments.length == 2) {
		width = arguments[0];
		height = arguments[1];

		ad = this.createAd(width, height);	
	
	} else {
		width = arguments[0];
		height = arguments[1];
		zone = arguments[2];
	
		ad = this.createAd(width, height, zone);
	}

	if (this.dcopt == false) {
		ad.setParam("dcopt", "ist");
		this.dcopt = true;
	}

	return ad;
}

function TiiAdFactoryGetCmAd(width, height, position, type) {
	ad = this.createAd(width, height);
	ad.setParam("cmpos", position);
	ad.setParam("cmtyp", type);
	ad.sitename = this.cmSitename;

	return ad;
}

function TiiAdFactoryGetTransitionalAd() {
	ad = this.getAd(0,0);
	return ad;
}

function TiiAdFactoryGetMultiAd(sizes) {
	var width = sizes[0].split("x")[0];
	var height = sizes[0].split("x")[1];
	var ad = this.getAd(width, height);
	var sizeValue = sizes.join(",");
	ad.setParam("sz", sizeValue);
	
	return ad;
}

function TiiAdFactorySetArticleId(articleId) {
	this.setParam("aid", articleId);
}

function TiiAdFactorySetChannel(channel) {
	this.setParam("ch", channel);
}

function TiiAdFactorySetPackageId(packageId) {
	this.setParam("pid", packageId);
}

function TiiAdFactorySetSubchannel(subchannel) {
	this.setParam("sch", subchannel);
}

function TiiAdFactorySetContentPage() {
	this.setParam("ptype", "content");
}

function TiiAdFactorySetChannelPage() {
	this.setParam("ptype", "channel");
}

function TiiAdFactorySetContentType(ctype) {
	this.setParam("ctype", ctype);
}

function TiiAdFactorySetZone(zone) {
	this.zone = zone;
}

function TiiAd(factory, width, height, tileNumber) {
	this.tileNumber		= tileNumber;
	this.width		= width;
	this.height		= height;
	this.params		= new Array();

	// Methods
	this.setParam		= TiiAdFactorySetParam;
	this.setMagicNumber	= TiiAdSetMagicNumber;
	this.setPosition	= TiiAdSetPosition;
	this.setZone		= TiiAdSetZone;
	this.write		= TiiAdWrite;

	// Private Methods
	this._formatParams	= TiiAd_formatParams;
	this._getAdParams	= TiiAd_getAdParams;
	this._getAdTag		= TiiAd_getAdTag;
	this._getAdUrl		= TiiAd_getAdUrl;
	this._getImageUrl	= TiiAd_getImageUrl;
	this._getClickUrl	= TiiAd_getClickUrl;
	this._getDebugHtml	= TiiAd_getDebugHtml;
	this._getSecureAdTag	= TiiAd_getSecureAdTag;
	
	// Copy factory settings
	this.randomNumber	= factory.randomNumber;
	this.adServer		= factory.adServer;
	this.tileNumber		= factory.tileCounter;
	this.zone		= factory.zone;
	this.sitename		= factory.sitename;
}

function TiiAdSetMagicNumber(mn) {
	this.setParam("mn", mn);
}

function TiiAdSetPosition(pos) {
	this.setParam("pos", pos);
}

function TiiAdSetZone(zone) {
	this.zone = zone;
}

function TiiAdWrite() {
	if (TiiAdsIsDebugMode()) {
		document.write(this._getDebugHtml() + "<br/>");
	}
	
	document.write(this._getAdTag());
}

function TiiAd_formatParams() {
	var adParams = "";
	for (var key in this.params) {
		var value = this.params[key];
		
		if (typeof(value) == "function") {
			continue;
		}

		if (typeof(value) == "string" || typeof(value) == "number") {
			adParams += ";" + key + "=" + escape(value).toLowerCase();
		} else {
			for (var i = 0; i < value.length; i++) {
				if (value[i] != "") {
					adParams += ";" + key + "=" + escape(value[i]).toLowerCase();
				}
			}
		}
	}
	
	return adParams;
}

function TiiAd_getAdParams() {
	var adParams	= this._formatParams();
	var tileParam	= ";tile=" + this.tileNumber;

	var secureParam = "";
	if (TiiAd_isSecure()) {
		secureParam = ";sec=1";
	}

	var ordParam	= ";ord=" + this.randomNumber;
	
	return this.sitename + "/" + this.zone.toLowerCase() + adParams + tileParam + secureParam + ordParam + "?";
}

function TiiAd_getAdTag() {
	var adTag;
	if (TiiAd_isSecure()) {
		adTag = this._getSecureAdTag();		
	} else {
		adTag = '<scr' + 'ipt type="text/javascript" src="' + this._getAdUrl() + '"></scr' + 'ipt>';
	}
	
	return adTag;
}

function TiiAd_getSecureAdTag() {
	return '<a href="' + this._getClickUrl() + '" target="_blank"><img src="' + this._getImageUrl() + '" width="' + this.width + '" height="' + this.height + '" border="0" /></a>';
}

function TiiAd_getAdUrl() {
	return this.adServer + "adj/" + this._getAdParams();
}

function TiiAd_getImageUrl() {
	return this.adServer + "ad/" + this._getAdParams();
}

function TiiAd_getClickUrl() {
	return this.adServer + "jump/" + this._getAdParams();
}

function TiiAd_getDebugHtml() {
	var output = '<input style="font-family: courier new; font-size: small; width:' + this.width + 'px; margin: 0; padding: 0" value="' + unescape(this._getAdUrl()) + '"/>';

	return output;
}

// Support Functions

function TiiAdsIsDebugMode() {
	return window.location.search.indexOf("debugads") >= 0;
}

function TiiAdsGetRandomNumber() {
	return Math.ceil(1+1E12*Math.random());
}

function TiiAd_isSecure() {
	return (document.location.protocol == "https:");
}

// Redirect Functions

function tiiAdSetType() {}
function tiiAdSetTarget() {}
function tiiHtmlAdWH(mn, width, height) {
	adFactory.getAd(width, height).write();
}

