function NewsArticle(){
	AjaxModule.apply(this, arguments); // superclass constructor
	this.emitters.catIDs = new CallbackList();
	this.emitters.catArgs = new CallbackList();
	this.emitters.newsID = new CallbackList();
	this.emitters.srcID = new CallbackList();
	this.rankHandler = '/mod/v3/news/newsRank.asp';
	this.minRankInterval = 24; //hours
}
function NewsArticle_load() {
	AjaxModule.prototype.load.apply(this, arguments);
}
function NewsArticle_doOnRequestsComplete(request) {
	if (this._xmlDoc) {
		this.newsID = this._xmlDoc.documentElement.getAttribute("id");
	}
	AjaxModule.prototype.doOnRequestsComplete.apply(this, arguments);
	if (this._xmlDoc) {
		this.emitters.newsID.invoke(this.newsID, this);
		this.emitters.srcID.invoke(this.newsID, this);
		var catIDs = [];
		var catArgs = [];
		var cats = this._xmlDoc.getElementsByTagName('cat');
		for (var i = 0; i < cats.length; i++) {
			catIDs.push(cats[i].getAttribute('id'));
			catArgs.push(cats[i].getAttribute('arg'));
		}
		this.emitters.catIDs.invoke(catIDs, this);
		this.emitters.catArgs.invoke(catArgs, this);
	}
	this.acquireRankTools();
}
function NewsArticle_rank(ranking) {
	if (!this._rankSubmitting) {
		new Ajax.Request(
			this.rankHandler,
			{
				method: 'get',
				parameters: 'news_id=' + this.newsID + '&ranking=' + ranking,
				onComplete: this.doOnRanked.bind(this, ranking),
				onFailure: this.doOnFailure.bind(this),
				onException: this.doOnException.bind(this)
			});
		this._rankSubmitting = true;
	}
}
function NewsArticle_doOnRanked(ranking, request) {
	this._rankSubmitting = false;
	if (request.responseXML) {
		var node = request.responseXML.documentElement;
		if (node.getAttribute("success")) {
			var expire = new Date();
			expire.setTime(expire.getTime() + Math.round(this.minRankInterval * 60 * 60 * 1000));
			setCookie("newsRank" + this.newsID, ranking, expire, "/");
			this.showUserRanking(ranking);
		}
	}
}
function NewsArticle_acquireRankTools() {
	var contEls = getDescendantElementsByClassName(this.htmlContEl, 'rankTool');
	this._rankTools = contEls.collect(function(contEl) {
		var rankTool = {
			contEl: contEl,
			rankIt: {
				contEl: getDescendantElementsByClassName(contEl, 'rankIt')[0],
				buttons: []
			},
			ranked: {contEl: getDescendantElementsByClassName(contEl, 'ranked')[0]},
			noCookies: {contEl: getDescendantElementsByClassName(contEl, 'noCookies')[0]},
			hlSrc: getFlatTree(contEl, 
				function(el) { return el.nodeName.toLowerCase() == 'img' && el.className == 'rk0' })[0].src
		};
		for (var i = 0; i < 5; i++) {
			rankTool.rankIt.buttons[i] = {};
			rankTool.rankIt.buttons[i].img = getFlatTree(rankTool.rankIt.contEl, 
				function(el) {return el.nodeName.toLowerCase() == 'img' && el.className == 'rk'  + (i + 1)})[0];
			rankTool.rankIt.buttons[i].origSrc = rankTool.rankIt.buttons[i].img.src;
		}
		return rankTool;
	});
	var ranking = getCookie("newsRank" + this.newsID);
	if (ranking) this.showUserRanking(parseInt(ranking));
	else if (!navigator.cookieEnabled) this.showUserRanking(-1);
	else this._rankTools.each(function(rankTool){
		rankTool.rankIt.contEl.style.display = '';
		rankTool.ranked.contEl.style.display = 'none';
		rankTool.noCookies.contEl.style.display = 'none';
	});
}
function NewsArticle_showUserRanking(ranking) {
	if (ranking >= 0) this._rankTools.each(function(rankTool){
		for (var i = 0; i < ranking; i++) {
			getFlatTree(rankTool.ranked.contEl, 
				function(el) {return el.nodeName.toLowerCase() == 'img' && el.className == 'rk'  + (i + 1)})[0].src = rankTool.hlSrc;
		}
		rankTool.rankIt.contEl.style.display = 'none';
		rankTool.ranked.contEl.style.display = '';
		rankTool.noCookies.contEl.style.display = 'none';
	});
	else this._rankTools.each(function(rankTool){
		rankTool.rankIt.contEl.style.display = 'none';
		rankTool.ranked.contEl.style.display = 'none';
		rankTool.noCookies.contEl.style.display = '';
	});

}
function NewsArticle_rankToolHighlight(ranking) {
	this._rankTools.each(function(rankTool){
		rankTool.rankIt.buttons.each(function(button, i){
			if (i < ranking) button.img.src = rankTool.hlSrc;
			else button.img.src = button.origSrc;
		});
	});
}
Object.extend(NewsArticle.prototype, AjaxModule.prototype);
NewsArticle.prototype.load = NewsArticle_load;
NewsArticle.prototype.doOnRequestsComplete = NewsArticle_doOnRequestsComplete;
NewsArticle.prototype.rank = NewsArticle_rank;
NewsArticle.prototype.doOnRanked = NewsArticle_doOnRanked;
NewsArticle.prototype.acquireRankTools = NewsArticle_acquireRankTools;
NewsArticle.prototype.rankToolHighlight = NewsArticle_rankToolHighlight;
NewsArticle.prototype.showUserRanking = NewsArticle_showUserRanking;


