if (typeof(ncard) == "undefined") {
	ncard = {};
}

if (typeof(ncard.WhatsNew) == "undefined") {
	ncard.WhatsNew = {};
}

ncard.WhatsNew = function(id, param)
{
	this.container = id;
	if (!this.container)
		return 0;
		
	this.iter = 0;
	this.news = [];
	
	this.oP = param ? param : {};
	//defaults
	var k, def = {timeout:5000, effect:"fade"};
	for (k in def) {
		if (typeof(this.oP[k]) != typeof(def[k])) {
			this.oP[k] = def[k];
		}
	}
	
	if (!this.oP.url) {
		return 0;
	}
	
	//Do AJAX and setup animation
	var pointer = this;
	WDN.jQuery.get(this.oP.url, function(data) {
		var newsEles = data.getElementsByTagName("news");
		var newsNum = newsEles.length;
		
		for (var i=0; i<newsNum; i++) {
			var sTitle = newsEles[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
			var sLink = "";
			try {
				sLink = newsEles[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
			} catch(e) {}
			pointer.news.push(new ncard.News(sTitle, sLink));
		}
		
		pointer.iter = Math.floor(Math.random()*newsNum);
		pointer.animate();
	});
};

ncard.WhatsNew.prototype.animate = function()
{
	var pointer = this;
	if (this.oP.effect == "fade") {
		WDN.jQuery("#" + this.container).fadeOut("normal", function() { pointer.iterateNews() });
	} else if (this.oP.effect == "slide") {
		WDN.jQuery("#" + this.container).slideUp("normal", function() { pointer.iterateNews() });
	}
};

ncard.WhatsNew.prototype.iterateNews = function()
{
	WDN.jQuery("#" + this.container).empty();
	if (this.news.length == 0) {
		WDN.jQuery("#" + this.container).append("No News News");
		return 0;
	}
	WDN.jQuery("#" + this.container).append(this.news[this.iter].toHTML());
	
	if (this.oP.effect == "fade") {
		WDN.jQuery("#" + this.container).fadeIn();
	} else if (this.oP.effect == "slide") {
		WDN.jQuery("#" + this.container).slideDown();
	}
	
	this.iter++;
	if (this.iter >= this.news.length) {
		this.iter = 0;
	}
	
	if (this.news.length > 1) {
		var pointer = this;
		this.toID = setTimeout( function() { pointer.animate() }, this.oP.timeout );
	}
};


if (typeof(ncard.News) == "undefined") {
	ncard.News = {};
}

ncard.News = function(sTitle, sLink)
{
	this.title = sTitle;
	this.href = sLink;
};

ncard.News.prototype.toHTML = function()
{
	var oReturn = "";
	
	if (this.href != "") {
		oReturn = document.createElement("a");
		oReturn.href = this.href;
		oReturn.title = this.title;
		oReturn.appendChild(document.createTextNode(this.title));
	} else {
		oReturn = document.createTextNode(this.title);
	}
		
	return oReturn;
};