ProductCatalogMenu = Class.create();
ProductCatalogMenu.prototype = 
{
    initialize: function(OuterDivID)
    {
	    // state variables
        this.Initialized = false;
        this.ClientID = OuterDivID;
               
               
        // object variables
        this.ContainingDiv = $(OuterDivID);
        this.LevelSection = null;
        this.SelectionCompactSection = null;
        this.SelectionFullSection = null;
        this.ContinueButton = null;
        
        // state variables
        this.CurrentLevel = null;
        this.CurrentSelection = null;
        
        
        this.ParseInitialValues();
        
        
        this.InitializeControls();        
        
  
        this.Initialized = true;

        return this.Initialized;
    },
    
    //Helper Function
    ParseInitialValues: function()
    {
        //Retrieve control constructors
        this.LevelConstructor = this.ContainingDiv.getAttribute("LevelConstructor");
        this.SelectionCompactConstructor = this.ContainingDiv.getAttribute("SelectionCompactConstructor");
        this.SelectionFullConstructor = this.ContainingDiv.getAttribute("SelectionFullConstructor");
        this.ContinueConstructor = this.ContainingDiv.getAttribute("ContinueConstructor");
        this.AutoSelectProduct = this.ContainingDiv.getAttribute("AutoSelectProduct");
        
        //Retrieve Other Attributes
        this.NumLevels = this.ContainingDiv.getAttribute("NumLevels");
        this.CurrentLevel = this.ContainingDiv.getAttribute("CurrentLevel");
        this.OnClientLevelChange = this.ContainingDiv.getAttribute("OnClientLevelChange");
        this.OnClientSelectionChange = this.ContainingDiv.getAttribute("OnClientSelectionChange");
        
        this.Part = this.ContainingDiv.getAttribute("Part");
        this.PreviousPart = this.Part;
        
        return true;
    },
    
    //Setup controls to observe valid states.
    InitializeControls: function()
    {
        this.InitializeLevelArea();
        this.InitializeSelectionArea();        
        
        if (this.ContinueConstructor != null && this.AutoSelectProduct == null)
        {
            this.ContinueButton = eval(this.ContinueConstructor);
        }
        
        return;
    },
    
    InitializeLevelArea: function()
    {
        this.LevelSection = null;

        this.LevelSection = eval(this.LevelConstructor);
        this.LevelSection.SetClientClickInContext(this, "this.LevelChange(this.LevelSection.GetSelected(), HandledEvent);");

        return;
    },
    
    InitializeSelectionArea: function()
    {
        this.SelectionCompactSection = null;
        this.SelectionFullSection = null;

        this.SelectionCompactSection = eval(this.SelectionCompactConstructor);
        this.SelectionCompactSection.SetClientClickInContext(this, "this.SelectionChange(true, this.SelectionCompactSection.GetSelected(), HandledEvent);");

        if (this.SelectionFullConstructor != null && this.SelectionFullConstructor.length > 0)
        {
            this.SelectionFullSection = eval(this.SelectionFullConstructor);
            this.SelectionFullSection.SetClientClickInContext(this, "this.SelectionChange(false, this.SelectionFullSection.GetSelected(), HandledEvent);");
        }
        
        return;
    },

       
    UpdateLevel: function(Level)
    {
        this.CurrentLevel = parseInt(Level);
    },

       
    //Event Handlers
    LevelChange: function(Commands, HandledEvent)
    {
        var Command;

        if (Commands != null && Commands.length > 0)
        {
            Command = Commands[0];
            
            this.CurrentLevel = Command.GetArgument("Level");
            
            if (this.OnClientLevelChange.length > 0)
            {
                eval(this.OnClientLevelChange + "(Command);");
                
                //this.InitializeLevelArea();
            }
        }
        
        return;
    },

    SelectionChange: function(FromCompact, Commands, HandledEvent)
    {
        var Command;
        var SelectProduct = false;

        if (Commands != null && Commands.length > 0)
        {
            Command = Commands[0];
            
            if (this.CurrentSelection != null && Command.Equals(this.CurrentSelection))
            {
                //This control command has already been handled.
                return;
            }
            
            this.CurrentSelection = Command;

            if (parseInt(this.CurrentLevel) == parseInt(this.NumLevels)) // Product Selected
            {
                this.Part = Command.GetArgument("Selection");
                
                if (this.Part != this.PreviousPart)
                {
                    if (this.ContinueConstructor != null && this.AutoSelectProduct == null)
                    {
                        $(this.ContinueButton.ClientID).style.display = "block";
                        $(this.ContinueButton.ClientID).style.visibility = "visible";
                    }
                    else
                    {
                        SelectProduct = true;
                    }
                    
                    this.PreviousPart = this.Part;
                }
            }
            else //Non product category selected.
            {
                if (this.ContinueConstructor != null && this.AutoSelectProduct == null)
                {
                    $(this.ContinueButton.ClientID).style.display = "none";
                    $(this.ContinueButton.ClientID).style.visibility = "hidden";
                }
            }
            
            
            if (this.SelectionFullSection != null && FromCompact)
            {
                this.SelectionFullSection.SetSelected(Commands);
            }
            else if (this.SelectionFullSection != null)
            {
                this.SelectionCompactSection.SetSelected(Commands);
            }
            
            if (this.OnClientSelectionChange.length > 0)
            {
                if (SelectProduct)
                {
                    eval(this.OnClientSelectionChange + "(Command, this.AutoSelectProduct);");
                }
                else
                {
                    eval(this.OnClientSelectionChange + "(Command);");
                }
            }
        }
        
        return;
    }
};
