var Navigation = {
	containerID: '',
	_maxHeight: 0,
	_object: null,
	_slider: null,
	_content: null,
	_pos: 0,
	_isClosed: true,
	_timeout: null,
	_animating: false,
	_logo: null,
	_logo2: null,
	_curLogo: null,
	_blocked: null,
	load: function() {
		if( typeof( window.onload ) == 'function' ) {
			this._loadFunc = window.onload;
		}
		var _self = this;
		window.onload = function() { _self.onload(); }
		return this;
	},
	onload: function() {
		this.init( this.containerID );
		if( this._loadFunc ) {
			this._loadFunc();
		}
	},
	init: function( id ) {
		this._object = _$( id );
		if( this._object ) {

			this._logo = _$( 'logo2' );
			this._logo2 = _$( 'logo1' );

			this._slider = getElementsByClassName( 'slider', this._object )[0];
			this._content = getElementsByClassName( 'content', this._object )[0];

			this.initMaxHeight();

			var _self = this;
			this._logo.onmouseover = function() { _self.startExpanding() };
//			this._logo2.onmouseover = function() { _self.startCollapsing() };

			this._blocked = this._object.className == 'blocked';

			if( this._object.className == 'closed' ) {
				this._curLogo = this._logo2;
				this.changeLogoTo( this._logo );
			} else {
				this._curLogo = this._logo;
				this.endProcess( this._maxHeight, false );
				this.changeLogoTo( this._logo2 );
			}
		}

	},
	setAjaxState: function ( ) {
//		var state = this._isClosed ? 'closed' : 'opened';
		var state = 'opened';
		new Ajax.Request( '/main-menu/set-menu-state?state=' + state, { method: 'post' } );
	},
	startCollapsing: function() {
		if( this._blocked ) return;
		if( this._isClosed == false && !this._animating ) {
			this._pos = this._maxHeight;
			this._animating = true;
			this.changeLogoTo( this._logo );
			this.collapse();
		}
	},
	startExpanding: function() {
		if( this._isClosed && !this._animating ) {
			this._pos = 4;
			this._animating = true;
			this.changeLogoTo( this._logo2 );
			this.expand();
		}
	},
	changeLogoTo: function ( logo ) {
		if( logo ) {
			if( this._curLogo ) {
				this._curLogo.style.display = 'none';
			}

			logo.style.display = '';
			this._curLogo = logo;
		}
	},
	endProcess: function( value, closed ) {
		clearTimeout( this._timeout );
		this._pos = value;
		this.setHeight( value );
		this._isClosed = closed;
		this._animating = false;
		this.setAjaxState();
	},
	expand: function(){
		this._pos += this._pos;
		if( this._pos < this._maxHeight ) {
			var _self = this;
			this.setHeight( this._pos );
			this._timeout = setTimeout( function(){ _self.expand(); }, 80 );
		} else {
			this.endProcess( this._maxHeight, false );
		}
	},
	collapse: function(){
		this._pos = this._pos/2;
		if( this._pos > 4 ) {
			var _self = this;
			this.setHeight( this._pos );
			this._timeout = setTimeout( function(){ _self.collapse(); }, 80 );
		} else {
			this.endProcess( 0, true );
		}
	},
	initMaxHeight: function () {
		this._maxHeight = this._content.offsetHeight;
	},
	setHeight: function (value){
		this._slider.style.height = value + 'px';
	}
}

Navigation.load();
