﻿/****************************************************************************************
* Tab Control Javascript file
* 
*/
/****************************************************************************************
* Brighthouse initialization code
*/
$(document).ready(function() {
    // Dynamically load css
    $('head').addStyleSheet(cmsBaseURL + "/static/stylesheets/jquery.tabs.css");
    
    $('.tab-control .divided-column-content').colfix();
    $('.tab-control').tabcontrol();
});


if (jQuery) (function($) {
    $.extend($.fn, {
        /**************************************************
        * Tab Control
        */
        tabcontrol: function(o) {
            this.each(function() {
                var control = $(this);
                var defaults = {
                    active: 1,
                    tabpadding: 19 // li margin + li padding
                };
                var opts = $.extend(defaults, o)
             
                // if UL.selectors does not exist
                var selectors = $(control.children('ul:first'))
                if (selectors.length == 0) {
                    // make UL.selectors and prepend to control
                    control.prepend('<ul class="selectors"></ul>')
                    selectors = $('ul.selectors', control);
                    // make tab-bottom and append to control
                    control.append('<div class="tab-bottom"><span></span></div>')
                    // for each h4 that has a name
                    $('h4[name]', control).each(function(index) {
                        var h4 = $(this);
                        var title = h4.html();
                        var tabid = h4.attr('name');
                        // assign hd's name to parent div's id 
                        h4.parent().attr('id', tabid);
                        h4.parent().addClass('tab-content');
                        // append tab-selector to ul.selectors
                        selectors.append('<li><a></a></li>')
                        var a = $('li:last a', selectors);
                        a.attr('href', '#' + tabid)
                        a.html(title)
                        // on tab-selector's <a> click
                        a.click(function(e) {
                            e.preventDefault();
                            if (!a.parent().hasClass('selected')) {
                                // find .selected div/li and unselect it
                                $('.selected', control).removeClass('selected');
                                // find div with matching id and make it selected
                                $('#' + tabid).addClass('selected');
                                // make li .selected
                                a.parent().addClass('selected')
                            }
                            a.blur();
                        });
                        h4.css('display', 'none')
                        // hide h4 element
                        if (index + 1 == opts.active) {
                            a.parent().addClass('selected')
                            h4.parent().addClass('selected');
                        }
                    });
                }else{
                    selectors.addClass('selectors')
                }
                // assign first class to UL.selectors' first li
                selectors.children('li:first').addClass('first');
                // set max width for tab-selectors
                var num = selectors.children('li').length;
                var tswidth = Math.floor((selectors.width() - opts.tabpadding) / num)
                selectors.children('li').width(tswidth - opts.tabpadding)
            })
        }
    });
})(jQuery);
