jQuery(document).ready(function($){

    //    Toggle form groups
    $('td.ensembleTitle').live('click', function() {
        var is_open = $('div#' + $(this).attr('rel')).is(':visible');

        $('td.ensembleTitle .ensembleIcon').text('+');
        $('div.ensembleBlock').hide();

        if (!is_open) {
            $(this).find('.ensembleIcon').text('-');
            $('div#' + $(this).attr('rel')).show();
        }
    });

    //    Progressbar
    $('div.progressbar').each(function() {
        var val = parseInt($(this).attr('value'));

        $(this).progressbar({
            value: val
        });

        if (val == 100) {
            $(this).addClass('complete');
            $(this).find('div').text(val+'%');
            $(this).prepend('<span></span>');
        } else if (val >= 12) {
            $(this).find('div').text(val+'%');
        } else  {
            $(this).prepend('<span>'+val+'%</span>')
        }
    });

    //    Countries
    //    $('select.country').live('change', function() {
    //        var rel = $(this).attr('rel');
    //        var value = $(this).val();
    //        $('select#city-'+rel).children('option').remove();
    //
    //        $.getJSON('/ajax/city/' + value, function(data){
    //            $.each(data, function(key, val) {
    //                $('select#city-'+rel).append('<option value="'+val.id+'">'+val.name+'</option>');
    //            });
    //        });
    //    });

    // Schools
    function manageSchoolSelect() {
        // unblock the unused schools qnd block the used ones
        $('select.school option:not(:selected)').each(function(){
            var val = $(this).val();
            if (val != '') {
                $('select.school option[value='+val+']').removeAttr('disabled');
            }
        });

        $('select.school option:selected').each(function(){
            var val = $(this).val();
            if (val != '') {
                $('select.school option[value='+val+']').attr('disabled', 'disabled');
                $(this).removeAttr('disabled').attr('selected', 'selected').select();
            }
        });
    }
    
    manageSchoolSelect();
    $('select.school').live('change', function() {
        manageSchoolSelect();
    });

    // Admin dashboard menu
    $('#dashboard_menu > li').live('mouseover', function() {
        $(this).addClass('hover');
        $('#dashboard_menu li.selected ul.submenu').hide();
        $(this).find('ul.submenu').show();
    });

    $('#dashboard_menu > li').live('mouseout', function() {
        $(this).removeClass('hover');
        $(this).find('ul.submenu').hide();
        $('#dashboard_menu li.selected ul.submenu').show();
    });

    $('#dashboard_menu ul.submenu li').live('mouseover', function() {
        $(this).addClass('hover');
    });

    $('#dashboard_menu ul.submenu li').live('mouseout', function() {
        $(this).removeClass('hover');
    });

    if ($.fn.dataTableExt) {
        $.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw ) {
            if ( typeof sNewSource != 'undefined' && sNewSource != null )
            {
                oSettings.sAjaxSource = sNewSource;
            }
            this.oApi._fnProcessingDisplay( oSettings, true );
            var that = this;
            var iStart = oSettings._iDisplayStart;

            oSettings.fnServerData( oSettings.sAjaxSource, [], function(json) {
                /* Clear the old information from the table */
                that.oApi._fnClearTable( oSettings );

                /* Got the data - add it to the table */
                var aData =  (oSettings.sAjaxDataProp !== "") ?
                that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;

                for ( var i=0 ; i<json.aaData.length ; i++ )
                {
                    that.oApi._fnAddData( oSettings, json.aaData[i] );
                }

                oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
                that.fnDraw();

                if ( typeof bStandingRedraw != 'undefined' && bStandingRedraw === true )
                {
                    oSettings._iDisplayStart = iStart;
                    that.fnDraw( false );
                }

                that.oApi._fnProcessingDisplay( oSettings, false );

                /* Callback user function - for event handlers etc */
                if ( typeof fnCallback == 'function' && fnCallback != null )
                {
                    fnCallback( oSettings );
                }
            }, oSettings );
        }
    }

    // Uploaded files edition
    $("input.file.completed").each(function() {
        $(this).after('<a class="button_link_blue edit_file" rel="' + $(this).attr('name') + '">Modifier</a>');
        $(this).hide();
    });
    $("a.edit_file").live('click', function() {
        $("input[name='" + $(this).attr('rel') + "']").show();
        $(this).hide();
    });

    // Are you sure popup
    $(".areYouSure").live('click', function() {
        var msg = $(this).attr('msg');
        
        if (confirm(msg)) {
            return true;
        } else {
            return false;
        }
    });

    // Year filter
    $('#study_year').live('change', function() {
        $(this).parent('form').submit();
    });

    // Classes
    $("form").addClass('form');
    $("input[type='radio']").addClass('radio');
    $("input[type='checkbox']").addClass('radio');
    $("input[type='submit']").addClass('button_blue');

});
