//ON DOM LOADED
$(document).ready(function() {



    $(".subMenu").mouseover(function() {

        //alert($(this).parent().children(".second-level").html())

        if ($(this).children(".second-level").html()) {
            $(this).children(".second-level").css('display', 'block');
            if ($(this).hasClass("active-with-children")) {
                $(this).removeClass("active-with-children");

            }
            else {
                $(this).addClass("active-with-children");
            }
        }

        return false;
    });

    $(".subMenu").mouseout(function() {
        if ($(this).children(".second-level").html()) {
            $(this).children(".second-level").css('display', 'none');
            if ($(this).hasClass("active-with-children")) {
                $(this).removeClass("active-with-children");

            }
            else {
                $(this).addClass("active-with-children");
            }
        }
        return false;
    });




    $(".dropDown").click(function() {
        if ($(this).parent().children(".second-level .third-level").html()) {
            $(this).parent().children(".third-level").slideToggle("slow");
            if ($(this).hasClass("active-with-children-third")) {
                $(this).removeClass("active-with-children-third");

            }
            else {
                $(this).addClass("active-with-children-third");
            }
        }
        if ($(this).parent().children(".second-level")) {
            $(this).parent().children(".second-level").slideToggle("slow");

            if ($(this).hasClass("active-with-children")) {
                $(this).removeClass("active-with-children");

            }
            else {
                $(this).addClass("active-with-children");
                /*
                $(this).animate( {  paddingTop:"4px" }, { queue:false, duration:500 } )
                .animate( { paddingBottom:"4px" }, 500 )
                .animate( { paddingTop:"px" }, 500);
                */
            }
        }
        return false;
    });

    /*
    $("#subnav a").click(function () {
    if($(this).parent().hasClass("selected")){
    $(this).parent().removeClass("selected");      
    }
    else{
    $(this).parent().addClass("selected");
    }
    });
    */




    // ACCESSIBLITY STUFF //
    // By default, the silo compressors are 'display:none', removing this class with JS enables the Compactor functionality //

    $('.silo a.compressor').each(function() {
        $(this).removeClass('hidden');
    });


    $('li.item h2 a.compressor').each(function() {
        $(this).removeClass('hidden');
    });


    // -------------------------------------------------------------------------------------------------------------------------------//
    // ---------------------------------------------------- COMPACTOR --------------------------------------------------------------- //
    // -------------------------------------------------------------------------------------------------------------------------------//

    //HELPFUL NOTE: 'open' and 'close' refer to the function of the compressor link, not the state of the silo.


    $('.silo a.compressor').click(function(e) {
        if ($(this).hasClass("close")) {
            silo = $(this).parents().get(1);
            closeSilo(e, silo);
        }
        else {
            silo = $(this).parents().get(1);
            openSilo(e, silo);
        }
    });


    function closeSilo(e, silo) {
        e.preventDefault();

        //CLOSE THE CURRENTLY OPEN SILO
        $(silo).find('.silo-content').addClass("hidden");
        $(silo).find('.silo-content').removeClass("show");

        //CHANGE IMAGE ICONS
        $(silo).find('a.compressor img').attr("src", "/files/0/59/plus.jpg");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(silo).find('a.compressor').addClass("open");
        $(silo).find('a.compressor').removeClass("close");


    }

    function openSilo(e, silo) {

        e.preventDefault();

        //CHANGE IMAGE ICONS
        $(silo).find('a.compressor img').attr("src", "/files/0/59/minus.jpg");

        //OPEN THE SILO THAT WAS CLICKED
        $(silo).find('.silo-content').addClass("show");
        $(silo).find('.silo-content').removeClass("hidden");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(silo).find('a.compressor').addClass("close");
        $(silo).find('a.compressor').removeClass("open");

    }

    /* Policies compressor */

    $('li.item h2 a.compressor').click(function(e) {
        if ($(this).hasClass("open")) {
            article = $(this).parents().get(1);
            openItem(e, article);
        }
        else {
            article = $(this).parents().get(1);
            closeItem(e, article);
        }
    });


    function closeItem(e, article) {
        e.preventDefault();

        //CLOSE THE CURRENTLY OPEN SILO
        $(article).find('.item-subcontent').addClass("hidden");
        $(article).find('.item-subcontent').removeClass("show");

        //CHANGE IMAGE ICONS
        $(article).find('a.compressor img').attr("src", "/files/0/59/plus-white.jpg");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(article).find('a.compressor').addClass("open");
        $(article).find('a.compressor').removeClass("close");

        //CHANGE BACKGROUND COLOR OF ARTICL.E
        $(article).removeClass("highlight");


    }

    function openItem(e, article) {

        e.preventDefault();

        //CHANGE IMAGE ICONS
        $(article).find('a.compressor img').attr("src", "/files/0/59/minus-grey.jpg");

        //OPEN THE SILO THAT WAS CLICKED
        $(article).find('.item-subcontent').addClass("show");
        $(article).find('.item-subcontent').removeClass("hidden");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(article).find('a.compressor').addClass("close");
        $(article).find('a.compressor').removeClass("open");

        //CHANGE BACKGROUND COLOR OF ARTICLE
        $(article).addClass("highlight");

    }

});

function ClearText(txtBox, txt) {
  if (txtBox.value == txt) {
    txtBox.value = "";
  }
}

function isKeyPressedEnter(e)
{
        var key;
        if(window.event)
            key = window.event.keyCode;
        else
            key = e.which;

  
        if (key == 13)
            return true;
            
return false;
}

