$(document).ready(function()
    {   
        $(".checkbox").click(function(event){

            if($(this).children("input").prop("checked"))
            {
                // uncheck
                $(this).children("input").prop("checked", false);
                $(this).removeClass("checked");
            }else{
                // check
                $(this).children("input").prop("checked", true);
                $(this).addClass("checked");
            }
        });
        
        // set inital status
        $(".checkbox").each(function()
        {
            if($(this).children("input").prop("checked"))
            {$(this).addClass("checked");}
        });
        
                
        $(".radio").click(function(event)
        {
            event.stopPropagation();
            if(!($(this).hasClass('option_date_other')))
                $( "#date_from, #date_to" ).val('');
            else
                $('#date_from').focus();
                    
            if(!$(this).children("input").prop("checked"))
            {               
                $(this).parent('.date_options').children("input").prop("checked", false);
                $(this).parent('.date_options').children(".radio").removeClass("selected");
                $(this).children("input").prop("checked", true);
                $(this).addClass("selected");
            } 
                    
        });
        
        $(".radio").each(function()
        {
            if($(this).children("input").prop("checked"))
            {$(this).addClass("selected");}
        });
                
    
                
        $( "#date_from, #date_to" ).bind('change', function(data, event)
        {                    
                    
            $('.date_options .radio').each(function()
            {
                $(this).children("input").prop("checked", false);
                $(this).removeClass("selected");
            });
            
                    
        });
               
                
        $.datepicker.regional['nl'] = {
            closeText: 'Sluiten',
            prevText: '←',
            nextText: '→',
            currentText: 'Vandaag',
            monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
            'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
            monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
            'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
            dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
            dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
            dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
            weekHeader: 'Wk',
            dateFormat: 'dd-mm-yy',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ''
        };
                
        $.datepicker.setDefaults($.datepicker.regional['nl']);
                
        var dates = $( "#date_from, #date_to" ).datepicker({
            changeMonth: true,
            numberOfMonths: 3,
            onSelect: function( selectedDate ) {
                var option = this.id == "date_from" ? "minDate" : "maxDate",
                instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );
                dates.not( this ).datepicker( "option", option, date );
                $(this).change();
            },
            showOn: "both",
            buttonImage: "http://www.impactentertainment.nl/assets/images/calendar.png",
            buttonImageOnly: true
        });
                
        $('#search_url').click(function(event)
        {
            $(this).parents('form').submit();
                   
            event.preventDefault();
        });
                
        $('#search_label').click(function(event)
        {
            $('#search_button').trigger('click');
            event.preventDefault();
            event.preventBubble();
        });
                
        $('#search_button').click(function(event)
        {
                    
            if($('#search_hidden').hasClass('visible'))
            {
                $(this).removeClass("search_button_expanded");
                
                $('#search_wrapper_current').slideDown(300);
                
                $('#search_overlay').fadeTo('fast', 0);
                $('#search_overlay').hide();
                
                
                        
                $('#search_hidden').slideUp(300,function()
                {$(this).removeClass('visible');});
                        
            }else
            {   
                $('#search_button').addClass("search_button_expanded");
                
                $('#search_wrapper_current').slideUp(300);
                
                $('#search_overlay').fadeTo('fast', 0.7);
                        
                $('#search_hidden').slideDown(300,function()
                {$(this).addClass('visible');});
            }
                        
            event.preventBubble();
            event.preventDefault();
        });
               
        $('#search_highlights_carousel').jcarousel({
            wrap: 'circular',
            scroll: 1
        });
        
        $("#simple_search .text").focus(function()
        {$(this).addClass('text_active');});
        
        $("#simple_search .text").blur(function()
        {
            if($(this).val() == '')
                $(this).removeClass('text_active');
        });
        
        if($("#simple_search .text").val()!='')
            $("#simple_search .text").addClass('text_active');
        
        $("#simple_search a.button").click(function(event)
        {
           $(this).parents('form').submit();
           event.preventDefault();
        });
        
        if($('#search_wrapper_current').height() > 1)
        {
          var offset = $('#frame-bottom').offset();
          offset.top = offset.top + $('#search_wrapper_current').height();

          $('#frame-bottom').offset(offset);

        }
                
        $('#search_clear').click(function(event)
        {
            $(".checkbox").each(function()
            {
                $(this).children("input").prop("checked", false);
                $(this).removeClass("checked");
            });

            $(".radio").each(function()
            {
                $(this).children("input").prop("checked",false);
                $(this).removeClass("selected");
            });
            
            $('#date_from').val('');
            $('#date_to').val('');
            $('input[name="keywords"]').val('');
            $('input[name="location"]').val('');

            event.preventDefault();
        });
    });
