$(document).ready(function(){
   
    // Lang menu
    $(document).click(function(){
        if ($("#selectLang").css("display") != "none")
            $("#selectLang").slideUp();
    });
    $("#langMenu .clickable").click(function(){
        if ($("#selectLang").css("display") == "none")
            $("#selectLang").slideDown();
        else
            $("#selectLang").slideUp();
        return false;
    });
    $(".langSelectAnchor").click(function(){
        var lang = $(this).attr("rel");
        $("#langSelectFormLangField").attr("value", lang);
        $("#langSelectForm").submit();
    });
    // end Lang menu

    // Forms
    var options = {
        dataType: 'script',
        beforeSend: function() {
            $('.errorBlock').hide();
            $('.successBlock').hide();
            $('.errorBlockContent').html('');
            $('.successBlockContent').html('');
        }
    };

    $('.ajaxForm').ajaxForm(options);
    $('.ajaxForm').submit(function(){
        var id = $(this).parent().attr('id');
        $('#'+id+' input[name*="submit"]').attr('disabled', 'disabled');
    });
    // end Forms

    // download reports
    createDialog('#downloadDialog', 400);
    $('#mnuDownload').click(function(){
        openDialog('#downloadDialog', {
            type: 'POST',
            url: '/ajax/download/action/sel_device/'
        }, downloadDeviceLoaded)
    });
    
    // file system
    createDialog('#fileSystemDialog', 480);
    $('#mnuFileSystem').click(function(){
        var deviceId = $(this).attr('deviceId');
        openDialog('#fileSystemDialog', {
            type: 'POST',
            url: '/ajax/fs/open/',
            data: {deviceId: deviceId}
        })
    });
    $('#fileSystemDialog').dialog('option', 'height', 540);
    
});

function downloadDeviceLoaded(data) {
    $('#selDeviceDownload').change(function(){
        var deviceId = $(this).val();
        $('#downloadDialog').html('<div class="dialogLoading"></div>');
        $.post('/ajax/download/action/sel_period/',
            {deviceId: deviceId},
            downloadPeriodsLoaded);
    });
}

function downloadPeriodsLoaded(data) {
    $('#downloadDialog').html(data);
    $('#selPeriodDownload').change(function(){
        var period = $(this).val();
        $('#downloadDialog').html('<div class="dialogLoading"></div>');
        $.post('/ajax/download/action/prepare/', 
            {
                deviceId: $('#deviceIdDownload').attr('value'),
                period: period
            }, function(data){
                $('#downloadDialog').html(data);
            })
    });
}

// Dialog
function createDialog(sel, width, btns) {
    $(sel).dialog({
        autoOpen:   false,
        width:      width,
        draggable:  true,
        buttons:    btns
    });
}

function openDialog(sel, options, onSuccess) {
    $(sel).html('<div class="dialogLoading"></div>');
    $(sel).dialog("open");
    $.ajax({
        type:   options.type,
        url:    options.url,
        data:   options.data,
        success: function(data) {
            $(sel).html(data);
            if (onSuccess != null) onSuccess(data);
        }
    });
}


