$(document).ready(function() {
    $('#frmConvert').submit(function() {
        $("#convert_btn").attr("disabled", "true");
        $("#convert_wait").show();
        
        var options = {
            target: '#notify_convert',
            success: function(responseText) {
                $("#convert_btn").removeAttr("disabled");
                $("#convert_wait").hide();
                if(responseText.indexOf('Error') == -1){
                    showTips($("#notify_convert"), 'success', responseText);
                    $("#list_of_converted").load("/uploaded/converted/"+ $("#file_key").val()+'?r='+ Math.round(Math.random() * 10000));
                }
                else{
                    showTips($("#notify_convert"), 'error', responseText);
                }
            }
        };
        $(this).ajaxSubmit(options);
        return false;
    });

    $('#frmUrl').submit(function() {
        $("#url_btn").attr("disabled", "true");
        $("#download_wait").show();
        $("#notify_download").html("");
        
        var options = {
            target: '#notify_download',
            dataType:"json",
            cache:false,
            success: function(data) {
                $("#url_btn").removeAttr("disabled");
                $("#download_wait").hide();
                if($.trim(data.msg).indexOf('Error') == -1){
                    window.location.href="/uploaded/file/"+data.msg;
                }else{
                    $("#notify_download").html(data.msg);
                }
            }
        };
        $(this).ajaxSubmit(options);
        return false;
    });

    if($("#frmUpload").length > 0){
        $(function() {
            $("#frmUpload").uploadProgress({
                jqueryPath: "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",
                uploadProgressPath: "/media/js/jquery.uploadProgress.js",
                start: function() {
                    $("#postForm").slideUp();
                    filename = $("#Filedata").val().split(/[\/\\]/).pop();
                    $("#progress_filename").html('Uploading ' + filename + "...");
                    $("#uploading").slideDown();
                },
                uploading: function(upload) {
                    $('#percents').html(" [ "+upload.percents+'% ] ');
                },
                progressBar: "#progressbar",
                progressUrl: "/progress",
                interval: 2000
            });
        });
    }
});

//Remove file
function removeUploaded(file_key){
    $.fn.jmodal({
        title: 'Confirmation Dialog',
        content: 'Are you sure to remove it?',
        buttonText: {
            ok: 'Yes',
            cancel: 'No'
        },
        okEvent: function(obj, args) {
            $.ajax({
                type: "get",
                url: "/uploaded/remove/"+ file_key,
                dataType: 'text',
                success: function(data){
                    if(data.indexOf('Error:') == -1){
                        $("#file_list_"+file_key).slideUp('slow');
                    }
                }
            });
        }
    });
}

//Remove file
function removeConverted(file_key){
    $.fn.jmodal({
        title: 'Confirmation Dialog',
        content: 'Are you sure to remove it?',
        buttonText: {
            ok: 'Yes',
            cancel: 'No'
        },
        okEvent: function(obj, args) {
            $.ajax({
                type: "get",
                url: "/converted/remove/"+ file_key,
                dataType: 'text',
                success: function(data){
                    if(data.indexOf('Error:') == -1){
                        $("#file_list_"+file_key).slideUp('slow');
                    }
                }
            });
        }
    });
}


function showTips( tipobj, status, data ){
    $(tipobj).removeClass();
    if(status =='success'){
        $(tipobj).addClass('box box-success').slideDown('slow');
    }
    else if(status =='error'){
        $(tipobj).addClass('box box-error').slideDown('slow');
    }
    else{
        $(tipobj).addClass('box box-info').slideDown('slow');
    }

    $(tipobj).slideDown();
    if(data != '')
        $(tipobj).html(data);

    setTimeout( function(){
        $(tipobj).slideUp();
    }, ( 4000 ) );
}

function closeObj (divobj){
    if($(divobj).css('display')!='none'){
        $(divobj).slideUp('normal');
    }
}

function openObj (divobj){
    if($(divobj).css('display')=='none'){
        $(divobj).slideDown('normal');
    }
}
