$(document).ready(function() {    
var noLeave = false;
var initDone = false;
function dbg(m) {
//alert(m + " " + $('#check_5').attr('checked'));
}
$('.choiceoff').mouseenter(function()
    {
        if (!$('#accessibilityMode').attr("checked") && !noLeave)
        {
            noLeave = true;
            var options = $(this).parent().find('.chooseoff');
            options.show('slide',{direction:'up', distance:200},500,function() {noLeave=false; dbg(1);});
            var wheight = $(window).height();
            if(options.height()+options.offset().top > wheight)
            {
                $(window).scrollTop(options.offset().top);
            }
            $(this).hide('slide',{direction:'down', distance:200});
        }
     
    });

$('.chooseoff').mouseleave(function()
    {
        if (!$('#accessibilityMode').attr("checked") && !noLeave)
        {
            //noLeave = true;
            $(this).parent().find('.choiceoff').show('slide',{direction:'up', distance:200},500,function() {noLeave=false; dbg(2);});
        dbg(3);
            $(this).hide('slide',{direction:'down', distance:200});
        dbg(4);
        }
    });

$('.chooseoff input:checkbox,.chooseoff input:radio').click(function() { $(this).change();});    
$('.chooseoff input:checkbox,.chooseoff input:radio').change(function()
    {
        if (this.checked)
        {
            $(this).addClass('selected');
        }
        else
        {
            $(this).removeClass('selected');
        }
        var choices = $(this).parents('.inputArea').find('.choiceoff');
        var options = $(this).parents('.inputArea').find('.chooseoff');
        var temp = $(choices).find('.choiceoffTemplate');
        var sel = $(choices).find('.noChoice');
        var labelFor='label[for="'+this.id+'"]';
        var label = $(labelFor);
        if (this.checked)
        {
            if(this.type == 'radio')
            {
                choices.find('.chosen').remove();
            }
            label.addClass('selected');
            var n = temp.clone();
            var l = label.html();
            n.find('.text').html(l);
            var exist = choices.find('.text').filter(function(idx) 
                {
                    var oldHtml = $(this).html();
                    return oldHtml == l;
                });
            if (exist.length == 0) {
                choices.append(n);
            }
            
            n.removeClass('choiceTemplate').addClass('chosen').show();
            $(sel).hide();
            $(this).nextAll(".children").show().find("input:checkbox").removeAttr('checked').change();
            $(this).parents('.children').prevAll(".parent").removeAttr('zchecked').change();
            var limit = options.find(".limit").html();
            if (limit)
            {
                var selected = options.find("[checked]");
                
                while (selected.length > limit) {
                    selected.not(this).slice(0,1).removeAttr('checked').change();
                    selected = options.find("[checked]");
                }
            }
            var wheight = $(window).height();
            if(options.height()+options.offset().top > wheight)
            {
                $(window).scrollTop(options.offset().top);
            }
        }
        else
        {
        if (initDone) {        dbg('unchecked');}
             label.removeClass('selected');
            var showSelect=true;
            choices.find('.chosen').each(function()
            {
                var t = $(this).find('.text');
                if (t.html()==label.html())
                {
                    $(this).remove();
                }
                else
                {
                    showSelect = false;
                }
            });
            if (showSelect)
            {
                $(sel).show();
            }
            if ($(this).nextAll(".children").find('input:checked').count == 0) {
                $(this).nextAll(".children").hide().find('input:checkbox').removeAttr('checked');
            }
        }
        return true;
    });  
    
    $('#accessibilityMode').click(function() 
        {
            if (this.checked)
            {
                $('.choiceoff').hide();
                $('.chooseoff').show();
            }
            else
            {
                $('.choiceoff').show();
                $('.chooseoff').hide();
            }
        });
        
    $('#accessibilityMode').change();
    $('.chooseoff input:checkbox,.chooseoff input:radio').change();  
    
    $('.projectName').mouseover(function(){
        var id = this.id;
        var pid = id.slice(5);
        $('.projectDescription').hide();
        var description = $('#description_'+pid);
        var dc = $('#descriptionContainer');
        dc.append(description.show());
        $('.projectName').removeClass('highlight');
        $(this).addClass('highlight');
        
    });
    
    $('.dropTrigger').mouseover(function(ev) {
    
        var pos = $(this).position();
        $(this).find('.dropDown').css('visibility','visible').css('left',pos.left).css('top',pos.top+$(this).height());
    });
    
    $('.dropTrigger').mouseout(function() {
        $(this).find('.dropDown').css('visibility','hidden');
    });
    
    $(".calendar").datepicker();
    
    
    initDone = true;
}  );

function CheckAll(sibling)
{
    var checks = $(sibling).parent().find('input[type="checkbox"]');
    checks.attr('checked','checked').change();
}

function CheckNone(sibling)
{
    var checks = $(sibling).parent().find('input[type="checkbox"]');
    checks.removeAttr('checked').change();
}
  
  
  