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.nID = this._xmlDoc.documentElement.getAttribute("id");
	}
	AjaxModule.prototype.doOnRequestsComplete.apply(this, arguments);
	if (this._xmlDoc) {
		this.emitters.newsID.invoke(this.nID, this);
		this.emitters.srcID.invoke(this.nID, 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.nID + '&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.nID, 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.nID);
	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;

//overwriting class itemclass. 
//Newslistitem is derivated from Ajaxlistitem which is derivated from prototypeclass itemClass. 
//Because we need some extra-img Parameters, we overload the constructor and the load function of the AjaxListItem.class in ajax_base.js

//extended function load
function NewsListItem_load() {
  if (!this._loading) {
		this._loading = true;
		this._xmlDoc = null;
		new Ajax.Request(
			this.ownerList.hrefItemXml,
			{
				method: 'get',
				parameters: {'id': this.id, 'rev' : this.revision, 'fisp' : this.fullImageSizePath, 'sisp' : this.smallImageSizePath, 'udis' : this.usedefaultimagesize, 'lid' : this.lid , 'nodeID' : this.nodeID,'rootNodeID' : this.rootNodeID},
				onComplete: this.doOnRequestComplete.bind(this),
				onFailure: this.doOnFailure.bind(this),
				onException: this.doOnException.bind(this)
			});
	}
}

//extended constructor
function NewsListItem(/* AjaxList */ownerList, /* DomNode */ownerNode, /* String */id/* = NULL */, /* String */revision/* = NULL */, /* String */nodeID/* = NULL */,fullImageSizePath, smallImageSizePath,usedefaultimagesize,lid) {
	AjaxListItem.apply(this, arguments); // superclass constructor
	this.fullImageSizePath = fullImageSizePath || ownerNode.getAttribute('fisp');
	this.smallImageSizePath = smallImageSizePath || ownerNode.getAttribute('sisp');	
	this.usedefaultimagesize = usedefaultimagesize || ownerNode.getAttribute('udis');
	this.lid = lid ||ownerNode.getAttribute('lid');

}
Object.extend(NewsListItem.prototype, AjaxListItem.prototype);
//derivation of constructor and function load
NewsList.prototype.itemClass = NewsListItem;
NewsListItem.prototype.load = NewsListItem_load;

function videoTeaserPagerNext(instance)
{
	var curP = instance.pager.getCurrentPage();
	instance.pager.setCurrentPage(curP+1);
}

function videoTeaserPagerPrev(instance)
{
	var curP = instance.pager.getCurrentPage();
	instance.pager.setCurrentPage(curP-1);
}

//writes stored video as embedded object in give div
function embeddedVideoNews(Video,VideoType,VideoHeight,VideoWidth,IDVideoDIV)
{
	if(VideoType == '17') //youtube
	{
		var eVideoYT = '<object width="'+VideoWidth+'" height="'+VideoHeight+'">'
						+'<param name="movie" value="'+Video+'&amp;hl=en&amp;fs=1&amp;border=0"></param>'
						+'<param name="allowFullScreen" value="true"></param>'
						+'<param name="allowscriptaccess" value="always"></param>'
						+'<embed src="'+Video+'&amp;hl=en&amp;fs=1&amp;border=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+VideoWidth+'" height="'+VideoHeight+'"></embed>'
						+'</object>';
						
		document.getElementById(IDVideoDIV).innerHTML = fixIE6EmbedVideo(eVideoYT.replace(/\?v=/g,'/v/'));
	}
	if(VideoType == '18') //vimeo
	{	
		var eVideoV = '<object width="'+VideoWidth+'" height="'+VideoHeight+'">'
						+'<param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" />'
						+'<param name="movie" value="'+Video+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=ff6600&amp;fullscreen=1" />'
						+'<embed src="'+Video+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=ff6600&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+VideoWidth+'" height="'+VideoHeight+'"></embed>'
						+'</object>';
		document.getElementById(IDVideoDIV).innerHTML = fixIE6EmbedVideo(eVideoV.replace(/com\//g,'com/moogaloop.swf?clip_id='));	
	}
}

//fixes a error when embedding video in IE6
function fixIE6EmbedVideo(embedCode) {
   if(embedCode && embedCode.toLowerCase().indexOf('classid') == -1) {
      var objPos = embedCode.toLowerCase().indexOf('object ') + 'object '.length;
      return embedCode.substr(0, objPos) + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' + embedCode.substr(objPos);
   } else {
      return embedCode;
   }
}


//displays standardlayout for CL News Search
function newsDefaultSearch()
{
	getElementsByClassName(document,"div","pager2").each(
		function(item)
		{
			item.style.display = 'none';
		}
	);
	getElementsByClassName(document,"div","Typ1").each(
		function(item)
		{
			item.style.display = '';
		}
	);	

	getElementsByClassName(document,"div","itemClear").each(
		function(item)
		{
			item.style.display = 'none';
		}
	);
	getElementsByClassName(document,"div","itemTop5").each(
		function(item)
		{
			item.style.display = '';
		}
	);
}

//displays layoutmode1 (show all matching news in category) for CL News Search
function newsViewAll(moduleID)
{
		getElementsByClassName(document,"div","itemClear").each(
		function(item)
		{
			item.style.display = 'none';
		}
	);
		getElementsByClassName(document,"div","item"+moduleID).each(
		function(item)
		{
			item.style.display = '';
		}
	);		
		getElementsByClassName(document,"div","pager2").each(
		function(item)
		{
			item.style.display = 'none';
		}
	);		
		getElementsByClassName(document,"div","Typ3").each(
		function(item)
		{
			item.style.display = '';
		}
	);		
		getElementsByClassName(document,"div","Typ3"+moduleID).each(
		function(item)
		{
			item.style.display = 'none';
		}
	);		
		getElementsByClassName(document,"div","Typ2"+moduleID).each(
		function(item)
		{
			item.style.display = '';
		}
	);		
}
