function MegaItem( LayerName, ImageName, Step ) {
	this.Active = false;
	this.LayerName = LayerName;
	this.ImageName = ImageName;
	this.Step = Step;
}

function MegaMenu( LeftOffset, TopOffset, Increment, UnactiveImage, ActiveImage ) {
	this.LeftOffset = LeftOffset;
	this.TopOffset = TopOffset;
	this.Increment = Increment;
	this.UnactiveImage = UnactiveImage;
	this.ActiveImage = ActiveImage;
	this.SelectedTopMenuID = 0;
	
	this.Offset = this.TopOffset;
	this.Count = 0;
	this.Items = new Array();
	
	this.NewItem = NewItem;
	this.Click = Click;
	this.Correct = Correct;
	this.Collapse = Collapse;
	
	function NewItem( LayerName, ImageName, Step ) {
		this.Items[ this.Count ] = new MegaItem( LayerName, ImageName, Step );
		this.Count++;
		return this.Count;
	}

	function Click( ItemNumber ) {
		
		// Collapse previous item
		if( ( this.SelectedTopMenuID > 0 ) && ( this.ItemNumber!=this.SelectedTopMenuID ) && ( this.Items[ ItemNumber ].Step < 1 ) ) {
			this.Items[ this.SelectedTopMenuID ].Active = !this.Items[ this.SelectedTopMenuID ].Active;
			if( this.Items[ this.SelectedTopMenuID ].ImageName.length > 0 )
				if( this.Items[ this.SelectedTopMenuID ].Active )
					ChangeLayerImage( this.Items[ this.SelectedTopMenuID ].LayerName, this.Items[ this.SelectedTopMenuID ].ImageName, this.ActiveImage );
				else
					ChangeLayerImage( this.Items[ this.SelectedTopMenuID ].LayerName, this.Items[ this.SelectedTopMenuID ].ImageName, this.UnactiveImage );	
			if ( this.Count - 1 > this.SelectedTopMenuID ) {
				SubItems = true;
				if( this.Items[ this.SelectedTopMenuID ].Active )
					for( Counter = this.SelectedTopMenuID + 1; Counter < this.Count; Counter++ ) {
						if( this.Items[ Counter ].Step <= this.Items[ this.SelectedTopMenuID ].Step )
							SubItems = false;
						if( SubItems && ( this.Items[ Counter ].Step == this.Items[ this.SelectedTopMenuID ].Step + 1 ) )
							ShowLayer( this.Items[ Counter ].LayerName );
					}
				else
					for( Counter = this.SelectedTopMenuID + 1; Counter < this.Count; Counter++ ) {
						if( !( this.Items[ Counter ].Step > this.Items[ this.SelectedTopMenuID ].Step ) ) SubItems = false;
						if( SubItems ) {
							if( this.Items[ Counter ].ImageName.length > 0 )
								ChangeLayerImage( this.Items[ Counter ].LayerName, this.Items[ Counter ].ImageName, this.UnactiveImage );
							HideLayer( this.Items[ Counter ].LayerName );
							this.Items[ Counter ].Active = false;
						}
					}
			}

		}

		// Take action with selected item	
		this.Items[ ItemNumber ].Active = !this.Items[ ItemNumber ].Active;
		if( this.Items[ ItemNumber ].ImageName.length > 0 )
			if( this.Items[ ItemNumber ].Active )
				ChangeLayerImage( this.Items[ ItemNumber ].LayerName, this.Items[ ItemNumber ].ImageName, this.ActiveImage );
			else
				ChangeLayerImage( this.Items[ ItemNumber ].LayerName, this.Items[ ItemNumber ].ImageName, this.UnactiveImage );	
		if ( this.Count - 1 > ItemNumber ) {
			SubItems = true;
			if( this.Items[ ItemNumber ].Active )
				for( Counter = ItemNumber + 1; Counter < this.Count; Counter++ ) {
					if( this.Items[ Counter ].Step <= this.Items[ ItemNumber ].Step )
						SubItems = false;
					if( SubItems && ( this.Items[ Counter ].Step == this.Items[ ItemNumber ].Step + 1 ) )
						ShowLayer( this.Items[ Counter ].LayerName );
				}
			else
				for( Counter = ItemNumber + 1; Counter < this.Count; Counter++ ) {
					if( !( this.Items[ Counter ].Step > this.Items[ ItemNumber ].Step ) ) SubItems = false;
					if( SubItems ) {
						if( this.Items[ Counter ].ImageName.length > 0 )
							ChangeLayerImage( this.Items[ Counter ].LayerName, this.Items[ Counter ].ImageName, this.UnactiveImage );
						HideLayer( this.Items[ Counter ].LayerName );
						this.Items[ Counter ].Active = false;
					}
				}
		}
		this.Correct();
		// if topmenu selected, change SelectedTopMenuID
		if( this.Items[ ItemNumber ].Step < 1 )
			this.SelectedTopMenuID=ItemNumber;
	}

	function Correct() {
		this.Offset = this.TopOffset;
		for( Counter = 0; Counter < this.Count; Counter++ ) {
			SetLayerXPos( this.Items[ Counter ].LayerName, this.LeftOffset + this.Increment * this.Items[ Counter ].Step );
			SetLayerYPos( this.Items[ Counter ].LayerName, this.Offset );
			if( LayerIsVisible( this.Items[ Counter ].LayerName ) )
				this.Offset += GetLayerHeight( this.Items[ Counter ].LayerName );
		}
	}
	
	function Collapse() {
		for( Counter = 0; Counter < this.Count; Counter++ ) {
			if( this.Items[ Counter ].ImageName.length > 0 )
				ChangeLayerImage( this.Items[ Counter ].LayerName, this.Items[ Counter ].ImageName, this.UnactiveImage );
			this.Items[ Counter ].Active = false;
			if( this.Items[ Counter ].Step > 0 )
				HideLayer( this.Items[ Counter ].LayerName );
			else
				ShowLayer( this.Items[ Counter ].LayerName );
				this.Offset += GetLayerHeight( this.Items[ Counter ].LayerName );
		}
		this.Correct();
	}
}