﻿/**********************************************************\
' Name     : Media.js
' Purpose  : Media specifik javscripts
' Created  : 2008-04-11; Internetfabriken
' Modified : 2008-10-15; Internetfabriken
' Comment  :
\**********************************************************/

/// <summary>
// MediaPanelHandler class
/// </summary>
function MediaPanelHandler()
{
    /* Private */
    var _currentIndex = 0;
    var _timerId = 0;
    var _panels = new Array();
    
    /// <summary>
    // Register a panel
    /// </summary>
    this.RegisterPanel = function(id)
    {
        try
        {
            // Add new panel (and hide it)
            var panel = new MediaPanel(id);
            panel.Hide();
            _panels.push(panel);
        }
        catch(err)
        {
            //alert('MediaPanelHandler.RegisterPanel(' + id + '): ' + err);
        }
    }
    
    /// <summary>
    // Register a hot spot
    /// </summary>
    this.RegisterHotSpot = function(id, hotspotButtonId, hotspotPanelId)
    {
        // Find panel and register hot spot
        for (var i=0; i<_panels.length; i++)
        {
            try
            {
                var panel = _panels[i];
                if (panel.id == id) panel.RegisterHotSpot(hotspotButtonId, hotspotPanelId);
            }
            catch(err)
            {
                //alert('MediaPanelHandler.RegisterHotSpot(' + id + ',' + hotspotButtonId + ',' + hotspotPanelId + '): ' + err);
            }
        }
    }
    
    /// <summary>
    /// Count panels
    /// </summary>
    this.CountPanels = function()
    {
        return _panels.length; 
    }

    /// <summary>
    /// Are we on the first index
    /// </summary>
    this.IsFirstIndex = function()
    {
        return (_currentIndex <= 0);
    }
 
    /// <summary>
    /// Are we on the last index
    /// </summary>
    this.IsLastIndex = function()
    {
        if (_panels.length > 0) return (_currentIndex >= (_panels.length-1));
        return true;
    }
          
    /// <summary>
    /// Show first panel (only)
    /// </summary>
    this.ShowFirst = function()
    {
        this.ShowIndex(0);
    }
 
    /// <summary>
    /// Show next panel (only)
    /// </summary>
    this.ShowNext = function()
    {
        this.ShowIndex(_currentIndex+1);
    }

    /// <summary>
    /// Show previous panel (only)
    /// </summary>
    this.ShowPrevious = function()
    {
        this.ShowIndex(_currentIndex-1);
    }           

    /// <summary>
    /// Show last panel (only)
    /// </summary>
    this.ShowLast = function()
    {
        this.ShowIndex(_panels.length-1);
    }
    
    /// <summary>
    /// Show a specific panel by index (only)
    /// </summary>
    this.ShowIndex = function(index)
    {
        if (_panels.length > 0)
        {
            try
            {
                // Set index
                if (_panels.length > 0)
                {
                    if (index > (_panels.length-1)) _currentIndex = 0;
                    else if (index < 0) _currentIndex = (_panels.length-1);
                    else _currentIndex = index;
                }
                else
                {
                    _currentIndex = 0;
                }
                
                // Hide all panels
                for (var i=0; i<_panels.length; i++)
                {
                    _panels[i].Hide();
                }
                
                // Show panel
                _panels[_currentIndex].Show();
            }
            catch(err)
            {
                //alert('MediaPanelHandler.Show(' + index + '): ' + err);
            }
        }
    }
 
    /// <summary>
    /// Show specific panel by id
    /// </summary>
    this.Show = function(id)
    {
        // Reset index
        _currentIndex = 0;
        
        // Find panel and register hot spot
        for (var i=0; i<_panels.length; i++)
        {
            try
            {
                var panel = _panels[i];
                if (panel.id == id)
                {
                    panel.Show();
                    break;
                }
            }
            catch(err)
            {
                //alert('MediaPanelHandler.Show(' + id + '): ' + err);
            }
        }
    }

    /// <summary>
    /// Show all panels
    /// </summary>
    this.ShowAll = function()
    {
        // Hide all panels
        for (var i=0; i<_panels.length; i++)
        {
            _panels[i].Show();
        }
    }

    this.TimerEnabled = true;
    
    /// <summary>
    /// Show panels with timer
    /// </summary>
    this.ShowTimer = function(instanceName, timerMs, running) {
        if (this.TimerEnabled || running == undefined) {
            if (!this.TimerEnabled && running == undefined) {
                this.TimerEnabled = true;
                running = true;
            }
            if (!running && _timerId > 0) {
                // Stop timer
                clearTimeout(_timerId);
                _timerId = 0;
            }
            else {
                // Show next
                if (running) this.ShowNext();
                _timerId = setTimeout(instanceName + ".ShowTimer('" + instanceName + "'," + timerMs + ", true)", timerMs);
            }
        }
    }

    /// <summary>
    /// Pause timer
    /// </summary>
    this.PauseTimer = function() {
        clearTimeout(_timerId);
        this.TimerEnabled = false;
    }
    

    /// <summary>
    /// Hide specific panel by id
    /// </summary>
    this.Hide = function(id)
    {
        // Find panel and register hot spot
        for (var i=0; i<_panels.length; i++)
        {
            try
            {
                var panel = _panels[i];
                if (panel.id == id)
                {
                    panel.Hide();
                    break;
                }
            }
            catch(err)
            {
                //alert('MediaPanelHandler.Hide(' + id + '): ' + err);
            }
        }
    }

    /// <summary>
    /// Hide all panels
    /// </summary>
    this.HideAll = function()
    {
        // Hide all panels
        for (var i=0; i<_panels.length; i++)
        {
            _panels[i].Hide();
        }
    }
    
    /// <summary>
    /// Toggle a hot spot on a specific panel
    /// </summary>
    this.ToggleHotSpot = function(id, hotSpotIndex)
    {
        // Find panel and toggle hot spot
        for (var i=0; i<_panels.length; i++)
        {
            try
            {
                var panel = _panels[i];
                if (panel.id == id)
                {
                    panel.ToggleHotSpot(hotSpotIndex);
                    break;
                }
            }
            catch(err)
            {
                //alert('MediaPanelHandler.ToggleHotSpot(' + id + ',' + hotSpotIndex + '): ' + err);
            }
        }
    }
}


