

// -----------------------------------------------------
// Inform News Widget 
// -----------------------------------------------------
var Inform = {};

// -----------------------------------------------------
// Utility Functions
// -----------------------------------------------------
Inform.Utils = {};

Inform.Utils.joinAndEncode = function (elementList, separator)
{
	var result = "";
	
	for (var i = 0; i < elementList.length; i++)
	{
		if (i > 0) result += separator;
		// result += encodeURIComponent(elementList[i]).replace(/\,/g, "%2c");
		result += escape(elementList[i]);
	};
	
	return result;
};

/* overloaded element constructor */
Inform.Utils.createElement = function (tag, attributes, children) 
{
	var element = document.createElement(tag);
	
	for (var i in attributes) 
	{
		if (i == "className" || i == "class") 
		{
			element.className = attributes[i];
		}
		else if (i == "for" || i == "htmlFor") 
		{
			element.setAttribute("htmlFor", attributes[i]);
			element.setAttribute("for", attributes[i]);
		} 
		else 
		{
			element.setAttribute(i, attributes[i]);
		};
	};
	
	if (arguments.length > 2) 
	{
		if (typeof children == "object" && children.constructor == Array) 
		{
			for (var i = 0; i < children.length; i++) 
			{
				Inform.Utils._addChild(element, children[i]);
			};
		} 
		else 
		{
			Inform.Utils._addChild(element, children);
		};
	};

	return element;
};

Inform.Utils._addChild = function (el, child) 
{
	if (typeof child == "object") 
	{
		el.appendChild(child);
	} 
	else if (typeof child == "string" || typeof child == "number") 
	{
		// el.appendChild(document.createTextNode(child));
		el.innerHTML += child;
	};
};

/* retrieve the element's active style value */
Inform.Utils.getStyle = function (el, styleProp) 
{
	if (window.getComputedStyle)
	{
		return window.getComputedStyle(el, null).getPropertyValue(styleProp);
	}
	else if (document.defaultView && document.defaultView.getComputedStyle)
	{
		try
		{
			return document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
		}
		catch (e)
		{
			return "";
		};
	}
	else if (el.currentStyle)
	{
		styleProp = styleProp.replace(/\-(.)/g, function () 
		{
			return arguments[1].toUpperCase();
		});
		return el.currentStyle[styleProp];
	};
	
	return null;
};

/* ad frame callbacks */
Inform.Utils.resetAdFrameSize = function (adHeight)
{
	document.getElementById("inform-ad").style.height = adHeight + "px";
};

Inform.Utils.renderAd = function (adContent)
{
	document.getElementById("inform-ad").innerHTML = adContent;
};

// -----------------------------------------------------
// Article List Controller
// -----------------------------------------------------
Inform.ArticleData = {

	wrapper: null,
	searchTerms: [],
	hostname: "",
	articles: {},
	
	_writer: null
};

/* initialize the list */
Inform.ArticleData.init = function ()
{
	this.wrapper = document.getElementById("inform-wrapper");
	
	if (!this.wrapper || this.wrapper.getElementsByTagName("A").length == 0)
	{
		alert("Improper integration.");
		return;
	};
	
	this.render();
};

/* store the search query */
Inform.ArticleData.setSearchTerms = function (searchTerms)
{
	this.searchTerms = searchTerms;
};

/* store the hostname */
Inform.ArticleData.setHostname= function (hostname)
{
	this.hostname = hostname;
};

/* render the list */
Inform.ArticleData.render = function ()
{
	this.clearWrapper();

	this.renderArticleList();
	this.renderStyleSheet();
};

/* add the stylesheet with the default elements */
Inform.ArticleData.renderStyleSheet = function ()
{
	if (document.getElementById("inform-stylesheet"))
	{
		return;
	};
	
	var styleSheet = Inform.Utils.createElement("LINK", {
		href: this.hostname + "/inform2/feeds/resources/css/search-module.css",
		type: "text/css",
		rel: "stylesheet",
		id: "inform-stylesheet"
	});
	
	var docHead = document.getElementsByTagName("head")[0];
	
	if (docHead.childNodes.length)
	{
		docHead.insertBefore(styleSheet, docHead.firstChild);
	}
	else
	{
		docHead.appendChild(styleSheet);
	};
};

