private_area_trial = false;

$(document).ready(function() {
    // $('a.a_check').click(check_do);


    $('.proxytbl tr td').bind('click', function(){
        if ( this.className != 't_check' ) {
            var ip = $(this).parent().find('.t_ip').html();
//            alert( ip );
        }
    });

    $("a.ip_quick").bind("click", function( event ) {
        var ip = $(this).html();

        $("#ip_container").html( '<img src="/images/checking-'
                                 + ( document.domain == 'sockslist.net'
                                     ? 'sockslist'
                                     : 'proxyhttp' )
                                 + '2.gif" width="93px" height="14px" alt="get'
                                 + 'IP info" class="proxy_checking"/>');
        var x = event.pageX;
        var y = event.pageY;
        if (! private_area_trial ) {
            $.ajax({
                url: '/privatearea/ip_quick/' + ip,
                cache: false,
                dataType: 'text',
                success: function (data, textStatus) {
                    $("#ip_container").html( data );
                    disposite_upper("#ip_container", x, y + 10).fadeTo(300, 0.95);
                },
                error:function (xhr, ajaxOptions, thrownError){
                    $("#ip_container").html( 'Error during request' );
                    disposite_upper("#ip_container", x, y + 10).fadeTo(300, 0.95);
                }
            });
        } else {
            $("#ip_container").html( '<strong>IP info (region, city, timezone,..)</strong><br /> is not available in trial mode' );
        }

        disposite_upper("#ip_container", x, y).fadeTo(300, 0.95);
        return false;
    });

    $("#ip_container").bind("click", function( event ) {
        $(this).css({ display:"none" });
    });

});

function disposite_upper( element, x, y ) {
//    var left = x - $(element).width()/2;
//    var top = y - $(element).height() - 25;
    var left = x;
    var top = y;
    return $(element).css({
           display:"block",
           left: left + "px",
           top: top + "px"}
    );
}

function get_work_proxy(x, y) {
    var proxy_work = '';
    $(".proxy_data").each(function() {
        if ( /works/.test( $(this).find('.proxy_result').html() ) ) {
            proxy_work = proxy_work + $(this).find('.proxy_ip_port').html() + "\n";
        }
    });

    $("#work_proxy").html(
        '<textarea name="i" cols="40" rows="16">' + proxy_work + '</textarea>'
    );

    $("#work_proxy_container").css({display:"block",
                                    left:x + -150 + "px",
                                    top:y + -150 + "px"}).fadeTo(300, 0.95);
    return false;
};

function hide_bad_proxy() {
    $(".proxy_data").each(function() {
        if ( /(NOT|Error)/.test( $(this).find('.proxy_result').html() ) ) {
            $(this).css({display:"none"});
        }
    });
    $("a.hide_bad_proxy").css({display:"none"});
    $("a.show_bad_proxy").css({display:""});
    return false;
};

function show_bad_proxy() {
    $(".proxy_data").each(function() {
        $(this).css({display:""});
    });
    $("a.show_bad_proxy").css({display:"none"});
    $("a.hide_bad_proxy").css({display:""});
    return false;
};

function get_type_http( r ) {
    var msg = '<span class="does_not_work">DOES NOT WORK</span>';
    if ( r > 0 ) {
        msg = '<span class="works">';
        if (r & 1 ) {
            msg += 'Regular ';
        }
        else if (r & 8) {
            msg += 'Anonymous ';
        }
        else if (r & 16) {
            msg += 'High anonymous ';
        }
        msg += 'proxy ';
        if (r & 4) {
            msg += 'with HTTPS support ';
        }
        msg += '</span>';
    }
    return msg;
}

function get_type_socks( r ) {
    var msg = '<span class="does_not_work">DOES NOT WORK</span>';
    if ( r > 0 ) {
        msg = '<span class="works">     Socks ver. ';
        if ( r & 64 ) {
            if ( r & 128 ) {
                msg += '4/5';
            }
            else {
                msg += '4';
            }
        }
        else if ( r & 128 ) {
            msg += '5';
        }
        msg += '</span>';
    }
    return msg;
}

function request_check_next( ips, type ) {
    var ip = ips.shift();
    if ( ip ) {
        $(".proxy_checking:first").show();
        request_check( ip, type, ips );
    }
}

function request_check( ip_port, type, ips ) {
    $.ajax({
        url: 'request_check?i='+ip_port,
        cache: false,
        dataType: 'text',
        success: function (data, textStatus) {
            request_checking_result( ip_port, type, ips );
        },
        error:function (xhr, ajaxOptions, thrownError){
           $(".proxy_checking:first").before('Error during request: '+xhr.statusText).remove();
            request_check_next( ips, type );
        }
    });
}

function request_checking_result( ip_port, type, ips ) {
    $.ajax({
        url: 'get_check_result?i='+ip_port,
        cache: false,
        dataType: 'text',
        success: function (data, textStatus) {
            var r = data.substring(2);
            //alert(r);
            if (r == -1) {
                setTimeout(
                    function() {
                        request_checking_result( ip_port, type, ips );
                    }
                    , 3000
                );
            }
            else if (r == -2) {
                $(".proxy_checking:first").before('Error during request').remove();
                request_check_next( ips, type );
            }
            else {
                var msg;
                if (type == 1) {
                    msg = get_type_http( r );
                }
                else if (type == 2) {
                    msg = get_type_socks( r );
                }
                $(".proxy_checking:first").before( msg ).remove();
                request_check_next( ips, type );
            }
        },
        error:function (xhr, ajaxOptions, thrownError){
            $(".proxy_checking:first").before('Error during request: '+xhr.statusText).remove();
            request_check_next( ips, type );
        }
    });
}

function nospy (str, fakechar) {
    var re = new RegExp(fakechar ,"g");
    return str.replace(re, "");
}

function show_stat (event) {
        var x = event.pageX;
        var y = event.pageY;

        $("#stat_container").bind("click", function( event ) {
            $(this).css({ display:"none" });
        });

        $("#stat_container").html('<img src="/images/preloader-11x11.gif" />');

        $.ajax({
            url: '/country/stat',
            cache: false,
            dataType: 'text',
            success: function (data, textStatus) {
                $("#stat_container").html( data );
                disposite_upper("#stat_container", x + 15, y).fadeTo(300, 0.95);
            },
            error:function (xhr, ajaxOptions, thrownError){
                $("#stat_container").html( 'Error during request' );
                disposite_upper("#stat_container", x + 15, y).fadeTo(300, 0.95);
            }
        });

        disposite_upper("#stat_container", x + 15, y).fadeTo(300, 0.95);
        return false;
}

