/**
* Navigation Ribbon: JavaScript module used to manage and build the Philips Navigation Ribbon.
* @author Adam J. McIntyre - adam.mcintyre@isobar.com
*/

$(document).ready(function() {(NavigationRibbon = function ($) {
    var navItems = _page.leftNav; var navIcons = _page.leftNavIcons; var navLevels = []; var levels = m_sActiveMenu.split('_'); var hasCarousel = false, hasImages = false, hasChildCarousel = false; return { init: function () { var o = this; $(document).ready(function () { o._buildLevels(); }); }, _buildLevels: function () {
        var tmpItemLocation; var topNav = []; for (var i = 0; i < navItems.length; i++) { if (typeof navItems[i] !== 'undefined') { topNav.push(navItems[i]); } }
        navLevels.push(topNav); if (window.showRibbon !== false) { for (var i = 1; i < levels.length; i++) { tmpItemLocation = levels.slice(0, i + 1).join('_'); navLevels.push(this._getSiblings(tmpItemLocation)); } }
        this._renderLevels();
    }, _getSiblings: function (levelStr) {
        var siblings = []; var idx = 1; var testLevel = levelStr.substr(0, levelStr.lastIndexOf('_')) + '_' + idx; var levelItem = navItems[testLevel]; while (typeof levelItem !== 'undefined') { siblings.push(levelItem); levelItem = navItems[testLevel.substr(0, testLevel.lastIndexOf('_')) + '_' + (++idx)]; }
        return siblings;
    }, _renderLevels: function () {
        var h = ''; h += this._renderTopNav()
        var totalLevels = navLevels.length; var level = navLevels[navLevels.length - 1]; if (totalLevels > 2) { h += this._renderSubLevel(totalLevels) }
        var carousel = hasCarousel ? 'carousel' : ''; var imgs = hasImages ? 'images' : 'no_images'; if (totalLevels == 4) { totalArrows = 'single_arrow'; }
        else if (totalLevels == 5) { totalArrows = 'double_arrow'; }
        $('#navigation_ribbon').addClass('level_' + navLevels.length + ' ' + carousel + ' ' + imgs).append(h).append('<div class="navigation_bottom"></div>'); var totalArrows = $('#navigation_ribbon div.navigation_arrow').length; if (totalArrows == 1) { $('#navigation_ribbon').addClass('single_arrow'); }
        else if (totalArrows == 2) { $('#navigation_ribbon').addClass('double_arrow'); }
        var totalNodes = $('#navigation_ribbon ul.navigation_list li').length; var activeIndex = levels[levels.length - 1]
        if (hasCarousel) {
            var startItem = 1; if (!hasChildCarousel && ((totalNodes > 4 && totalLevels == 5) || (totalNodes > 6 && totalLevels < 5))) { startItem = activeIndex; }
            else if (!hasChildCarousel && (activeIndex == null && children.length > 4 && lastIdx >= 4)) { startItem = activeIndex; }
            var scrollAmount = 8; if (totalLevels == 5 || totalArrows == 2) { scrollAmount = 4; }
            else if ((totalLevels >= 3 || totalArrows == 1) && !(totalArrows == 0 && totalNodes >= 8)) { scrollAmount = 6; }
            $('#navigation_ribbon').find('div.subnav_container').jcarousel({ scroll: scrollAmount, size: totalNodes, start: startItem, visible: scrollAmount, buttonNextHTML: null, buttonPrevHTML: null, initCallback: function (carousel) {
                if (startItem > 1) { $(carousel.list).find('li.first:first').removeClass('first'); }
                $('#navigation_carousel_next').click(function (e) { e.preventDefault(); carousel.next(); return false; }); $('#navigation_carousel_prev').click(function (e) { e.preventDefault(); carousel.prev(); return false; });
            }, itemFirstInCallback: function (carousel, li) { $(li).addClass('first'); }, itemFirstOutCallback: function (carousel, li) { $(li).removeClass('first'); } 
            });
        } 
    }, _renderTopNav: function () {
        var activeItem = levels[0] - 1; var level = navLevels[0]; var h = '<ul id="navigation_level_1">'; for (var j = level.length - 1, currentItem = level[level.length - 1]; j >= 0; j--, currentItem = level[j]) {
            h += '<li'; if (j == activeItem) { h += ' class="active"' }
            h += '><a href="' + currentItem.link + '">' + currentItem.text + '</a></li>';
        }
        h += '</ul>'; return h;
    }, _renderSubLevel: function (lastIdx) {
        var lastLevel = navLevels[lastIdx - 1]; var prevLevels; var h = '<div class="subnav_container">'; var children; switch (lastIdx) {
            case 5: prevLevels = [navLevels[2][levels[2] - 1], navLevels[3][levels[3] - 1], navLevels[4]]; h += this._renderChevrons(prevLevels.length, prevLevels); children = prevLevels[prevLevels.length - 1]; h += this._renderCarouselNodes(children, levels[lastIdx - 1], 5, levels.splice(0, lastIdx - 1).join('_')); break; case 4: prevLevels = [navLevels[2][levels[2] - 1], navLevels[3][levels[3] - 1]]; children = this._getChildren(levels.join('_')); if (children.length > 0) { h += this._renderChevrons(prevLevels.length, prevLevels); h += this._renderCarouselNodes(children, null, 4, levels.join('_')); hasChildCarousel = true; }
                else { h += this._renderChevrons(prevLevels.length - 1, prevLevels); h += this._renderCarouselNodes(navLevels[3], [levels[3]], 4, levels.splice(0, lastIdx - 1).join('_')); }
                break; case 3: prevLevels = [navLevels[2][levels[2] - 1]]; h += this._renderChevrons(prevLevels); children = this._getChildren(levels.join('_')); if (children.length > 0) { h += this._renderChevrons(prevLevels.length, prevLevels); h += this._renderCarouselNodes(children, null, 3, levels.join('_')); hasChildCarousel = true; }
                else { h += this._renderChevrons(prevLevels.length - 1, prevLevels); h += this._renderCarouselNodes(navLevels[2], [levels[2]], 3, levels.splice(0, lastIdx - 1).join('_')); }
                break;
        }
        h += '</div>'; return h;
    }, _getChildren: function (level) { return this._getSiblings(level + '_1'); }, _renderChevrons: function (chevrons, levels) {
        var h = ''; if (chevrons > 0) { h += '<div class="navigation_arrow first"><a href="' + levels[0].link + '"><span>' + this.getText(levels[0].text) + '</span></a></div>'; }
        if (chevrons > 1) { h += '<div class="navigation_arrow second"><a href="' + levels[1].link + '"><span>' + this.getText(levels[1].text) + '</span></a></div>'; }
        return h;
    }, _renderCarouselNodes: function (children, activeItem, lastIdx, iconPath) {
        h = '<div class="jcarousel-clip"><ul class="navigation_list">'; for (var i = 0, currentItem = children[0]; i < children.length; i++, currentItem = children[i]) {
            if (i == 0) {
                if (i + 1 == activeItem) { h += '<li class="first active">'; }
                else { h += '<li class="first">'; } 
            }
            else if (i == children.length - 1) {
                if (i + 1 == activeItem) { h += '<li class="last active">'; }
                else { h += '<li class="last">'; } 
            }
            else {
                if (i + 1 == activeItem) { h += '<li class="active">'; }
                else { h += '<li>'; } 
            }
            h += '<span class="highlight"></span>'; var myIconItem = navIcons[iconPath + '_' + (i + 1)]; if (typeof myIconItem === 'object') {
                var myActiveIcon = '', myHoverIcon = ''; if (i + 1 == activeItem) { myActiveIcon = '<img src="' + myIconItem.On + '" alt="' + myIconItem.OnAlt + '" class="default"/>' }
                else {
                    myActiveIcon = '<img src="' + myIconItem.Off + '" alt="' + myIconItem.OffAlt + '" class="default"/>'
                    myHoverIcon = '<img src="' + myIconItem.On + '" alt="' + myIconItem.OnAlt + '" class="hover"/>'
                }
                h += '<a href="' + currentItem.link + '">'
+ myActiveIcon + myHoverIcon
+ '<span>' + this.getText(currentItem.text) + '</span></a></li>'; hasImages = true;
            }
            else { h += '<a href="' + currentItem.link + '" class="no_image"><span class="outer"><span class="inner">' + this.getText(currentItem.text) + '</span></span></a></li>'; } 
        }
        h += '</ul></div>'; if ((children.length > 4 && lastIdx == 5) || (children.length > 6 && lastIdx < 5) || (activeItem == null && children.length > 4 && lastIdx >= 4)) { hasCarousel = true; h += '<div class="carousel_nav">'; h += '<a href="#next" id="navigation_carousel_next" class="next"><span>Next</span></a>'; h += '<a href="#previous" id="navigation_carousel_prev" class="previous"><span>Previous</span></a>'; h += '</div>'; }
        return h;
    }, getText: function (str) {
        var MAX_CHARS = 30; if (str.length <= MAX_CHARS) { return str; }
        else {
            var words = str.split(/\s/); var tmpStr = ''; var i = 0; do {
                if (tmpStr.length + words[i].length > MAX_CHARS) { break; }
                else { tmpStr += words[i] + ' '; }
                i++;
            } while (tmpStr.length < MAX_CHARS - 3)
            if (tmpStr == '') { tmpStr = words[0].substr(0, MAX_CHARS - 3); }
            return '<span class="truncated" title="' + str + '">' + tmpStr + '&hellip;</span>';
        } 
    } 
    }
} (jQuery)).init();
});
