$(document).ready(function () {

    var count = 1;
    var ul = "";
    var li;
    var divContent = "";
    var linkTitle;
    var url;
    var urls;
    if (urls) {
        url = urls.split(",")[0];
    }
    
    //$("#bottom_links").html('loading...');
 
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "TopMenu",
        CAMLQuery: "<Query><OrderBy><FieldRef Name='DisplayOrder' Ascending='TRUE' /></OrderBy></Query>",
        CAMLViewFields: "<ViewFields Properties='True'></ViewFields>",
        completefunc: function (xData, Status) {
            $(xData.responseXML).find("[nodeName='z:row']").each(function () {

                linkTitle = $(this).attr("ows_Title");
                ul = "<ul id='menu" + count + "Feeder'>";
                li = "<li class='title'><a href='#'>" + $(this).attr("ows_Title") + "</a></li>";

                // generate the sublinks
                $().SPServices({
                    operation: "GetListItems",
                    async: false,
                    listName: "BottomLinks",
                    CAMLQuery: "<Query><Where><Contains><FieldRef Name='Parent'/><Value Type='Lookup'>" + linkTitle + "</Value></Contains   ></Where></Query>",
                    CAMLViewFields: "<ViewFields Properties='True'></ViewFields>",
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                            urls = $(this).attr("ows_URL");
                            if (urls) {
                                url = urls.split(",")[0];
                            }
                            li += "<li><a class='in_link' href='" + url + "'>" + $(this).attr("ows_Title") + "</a></li>";

                        });
                    }
                });


                ul += li + "</ul>";

                count += 1;
                divContent += ul;
            });

            $("#bottom_links").html(divContent);
            $('ul:last', $("#bottom_links")).addClass("last");
        }

    });

    $(".scroll-content-item").mouseover(function () {
        $(".diff").toggleClass("diff");
    });

    $('#TopMenuPane').mouseleave(function () {
        $(".scroll-pane").animate({ top: -100 }, 500);
        $(".menu_trigger").removeClass("current");
    });

    $(".menu_trigger").live("click", function () {
        if ($(this).hasClass("current")) {
            $(".menu_trigger").removeClass("current");
            $(".scroll-pane").animate({ top: -100 }, 300);
        }
        else {
            $(".menu_trigger").removeClass("current");
            $(this).toggleClass("current");
            showAllHeaders();
            var hrefValue = $('#icon1 .icon_text a').attr('go_to');

            //*****************************************************************

            if ($(this).attr("id") == 'menu1') {
                // if size < 6 hide the slide
                // if size < 12 
                for (i = $('#menu1Feeder').children().size(); i <= 12; i++) {
                    $('#icon' + i).hide();
                }
                if ($('#menu1Feeder').children().size() <= 6) {
                    $('#scroll').hide();
                }

                $('#menu1Feeder li a').each(function (index, value) {
                    $('#icon' + index + ' .icon_text a').text($(this).text()).attr('go_to', $(this).attr('go_to'));
                });

                for (i = $('#menu1Feeder').children().size(); i <= 12; i++) {
                    $('#icon' + i).hide();
                }

            }
            else if ($(this).attr("id") == 'menu2') {
                // if size < 6 hide the slide
                // if size < 12 
                for (i = $('#menu2Feeder').children().size(); i <= 12; i++) {
                    $('#icon' + i).hide();
                }
                if ($('#menu2Feeder').children().size() <= 6) {
                    $('#scroll').hide();
                }

                $('#menu2Feeder li a').each(function (index, value) {
                    $('#icon' + index + ' .icon_text a').text($(this).text()).attr('go_to', $(this).attr('go_to'));
                });

                for (i = $('#menu2Feeder').children().size(); i <= 12; i++) {
                    $('#icon' + i).hide();
                }
            }
            else if ($(this).attr("id") == 'menu3') {
                // if size < 6 hide the slide
                // if size < 12 
                for (i = $('#menu3Feeder').children().size(); i <= 12; i++) {
                    $('#icon' + i).hide();
                }
                if ($('#menu3Feeder').children().size() <= 6) {
                    $('#scroll').hide();
                }

                $('#menu3Feeder li a').each(function (index, value) {
                    $('#icon' + index + ' .icon_text a').text($(this).text()).attr('go_to', $(this).attr('go_to'));
                });

                for (i = $('#menu3Feeder').children().size(); i <= 12; i++) {
                    $('#icon' + i).hide();
                }
            }

            if ($(".scroll-pane").css("top") == "-100") {
                $(".scroll-pane").animate({ top: 0 }, 500);
            }
            else {
                $(".scroll-pane").animate({ top: -100 }, 500);
                $(".scroll-pane").animate({ top: 0 }, 500);
            }
        }
    });
});

function showAllHeaders() {
    $('#icon1').show();
    $('#icon2').show();
    $('#icon3').show();
    $('#icon4').show();
    $('#icon5').show();
    $('#icon6').show();
    $('#icon7').show();
    $('#icon8').show();
    $('#icon9').show();
    $('#icon10').show();
    $('#scroll').show();
    $(".scroll-content").css("margin-left", 0);
    $(".scroll-bar").slider("value", "0");
}

