$(document).ready(   
    function () {   

        function validate(formData, jqForm, options) { 
            for (var i=0; i < formData.length; i++) { 
                if (!formData[i].value) { 
                    alert('Vul alle velden in'); 
                    return false; 
                } 
            } 
        }
         
        function showResponse(responseText, statusText)  {  
            //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.');
            //var t=setTimeout("location.href = location.href;",10000);
        } 

        var options = { 
            target:        '.edit_wedstrijd',
            //beforeSubmit:  validate,
            success:       showResponse
        }; 

        var options = { 
            target:        '#addVideoForm',
            beforeSubmit:  validate,
            success:       showResponse
        }; 


        $('.editItemForm').submit(function() {
            $(this).ajaxSubmit(options_video); 
            return false; 
        });

        $('#addVideoForm').submit(function() {
            $(this).ajaxSubmit(options); 
            return false; 
        });

        $(".date-pick").datepicker({    
            showOn: 'button', 
            buttonImage: '../img/calendar_icon.png', 
            buttonImageOnly: true,
            buttonText: 'selecteer datum',
            dateFormat: 'dd-mm-yy',
            monthNames: ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'],
            dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
            dayNamesShort: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
            onSelect: function(dateText, inst) {
                $(".datepicker_value").val(dateText);
            }
        });
        //$('.date_pick').datepicker('setDate', new Date ('2009', '12', '12'));

        $('.ui-state-default').hover(
            function(){ $(this).addClass('ui-state-hover'); }, 
            function(){ $(this).removeClass('ui-state-hover'); }
        );
        $('#btn_gastenboek').click(function(){
            $(this).toggleClass('ui-state-active');
            location.href = '/gastenboek/teken';
        });
        $('#wedstrijdbeheer').click(function(){
            $(this).toggleClass('ui-state-active');
            location.href = '/wedstrijden/beheer';
        });

        $('.ui-state-default').click(function(){
            editItem($(this).attr('name'));
        });

        $('#addVideo').dialog({
            autoOpen: false,
            modal: true,
            width: '550px',
            overlay:    {
                opacity:    0.75,
                background: '#000'
            },
            close: function(ev, ui) {
                location.href = location.href;
            }
                            
        });
       
        $('#btn_video').click(function(){
            $(this).toggleClass('ui-state-active');
            $('#editor1').addClass('ckeditor');
            $('#addVideo').dialog('open');
            return false;
        });

        function editItem(id) {
            $('#edit_wedstrijd_' + id).dialog({
                autoOpen: false,
                modal: true,
                width: '550px',
                overlay:    {
                    opacity:    0.75,
                    background: '#000'
                },
                close: function(ev, ui) {
                    location.href = location.href;
                }
                                
            });
            $('#edit_wedstrijd_' + id).dialog('open');
        }

        $('#tabs').tabs();

        $('#futyo').tabs();
        $('#andre').tabs();
        $('#melkboer').tabs();
        $('#annadrank').tabs();
        $('#passagier').tabs();
        $('#champ').tabs();
        $('#stalker').tabs();
        $('#dennis').tabs();
        $('#ehoezen').tabs();
        $('#schnappie').tabs();
        $('#coach').tabs();
        $('#neuse').tabs();
        $('#kuper').tabs();
        $('#jose').tabs();
        $('#putten171').tabs();
        $('#adolf').tabs();
        $('#tuinboon').tabs();
        $('#maus').tabs();
        $('#zwalkie').tabs();
        $('#donnie').tabs();
        $('#ricardo').tabs();
        $('#ronnie').tabs();
        $('#wiecher').tabs();
        $('#whoezen').tabs();
        $('#koekkie').tabs();

        $('#accordion').accordion({ autoHeight: false });
        $('#homepage').accordion({ autoHeight: false });
        $("a.single").fancybox({
            'hideOnContentClick': false,
            'frameWidth': 570,
            'frameHeight': 533,
            'overlayShow': true,
            'overlayColor': '#000',
            'overlayOpacity': '0.75'
        });
        $("a.group").fancybox({
            'hideOnContentClick': false,
            'overlayShow': true,
            'overlayColor': '#000',
            'overlayOpacity': '0.75'
        });
        
        $("#statistieken").tablesorter({
            sortList: [[0,0],[2,0]],
            widgets: ["zebra"]
        }); 
    }
);

jQuery(function(){
    jQuery('#cropbox').Jcrop({
        onSelect: updateCoords,
        aspectRatio: 275/250 
    });
});

function updateCoords(c) {
    jQuery('#x').val(c.x);
    jQuery('#y').val(c.y);
    jQuery('#w').val(c.w);
    jQuery('#h').val(c.h);
};

function checkCoords() {
    if (parseInt(jQuery('#w').val())>0) return true;
    alert('Selecteer een gebied van de foto welke uitgesneden moet worden.');
    return false;
};
