Element.implement({
	setCoordinates : function (left, top, width, height) {
		if (left != null) this.setStyle('left', left);
		if (top != null) this.setStyle('top', top);
		if (width != null) this.setStyle('width', width);
		if (height != null) this.setStyle('height', height);
	},
	setPosition : function (left, top) {
		if (left != null) this.setStyle('left', left);
		if (top != null) this.setStyle('top', top);
	},
	getInnerSize : function () {
		var coords = this.getCoordinates();
		return {
			width: coords.width-this.getStyle('padding-left').toInt()-this.getStyle('padding-right').toInt(),
			height: coords.height-this.getStyle('padding-top').toInt()-this.getStyle('padding-bottom').toInt()
		}
	}
});

var News = new Class({
	initialize: function() {	
	},
	resize : function() {
		this.calculateNews();
	},
	calculateNews : function ()  {
		$$('div.newsdata').each( function (news) {
			//var item = news.getCoordinates();
			var place = $('news-place').getCoordinates();
			if (this.newsTop == null)
				this.newsTop = place.top;
			
			var shade = news.getPrevious();
			
			var top = news.get('wx-top') != null ? parseInt(news.get('wx-top')): 0;
			var right = news.get('wx-right') != null ? parseInt(news.get('wx-right')): 0;
			var nw = parseInt(news.get('wx-width'));
			var nh = parseInt(news.get('wx-height'));
			var left = place.left+place.width-nw-right;
			
			this.newsTop += top;
		
			shade.setCoordinates(left, this.newsTop, nw, nh);
			news.setCoordinates(left, this.newsTop, nw, nh);
			
			var nbdy = news.getElement('.body');
			nbdy.setStyle('height', news.getInnerSize().height-news.getElement('.title').getCoordinates().height-4);
			
			if (nbdy.get('sbar-checked') == null) {
				nbdy.set('sbar-checked', 1);
				if (nbdy.getScrollSize().y > nbdy.getCoordinates().height) { // scrollbar required
					var newscr = new Element('div', {'class' : 'newscr'});
					var knob = new Element('div', {'class' : 'knob'}).inject(newscr);
					newscr.inject(news, 'top');
					
					
					new Slider(newscr, knob, {
						range: [0, 100],
						onChange: function(value){
							var par = this.options.newsEl;
							var y = Math.round((par.getScrollSize().y-par.getSize().y)/100*value);
							par.scrollTo(0, y);
						},
						mode: 'vertical',
						newsEl: nbdy
					});
				}
			}
			
			var shbotm = shade.getPrevious();
			var shtop = shbotm.getPrevious();
			var shright = shtop.getPrevious();
			var shleft = shright.getPrevious();
			
			shtop.setCoordinates(left, this.newsTop-3, nw, 3);
			shbotm.setCoordinates(left, this.newsTop+nh, nw, 3);
			shleft.setCoordinates(left-2, this.newsTop, 3, nh);
			shright.setCoordinates(left+nw, this.newsTop, 3, nh);
			
			this.newsTop += parseInt(news.getStyle('height').toInt());
		}.bind(this));
		this.newsTop = null;
	},
	introChain : function( ) {
		
	}

});
