$(function(){
    $('.expand').each(function(){if(!$(this).attr('checked')) {$(this).siblings('.expandtarget').hide();}});
    $('.expand').click(function(){
	$(this).siblings('.expandtarget').toggle();
    });
    $('#selectalltop').click(function() {
	var checked = this.checked;
	$('.expand').each(function(i,x){x.disabled=checked;});
    });
    $('.selectall').click(function(){
	var selall = this;
	$(this).parents('table').find('input').each(function(x,y){y.checked=selall.checked;});
    });
    $("#nav > li").hover( function() { 
        $(this).addClass("hover");
        $("ul", this).show();
        $(this).css('z-index', '101');
    },function(){
        $(this).removeClass("hover");
        $("ul", this).hide();
        $(this).css('z-index', '100');
    });
    $('form').submit(function() {
        try {
            var valid = true;
            $('.expand').each(function() {
                $(this).parent().css('border-style', 'none');
                if($(this).attr('checked')) {
                    input = $(this).siblings('.expandtarget').children('input');
                    if(input.val() == '') {
                        if(valid) alert('Please fill in values for everything that you checked');
                        $(this).parent().css('border', '1px solid red');
                        valid=false;
                    }
                }
            });
            return valid;
        } catch(e){return false;}
    });

    $("#nav li.travel").hover( function() {
        $("#sortby").hide();
    },function(){
        $("#sortby").show();
    });

    $("#nav > li").hover( function() { 
        $("a:first", this).addClass("open");
        $("ul", this).show();
        $(this).css('z-index', '101');
    },function(){
        $("a:first", this).removeClass("open");
        $("ul", this).hide();
        $(this).css('z-index', '100');
    });

});

function toggleVis(obj) {
    var el = document.getElementById(obj);
    if ( el.style.display != 'none' ) {
	el.style.display = 'none';
    } else {
	el.style.display = 'block';
    }
}
