$(document).ready(function(){
    $(".surflink").click(function(e){
        var oLeft = 0, oTop = 0;
        element = this;
        if (element.className == 'surflink') {
            do {
                oLeft += element.offsetLeft;
                oTop  += element.offsetTop;
            } while (element = element.offsetParent);
            var sx = e.pageX - oLeft;
            var sy = e.pageY - oTop;
            var elid = $(this).attr('id');
            fixed(elid, sx, sy);
        }
    });
});

function startClock() {
    if (vtime == stattime) {
        console.log("ass"+vtime);
        document.getElementById('blocktimer').style.display = '';
        document.getElementById('blockwait').style.display = 'none';
        
    }
    if (vtime >= 0) {
        //console.log("ass2");
        document.forms['frm'].clock.value = vtime;
        vtime --;
        tm = setTimeout("startClock(0)", 1000);
        cptfix=1;
    } else {
        if (tm)
            clearTimeout(tm);
        nextstep(0, cnt);
        cptfix=1;
    }
}

//////////////
function checkPageFocus() {
    var info = document.getElementById("message");
    if ( document.hasFocus() ) {
        info.innerHTML = "Документ имеет фокус.";
    } else {
        info.innerHTML = "Документ не имеет фокус.";
    }
}

function vernum(vnum) {
    nextstep(vnum, cnt);
    return false;
}
function getHTTPRequest()
{
    var req = false;
    try {
        req = new XMLHttpRequest();
    } catch(err) {
        try {
            req = new ActiveXObject("MsXML2.XMLHTTP");
        } catch(err) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(err) {
                req = false;
            }
        }
    }
    return req;
}                        
function nextstep(num, cnt){
    var myReq = getHTTPRequest();
    var params = "num="+num+"&cnt="+cnt;
    function setstate(){
        if ((myReq.readyState == 4)&&(myReq.status == 200)) {
            var resvalue = myReq.responseText;
            if (resvalue != '') {
                if (resvalue.substr(0, 2) == 'OK') { 
                    vars = resvalue.split(";"); 
                    //console.log("->"+vars+"->"+vars[2]);
                    document.getElementById("blockverify").innerHTML = '<div class="blocksuccess">Спасибо за посещение!<br /><span>Плата за просмотр ('+vars[1]+' Монет.) зачислена</span></div>';
                    if ((vars[2] != '0')&&(vars[2].length > 1)) {
                        setTimeout("top.location = '"+vars[2]+"'", 500);
                    }else{
                        close();
                    }
                } else
                if (resvalue == '0')
                    //alert(resvalue);
                    document.getElementById("blockverify").innerHTML = '<div class="blockerror">Действие не произведено</div>';
                else
                    document.getElementById("blockverify").innerHTML = resvalue;
            }
        } else {
            document.getElementById("blockverify").innerHTML = "<span class='loading' title='Подождите пожалуйста...'></span>";
        }
    }
    myReq.open("POST", "/ajax.php?action=stepserf", true);
    myReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    myReq.setRequestHeader("Content-lenght", params.length);
    //myReq.setRequestHeader("Connection", "close");
    myReq.onreadystatechange = setstate;
    myReq.send(params);
    return false;
}
function goserf(obj){
    obj.parentNode.innerHTML = "<span style='margin-left: 52px;' class='textgreen'>Спасибо за визит</span>";
    return false;
}
function fixed(p1, p2, p3)
{
    var myReq = getHTTPRequest();
    var params = "p1="+p1+"&p2="+p2+"&p3="+p3;
    function setstate()
    {
        if ((myReq.readyState == 4)&&(myReq.status == 200)) {
            var resvalue = myReq.responseText;
            if (resvalue != '') {
                if (resvalue.length > 12) {
                    if (elem = document.getElementById(p1)) {
                        elem.style.backgroundImage = 'none';
                        elem.className = 'goadvsite';
                        elem.innerHTML = '<div><a style="margin-left: 36px; margin-top: -5px;" target="_blank" href="/'+resvalue+'" onclick="javascript:goserf(this);">Просмотреть сайт рекламодателя</a></div>';
                    }
                } else {
                    if (elem = document.getElementById(resvalue)) {
                        $(elem).fadeOut('low', function() {
                            elem.innerHTML = "<td colspan='3'></td>";
                        });
                    }
                }
            }
        }
    }
    myReq.open("POST", "/ajax.php?action=fixedserf", true);
    myReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    myReq.setRequestHeader("Content-lenght", params.length);
    //myReq.setRequestHeader("Connection", "close");
    myReq.onreadystatechange = setstate;
    myReq.send(params);
    return false;
}
function number_format(number, decimals, dec_point, thousands_sep) {
    var i, j, kw, kd, km;
    if (isNaN(decimals = Math.abs(decimals))) {
        decimals = 2;
    }
    if (dec_point == undefined) {
        dec_point = ",";
    }
    if (thousands_sep == undefined) {
        thousands_sep = ".";
    }
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
    if ((j = i.length) > 3) {
        j = j % 3;
    } else {
        j = 0;
    }
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
    return km + kw + kd;
}
 function showhide(bid) {
    if (document.getElementById('cattitle'+bid).className == 'cattitle-open')
        document.getElementById('cattitle'+bid).className = 'cattitle-close'; else
        document.getElementById('cattitle'+bid).className = 'cattitle-open';
    $('#catblock'+bid).slideToggle('fast');
}