// - - - - HomeAnim	- - - -
HomeAnim = function ( container, bottomContainer ) 
	{
		if( container == null )
			return;
			
		this.AutoTimer = 10;
		this.Interval = null;
		this.Container = container;
		this.BottomContainer = bottomContainer;
		this.Items = [];
		this.ActiveItem = null;
		this.Init();
	}
HomeAnim.prototype.Init = function()
	{
		var homeAnim = this;
		var bottomItems = $( this.BottomContainer ).children();
		$( this.Container ).children().each( function( ind, elem )
				{ 
					homeAnim.Items[ ind ] = new HomeAnimItem( elem, bottomItems[ind], ind, homeAnim );
				} );
		
		var anim = this;
		this.Interval = window.setInterval( function(){ anim.AutoMove(); }, 1000 * this.AutoTimer );
	}
HomeAnim.prototype.AutoMove = function()
	{
		if( this.ActiveItem == null )
			return;
		
		var indexToOpen = this.Items.length > this.ActiveItem.Index+1 ? this.ActiveItem.Index+1 : 0;
		
		var anim = this;
		var callback = null;
		if( indexToOpen == 0 )
			callback = function(){ anim.ResetHidden(); };
		
		this.OpenItem( this.Items[ indexToOpen ], callback, false );
	}
HomeAnim.prototype.ResetHidden = function()
	{
		for( var i=0; i<this.Items.length; i++ )
		{
			if( i <= 5 && !this.Items[ i ].IsVisible )
				this.Items[ i ].Show();
			if( i > 5 && this.Items[ i ].IsVisible )
				this.Items[ i ].Hide();
		}
	}
HomeAnim.prototype.OpenItem = function( animItem, callback, isUserAction )
	{
		if( this.ActiveItem == null || this.ActiveItem == animItem )
			return;
		if( this.ActiveItem != null )
			this.ActiveItem.Close();
		this.ActiveItem = null;
		
		if( isUserAction )
		{
			window.clearInterval(this.Interval);
			this.Interval = null;
		}
		var homeAnim = this;
		var callback = callback != null ? callback : null;
		if( animItem.Index >= 5 && this.Items.length > animItem.Index+1 && !this.Items[ animItem.Index+1 ].IsVisible )
		{
			callback = function() {
					homeAnim.Items[ animItem.Index+1 ].Show();
					homeAnim.Items[ animItem.Index-5 ].Hide();
				}
		}
		if( animItem.Index > 0 && !this.Items[ animItem.Index-1 ].IsVisible )
		{
			callback = function() {
					homeAnim.Items[ animItem.Index-1 ].Show();
					if( homeAnim.Items.length > animItem.Index+5 ) 
						homeAnim.Items[ animItem.Index+5 ].Hide();
				}
		}
		animItem.Open( callback );
	}
	
// - - - - HomeAnimItem	- - - -
HomeAnimItem =  function( elem, bottomElem, index, homeAnim ) 
	{
	 	this.Elem = elem;
	 	this.BottomElem = bottomElem;
	 	this.Index = index;
	 	this.BackDiv = null;
	 	this.Anim = homeAnim;
	 	this.IsActive = false;
	 	this.IsVisible = false;
	 	this.Init();
	}
HomeAnimItem.prototype.Init = function()
	{
		if( $(this.Elem).hasClass( 'HAOpened' ) )
		{
			this.Anim.ActiveItem = this;
			this.IsActive = true;
		}
		
		this.IsVisible = $(this.Elem).css('display') != 'none';
		
		var animItem = this;
		this.BackDiv = $(this.Elem).find('div.HABack').get(0);
		$(this.Elem).click( function(){ animItem.Anim.OpenItem( animItem, null, true ); } );
	}
HomeAnimItem.prototype.Open = function( callback )
	{
		var animItem = this;
		$(animItem.BackDiv).animate( 
			{ opacity: "0" }, 
			"600", 
			function(){ 
				$(animItem.BackDiv).css( "display", "none" );
				$(animItem.BottomElem).animate( { width: "415px" }, "600"); 
				$(animItem.Elem).animate( 
					{ width: "415px" }, 
					"600", 
					function(){ 
						$(animItem.BackDiv).css( "height", "0px" );
						$(animItem.BackDiv).css( "display", "" );
						$(animItem.BackDiv).css( "opacity", "" );
						animItem.Elem.className = "HAOpened";
						$(animItem.BackDiv).animate(
							{ height: "163px" }, 
							"600", 
							function(){
								$(animItem.BackDiv).css( "height", "" );
								animItem.IsActive = true;
								animItem.IsVisible = true;
								animItem.Anim.ActiveItem = animItem;
								
								if( animItem.Anim.Interval == null )
									animItem.Anim.Interval = window.setInterval( function(){ animItem.Anim.AutoMove(); }, 1000 * animItem.Anim.AutoTimer );
								
								if( callback != null )
									callback();
							});
					});
			});
	}
HomeAnimItem.prototype.Close = function()
	{
		var animItem = this;
		$(animItem.BackDiv).animate( 
			{ height: "0px" }, 
			"600", 
			function(){ 
				$(animItem.BackDiv).css( "display", "none" );
				$(animItem.BottomElem).animate( { width: "79px" }, "600"); 
				$(animItem.Elem).animate( 
					{ width: "79px" }, 
					"600", 
					function(){ 
						$(animItem.BackDiv).css( "opacity", "0" );
						$(animItem.BackDiv).css( "display", "" );
						$(animItem.BackDiv).css( "height", "" );
						animItem.Elem.className = "HAClosed";
						$(animItem.BackDiv).animate( 
							{ opacity: 0.5 }, 
							"600", 
							function(){
								$(animItem.BackDiv).css( "opacity", "" );
								animItem.IsActive = false;
							});
					} );
			});
	}
HomeAnimItem.prototype.Show = function()
	{
		var animItem = this;
		$(animItem.Elem).css( "width", "0px" );
		$(animItem.Elem).css( "display", "" );
		$(animItem.Elem).animate( 
			{ width: "79px" }, 
			"fast", 
			function(){ 
				$(animItem.Elem).css( "width", "" );
				animItem.IsVisible = true;
			});

		$(animItem.BottomElem).css( "width", "0px" );
		$(animItem.BottomElem).css( "display", "" );
		$(animItem.BottomElem).animate( { width: "79px" }, "fast" );
	}
HomeAnimItem.prototype.Hide = function()
	{
		var animItem = this;
		$(animItem.BottomElem).animate( { width: "0px" }, "fast" );
		$(animItem.Elem).animate( 
			{ width: "0px" }, 
			"fast", 
			function(){ 
				$(animItem.BottomElem).css( "display", "none" );
				$(animItem.Elem).css( "display", "none" );
				animItem.IsVisible = false;
			});
	}


$(document).ready( function()
						{
							new HomeAnim( $('#oHAContainer').get(0), $('#oHABottomContainer').get(0) );
						} );
