/* mootools scroll */
var ScrollBar = new Class({

	Implements: [Events, Options],

	options: {
		maxThumbSize: 20,
		wheel: 8,
		arrows: true
	},

	initialize: function(textv, options){
		this.setOptions(options);
		
		this.content = new Element('div', {'class':'scroller_content'}).injectBefore(textv);
		textv.injectInside(this.content);
		
		this.main = new Element('div', {'class':'scroller_main'}).injectBefore(this.content);
		this.content.injectInside(this.main);
		
		var w = parseInt(textv.getStyle('width'));
		this.main.setStyles({
				width: w-30,
				height: textv.getStyle('height'),
				"position":"relative",
				'float':'left',
				'display': 'inline',
				'margin-left':textv.getStyle('margin-left'),
				'top': textv.getStyle('top')
		});
		this.content.setStyles({ 
				width: w-30,
				height: textv.getStyle('height'),
				'overflow': 'hidden',
				'float':'left',
				'display': 'inline',
				"position":"relative"
		});
		textv.setStyles({height:'','margin-left':0, 'width': w-30, 'top': 0});
				
		if (this.options.arrows == true) { this.arrowOffset = 26; }
		else { this.arrowOffset = 0; }

		this.vScrollbar = new Element('div', { 'class': 'vScrollbar' }).injectAfter(this.content);
		this.vScrollbar.setStyles({'float': 'left',	'margin': '0 2px 0 4px'});

		if (this.options.arrows == true) {
			this.arrowUp = new Element('div', { 'class': 'arrowUp' }).injectInside(this.vScrollbar);
		}	

		this.vTrack = new Element('div', { 'class': 'vTrack' }).injectInside(this.vScrollbar);
		this.vTrack.setStyles({'height':380});
			
		this.vThumb = new Element('div', { 'class': 'vThumb' }).injectInside(this.vTrack);
		
		if (this.options.arrows == true) {
			this.arrowDown = new Element('div', { 'class': 'arrowDown' }).injectInside(this.vScrollbar);
		}		
		
		this.bound = {
			'vStart' : this.vStart.bind(this),
			'end'	 : this.end.bind(this),
			'vDrag'	 : this.vDrag.bind(this),
			'wheel'	 : this.wheel.bind(this),
			'vPage'	 : this.vPage.bind(this)
		};

		this.vPosition = {};
		this.vMouse = {};
		this.update();
		this.attach();
		this.zero();
	},

	zero: function() {
		this.content.scrollTop = 0;
		this.vPosition.now = 0;
		this.vUpdateThumbFromContentScroll();
		this.update();
	},
	
	update: function() {
		textv = this.content.getElement('div') || this.content.getElement('ul');
		
		this.main.setStyle('height', this.content.offsetHeight);
		this.vTrack.setStyle('height', this.content.offsetHeight - this.arrowOffset);
		this.main.setStyle('width', this.content.offsetWidth + ((textv.offsetWidth != this.content.offsetWidth - 20)?20:0));
		
		// Remove and replace vertical scrollbar
		// console.log('content scrollHeihgt:'+this.content.scrollHeight);
		// console.log('main offsetHeight'+this.main.offsetHeight);
		
		if (this.content.scrollHeight <= this.main.offsetHeight) {
			this.vScrollbar.setStyle('display', 'none');
			this.content.setStyle('width', this.content.offsetWidth + 20);
		}
		else {
			this.vScrollbar.setStyle('display', 'block');
		}
		
		/* poprawka dla scrollerów dokładanych i update'owanych przez ajax'a */
		if(textv.offsetWidth != this.content.offsetWidth)
			this.content.setStyle('width', textv.offsetWidth);
		/* koniec poprawki */
		
		// Vertical
		this.vContentSize = this.content.offsetHeight;
		this.vContentScrollSize = this.content.scrollHeight;
		this.vTrackSize = this.vTrack.offsetHeight;
		this.vContentRatio = this.vContentSize / this.vContentScrollSize;
		this.vThumbSize = 22; 		
		this.vSlideSize = this.vTrackSize - this.vThumbSize;
		
		this.vScrollRatio = this.vContentScrollSize - this.vContentSize;
		this.vThumb.setStyle('height', this.vThumbSize);

		this.vUpdateThumbFromContentScroll();
		this.vUpdateContentFromThumbPosition();
	},

	vUpdateContentFromThumbPosition: function(){
		this.content.scrollTop = (this.vPosition.now / this.vSlideSize) * (this.vContentScrollSize - this.vContentSize);
	},
	
	vUpdateThumbFromContentScroll: function(){
		this.vPosition.now = ((this.content.scrollTop * this.vSlideSize) / this.vScrollRatio).limit(0, this.vSlideSize);
		this.vThumb.setStyle('top', this.vPosition.now);
	},
	
	attach: function(){
		this.vThumb.addEvent('mousedown', this.bound.vStart);
		
		if (this.options.wheel) this.content.addEvent('mousewheel', this.bound.wheel);
		
		if (this.options.arrows == true){
			this.arrowUp.addEvent('mousedown', function(event){
					this.interval = (function(event){
					this.content.scrollTop -= this.options.wheel;
					this.vUpdateThumbFromContentScroll();
				}.bind(this).periodical(60))
			}.bind(this));
		
			this.arrowUp.addEvent('mouseup', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowUp.addEvent('mouseout', function(event){
				$clear(this.interval);
			}.bind(this));
					
			this.arrowDown.addEvent('mousedown', function(event){
					this.interval = (function(event){
					this.content.scrollTop += this.options.wheel;
					this.vUpdateThumbFromContentScroll();
				}.bind(this).periodical(40))
			}.bind(this));
		
			this.arrowDown.addEvent('mouseup', function(event){
				$clear(this.interval);
			}.bind(this));
		
			this.arrowDown.addEvent('mouseout', function(event){
				$clear(this.interval);
			}.bind(this));
		}			
	},
	
	wheel: function(event){
		this.content.scrollTop -= event.wheel * this.options.wheel;
		this.vUpdateThumbFromContentScroll();
		event.stop();
	},

	vPage: function(event){
		if (event.page.y > this.vThumb.getPosition().y) this.content.scrollTop += this.content.offsetHeight;
		else this.content.scrollTop -= this.content.offsetHeight;
		this.vUpdateThumbFromContentScroll();
		event.stop();
	},
	
	vStart: function(event){
		this.vMouse.start = event.page.y;
		this.vPosition.start = this.vThumb.getStyle('top').toInt();
		document.addEvent('mousemove', this.bound.vDrag);
		document.addEvent('mouseup', this.bound.end);
		this.vThumb.addEvent('mouseup', this.bound.end);
		event.stop();
	},
	
	end: function(event){
		document.removeEvent('mousemove', this.bound.vDrag);
		document.removeEvent('mousemove', this.bound.hDrag);			
		document.removeEvent('mouseup', this.bound.end);
		this.vThumb.removeEvent('mouseup', this.bound.end);
		event.stop();
	},

	vDrag: function(event){
		this.vMouse.now = event.page.y;
		this.vPosition.now = (this.vPosition.start + (this.vMouse.now - this.vMouse.start)).limit(0, this.vSlideSize);
		this.vUpdateContentFromThumbPosition();
		this.vUpdateThumbFromContentScroll();
		event.stop();
	}
});

/* obsługa scrollerów */
function addScrollers(context) {
	var i;
	if(document.scr) { i = document.scr.length; }
	else { document.scr = new Array(); i = 0; }
	
	var prefix = '';
	if(context != 'document') {
		prefix = context;
	}
	$$(prefix+'.scroll').each(function(textv){
		
		if(textv.getParent().hasClass('scroller_content')) return;
		
		var height = 0;

		textv.getChildren().each(function (el) {
			if(el.getStyle('display') != 'none') {
				if(el.get('tag')=='ul') {
					if(el.offsetHeight == 0) {
						el.getChildren().each(function(li) {
							if(li.hasClass('gallery') || $$('#infoNoneTopNews').length > 0) {
								if(li.getSize().size) height += parseInt(li.getSize().size.y);
								else height += li.getSize().y;
							}
						});
						if($$('#infoNoneTopNews').length == 0) 
							height = parseInt(height/3);
					}
					else {
						height += el.offsetHeight;
					}
				}
				else {
					if(el.getSize().size) height += parseInt(el.getSize().size.y);
					else height += el.getSize().y;
				}
			}
		});
		
		var b = parseInt(textv.getStyle('height').slice(0,-2))+12;
		
		// window.status += textv.id+': '+ height+' '+b+ ' | ';
		
		if(height > b) {
			textv.setStyle('display', 'block');
			textv.setStyle('overflow', 'visible');
			document.scr[i] = new ScrollBar(textv);
			i += 1;
		}
	});
}

/* update i zerowanie scrollerów */
function updateScrollers () { for(i in document.scr) { try { if(typeof document.scr[i] == 'object') { document.scr[i].update(); } } catch(exception) {} } }
function zeroScroller(which) { for(i in document.scr) { try { if(typeof document.scr[i] == 'object' && document.scr[i].content.getElement('div').getAttribute('id') == which) { document.scr[i].zero();	} }	catch(exception) {} } }
/* koniec obsługi scrollerów */
