/**
 * Copyright 2006, Internet Broadcasting Systems. All Rights Reserved.
 * Version:   $Name: REL_2_46_1 $
 * ID:        $Id: AdObj.js,v 1.11 2007/06/14 19:48:02 ddiller Exp $
*/

using("ibsys");
	
/**
 * AdObj.js
 *
 * Generic AdObj holds properties to ease calling pixel ads for specific uses
 *
 * Usage of class methods:
 *	<li>AdObj.create(position,sz,swId,coId,section,topic,tag,pagetype) - create and return a new AdObj</li>
 *  <li>AdObj.refreshPosition(positions...) - refresh one or more positions</li>
 *  <li>AdObj.refreshAll() - refresh all ads</li>
 *	<li>AdObj.remove(name) - remove ad</li>
 *
 * @constructor
 * @param {String} position Corresponds to ad position, like mediawidow, banner or tile
 * @param {String} sz Size, formatted as 2x2
 * @param {String} swId Section widget id
 * @param {String} coId Content id
 * @param {String} section Section widget section
 * @param {String} topic
 * @param {String} tag
 */
ibsys.AdObj = function(position,sz,swId,coId,section,topic,tag,pagetype) {
	//alert("created");
	this.position = position;
	this.sz = sz;
	this.swId = swId;
	this.coId = coId ?  coId : "";
	this.section = section;
	this.topic = topic;
	this.tag = tag;
	this.pagetype = pagetype;
	
	var size = this.sz.split("x");
	this.width = size[0];
	this.height = size[1];
}


//Refresh this ad.
ibsys.AdObj.prototype.refresh = function(adid) {
	adid = (typeof adid == "undefined") ? "" : adid;
	var arr = document.getElementsByClassName("ad"+this.position);
	if(arr.length == 0)
		return;
	
	var rnd = Math.round(Math.random()*1000000000000);
	var src = "http://iv.doubleclick.net/adi/nbcu.nbcsports/";
//http://iv.doubleclick.net/adj/<%=siteName%>/<%=pageName%>;site=<%=site%>;sect=<%=sect%>;sub=<%=sub%>;sub2=<%=sub2%>;cont=<%=cont%>;genre=<%=genre%>;!category=<%=category%>;ucid=<%=ucid%>;"+ibsys.AdObj.getLocalInfo+";sz=<%=size%>;tile=<%=tile%>;pos=<%=pos%>;ord="+randNumber+"?
//create(position,sz,swId,coId,section,topic,tag,pagetype) {
	src += this.topic+"_"+this.section+";site=nbcsports;sect="+this.topic+";sub="+this.section+";"+(this.tag ==""?"":(this.tag+";"))+"cont="+this.pagetype+";genre=sports;!category=nbcsports;ucid="+this.coId+";"+ibsys.AdObj.getLocalInfo()+";sz="+this.sz+";"+(this.sz=="728x90"?"dcopt=ist;":"")+"tile="+this.position+";pos="+this.position+";ord="+rnd;
	//alert(src);
	//src += this.topic+"_"+this.section+";!category=nbcsports;site=nbcsports;sect="+this.section+";top="+this.topic+";tag="+this.tag+";pos="+this.position+";"+ibsys.AdObj.getLocalInfo()+";comp="+adid;
	//src += ";ad=true;tile="+this.position+";pagtype="+this.pagetype+";sz="+this.sz+";ord="+rnd;
	
	arr[0].innerHTML = "<iframe id="+this.position+" src='"+src+"' width='"+this.width+"' height='"+this.height+"' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'></iframe>";
	/* window.console.log(src) */
}


//collection of Ad Objects
ibsys.AdObj.elements = new Object();


//Create a ibsys.AdObj instance and add it to the collection
ibsys.AdObj.create = function create(position,sz,swId,coId,section,topic,tag,pagetype) {
	//alert("CREATE FUNCTION");
	return ibsys.AdObj.elements[position] = new ibsys.AdObj(position,sz,swId,coId,section,topic,tag,pagetype);
}


//Refresh ads by position: ibsys.AdObj.refreshPosition("banner1","tile1","square");
ibsys.AdObj.refreshPosition = function() {
	for(var j=0; j < arguments.length; j++)
		if(ibsys.AdObj.elements[arguments[j]])
			ibsys.AdObj.elements[arguments[j]].refresh();
}


//Refresh all ads
ibsys.AdObj.refreshAll = function(adid) {
	for(var p in ibsys.AdObj.elements)
		ibsys.AdObj.elements[p].refresh(adid);
}

ibsys.AdObj.adIds = new Object();

/**
 * Returns an event handler for WMP CurrentItemChange event.
 *
 * The returned handler refreshes ads if the currentMedia has a
 * "dcAdTag" parameter.
 *
 * @param <String> videoPlugID
 * @return <function>
 */
ibsys.AdObj.createAdRefreshHandler = function(videoPlugID) {
	return (function()
	{
		if(!document ||
		   !document.body ||
		   !document.getElementById(videoPlugID))
		{
			return;
		}

		// NOTE: may need to change this if the format of the 'adid' parameter
		// value changes.  currently it is 'adid=[0-9]+'
		var adTagParam = document.getElementById(videoPlugID).currentMedia.getItemInfo("dcAdTag");

		if(/adid=[0-9]+/.exec(adTagParam) != null && !ibsys.AdObj.adIds[adTagParam])
		{
			ibsys.AdObj.adIds[adTagParam] = true;
			// log("Refreshing: " + adTagParam);
			ibsys.AdObj.refreshAll(adTagParam);
			window.setTimeout(
				(function()
				{
					ibsys.AdObj.adIds[adTagParam] = false;
				}), 1000);
		}
	});
}

/**
 * Remove an ad by name.
 * @param <String> name
 * @return true if the object was deleted.
 */
ibsys.AdObj.remove = function(name) {
	if(ibsys.AdObj.elements[name])
		return delete ibsys.AdObj.elements[name];
	else
		return false;
}


/**
 * Get localization info for ad
 * loca is true | partial | false
 * @return {String}
 */
ibsys.AdObj.getLocalInfo = function() {
	if (nbcsp.Localization.getStatus() != "not")
		return "loc="+nbcsp.Localization.getStatus()+";aff="+(typeof nbcsp.Localization.affiliate.calls != null ? nbcsp.Localization.affiliate.calls : '');
	else
		return "loc=false;aff=";
}

