$(document).ready(function(){

	//Find the id of the selected page
	var pageid = $('#wrapper').attr('class');
	//Highlight the selected page
    if( pageid > 0 ) {
        $("a[rel=" + pageid + "]").addClass("selected");
    }
    
	//Search through the navigation for submenus and add toggles to them

    var headerLinks = $('#header ul li a');
    if( headerLinks.length > 0) {
        $(headerLinks).each(
            function(i) {

                //Check if we have any submenus to deal with
                if($(this).parent().children('ol').length != 0) {

                    //Find the submenu
                    var submenu = $(this).parent().children('ol');

                    //Show the submenus when mousing over.
                    $(this).mouseover(
                        function(){
                            $(submenu).show(50);
                        }
                    );

                    //Collapse all submenus when we mouseover a top-level
                    $(this).mouseout(
                        function(){
                            collapse();
                        }
                    );

                    //Show submenu if a child is selected
                    $("li a", submenu).each(
                        function(i){

                            $(this).mouseover(
                                function(){
                                    $(submenu).show();
                                }
                            )

                            $(this).mouseout(
                                function(){
                                    $(submenu).hide();
                                }
                            )

                            //Show submenu is a child is selected
                            if($(this).attr('class') == "selected") {
                                //$(submenu).show();
                            }
                        }
                    )
                }
            }
        );
    }
	
	// Collapse the navigation
	collapse();
	
	//Collapse submenus
	function collapse() {
		$('#header ul li > ol').each(
			function(i){
				$(this).hide();
			}
		);
	}
	
	
	//Check if there are language divs, if so, do the tooltips
    var languages = $("#languages");
	if(languages.length != 0){
		$(languages).hide();
		$("#widecontent ul li a").tooltip({ 
		    bodyHandler: function() { 
		        return $($(this).attr("href")).html(); 
		    }, 
		    showURL: false 
		});
	}
	
	//Zebra stripe tables
	$("table tbody").each(function(i){
		
		for(i=0; i<$(this).length; i++) {
			$("tr", $(this)[i]).each(
				function(j){
					if(j%2 == 0) {$(this).addClass("alt")}
				}
			);
		}
	});


	
});
