/* iframe Ad Factory */
/***** This library needs the TII Ad and the (jQuery or TIMEINC Utilities) libraries *****/
var iframeAdFactory = {};

/* Queuing to synchronize first call */
/* qc = queue controller */
iframeAdFactory.qc = {};
iframeAdFactory.qc.queue = new Array ();
iframeAdFactory.qc.index = 0;
/* qo = queue object */
iframeAdFactory.qo = function (data)
{
	this.data = data;
};
iframeAdFactory.adIdAlreadyExists = function (adId)
{
	for (var i = 0, l = this.qc.queue.length; i < length; i++)
	{
		var testData = this.qc.queue [i].data;
		if (testData.id == adId && typeof this.qc.initialLoadHasCompleted == 'boolean' && this.qc.initialLoadHasCompleted) { return { adId: adId, adIdExists : true, data : testData } }
	}
	return { adIdExists : false };
}
iframeAdFactory.getAd = function (id, width, height, sizes, isMaster, getAsInnerHTML, hookContainer)
{
	var hookId = 'hook_' + id;
	var html = '<div id="' + hookId + '"></div>';
	var testAdId = this.adIdAlreadyExists (id);
	if (!testAdId.adIdExists)
	{
		var data = { adSource : 'TII', hookId : hookId, id : id, width : width, height : height, sizes : sizes, isMaster : isMaster}
		this.qc.queue [this.qc.index++] = new this.qo (data);
		if (typeof hookContainer == 'object' && hookContainer && typeof hookContainer.nodeName == 'string') { hookContainer.innerHTML = html; }
		else
		{
			if (typeof getAsInnerHTML == 'boolean' && getAsInnerHTML) { return html; }
			else { document.write (html); }
		}
	}
	else
	{
		if (typeof hookContainer == 'object' && hookContainer && typeof hookContainer.nodeName == 'string')
		{
			hookContainer.innerHTML = html;
			for (var i = 0, l = this.iframeCount; i < l; i++)
			{
				var iframe = this.iframes [i];
				if (iframe.id == testAdId.adId)
				{
					hookContainer.getElementsByTagName ('div').item (0).appendChild (iframe);
				}
			}
		}
	}
};
iframeAdFactory.getQuigoAd = function (pid, placementId, zw, zh, ps, getAsInnerHTML)
{
	var hookId = 'hook_' + placementId;
	var html = '<div id="' + hookId + '"></div>';
	var data = { adSource : 'Quigo', hookId : hookId, pid : pid, placementId : placementId, zw : zw, zh : zh, ps : ps }
	this.qc.queue [this.qc.index++] = new this.qo (data);
	if (typeof getAsInnerHTML == 'boolean' && getAsInnerHTML) { return html; }
	else { document.write (html); }
};
iframeAdFactory.qc.populateHook = function (data)
{
	var d = data;
	if (d.adSource == 'TII') { document.getElementById (d.hookId).innerHTML = iframeAdFactory.getAdInnerCall (d.id, d.width, d.height, d.sizes, d.isMaster); }
	else if (d.adSource == 'Quigo') { document.getElementById (d.hookId).innerHTML = iframeAdFactory.getQuigoAdInnerCall (d.pid, d.placementId, d.zw, d.zh, d.ps); }
}
iframeAdFactory.qc.masterHasCompletedInitialLoad = false;
iframeAdFactory.qc.completeInitialLoadOfOthers = function ()
{
	for (var i = 0, l = this.index; i < l; i++)
	{
		var qo = this.queue [i];
		if (i != this.masterIndex)
		{
			this.populateHook (qo.data);
		}
	}
	this.initialLoadHasCompleted = true;
}
iframeAdFactory.begin = function ()
{
	for (var i = 0, l = iframeAdFactory.qc.index; i < l; i++)
	{
		var qo = iframeAdFactory.qc.queue [i];
		var isMaster = qo.data.isMaster;
		if (typeof isMaster == 'boolean' && isMaster)
		{
			iframeAdFactory.qc.masterIndex = i;
			iframeAdFactory.qc.populateHook (qo.data);
			break;
		}
	}
};
if (typeof jQuery == 'function') { jQuery (window).load (iframeAdFactory.begin); }
else if (typeof TIMEINC == 'object') { TIMEINC.addEventListener (window, 'load', iframeAdFactory.begin, false); }