function NewsArticleStatic() {
	NewsArticle.apply(this, arguments); // superclass constructor
	AjaxModuleStatic.apply(this, arguments); // superclass constructor
}
function NewsArticleStatic_doOnRequestsComplete() {
	AjaxModuleStatic.prototype.doOnRequestsComplete.apply(this, arguments);
	this.acquireRankTools();
}
function NewsArticleStatic_processStaticContent() {
	AjaxModuleStatic.prototype.processStaticContent.apply(this, arguments);
	this.acquireRankTools();
}
Object.extend(NewsArticleStatic.prototype, NewsArticle.prototype);
Object.extend(NewsArticleStatic.prototype, AjaxModuleStatic.prototype);
NewsArticleStatic.prototype.doOnRequestsComplete = NewsArticleStatic_doOnRequestsComplete
NewsArticleStatic.prototype.processStaticContent = NewsArticleStatic_processStaticContent;


function NewsList() {
	AjaxList.apply(this, arguments); // superclass constructor
	this.teaserLength = 200;
}
function NewsList_load() {
	AjaxList.prototype.load.apply(this, arguments);
}
function NewsList_doOnItemRequestComplete(request, item) {
	if (item._xmlDoc) {
		var teaser = item._xmlDoc.documentElement.getAttribute('tsr');
		if (teaser && teaser.length > this.teaserLength) {
			teaser = teaser.substr(0, this.teaserLength)
			switch (teaser.charAt(teaser.length)) {
				case ' ', '.', ',', ':', '!', '?': break;
				default:
					var p = teaser.lastIndexOf(' ');
					if (p > 0) teaser = teaser.substr(0, p);
					break;
			}
			item._xmlDoc.documentElement.setAttribute('tsr', teaser.substr(0, this.teaserLength));
		}
		AjaxList.prototype.doOnItemRequestComplete.apply(this, arguments);
	}
}
Object.extend(NewsList.prototype, AjaxList.prototype);
NewsList.prototype.load = NewsList_load;
NewsList.prototype.doOnItemRequestComplete = NewsList_doOnItemRequestComplete;

function NewsListItem() {
	AjaxListItem.apply(this, arguments); // superclass constructor
}
Object.extend(NewsListItem.prototype, AjaxListItem.prototype);
NewsList.prototype.itemClass = NewsListItem;

