﻿

function Menu(id)
{
    var _this = this;
    var _id = id;
    var _menu = null;
    var _links = null;
    
    function _getMenu()
    {
        if (_menu == null)
        {
            _menu = $('#' + _id);
        }
        return _menu;
    }

    function _fillMenuEvenly()
    {
        ///<summary>
        /// Fills the menu items out evenly across the width of the available menu space
        ///</summary>
        
        var width = 0;
        _links.each(function() { width += $(this).outerWidth(); });
        
        var menuWidth = _menu.find('ul').width();

        if (width < menuWidth)
        {
            var count = _links.length;
            var fillerWidth = (menuWidth - width) / count;

            _links.each(function()
            {
                var currentWidth = $(this).width();
                $(this).width( Math.floor(currentWidth + fillerWidth) );
            });

            width = 0;
            _links.each(function() { width += $(this).outerWidth(); });

            if (width < menuWidth)
            {
                var fillerWidth = menuWidth - (width + 1);
                var lastItemWidth = _links.last().width();
                
                _links.last().width(lastItemWidth + fillerWidth);
            }
        }
    }

    function _hidePanels()
    {
        _links.parent().attr('class', '').find('.nav-panel').hide();
    }
    
    function _togglePanel(link)
    {
        var menuItem = link.parent();
        var panel = menuItem.find('.nav-panel');

        if (panel.length == 0)
        {
            return;
        }
        
        if (panel.is(':visible'))
        {
            menuItem.attr('class', '');
            panel.hide();
            return;
        }

        _hidePanels();

        menuItem.attr('class', 'selected');
        panel.show();
    }
    
    function _initializeEvents()
    {
        _links.click(function()
        {
            _togglePanel($(this));
        });
    }

    this.initialize = function()
    {
        var menu = _getMenu();
        _links = menu.find('.nav-link');

        _initializeEvents();
        _fillMenuEvenly();

        //clean up top padding of group items
        $('.navigation .nav-panel .nav-panel-items table tr:first-child .sub-group').css('margin-top', '0px');
    }
    
}


function Banner(id, delay)
{
    var _this = this;
    var _id = id;
    var _currentIndex = 0;
    var _images = null;
    var _preview = null;
    
    var _cycleInterval = null;
    var _delay = delay;
    
    _cycleBanner = function()
    {
        _cycleInterval = setInterval(function()
        {
            _this.next();

        }, _delay);
    }

    _resetCycle = function()
    {
        clearInterval(_cycleInterval);
        setTimeout(function() { _cycleBanner(); }, _delay);
    }

    _setPreview = function(image)
    {
        _preview.find('a').attr('href', image.attr('imageLink'));

        var img = _preview.find('a img');

        img.fadeOut('fast', function()
        {
            _preview.find('a img').attr('src', image.attr('src'));
            img.fadeIn('slow');
        });

        _resetCycle();
    }

    this.initialize = function()
    {
        _images = $('#' + _id + ' .thumbs ul img');
        _preview = $('#' + _id + ' .preview');

        _images.eq(0).parent().css('border', '1px solid #ccc');

        //center thumbs
        var w = 0;
        $('.banner .thumbs ul li').each(function() { w += $(this).outerWidth(); });
        $('.banner .thumbs ul').width(w + 50);

        _delay = _delay * 1000;

        if (_delay <= 0)
        {
            _delay = 5000;
        }
        
        _cycleBanner();
    }

    this.preview = function(index)
    {
        if (index == _currentIndex)
        {
            return;
        }

        _currentIndex = index;

        if ((_currentIndex > (_images.length - 1)))
        {
            _currentIndex = 0;
        }

        if (_currentIndex < 0)
        {
            _currentIndex = _images.length - 1;
        }

        var image = _images.eq(_currentIndex);

        _images.parent().css('border', '1px solid #362f2b');
        image.parent().css('border', '1px solid #ccc');

        _setPreview(image);
    }

    this.previous = function()
    {
        _this.preview(_currentIndex - 1);
    }

    this.next = function()
    {
        _this.preview(_currentIndex + 1);
    }

}


function getElementsByTagAndClassName(tagName, className)
{

    var nodeList = document.getElementsByTagName(tagName)

    var elements = [];
    var index = 0;
    for (i = 0; i < nodeList.length; i++)
    {

        var attribute = nodeList[i].getAttribute("class") == null
                                  ? nodeList[i].getAttribute("className")
                                  : nodeList[i].getAttribute("class");
        if (attribute != null)
        {
            if (attribute.indexOf(className) != -1)
            {
                elements[index] = nodeList[i];
                index++
            }
        }
    }
    return elements;
}

function hideDefaultPoweredByLogo()
{

    var defaultPoweredByLogo = getElementsByTagAndClassName('p', 'poweredBySitefinityLogo');

    for (i = 0; i < defaultPoweredByLogo.length; i++)
    {

        var filler = document.createElement("span");
        filler.style.display = "none";


        var parent = defaultPoweredByLogo[i].parentNode;
        parent.replaceChild(filler, defaultPoweredByLogo[i]);

    }
    return false;
}

$(function()
{
    //hideDefaultPoweredByLogo();
});