/* render the articles */
Inform.ArticleData.renderArticleList = function ()
{
	this.wrapper.appendChild(Inform.Utils.createElement("H3", null, this.searchTerms.join(", ") + " News"));

	var listWrapperEl = Inform.Utils.createElement("DL", {
		id: "inform-data"
	});
	
	var hasArticles = false;
	var itemTypes = ["news"];
	
	for (var j = 0; j < itemTypes.length; j++)
	{
		var articleList = this.articles[itemTypes[j]];
	
		for (var i = 0; i < articleList.length; i++)
		{
			var stateClassName = i == 0 ? "first" : (i == articleList.length - 1 ? "last" : "");
			
			listWrapperEl.appendChild(
				Inform.Utils.createElement("DT", {
					className: stateClassName
				}, [
					// Inform.Utils.createElement("SPAN", null, "..."),
					Inform.Utils.createElement("A", {
						href: articleList[i].externalLink,
						target: "_blank"
					}, articleList[i].title)
				])
			);
			/*
			listWrapperEl.appendChild(
				Inform.Utils.createElement("DD", {
					className: "date " + stateClassName
				}, articleList[i].date + ",")
			);
			*/
			listWrapperEl.appendChild(
				Inform.Utils.createElement("DD", {
					className: "publication " + stateClassName
				}, articleList[i].publication)
			);
			
			hasArticles = true;
		};
	};
	
	// alert(listWrapperEl.innerHTML);
	
	listWrapperEl.className = hasArticles ? "" : "empty";
	
	this.wrapper.appendChild(listWrapperEl);
		
	this.wrapper.appendChild(Inform.Utils.createElement("A", {
		// href: "http://www.inform.com/" + this.searchTerms.join(","),
		href: "http://www.inform.com/" + Inform.Utils.joinAndEncode(this.searchTerms, ","),
		id: "inform-more-news"
	}, "More " + this.searchTerms.join(", ") + " News"));
	
	// alert("More " + this.searchTerms.join(", ") + " News");

	this.wrapper.appendChild(Inform.Utils.createElement("DIV", {
		id: "inform-ad"
	}));
	
	/* the following code is the hackiest hack ever. we override document.write to get the ad content. */
	
	if (this._writer == null)
	{
		this._writer = document.write;
	};
	
	document.write = function (adContent)
	{
		Inform.Utils.renderAd(adContent);
	};

	var adScript = Inform.Utils.createElement("SCRIPT", {
		src: "http://context.inform.com/cgi-bin/context.cgi?id=86827154&db=context&format=plain&numresults=1",
		type: "text/javascript"
	});
	
	if (adScript.readyState)
	{
		adScript.onreadystatechange = function ()
		{
			if (adScript.readyState == "loaded")
			{
				document.write = Inform.ArticleData._writer;
			};
		};
	}
	else
	{
		adScript.onload = function ()
		{
			document.write = Inform.ArticleData._writer;
		};
	};
		
	this.wrapper.appendChild(adScript);
	
	/*
	this.wrapper.appendChild(Inform.Utils.createElement("IFRAME", {
		id: "inform-ad-loader",
		src: this.hostname + "/inform2/feeds/resources/content/ad-module.aspx?q=" + escape(this.searchTerm),
		frameBorder: "0"
	}));
	*/
	
	this.wrapper.appendChild(Inform.Utils.createElement("A", {
		id: "inform-branding",
		href: "http://www.inform.com",
		target: "_blank"
	}));

	this.wrapper.appendChild(Inform.Utils.createElement("IFRAME", {
		id: "inform-tracker",
		src: this.hostname + "/inform2/feeds/resources/content/tracker.aspx?u=" + escape(location.href),
		frameBorder: "0"
	}));

};

