var highlight = ['#843c3c','#3c843c','#3c3c84','#84843c','#843c84'];
window.addEvent('domready', function(){
	num = 0;
	hei = 0;
	$('menu').getElements('li a').each(function(o){
		o.bck = o.getStyle('background-color');
		o.fx = new Fx.Tween(o, {
			link: 'cancel'
		});
		o.addEvent('mouseenter', function(){
			Tween(this,'on');
		});
		o.addEvent('mouseleave', function(){
			Tween(this,'off');
		});
		num++;
	});
	
	// automatyczny adjust menu galerii
	if($('menu').hasClass('gallery')){
		m = $('menu').getCoordinates();
		b = $('bottom').getPosition();
		mH = b.y-m.top+20;
		HO = Math.ceil(mH/(num-0.2));
		H = 0.8 * HO;
		bB = H*0.25;
		$('menu').getElements('li a').each(function(o){
			heiT = o.getStyle('padding-top').toInt();
			heiB = o.getStyle('padding-bottom').toInt();
			hei = heiT + heiB;
			h = o.getSize().y-hei;
			o.setStyle('padding-top', (H-h-hei)/2+heiT+1);
			o.setStyle('padding-bottom', (H-h-hei)/2+heiB);
			o.getParent().setStyle('border-bottom-width',bB);
		});
		
	}
	
	setBottom();
	setMenu();
	window.addEvent('resize', function(){ setBottom(); });
	
		$$('.scroll').each(function(o){
			new friendlyScroll(o);
		});
});
window.addEvent('load', function(){
	setBottom();
});
var lastcolor;
function Tween(o,m){
	if(m=='on'){
		do
			newcolor = highlight[$random(0,highlight.length-1)];
		while (newcolor == lastcolor);
		lastcolor = newcolor;
		o.fx.start('background-color', newcolor);
	}
	else if(m=='off')
		o.fx.start('background-color', o.bck);
}

function setBottom(){
	w = window.getScrollSize();
	p = $('bottom').getCoordinates();
	h = w.y - p.top;
	if(h>p.height)
		$('bottom').setStyle('height', h);
}

function setMenu(){
	m = $('menu').getCoordinates();
	b = $('bottom').getPosition();
	$('longer').setStyle('height',b.y-m.bottom+20);
}

friendlyScroll = new Class({
	scrollMain: new Element('div', {'class': 'scrollMain', events: {'mousewheel': function(e){
		e = new Event(e).stop();
		this.obj.scrollDiv(e.wheel);
	}}}),
	scrollArea: new Element('div', {'class': 'scrollArea'}),
	scrollBar: new Element('div'),
	arrowUp: new Element('a', {href: '#', 'class': 'scrollArrowUp', events: {'click': function(e){
		new Event(e).stop();
		this.obj.scrollDiv(1);
	}, 'mousedown': function(e){
		this.obj.scrollStart(1);
	}, 'mouseup': function(e){
		this.obj.scrollStop();
	}, 'mouseleave': function(e){
		this.obj.scrollStop();
	}}}),
	arrowDown: new Element('a', {href: '#', 'class': 'scrollArrowDown', events: {'click': function(e){
		new Event(e).stop();
		this.obj.scrollDiv(-1);
	}, 'mousedown': function(e){
		this.obj.scrollStart(-1);
	}, 'mouseup': function(e){
		this.obj.scrollStop();
	}, 'mouseleave': function(e){
		this.obj.scrollStop();
	}}}),
	
	initialize: function(o){
		
		o.setStyle('overflow','hidden');
		if(o.getScrollSize().y != o.getSize().y){
			this.scrollArea.setStyle('height', o.getSize().y);
			this.scrollArea.div = o;
			this.arrowUp.inject(this.scrollArea);
			this.arrowUp.obj = this;
			this.scrollBar.inject(this.scrollArea);
			this.arrowDown.inject(this.scrollArea);
			this.arrowDown.obj = this;
			
			this.scrollMain.inject(o,'before');
			this.scrollMain.adopt(this.scrollArea,o);
			this.scrollMain.obj = this;
			o.setStyle('width',o.getStyle('width').toInt()-5-this.scrollArea.getSize().x);
			
			this.scrollBar.setStyle('height',o.getSize().y-(this.arrowUp.getSize().y+this.arrowDown.getSize().y));
			
		}
			
	},
	
	scrollDiv: function(direction){
		step = -1*direction*10;
		this.scrollArea.div.scrollTo(0,this.scrollArea.div.getScroll().y+step);
		
	},
	
	scrollStart: function(d){
		this.scrollDiv(d);
		this.timer = this.scrollStart.delay(50,this,d);
	},
	
	scrollStop: function(){
		$clear(this.timer);
	}
});










