function setCookie(name, value)
{
    var date = new Date();  
    date.setTime(date.getTime()+(24*60*60*1000));  
    var expires = "; expires="+date.toGMTString(); 
    document.cookie = name+"="+escape(value)+expires+"; path=/";
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1)
        { 
            c_start = c_start + c_name.length+1 
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) 
                c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        } 
    }
    return ""
}

function getAsyn() {

    var aj = getCookie('_aj');
    if (aj.length > 0) {
        var jobs = JSON.parse(aj);
        return jobs;
    }
    return [];
}

function refreshAsyn(jobs) {
    aj = JSON.stringify(jobs) 
    setCookie('_aj', aj)
}

function _listReloadTrigger() {
    $('.icon-refresh').trigger('click');
}

function _Jobstart(rtype, action) {
    _listReloadTrigger()
    $('.top-right').notify({
        message: { text: rtype + " " + action + ' Starting'},
        fadeOut: {
            delay: Math.floor(Math.random() * 500) + 2500
        },
        transition: 'fade',
        type: 'blackgloss'
    }).show();
}


function setAsyn(jobid, rtype, action)
{
    var jobs = getAsyn();
    jobs.push({'jobid': jobid, 'rtype': rtype, 'action': action});
    refreshAsyn(jobs);
    setTimeout("_Jobstart('"+rtype+"','"+action+"')", 3000)
    setTimeout("_listReloadTrigger()", 5000)
    setTimeout("_listReloadTrigger()", 9000)
    setTimeout("_listReloadTrigger()", 15000)
}

function _doAsyn(jobs, i) {

    var job = jobs[i];
    var data = {
        "action": "detail",
        "jobid": job['jobid']
    };

    var needRemove = false;
    $.get('/api/rctrl/v1/message/asyncjob/', data, function(response){
        var data = JSON.parse(response)
        if (data.status) {
            if (data.asyncjob.done) {       // job 结束
                if (data.asyncjob.result) {
                    if (data.asyncjob.result.code == 0) {
                        //  TODO:  更新状态栏
                        console.log(job['rtype'] + " " + job['action'] + ' success !')
                        $('.top-right').notify({
                            message: { text: job['rtype'] + " " + job['action'] + ' Success'},
                            fadeOut: {
                                delay: Math.floor(Math.random() * 500) + 2500
                            },
                            transition: 'fade',
                            type: 'blackgloss'
                        }).show();
                    }
                    else {
                        //  TODO:  更新状态栏
                        console.log(job['rtype'] + " " + job['action'] + ' error !')
                        $('.top-right').notify({
                            message: { text: job['rtype'] + " " + job['action'] + ' Error'},
                            fadeOut: {
                                delay: Math.floor(Math.random() * 500) + 2500
                            },
                            transition: 'fade',
                            type: 'danger'
                        }).show();
                    }
                    needRemove = true
                }
            }
        }
        else
            needRemove = true;

        if(needRemove) {
            jobs.splice(i, 1)
            refreshAsyn(jobs);
            $('.icon-refresh').trigger('click');
        }
    });
}

function asyn() {
    var jobs = getAsyn();

    for (var i=0; i<jobs.length; i++) {
        _doAsyn(jobs, i)
    }
}



setInterval("asyn()",2000);
