﻿/**********************************************************\
' Name     : navigation.js
' Purpose  : Navigation class
' Created  : 2008-04-02; Internetfabriken
' Modified : 2008-04-02; Internetfabriken
' Comment  :
\**********************************************************/

/// <summary>
// Navigation child panel class
/// </summary>
function NavigationChildPanel(id, panelId, buttonId)
{
    this.id = id;
    this.panelId = panelId;
    this.buttonId = buttonId;
}

/// <summary>
// Navigation class
/// </summary>
function Navigation()
{
    /* Constants */
    var cookieName = 'NavigationPanelId';
    var buttonOnCssClass = 'button_navigation_active';
    var buttonOffCssClass = 'button_navigation';
    
    /* Private */
    var _panelId = null;
    var _showBehavior = null;
    var _hideBehavior = null;
    var _childPanels = new Array();
    
    /// <summary>
    // Register the main panel
    /// </summary>
    this.RegisterMainPanel = function(panelId)
    {
         _panelId = panelId;
    }
    
    /// <summary>
    // Register show animation behaviour
    /// </summary>
    this.RegisterShowBehavior = function(behaviour)
    {
         _showBehavior = behaviour;
    }
    
        /// <summary>
    // Register hide animation behaviour
    /// </summary>
    this.RegisterHideBehavior = function(behaviour)
    {
         _hideBehavior = behaviour;
    }
    
    /// <summary>
    // Register a child panel
    /// </summary>
    this.RegisterChildPanel = function(id, panelId, buttonId)
    {
        try
        {
            for (var i=0; i<_childPanels.length; i++)
            {
                if (_childPanels[i].id == id)
                {
                    // Found existing panel, update it
                    if (panelId != null) _childPanels[i].panelId = panelId;
                    if (buttonId != null) _childPanels[i].buttonId = buttonId;
                    return;
                }
            }
            // Add new panel
            _childPanels.push(new NavigationChildPanel(id, panelId, buttonId));
        }
        catch(err)
        {
            //alert('Navigation.RegisterChildPanel(): ' + err);
        }
    }
    
    /// <summary>
    /// Initiates navigation by checking a cookie and displays the navigation if needed
    /// </summary>
    this.Init = function(preferredId, animate)
    {
        try
        {
            var cookieId = GetCookie(cookieName);
            if (cookieId != null)
            {
                if (cookieId.length > 0) this.Toggle(preferredId, animate);
            }
        } 
        catch(err)
        {
            //alert('Navigation.Init(): ' + err);
        }
    }
   
    /// <summary>
    /// Toggle main panel to/from specified child panel
    /// </summary>
    this.Toggle = function(id, animate)
    {
        // Find child index and get any current index
        var childIndex  = -1;
        var currentChildIndex = -1;
        for (var i=0; i<_childPanels.length; i++)
        {
            try
            {
                var childPanel = _childPanels[i];
                if (childPanel.id == id) childIndex = i;
                if ($(childPanel.panelId).style.display=='block') currentChildIndex = i;
            }
            catch(err)
            {
                //alert('Navigation.Toggle(): ' + err);
            }
        }
        
        if (childIndex >= 0)
        {
            // Found child index, decide what to do
            var navigationPanel = $(_panelId);
            if (navigationPanel.style.display == 'block' && childIndex == currentChildIndex)
            {
                // Main panel is visible and current child is already visible, hide them
                this.Hide(animate);
            }
            else
            {
                // Show main panel with wanted child panel 
                this.Show(id, animate);
            }
        }
        else
        {
            // Unknown child index, hide main panel
            this.Hide(animate);
        }       
    }
    
    
    /// <summary>
    /// Hide main panel (and any current child panel)
    /// </summary>
    this.Hide = function(animate)
    {
        // Hide child panels (well, just change the button class)
        for (var i=0; i<_childPanels.length; i++)
        {
            try
            {
                var childPanel = _childPanels[i];
                if (childPanel.buttonId != null) $(childPanel.buttonId).className=buttonOffCssClass;
            }
            catch(err)
            {
                //alert('Navigation.Hide(): ' + err);
            }
        }
        
        // Hide main panel
        try
        {
            var navigationPanel = $(_panelId);
            if (navigationPanel.style.display == 'block')
            {
                if (animate && _hideBehavior != null)
                {
                    var behavior = $find(_hideBehavior).get_OnClickBehavior().get_animation();
                    behavior.play();
                }
                else
                {
                    navigationPanel.style.display = 'none';
                }
            }
        }
        catch(err)
        {
            //alert('Navigation.Hide(): ' + err);
        }
        
        // Save status to cookie
        SetCookie(cookieName,'',null,'/');
    }


    /// <summary>
    /// Show main panel with specified child panel
    /// </summary>
    this.Show = function(id, animate)
    {
        // Show new child panel and hide others
        for (var i=0; i<_childPanels.length; i++)
        {
            try
            {
                var childPanel = _childPanels[i];
                if (childPanel.id == id)
                {
                    $(childPanel.panelId).style.display='block';
                    if (childPanel.buttonId != null) $(childPanel.buttonId).className=buttonOnCssClass;
                }
                else
                {
                    $(childPanel.panelId).style.display='none';
                    if (childPanel.buttonId != null) $(childPanel.buttonId).className=buttonOffCssClass;
                }
            }
            catch(err)
            {
                //alert('Navigation.Show(): ' + err);
            }
        }
        
        // Show main panel and animate it if wanted
        try
        {
            var navigationPanel = $(_panelId);
            if (navigationPanel.style.display == 'none')
            {
                if (animate && _showBehavior != null)
                {
                    var behavior = $find(_showBehavior).get_OnClickBehavior().get_animation();
                    behavior.play();
                }
                else
                {
                    navigationPanel.style.display = 'block';
                }
            }
            
            // Save status to cookie
            SetCookie(cookieName,id,null,'/');
        }
        catch(err)
        {
            //alert('Navigation.Show(): ' + err);
        }
    }
}