/*global $, ga, SF*/

function formatAbbr(s) {
    var $abbr = $('abbr', '<div>' + s + '</div>');
    if ($abbr.length) {
        return $abbr.attr('title');
    } else {
        // strip out HTML tags
        var html_tags_regex = new RegExp('<(?:.|\\s)*?>', 'g');
        return $.trim(s.replace(html_tags_regex, ""));
    }
}

if($.tablesorter){
    // add a custom parser for file sizes
    $.tablesorter.addParser({
        id: 'filesize',
        is: function(s) {
            // return false so this parser is not auto detected
            return false;
        },
        format: function(s) {
            var matches = s.match(/(\d+\.?\d*)\s(\w+)/) || [],
            size = parseFloat(matches[1]) || 0,
            suffix = matches.length ? matches[2].toLowerCase() : '',
            bytes = size; // default case

            // using standard SI definition
            switch(suffix) {
                case 'kb': bytes = size * 1000; break;
                case 'mb': bytes = size * 1000000; break;
                case 'gb': bytes = size * 1000000000; break;
            }

            return bytes;
        },
        // set type, either numeric, text, or natural
        type: 'numeric'
    });

    $.tablesorter.addParser({
        id: 'type',
        is: function(s) {
            // return false so this parser is not auto detected
            return false;
        },
        format: function(s) {
            var type = 0;
            if ($(s).hasClass('folder')) {
                type = 1;
            } else if ($(s).hasClass('file')) {
                type = 2;
            } else {
                type = 3;
            }
            return type;
        },
        // set type, either numeric, text, or natural
        type: 'numeric'
    });

    $.tablesorter.addParser({
        id: 'abbrdate',
        is: function(s) {
            // return false so this parser is not auto detected
            return false;
        },
        format: formatAbbr,
        // set type, either numeric, text, or natural
        type: 'text'
    });
}



$('#download-redirect-dialog form').submit(function(e) {
    e.preventDefault();
    if ($('input:text', this).val()) {
        $('label.error', this).removeClass('error');
        $('span.error', this).remove();
        $.ajax({
            url: 'download_redirect',
            type: 'POST',
            data: $(this).serialize(),
            beforeSend: function(xhr){
                xhr.setRequestHeader("Accept","application/json; charset=utf-8");
            },
            success: function(data, statusText) {
                $('#download-redirect-dialog').dialog('close');
                $('#messages').notify({
                    status: 'success',
                    message: 'Download redirect settings saved.'
                });
            },
            error: function(data, statusText) {
                $('#download-redirect-dialog').dialog('close');
                var exc = {};
                try {
                    exc = $.parseJSON(data.responseText);
                } catch (e) {
                    if ('console' in window) {
                        window.console.error("Invalid JSON: " + data.responseText);
                    }
                }
                if (!('error' in exc) || typeof(exc.error) !== 'string') {
                    exc.error = 'An error occurred while creating the folder.';
                }
                $('#messages').notify({
                    status: 'error',
                    message: exc.error
                });
            }
        });
    } else {
        $('input:text', this).focus().select();
        $('label', this).addClass('error');
        $(this).prepend('<span class="error">The folder name is invalid, please try again.</span>');
    }
    return false;
});

$('.download-redirect').click(function(e) {
    e.preventDefault();
    $('#download-redirect-dialog').dialog('open');
    return false;
});

$(function(){
    $('#download-redirect-dialog').dialog({
        autoOpen: false,
        modal: true,
        title: 'Download Details',
        width: 450,
    });
    $('.ui-dialog').css({zIndex: 1000});
});
