﻿$(document).ready(function () {
    //sort category links on 3rd and 4th level pages
    $(".category_links p").sortElements(function (a, b) {
        return $(a).children("a").text() > $(b).children("a").text() ? 1 : -1;
    })

    var total = $(".category_links p").size();
    var newTotal = 0;
    var avg = Math.ceil(total / 3);
    var newAvg = 0;

    for (var i = 0; i < 3; i++) {
        if (i == 0) {
            $(".category_links p").slice(0, avg).wrapAll("<div class='links_column' />");
            newTotal = total - avg;
        }
        else if (i == 1) {
            $(".category_links p").slice(avg, avg + newAvg).wrapAll("<div class='links_column' />");
            newTotal = newTotal - newAvg;
        }
        else {
            $(".category_links p").slice(total - newTotal, total).wrapAll("<div class='links_column' />");
        }
        newAvg = Math.ceil(newTotal / 2);
    };


    //sort capability links on main Services page
    $(".capability_menu p").sortElements(function (a, b) {
        return $(a).children("a").text() > $(b).children("a").text() ? 1 : -1;
    })

    total = $(".capability_menu p").size();
    newTotal = 0;
    avg = Math.ceil(total / 4);
    newAvg = 0;
    var newAvg1 = 0;
    var newAvg2 = 0;

    for (var i = 0; i < 4; i++) {
        if (i == 0) {
            $(".capability_menu p").slice(0, avg).wrapAll("<div class='links_column' />");
            newTotal = total - avg;
            newAvg1 = Math.ceil(newTotal / 3);
        }
        else if (i == 1) {
            $(".capability_menu p").slice(avg, avg + newAvg1).wrapAll("<div class='links_column' />");
            newTotal = newTotal - newAvg1;
            newAvg2 = Math.ceil(newTotal / 2);
        }
        else if (i == 2) {
            $(".capability_menu p").slice(avg + newAvg1, avg + newAvg1 + newAvg2).wrapAll("<div class='links_column' />");
            newTotal = newTotal - newAvg2;
        }
        else {
            $(".capability_menu p").slice(total - newTotal, total).wrapAll("<div class='links_column' />");
        }
    };

    //sort category links on main Services page
    $(".category_menu p").sortElements(function (a, b) {
        return $(a).children("a").text() > $(b).children("a").text() ? 1 : -1;
    })

    total = $(".category_menu p").size();
    newTotal = 0;
    avg = Math.ceil(total / 4);
    newAvg = 0;
    newAvg1 = 0;
    newAvg2 = 0;

    for (var i = 0; i < 4; i++) {
        if (i == 0) {
            $(".category_menu p").slice(0, avg).wrapAll("<div class='links_column' />");
            newTotal = total - avg;
            newAvg1 = Math.ceil(newTotal / 3);
        }
        else if (i == 1) {
            $(".category_menu p").slice(avg, avg + newAvg1).wrapAll("<div class='links_column' />");
            newTotal = newTotal - newAvg1;
            newAvg2 = Math.ceil(newTotal / 2);
        }
        else if (i == 2) {
            $(".category_menu p").slice(avg + newAvg1, avg + newAvg1 + newAvg2).wrapAll("<div class='links_column' />");
            newTotal = newTotal - newAvg2;
        }
        else {
            $(".category_menu p").slice(total - newTotal, total).wrapAll("<div class='links_column' />");
        }
    };

});
