$(document).ready(function() {



    //back to top
    $('a.top').click(function() {
        $('html, body').animate({ scrollTop: '0px' }, 300);
        return false;
    });


    //fancybox
    $("a.lightbox").fancybox({
        'hideOnContentClick': false,
        'imageScale': true,
        'frameWidth': 920,
        'frameHeight': 600
    });

    //mail to
    $("#contactSubmit").click(function() {
        var body = $("#contactComment").val();
        window.location.href = "mailto:nedm@ccsso.org?subject=Comment%20from%20Website&body=" + body;


    });

    // AttInfo section function
    $('#showAll').click(function() {
        $("#attributeInfo").slideUp(300);
        $("#attInfo").children("span.title").hide();
        $('#attInfo').toggle();
        $('#showAll').text($('#showAll').text() == 'Show All' ? 'Hide All' : 'Show All');
    });

    //my box functions
    //$('div.box').each(function(i) {
    //$(this).children("div.innerBox").slideUp(400);
    //});

    $('div.box h2 span').addClass('minus');
    $('div.box h2').click(function(event) {
        var that = $(this).children("span");

        current = that;
        ToggleMe(that);


        $('div.box h2 span').each(function(i) {
            if (that != current) {
                $(that).parents("div.box").children("div.innerBox").slideUp(200);
                $(that).removeClass('minus').addClass('plus');
            }
        });


        $(that).parents("div.box").children("div.innerBox").slideToggle(200);


    });
    //Attribute box section functions
    $('#attributes td.attribute span').addClass('plus');
    $('#attributes td.attribute').each(function(i) {
        $(this).children("p,div,ul").hide();
    });

    $('#attributes td.attribute span').click(function(event) {
        if (this == event.target) {
            ToggleMe(this);
            $("#attributeInfo").slideUp(300);
            document.getElementById('attributeInfo').innerHTML = $(this).parents("td").html();

            //$("#attributeInfo").html($(this).parents("td").html())
//            if (document.all) {
//                //alert('IE');
                //Why we are doing this: Click on attribute name (when you do second time) is not working in IE so replace whereever " with &quot;
                //**********
                //Reference:
                //**********
                //Drill down into Association.
                //Drill down into EventAssociation.
                //Open perpetrator.
                //Click on Disciplinary Action in Attributes.
                //Click on Disciplinary Action End Date.
                //Now click on Disciplinary Action again. Nothing happens. Once you click on any attribute, you can not go back and open it again
                //$("#attributeInfo").html($(this).parents("td").html().replace(/"/g, '&quot;').replace('&quot;showMoreCodeSets', '"showMoreCodeSets').replace('parentNode);&quot;', 'parentNode);"').replace('value=&quot;', 'value="').replace('&lt;/div&gt;&quot;', '</div>"').replace('&quot;hidden&quot;', '"hidden"').replace('input id=&quot;', 'input id="').replace('&quot; value=', '" value=').replace('&quot;title minus&quot;', '"title minus"').replace('&quot;title plus&quot;', '"title plus"'));
                
                // Above line fixed the repeated attribute click but broke the a more codes anchor
                // problematic innerHTML implementation with IE
//                document.getElementById('attributeInfo').innerHTML = $(this).parents("td").html();
//            }
//            else {
//                //alert('FF');
//                $("#attributeInfo").html($(this).parents("td").html());
//            }

            $("#attributeInfo").children().show();
            $("#attInfo").hide(); // Hiding the div containing complete info			 
            $('#showAll').text('Show All'); // Renaming the text based on text
            $("#attributeInfo").children("span.title").hide();
            $.scrollTo("#attributeInfo");
            $("#attributeInfo").slideToggle(300);
        }
    });


    //end my box functions


    $("#clickAttribute").click();


    $("ul.leftnav li ul").hide();
    $('ul.leftnav li:has(ul)').each(function(i) {
        $(this).children("ul").slideUp(400);
    });
    $('ul.leftnav li:has(ul)').addClass('plus');

    var current = $('ul.leftnav li').find("a").filter(function() { return this.href.toLowerCase() == location.href.toLowerCase(); });
    if (current.length) {

        current.addClass("selected").parents("ul, li:has(ul)").show(function() {

            if ($(this).is('li')) {
                $(this).removeClass('plus').addClass('minus');
                $(this).children('ul').show();
            }
        });
    }
    function ToggleMe(el) {
        $(el).toggleClass('plus').toggleClass('minus');
    }

    $('li.p1:has(ul)').click(function(event) {
        if (this == event.target) {
            current = this;
            ToggleMe(this);


            $('ul#leftnav li:has(ul)').each(function(i) {
                if (this != current) { $(this).children("ul").slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });


            $(this).children("ul:eq(0)").slideToggle(400);

        }
    });

    $('li.p2:has(ul)').click(function(event) {
        if (this == event.target) {
            current = this;
            ToggleMe(this);

            $('li.p2:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $('li.p3:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $(this).children("ul:eq(0)").slideToggle(400);


        }
    });

    $('li.p3:has(ul)').click(function(event) {
        if (this == event.target) {
            current = this;
            ToggleMe(this);

            $('li.p3:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $('li.p4:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $(this).children("ul:eq(0)").slideToggle(400);

        }
    });

    $('li.p4:has(ul)').click(function(event) {
        if (this == event.target) {
            current = this;
            ToggleMe(this);

            $('li.p4:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $('li.p5:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $(this).children("ul:eq(0)").slideToggle(400);

        }
    });
    $('li.p5:has(ul)').click(function(event) {
        if (this == event.target) {
            current = this;
            ToggleMe(this);

            $('li.p5:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $('li.p6:has(ul)').each(function(i) {
                if (this != current) { $(this).children('ul').slideUp(400); $(this).removeClass('minus').addClass('plus'); }
            });

            $(this).children("ul:eq(0)").slideToggle(400);

        }
    });
    $('li.p6:has(ul)').click(function(event) {
        if (this == event.target) {
            current = this;
            ToggleMe(this);

            $('li.p6:has(ul)').each(function(i) {
                if (this != current) { $(this).children().slideUp(400); ; $(this).removeClass('minus').addClass('plus'); }
            });

            $(this).children("ul:eq(0)").slideToggle(400);

        }
    });

});


