
/* 10News.com scripts */
// Begin national js scripts


using("ibsys.site");

ibsys.site.VideoMunge = new Array();

ibsys.site.VideoMungeUtils = 
{
	getVideoInfo: function(coid)
	{
		for(var j=0; j < ibsys.site.VideoMunge.length; j++)
			if(ibsys.site.VideoMunge[j].coid == coid)
				return ibsys.site.VideoMunge[j];
		return null;
	}
};

(function()
{
	function iterator(array)
	{
		var index = 0;
		return ({
			hasNext: function hasNext()
			{
				return array != null && index < array.length;
			},
			next: function next()
			{
				return array[index++];
			}
		});
	}

	function forEach(arr)
	{
		return (function(f)
		{
			for(var iter = iterator(arr); iter.hasNext();)
				f(iter.next());
		});
	}

	Array.deepIndexOf = function(arr,field,value)
	{
		for(var j=0; j < arr.length; j++)
		{
			if(arr[j][field] == value)
				return j;
		}
		return -1;
	};

	Object.copy = function(src,props)
	{
		var dest = {};
		if(props)
		{
			for(var j=0; j < props.length; j++)
				dest[props[j]] = src[props[j]];
		}
		else
		{
			for(i in src)
				dest[i] = src[i];
		}
		return dest;
	};

	Array.clear = function(arr)
	{
		while(arr.length != 0)
			arr.splice(0,1);
		return arr;
	};

	Array.fill = function(arr, aValue)
	{
		for(var j=0; j < arr.length;
			arr[j++] = aValue);
	};

	if(ibsys.site.VideoMunge.length != 0)
		Array.clear(ibsys.site.VideoMunge);

	for(var i in ibIndex)
	{
		var index = ibIndex[i];

		forEach(index.co)((function(val,j)
		{
			if(val.type == "121")
			{
				if(Array.deepIndexOf(ibsys.site.VideoMunge,"coid",val.coid) == -1)
				{
					ibsys.site.VideoMunge.push(Object.copy(val,
						[ "videocategory","videosubcategory",
						  "coid","owner","type","headline","topic_path"
						  ]));
					// log("Copying object: " + val.coid);
				}
			}
			forEach(val.ibs_related)((function(related)
			{
				if(related.type == "video")
				{
					if(Array.deepIndexOf(ibsys.site.VideoMunge,"coid",related.coid) == -1)
					{
						ibsys.site.VideoMunge.push(
							Object.copy(related));

						// log("Copying related: " + related);
					}
				}
			}));
		}));
	}

	// OK here we make up a list of content IDs that do not have sufficient
	// information, then make an XML request to fill in the stuff that we
	// still needd.
	var requestIDs = [];

	forEach(ibsys.site.VideoMunge)((function(val)
	{
		if(!val.videocategory || !val.topic_path)
		{
			requestIDs.push(val.coid);
			// log("Mungeable: " + val.coid);
		}
		else
		{
			// log("NOT Mungeable: " + val.coid);
		}
	}));

	function getXML(url, handler)
	{
		var r = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
		r.onreadystatechange = (function()
		{
			if(r.readyState == 4)
				handler(r.responseXML, r.responseText);
		});
		r.open("GET", url, true);
		r.send(null);
	}

	if(requestIDs.length != 0)
	{
		getXML("/_objectxml/" + requestIDs.join(",") + "/detail.html",
			(function(xml,txt)
			{
				if(!xml || !xml.documentElement)
					return;
				var nl = xml.documentElement.getElementsByTagName("video");

				for(var j=0; j < nl.length; j++)
				{
					var val = {};
					for(var ii=0; ii < nl.item(j).attributes.length; ii++)
					{
						var attr = nl.item(j).attributes.item(ii);
						val[attr.name == "id" ? "coid" : attr.name] = attr.value;
					}
					var index = Array.deepIndexOf(ibsys.site.VideoMunge,"coid",val.coid);
					if(index != -1)
						ibsys.site.VideoMunge[index] = val;
					else
						ibsys.site.VideoMunge.push(val);
				}
			})
		);
	}
})();


// End national_js scripts


