var MainNewsLoader = Class.create({
	label: null,
	helperPath: null,
	articlePath: null,
	position: null,
	title: null,

	initialize: function(label, helperPath, articlePath, position) {
		this.label = label;
		this.helperPath = helperPath;
		this.articlePath = articlePath;
		this.position = position;

		var _this = this;
		$$('#box'+this.label+' a[direction]').each(function(item){
			item.observe('click', _this.update.bindAsEventListener(_this));
		});
	},

	update: function(event)
	{
		event.preventDefault();
		var _this = this;
		var oldPosition = this.position;
		var url =  Event.element(event).getAttribute('direction')== 'next' ? this.helperPath+'&offset='+(++this.position) : this.helperPath+'&offset='+(--this.position);
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(result) {
		  	data = result.responseText.evalJSON();
			if (data && data.id) {
				var articleLnk = data.url;
				$$('#box'+_this.label+' div.boxTitle').first().innerHTML = '<a href="' + articleLnk + '">' + data.title + '</a>';
				$$('#box'+_this.label+' a[direction=next]').first().style.display = data.nextEnabled ? '' : 'none';
				$$('#box'+_this.label+' a[direction=prev]').first().style.display = data.prevEnabled ? '' : 'none';

/*				$$('#box'+_this.label+' div.boxNext').first().style.display = data.nextEnabled ? '' : 'none';
				$$('#box'+_this.label+' div.boxPrev').first().style.display = data.prevEnabled ? '' : 'none';*/
				$$('#box'+_this.label+' a.boxPreview').first().setAttribute('href', articleLnk);
				$$('#box'+_this.label+' a.boxPreview > img').first().setAttribute('src', data.preview);
			} else {
				_this.position = oldPosition;
				$$('#box'+_this.label+' div.'+ (Event.element(event).getAttribute('direction')== 'next' ? 'boxNext' : 'boxPrev')).first().hide();
			}
		  },

		  onComplete: function(result){//error
		  	//alert(result.responseText);
		  }
		});
	}
});