/* clear the article list */
Inform.ArticleData.clearWrapper = function ()
{
	var childCount = this.wrapper.childNodes.length;
	var childIndex = 0;
	
	for (var i = 0; i < childCount; i++)
	{
		if (!(this.wrapper.childNodes[childIndex].nodeType == "1" && this.wrapper.childNodes[childIndex].tagName == "SCRIPT"))
		{
			this.wrapper.removeChild(this.wrapper.childNodes[childIndex]);
		}
		else
		{
			childIndex++;
		};
	};
};

/* store an article */
Inform.ArticleData.addArticle = function (articleData)
{
	if (typeof articleData.type == "undefined")
	{
		articleData.type = "news";
	};

	if (typeof this.articles[articleData.type] == "undefined")
	{
		this.articles[articleData.type] = new Array();
	};
	
	this.articles[articleData.type].push(articleData);
};

// -----------------------------------------------------
// Store the search input
// -----------------------------------------------------
Inform.ArticleData.setHostname("http://widget.inform.com");

// -----------------------------------------------------
// Store the search input
// -----------------------------------------------------
Inform.ArticleData.setSearchTerms(["Frank Lloyd Wright"]);

// -----------------------------------------------------
// Store the articles
// -----------------------------------------------------

Inform.ArticleData.addArticle({
	title: "THE WRIGHT STUFF",
	type: "news",
	date: "5 Hours Ago",
	articleId: "67309841",
	publication: "Nylon Magazine",
	externalLink: "http://www.inform.com/articles/67309841/?puburl=http%3a%2f%2fwww.nylonmag.com%2f%3fsection%3darticle%26parid%3d1418"
});
	
Inform.ArticleData.addArticle({
	title: "Buffalo&#145;s &#146;other&#145; Frank Lloyd Wright house will open to overnight guests",
	type: "news",
	date: "5/7/2008",
	articleId: "66966507",
	publication: "Buffalo News",
	externalLink: "http://www.inform.com/articles/66966507/?puburl=http%3a%2f%2fxads.zedo.com%2fads2%2fc%3fa%3d122150%3bg%3d0%3bc%3d311000000%3bi%3d0%3bx%3d3840%3bn%3d311%3bs%3d0%3bk%3dhttp%3a%2f%2fwww.buffalonews.com%2fcityregion%2fbuffaloerie%2fstory%2f339197.html%3fimw%3dY"
});
	
Inform.ArticleData.addArticle({
	title: "Shopping: Joy&#146;s picks",
	type: "news",
	date: "14 Hours Ago",
	articleId: "67254141",
	publication: "Cincinnati Enquirer",
	externalLink: "http://www.inform.com/articles/67254141/?puburl=http%3a%2f%2fnews.enquirer.com%2fapps%2fpbcs.dll%2farticle%3fAID%3d%2f20080509%2fLIFE04%2f805090334%2f1079"
});
	
Inform.ArticleData.addArticle({
	title: "New school elicits students&#146; awe",
	type: "news",
	date: "5/7/2008",
	articleId: "66935916",
	publication: "Indianapolis Star",
	externalLink: "http://www.inform.com/articles/66935916/?puburl=http%3a%2f%2fwww.indystar.com%2fapps%2fpbcs.dll%2farticle%3fAID%3d%2f20080507%2fLOCAL0505%2f805070328"
});
	
Inform.ArticleData.addArticle({
	title: "Commencement 2008: Graduate Helps High School Students Tackle AP Exams",
	type: "news",
	date: "5 Hours Ago",
	articleId: "67308985",
	publication: "Yahoo! News",
	externalLink: "http://www.inform.com/articles/67308985/?puburl=http%3a%2f%2fnews.yahoo.com%2fs%2fusnw%2f20080509%2fpl_usnw%2fcommencement2008__graduate_helps_high_school_students_tackle_ap_exams%3b_ylt%3dA9G_RoLalSRIHj8BCRoEKekE"
});
	

// -----------------------------------------------------
// Initialize the list, and render the articles
// -----------------------------------------------------
Inform.ArticleData.init();