/* Inner methods and properties */
iframeAdFactory.iframes = new Array ();
iframeAdFactory.iframeCount = iframeAdFactory.tileCounter = 0;
iframeAdFactory.randomNumberProperty = '&randomNumber=';
iframeAdFactory.randomNumberPair = iframeAdFactory.randomNumberProperty + TiiAdsGetRandomNumber ();
iframeAdFactory.synchronizedDomainPair = '';
iframeAdFactory.getAdInnerCall = function (id, width, height, sizes, isMaster)
{
	var hasSizes = typeof sizes != 'undefined' && sizes && sizes.length > 0;
	var iframeIsMaster = ((typeof isMaster == 'undefined' || !isMaster) ? false : true);
	if (iframeIsMaster) { this.masterId = id; }
	var dcoptPair = 'dcopt=' + !iframeIsMaster;
	var tileCounterPair = 'tileCounter=' + (++this.tileCounter);
	var adFactoryPropertyPairs = '&adFactoryPropertyPairs=' + encodeURIComponent (dcoptPair + '&' + tileCounterPair);
	return '<iframe id="' + id + '" src="' + this.url + '?' + this.queryString + '&getAdX=' + width + '&getAdY=' + height + (hasSizes ? '&multiAdSizes=' + sizes.join (',') : '') + adFactoryPropertyPairs + this.synchronizedDomainPair + this.randomNumberPair + '" width="' + width + '" height="' + height + '" frameborder="0" scrolling="no" onload="iframeAdFactory.process (this' + (hasSizes ? ', true' : '') + ')"></iframe>';
};
iframeAdFactory.resizeIframe = function (iframe)
{
	if (iframe.hasSizes) { iframe.height = iframe.contentWindow.document.body.scrollHeight; }
	if (iframe.isMaster) { this.refreshOthers (); }
};
iframeAdFactory.process = function (iframe, hasSizes, adType) { this.register (iframe, adType); };
iframeAdFactory.performedOverride = false;
iframeAdFactory.register = function (iframe, adType)
{
	if (this.qc && !this.qc.masterHasCompletedInitialLoad)
	{
		this.qc.masterHasCompletedInitialLoad = true;
		this.qc.completeInitialLoadOfOthers ();
	}
	iframe.isMaster = false;
	iframe.hasSizes = typeof hasSizes != 'undefined' ? hasSizes : false;
	iframe.adType = typeof adType == 'string' ? adType : '';
	if (iframe.hasSizes) { this.resizeIframe (iframe); }
	if (iframe.id == this.masterId) { iframe.isMaster = true; this.masterIframe = iframe;	}
	else { this.iframes [this.iframeCount++] = iframe; }
};
iframeAdFactory.refresh = function (makeUnsynchronized)
{
	this.makeUnsynchronized = (typeof makeUnsynchronized != 'undefined' && makeUnsynchronized) ? true : false;
	if (!this.performedOverride)
	{
		this.performedOverride = true;
		this.process = function (iframe)
		{
			if (iframe.hasSizes) { this.resizeIframe (iframe); }
			if (iframe.isMaster) { this.refreshOthers (); }
		}
	}
	this.randomNumberPair = this.randomNumberProperty + TiiAdsGetRandomNumber ();
	this.masterIframe.src = this.getNewIframeSrc (this.masterIframe.src);
};
iframeAdFactory.refreshOthers = function ()
{
	var self = iframeAdFactory;
	for (var i = 0; i < self.iframeCount; i++)
	{
		if (this.iframes [i].adType == 'quigo')
		{ this.iframes [i].src = this.iframes [i].src; }
		else
		{ this.iframes [i].src = self.getNewIframeSrc (self.iframes [i].src); }
	}
};
iframeAdFactory.getNewIframeSrc = function (src)
{
	if (this.makeUnsynchronized) { this.randomNumberPair = this.randomNumberProperty + TiiAdsGetRandomNumber (); }
	return src.substring (0, src.indexOf (this.randomNumberProperty)) + this.randomNumberPair;
};
iframeAdFactory.synchronizeDomains = function ()
{
	var domainSplit = document.domain.split ('.'), length = domainSplit.length;
	var domain = domainSplit [length - 2] + '.' + domainSplit [length - 1];
	document.domain = domain;
	this.synchronizedDomainPair = '&synchronizedDomain=' + domain;
};

/* Quigo portion */
iframeAdFactory.quigoAdsEnabled = true;
iframeAdFactory.enableQuigoAds = function (b) { this.quigoAdsEnabled = b; };
iframeAdFactory.quigoJWDefault = 'ads.adsonar.com';
iframeAdFactory.setQuigoJW = function (jw) { this.jw = jw; };
iframeAdFactory.getQuigoAdInnerCall = function (pid, placementId, zw, zh, ps)
{
	if (!this.quigoAdsEnabled) { return; }
	if (typeof this.jw != 'string' || this.jw.length == 0) { this.jw = this.quigoJWDefault; }
	var queryString = 'pid=' + pid + '&placementId=' + placementId + '&zw=' + zw + '&zh=' + zh + '&ps=' + ps + '&jw=' + this.jw;
	return '<iframe id="i_' + placementId + '" src="' + this.quigoUrl + '?' + queryString + '" width="' + zw + '" height="' + zh + '" frameborder="0" scrolling="no" onload="iframeAdFactory.process (this, false, \'quigo\')"></iframe>';
};
var tiiQuigoSetEnabled = function (b) { iframeAdFactory.enableQuigoAds (b); }
var tiiQuigoWriteAd = function (a, b, c, d, e) { iframeAdFactory.getQuigoAd (a, b, c, d, e); };