﻿function initHomeSubSubject()
{

    var open = false; 
    
    var topUL = $(".multiLevelCheckBoxList");

    $(topUL).children("li").each( function() 
    {
        // if this li has a class of selector then ignore it        
        if ( !$(this).hasClass("selector") ) 
        {
            var innerUL = $($(this).children()[2]).filter("ul");            

            if( $( innerUL ).length > 0 )
            {

                innerUL.addClass("sub-level");

                $(innerUL).before('<a href="" class="arrow down sub-level-button"></a>');

                $(innerUL).hide();

                // add click event to the top level tick box, if top level is checked/unchecked then all sublevel boxes follow suit
                var checkBox = $($(this).children()[0]);

                $(checkBox).click(function(e) 
                {

                    if (this.checked == true) 
                    {
                        // find the UL                
                        var sublevel = $(this).next().next().next();

                        // find the LIs  
                        $(sublevel).children().each(function() {
                            // check the tick box
                            $(this).children()[0].checked = true;
                        });

                    }
                    else 
                    {
                        // find the UL  
                        var sublevel = $(this).next().next().next();

                        // find the LIs  
                        $(sublevel).children().each(function() {
                            // check the tick box
                            $(this).children()[0].checked = false;
                        });
                    }

                });
            
            }
            
        }
    });             
    
    function toggleSetup( arrowButton )
    {
        var checkBox = $( arrowButton ).prev().prev();
    
        if ( $( arrowButton ).hasClass( "up" ) ) // pod is open so close it
		{
			// swap css class
			$( arrowButton ).addClass( "down" );
			$( arrowButton ).removeClass( "up" );					
														
		}
		else                                     // pod is closed so open it
		{
			// swap css class
			$( arrowButton ).addClass( "up" );
			$( arrowButton ).removeClass( "down" );								        		   
		}
    }
    
    $(".sub-level-button").click( function( e )
    {
        e.preventDefault();
    
        $( this ).next().slideToggle( "4000", toggleSetup( this ) );                
        
    });

}