/**
	Recursive function for news day block formatting
	It will decrease the length of the news body while the 
	height of the block higher than overflow.
*/
function FormatNewsDay() {
	objContainer 	= $('newsDayContainer');
	objTopBlock 	= $('newsDayTop');
	objBottomBlock 	= $('newsDayBottom');
	objBody 		= $('newsDayBody');
	
	if(objContainer && objTopBlock && objBottomBlock && objBody) {
		
		arrContainerSize 	= objContainer.getSize()
		arrTopSize 			= objTopBlock.getSize()
		arrBottomSize 		= objBottomBlock.getSize()
				
		if(arrBottomSize["size"]["y"] + arrTopSize["size"]["y"] > arrContainerSize["size"]["y"]) {
			
			if(newsText = objBody.getFirst().innerHTML) {
				
				objBody.getFirst().innerHTML = newsText.substring(0, newsText.length - 15) + "...";
				
				FormatNewsDay();
			}
		} else {
			var vInArr = objBody.innerHTML.match(/<span>(.*?)<\/span>&nbsp;(.*)/i);
			objBody.innerHTML = vInArr[1];
			if (vInArr[2]) objBottomBlock.innerHTML = objBottomBlock.innerHTML + vInArr[2];
		}
	}
}

var ajaxNews = new Class({
	initialize: function(inUrl,inOptions) {
		this.tBuffer  = '';
		this.tUrl     = inUrl;
		this.tOptions = {
			onComplete: function(response) { this._after() }.bind(this), 
			onFailure:  function()         { alert('Ajax Error') } 
		};
		$extend(this.tOptions,inOptions);
		this.tElem    = this.tOptions.control ? $(this.tOptions.control) : false;
	},
	_before: function() {
		if (this.tElem) { 
			this.tBuffer=this.tElem.innerHTML;
			this.tElem.innerHTML='<img src="' + this.tOptions.imgPath + 'img/rb3.gif" width="15" height="15" alt="" title="" class="vmid fr mrm5" />' + 
			                     '<img src="' + this.tOptions.imgPath + 'img/lb3.gif" width="15" height="15" alt="" title="" class="vmid fr mrm5" />';
			
		}
	},
	_after: function() {
		if (this.tBuffer) {
			var vNextId = this.tOptions.next&&$(this.tOptions.next) ? $(this.tOptions.next).innerHTML : 0;
			var vPrevId = this.tOptions.prev&&$(this.tOptions.prev) ? $(this.tOptions.prev).innerHTML : 0;
			if (vNextId) this.tBuffer=this.tBuffer.replace(/\.next\(\d+\)/,'.next(' + vNextId + ')');
			if (vPrevId) this.tBuffer=this.tBuffer.replace(/\.prev\(\d+\)/,'.prev(' + vPrevId + ')');
			this.tElem.innerHTML=this.tBuffer;
			this.tBuffer='';
			FormatNewsDay();
		}
	},
	_send: function(inId,inType) {
		$H(this.tOptions).extend({'data':(inType==='prev' ? 'ajaxPrevId=' : 'ajaxNextId=') + inId.toInt() + '&ajaxAllIds=' + this.tOptions.all});
		new Requester(this.tUrl,this.tOptions);
	},
	prev: function(inId) {
		this._before(inId);
		this._send(inId,'prev');
	},
	next: function(inId) {
		this._before();
		this._send(inId,'next');
	}
});