
/* {{{ show msg
 */
function showmsg(msg , eleid){
    $(eleid).focus();
    $('#error_msg').html(msg).fadeIn();
    setTimeout(function(){
        $('#error_msg').fadeOut().html('');
    }, 1000);
}
/* }}} */
/* {{{ show msg
 */
function showmsg2(msg , eleid){
    alert(msg);
    $(eleid).focus();
}
/* }}} */
function next(sec){
    if(sec >= 1){
        $('#ga-msg').html('采集完成! ' + sec + ' 秒后 , 程序将继续采集 ...');
        setTimeout( function(){
            next(sec - 1);
        } , 1000);
    }else{
        location.href = autourl;
    }
}
function ga(url , queue){
    if(queue.length > 0){
        var cur = queue.shift();
        var u = url , q = queue;
        if(!ajaxing){
            $.ajax({
                url          : url,
                type         : "POST",
                data         : "url=" + cur.url,
                dataType     : 'json',
                beforeSend   : function(){
                    ajaxing = true;
                    $('#status' + cur.id).html('<img src="' + loading + '" /> <span style="color:#500">拉取中 ...</span>');
                },
                success      : function(d){
                    ajaxing = false;
                    if(cur.id > 3){
                        $('#link-ol').scrollTop($('#link-ol').scrollTop() + height);
                    }
                    if(d.code != '0'){
                        $('#status' + cur.id).html('<span style="color:red">' + d.msg + '</span>');
                        $('#fnum').html(parseInt($('#fnum').html()) + 1);
                    }else{
                        $('#status' + cur.id).html('<span style="color:green">' + d.msg + ' ...</span>');
                        $('#snum').html(parseInt($('#snum').html()) + 1);
                    }
                    setTimeout( function(){
                        ga(u , q);
                    } , 500 );
                },
                error        : function(){
                    ajaxing = false;
                    $('#status' + cur.id).html('<span style="color:red">失败了 ...</span>');
                    if(cur.id > 3){
                        $('#link-ol').scrollTop($('#link-ol').scrollTop() + height);
                    }
                    $('#fnum').html(parseInt($('#fnum').html()) + 1);
                    setTimeout( function(){
                        ga(u , q);
                    } , 100);
                }
            });
        }
    }else{
        next(5);
    }
}


function gaurl(url , queue){
    if(queue.length > 0){
        var cur = queue.shift();
        var u = url , q = queue;
        if(!ajaxing){
            $.ajax({
                url          : url,
                type         : "POST",
                data         : cur,
                dataType     : 'json',
                beforeSend   : function(){
                    ajaxing = true;
                    $('#status' + cur.eid).html('<img src="' + loading + '" /> <span style="color:#500">拉取中 ...</span>');
                },
                success      : function(d){
                    ajaxing = false;
                    if(cur.eid > 3){
                        $('#link-ol').scrollTop($('#link-ol').scrollTop() + height);
                    }
                    if(d.code != '0'){
                        $('#status' + cur.eid).html('<span style="color:red">' + d.msg + '</span>');
                        $('#fnum').html(parseInt($('#fnum').html()) + 1);
                    }else{
                        $('#status' + cur.eid).html('<span style="color:green">' + d.msg + ' ...</span>');
                        $('#snum').html(parseInt($('#snum').html()) + 1);
                    }
                    setTimeout( function(){
                        gaurl(u , q);
                    } , 500 );
                },
                error        : function(){
                    ajaxing = false;
                    $('#status' + cur.eid).html('<span style="color:red">失败了 ...</span>');
                    if(cur.eid > 3){
                        $('#link-ol').scrollTop($('#link-ol').scrollTop() + height);
                    }
                    $('#fnum').html(parseInt($('#fnum').html()) + 1);
                    setTimeout( function(){
                        gaurl(u , q);
                    } , 100);
                }
            });
        }
    }else{
        next(5);
    }
}
$(function(){
    $('#ga-field-target').on('change' , function(){
        if($(this).val() != ''){
            var v = $('#ga-field-target option:selected').text().split(" : ");
            $('#ga-field-name').val(v[0]);
        }
    });

    $('#gasbtn').bind('click' , function(){
        
        if($('#ga-name').val() == ''){
            showmsg2('请输入名称' , '#ga-name');
        }
        else if($('#ga-host').val() == ''){
            showmsg2('请输入域名 , 请以 / 结尾' , '#ga-name');
        }
        else if($('#ga-conf-url').val() == ''){
            showmsg2('请输入列表页地址' , '#ga-name');
        }
        else if($('#ga-conf-pregx').val() == ''){
            showmsg2('请输入分页地址表达式' , '#ga-conf-pregx');
        }
        else if($('#ga-conf-iregx').val() == ''){
            showmsg2('请输入内容地址表达式' , '#ga-conf-iregx');
        }
        else{
            $('#form1').submit();
        }
    });

    $('#ga-list-ol').on('click' , '.ga-ol-del' , function(){
        if(confirm('确定删除?')){
            $(this).parent().remove();
        }
    });

    $('#ga-new-btn').on('click' , function(){
        var ga_field_regx   = $('#ga-field-regx').val() ,
            ga_field_name   = $('#ga-field-name').val() ,
            ga_field_target = $('#ga-field-target').val();
        if(ga_field_name == ''){
            showmsg('请输入名称' , '#ga-field-name');
        }
        else if(ga_field_target == ''){
            showmsg('请选择本地字段' , '#ga-field-target');
        }
        else if(ga_field_regx == ''){
            showmsg('请输入表达式' , '#ga-field-regx');
        }
        else{
            var html = '<li class="' + ga_field_target + '" id="ol-' + ga_field_target + '">';
            html += '&nbsp;';
            html += '<a href="javascript:void(0);" class="ga-ol-del">[ 删除 ]</a>&nbsp;';
            html += ga_field_name + '#' + ga_field_target + '<br/>';
            html += '<input type="hidden" name="ga[conf][list][name][]" value="' + ga_field_name + '"/>';
            html += '<input type="hidden" name="ga[conf][list][target][]" value="' + ga_field_target + '"/>';
            html += '<textarea name="ga[conf][list][regx][]" class="input textarea" style="vertical-align:top;width:600px;height:80px;">'+ ga_field_regx +'</textarea>';
            html += '</li>';
            $('#ga-list-ol').find('li.none').remove();
            $('#ga-list-ol').find('li.' + ga_field_target).remove();
            $('#ga-list-ol').append(html);
        }
    });
});