// JavaScript Document

function tabs() {
    //newtab is a test for updated subnav (linear)
    if (location.href.indexOf("/degrees/") > 1) { loadCSS("jquery.tabs.alt.css"); var newtab = true; };

    // We can use this object to reference the panels container
    var panelContainer = $('div#panels');

    // Determine which tab should show first based on the URL hash
    panelLocation = location.hash.slice(1);
    panelLocation = panelLocation.split("?")[0] //minus any querystrings

    // Hide all panels
    panelContainer.find('div.panel').hide();

    if ((location.href.indexOf("financial-aid-opportunities-overview") > 1) || (location.href.indexOf("military-affiliation") > 1)) {
        $('<select id="cboOptions"><option>Select One...</option></select>').insertBefore(panelContainer);
        //move first panel (default page article that always shows)
        panelContainer.find('div.panel:first').insertBefore('select#cboOptions').show();
        // For each panel, create a tab
        panelContainer.find('div.panel[arttitle!=""]').each(function (n) {
            $('select#cboOptions').append('<option value="' + $(this).attr('arttitle') + '">' + $(this).attr('arttitle').replace(/-/g, ' ') + '</option>');
        });
        //if there is a hash
        if (panelLocation) {
            var panelNum = panelLocation;
            // Display the initial panel
            panelContainer.find('div.panel[arttitle=' + panelNum + ']').fadeIn('slow');
            // Change the class of the current tab
            $('select#cboOptions').find('option[value=' + panelNum + ']').attr('selected', 'selected');
        }
        $('select#cboOptions').change(function () {
            var panelNum = $(this).find('option:selected').val();
            // Hide all panels
            panelContainer.find('div.panel').hide();
            // Display the initial panel
            panelContainer.find('div.panel[arttitle=' + panelNum + ']').fadeIn('slow');
        });
        panelContainer.css('margin-left', '10px');
    }

    else {
        if (newtab == true) {
            $("div.panel:nth-child(3)").nextAll().remove();
        }

        // Create a DIV for the tabs and insert it before the panel container
        $('<ul id="ultabs"></ul>').insertBefore(panelContainer);
        // Find panel names and create nav
        // -- Loop through each panel
        panelContainer.find('div.panel').each(function (n) {
            // For each panel, create a tab
            $('ul#ultabs').append('<li><a href="#' + $(this).attr('arttitle') + '">' + $(this).attr('arttitle').replace(/-/g, ' ') + '</a></li>');

        });

        // Hide all panels
        panelContainer.find('div.panel').hide();

        // Determine which tab should show first based on the URL hash
        if (panelLocation) {
            var panelNum = panelLocation;
        } else {
            var panelNum = $('div.panel:first').attr('arttitle');
        }
        // Display the initial panel
        panelContainer.find('div.panel[arttitle=' + panelNum + ']').fadeIn('slow');

        // Change the class of the current tab
        $('ul#ultabs').find('a[href*=' + panelNum + ']').parent('li').addClass('active');
        //alert($('ul#ultabs').find('a[href*='+panelNum+']').text());

        if (newtab == true) {
            $("ul#ultabs").addClass("select" + ($("ul#ultabs li.active").index() + 1));
            $("ul#ultabs li.active").prevAll().addClass("active");
        };

        // What happens when a tab is clicked
        // -- Loop through each tab
        $('ul#ultabs').find('a').each(function (n) {
            var tabselect;
            // For each tab, add a 'click' action
            $(this).mouseover(function () {
                tabselect = $("ul#ultabs").attr("class");
                $("ul#ultabs").removeClass();
                $("ul#ultabs").toggleClass("select" + (n + 1));
            });
            $(this).mouseout(function () { $("ul#ultabs").toggleClass("select" + (n + 1)); $("ul#ultabs").addClass(tabselect); });


            $(this).not($("a[href*='request-information']")).click(function () {
                // Hide all panels
                panelContainer.find('div.panel').hide();
                // Find the required panel and display it
                panelContainer.find('div.panel:nth-child(' + (n + 1) + ')').fadeIn('slow');
                // Give all tabs the 'tab' class
                tabselect = $("ul#ultabs").attr("class");

                $('ul#ultabs li.active').removeClass();
                // Give the clicked tab the 'tab-active' class
                $(this).parent('li').addClass('active');
                if (newtab == true) {
                    $(this).parent("li").prevAll().addClass("active");
                    $("ul#ultabs").removeClass();
                    $("ul#ultabs").addClass("select" + (n + 1));
                };
            });
        });
    }

    $('ul#topnav a[href*=#]').click(function () {
        panelNum = $(this).attr('href').split('#')[1];
        if ($('select#cboOptions')) {
            if ($('select#cboOptions').find('option[value=' + panelNum + ']').length > 0) {
                // Hide all panels
                panelContainer.find('div.panel').hide();
                // Find the required panel and display it
                panelContainer.find('div.panel[arttitle=' + panelNum + ']').fadeIn('slow');
                //select the current Option
                $('select#cboOptions').find('option[value=' + panelNum + ']').attr('selected', 'selected');
            }
        }
        if ($('ul#ultabs').find('a[href*=#' + panelNum + ']').length > 0) {
            //$(this).parent().find('a').removeClass().addClass('tab');
            $('ul#ultabs li.active').removeClass();

            // Change the class of the current tab
            $('ul#ultabs').find('a[href*=' + panelNum + ']').parent('li').addClass('active');

            if (newtab == true) {
                $("ul#ultabs").addClass("select" + panelNum);
                $("ul#ultabs li.active").prevAll().addClass("active");
            };

            // Hide all panels
            panelContainer.find('div.panel').hide();
            // Find the required panel and display it
            panelContainer.find('div.panel[arttitle=' + panelNum + ']').fadeIn('slow');
        }
    });

}
