﻿/*
    Name:       Navigation.js
    Purpose:    Navigation class
    Created:    2008-10-06; Internetfabriken
    Modified:   2008-10-28; Internetfabriken
    Comment:    Depends on GetCookie(), SetCookie()
*/

/// <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(id, showId, showCookieId, animate)
    {
        var foundId = false;
        
        try
        {
            if (showId)
            {
                for (var i=0; i<_childPanels.length; i++)
                {
                    var childPanel = _childPanels[i];
                    if (childPanel.id == id)
                    {
                        // Found id, show child panel
                        this.Show(id, animate);
                        foundId = true;
                    }
                }
            }
            
            if (showCookieId && !foundId)
            {
                var cookieId = GetCookie(cookieName);
                if (cookieId != null && cookieId.length > 0)
                {
                    this.Show(cookieId, 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).get(0).style.display=='block') currentChildIndex = i;
            }
            catch(err)
            {
                //alert('Navigation.Toggle(): ' + err);
            }
        }
        try
        {
            if (childIndex >= 0)
            {
                // Found child index, decide what to do
                var navigationPanel = $("#" + _panelId).get(0);
                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);
            }
        }
        catch(err)
        {
            //alert('Navigation.Toggle(): ' + err);
        }
    }
    
    
    /// <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).get(0).className = buttonOffCssClass;
            }
            catch(err)
            {
                //alert('Navigation.Hide(): ' + err);
            }
        }
        
        // Hide main panel
        try
        {
            var navigationPanel = $("#" + _panelId).get(0);
            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).get(0).style.display = 'block';
                    if (childPanel.buttonId != null) $("#" + childPanel.buttonId).get(0).className = buttonOnCssClass;
                }
                else {
                    $("#" + childPanel.panelId).get(0).style.display = 'none';
                    if (childPanel.buttonId != null) $("#" + childPanel.buttonId).get(0).className = buttonOffCssClass;
                }
            }
            catch (err) {
                //alert('Navigation.Show(): ' + err);
            }
        }

        // Show main panel and animate it if wanted
        try {
            var navigationPanel = $("#" + _panelId).get(0);
            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);
        }
    }
}