var Sidebar = new Class({
	sidebar: null,
	sidebarCatcher: null,
	content: null,
	orgSidebarTop: null,
	currentSidebarTop: null,
	
	initialize: function() {
		this.sidebar = $$('.sidebar')[0];
		if(this.sidebar == null) {
			return;
		}
		
		this.sidebarCatcher = $$('.sidebar-catcher')[0]; 
		this.sidebar.addEvent('mouseover', function(event) {
			this.show(event);
		}.bind(this));
		this.sidebarCatcher.addEvent('mouseover', function(event) {
			this.show(event);
		}.bind(this));
		this.sidebar.addEvent('mouseout', function(event) {
			this.hide(event);
		}.bind(this));
		this.sidebarCatcher.setStyle('height', this.sidebar.getSize().y+'px');
		this.scroll.periodical(500, this);
		this.orgSidebarTop = this.sidebar.getStyle('top').toInt();
		this.sidebar.set('morph', {
			duration: 1000,
			transition: Fx.Transitions.Quart.easeOut
		});
		this.sidebarCatcher.set('morph', {
			duration: 1000,
			transition: Fx.Transitions.Quart.easeOut
		});
	},
	
	show: function() {
		this.sidebar.set('tween', {
			duration: 750,
			transition: Fx.Transitions.Quart.easeOut
		});
		this.sidebar.tween('width',  '200px');
	},
	
	hide: function() {
		this.sidebar.set('tween', {
			duration: 1250,
			transition: Fx.Transitions.Bounce.easeOut
		});
		this.sidebar.tween('width', '20px');
	},
	
	scroll: function() {
		var newSidebarTop = this.orgSidebarTop + window.getScroll().y - this.orgSidebarTop + 50;
		var sidebarHeight = this.sidebar.getScrollSize().y;
		var windowHeight = window.getSize().y;
		
		if(newSidebarTop < this.orgSidebarTop)
			newSidebarTop = this.orgSidebarTop;
		if(newSidebarTop != this.currentSidebarTop && sidebarHeight < windowHeight) {
			this.sidebar.morph({
				'top': newSidebarTop+'px'
			});
			this.sidebarCatcher.morph({
				'top': newSidebarTop+'px'
			});
			this.currentSidebarTop = newSidebarTop;
		}
	}
});

window.addEvent('domready', function() {
	new Sidebar();
});