/// <summary>
// Media panel class
/// </summary>
function MediaPanel(id)
{
    this.id = id;
    var _hotSpots = new Array();
 
    /// <summary>
    /// Register HotSpot
    /// </summary>
    this.RegisterHotSpot = function(buttonId, panelId)
    {
        try
        {
            var hotSpot = new MediaHotSpot(buttonId, panelId);
            hotSpot.Hide();
            _hotSpots.push(hotSpot);
        }
        catch(err)
        {
            //alert('MediaPanel.RegisterHotSpot(' + buttonId + ',' + panelId + '): ' + err);
        }
    }
    
    /// <summary>
    /// Show panel
    /// </summary>
    this.Show = function()
    {
        try
        {
            // Show hotspots
            for (var i=0; i<_hotSpots.length; i++)
            {
                _hotSpots[i].Show();
            }
            
            // Show panel
            var panel = $("#" + this.id).get(0);
            panel.style.display='block';
        }
        catch(err)
        {
            //alert('MediaPanel.Show(): ' + err);
        }
    }
    
    /// <summary>
    /// Hide panel
    /// </summary>
    this.Hide = function()
    {               
        try
        {
            // Hide hotspots
            for (var i=0; i<_hotSpots.length; i++)
            {
                _hotSpots[i].Hide();
            }
            
            // Hide panel
            var panel = $("#" + this.id).get(0);
            panel.style.display='none';
        }
        catch(err)
        {
            //alert('MediaPanel.Hide(): ' + err);
        }
    }
    
    /// <summary>
    /// Toggle a hot spot button (eg. show and hide its panel)
    /// </summary>
    this.ToggleHotSpot = function(index)
    {
        // Find hotspot and toggle its panel
        for (var i=0; i<_hotSpots.length; i++)
        {
            try
            {
                var hotSpot = _hotSpots[i];      
                if (i == index) hotSpot.TogglePanel();
                else hotSpot.HidePanel();
            }
            catch(err)
            {
                //alert('MediaPanel.ToggleHotSpot(' + index + '): ' + err);
            }
        }
    }
    
}

/// <summary>
// Media hot spot class
/// </summary>
function MediaHotSpot(buttonId, panelId)
{
    var _buttonId = buttonId;
    var _panelId = panelId;
    
    /// <summary>
    // Show hot spot
    /// </summary>
    this.Show = function()
    {
        try
        {
            var hotSpotButton = $("#" + _buttonId).get(0);
            hotSpotButton.style.display='block';

            var hotSpotPanel = $("#" + _panelId).get(0);
            hotSpotPanel.style.display='none';
        }
        catch(err)
        {
            //alert('MediaHotSpot.Show(): ' + err);
        }
    }
    
    /// <summary>
    // Hide hot spot
    /// </summary>
    this.Hide = function()
    {
        try
        {
            var hotSpotButton = $("#" + _buttonId).get(0);
            hotSpotButton.style.display='none';

            var hotSpotPanel = $("#" + _panelId).get(0);
            hotSpotPanel.style.display='none';
        }
        catch(err)
        {
            //alert('MediaHotSpot.Hide(): ' + err);
        }
    }

    /// <summary>
    // Show hot spot panel
    /// </summary>
    this.ShowPanel = function()
    {
        try
        {
            var hotSpotPanel = $("#" + _panelId).get(0);
            hotSpotPanel.style.display='block';
        }
        catch(err)
        {
            //alert('MediaHotSpot.ShowPanel(): ' + err);
        }
    }
    
    /// <summary>
    // Hide hot spot panel
    /// </summary>
    this.HidePanel = function()
    {
        try
        {
            var hotSpotPanel = $("#" + _panelId).get(0);
            hotSpotPanel.style.display='none';
        }
        catch(err)
        {
            //alert('MediaHotSpot.Hide(): ' + err);
        }
    }
        
    /// <summary>
    // Show/Hide hot spot panel
    /// </summary>
    this.TogglePanel = function()
    {
        try
        {
            var hotSpotPanel = $("#" + _panelId).get(0);
            if (hotSpotPanel.style.display == 'block') hotSpotPanel.style.display='none';
            else hotSpotPanel.style.display='block';
        }
        catch(err)
        {
            //alert('MediaHotSpot.TogglePanel(): ' + err);
        }
    }       
}