var defaultFieldText = typeIdHere;
var processing = false;
var process = 0;
var checkedId = "";
var aboutus=false;
function getRandom()
{
    return Math.floor(Math.random()*1000000000);
}
function autoStart()
{
    openResult();
}
function start()
{
    $(".idfield").bind('focus',checkInputField);
    $(".idfield").bind('blur',resetInputField);
    enableFields();
    loadHistory();
    if(!(typeof(startid)=='undefined'||startid===null))
    {
        $(".idfield").attr('value',startid);
        setTimeout('autoStart()',500);
    }
    //beginChecksCounter();
}
function beginChecksCounter()
{
 $.ajax({
  url: "/counter.php?t="+getRandom(),
  success: function(text){
   $("#checks").html(text);
   setTimeout("beginChecksCounter()",10000);
  },
  error: function(){
   setTimeout("beginChecksCounter()",10000);
  }
 });
}
function checkInputField()
{
    if(getId()==defaultFieldText)
        clearFieldText();
}
function getId()
{
    $id=$(".idfield").attr('value');
    $newid="";
    $mailext="";
    if($id!=defaultFieldText&&$id!="")
    {
        $id=$id.toLowerCase();
        $id=$id.replace(/ /,"");
        $id=$id.replace(/\t/,"");
        $id=$id.replace(/\n/,"");
        for($i=0;$i<$id.length;$i++)
        {
            if(!isArond($id.charAt($i)))
                $newid+=$id.charAt($i);
            else
            {
                if($id.substring($id.length-9)=="ymail.com")
                    $mailext = "ymail.com";
                if($id.substring($id.length-14)=="rocketmail.com")
                    $mailext = "rocketmail.com";
                break;
            }
        }
        $id=$newid + (($mailext=="")?"":"@"+$mailext);
        $(".idfield").attr('value',$id);
        return $id;
    } else
        return $id;
}
function clearFieldText()
{
    $(".idfield").attr('value','');
}
function resetInputField()
{
    if(getId()=='')
        resetFieldText();
}
function resetFieldText()
{
    $(".idfield").attr('value',defaultFieldText);
}
function enableFields()
{
    $(".form").unbind('submit',empty);
    $(".form").bind('submit',beginCheck);
    $(".bsend").unbind('click',empty);
    $(".bsend").bind('click',beginCheck);
    $(".idfield").focus();
}
function empty()
{
    return false;
}

function loadHistory()
{
    var hist = getHistory();
    refreshHistory(hist);
}

function getHistory()
{
    var hist = $.cookie('hist');
    if (null === hist) {
        hist = [];
    } else {
        hist = $.evalJSON(hist);
    }

    return hist;
}

function saveHistory(hist)
{
     $.cookie(
        'hist', $.toJSON(hist),
        {expires: 356, path: '/', domain: sName, secure: false}
    );
}

function addToCookie(id)
{
    var hist = getHistory(),
        found = false,
        idx;
    for (idx in hist) {
        if (hist[idx].name === id) {
            found = true;
            hist[idx].count++;
        }
    }

    if (false === found) {
        hist.push({
            name: id,
            count: 1
        });
    }

    orderHistory(hist);
    saveHistory(hist);
    refreshHistory(hist);
}
function orderHistory(hist)
{
    var i,
        histLen = hist.length - 1,
        l, r,
        aux;
    for (i = 0; i < histLen; i++) {
        l = i;
        r = i + 1;
        if (hist[l].count < hist[r].count) {
            aux = hist[l];
            hist[l] = hist[r];
            hist[r] = aux;
        }
    }
}
function addToHistory(idx, id)
{
    var par;
    if (idx % 2 === 0) {
        par = 'even';
    } else {
        par = 'odd';
    }

    $('#history').append(
        "<p class='hist " + par + "' id='phis" + idx +
        "'><img src='" + mediaPath +"/imgs/remove-button2.gif' alt='" + idx +
        "' id='remhis" + idx + "'/><span id='his" + idx + "' >" +
        id.name +
        "</span><div class='clearing'></div></p>"
    );
    $("#his"+idx).bind('click',hisCheck);
    $("#remhis"+idx).bind('click',remHis);
}

function remHis()
{
    var id = $(this).attr('alt'),
        hist = getHistory();
    hist.splice(id, 1);
    saveHistory(hist);
    refreshHistory(hist);
}
function refreshHistory(hist)
{
    $('.hist').remove();
    var i,
        histLen = hist.length;
    for (i = 0; i < histLen; i++) {
        addToHistory(i, hist[i]);
    }
}
function hisCheck()
{
    $(".idfield").attr('value',$(this).html());
    beginCheck();
}
function beginCheck()
{
    openResult();
    return false;
    if(getId()==""||getId()==defaultFieldText)
        return false;
    $id=getId();
    if(!isValidId($id))
        return false;
    window.location = '/?yid='+$id;
    return false;
}
function openResult()
{
    if(getId()==""||getId()==defaultFieldText)
        return false;
    $id=getId();
    if(!isValidId($id))
        return false;
    checkedId=$id;
    disableFields();
    $("#resultcontainer_ad").slideUp('fast');
    $(".result").show();
    $("#resultcontainer").slideDown('slow');
    $("#loadingstatus").fadeIn('fast');
    $("#theavatar").fadeOut('fast');
    $("#avatar").attr("src", mediaPath + "/imgs/pixel.gif");
    $("#statusresult").fadeOut('fast');
    $(".links_social").fadeOut('fast');
    $(".links_ym_profile").fadeOut('fast');
    $("#social_detector").fadeOut('fast');
    $("#busy").fadeOut('fast');
    process++;
    addToCookie(getId());
    $.ajax({type:"GET",url:"getstatus.php?id="+encode(getId())+"&t="+Math.random(),success:procesResult,error:failedRequest});
    setTimeout('timeout('+process+')',30000);
    return false;
}
function isValidId($id)
{
    $newid="";
    $mailext="";
    for($i=0;$i<$id.length;$i++)
    {
        if(!isArond($id.charAt($i)))
            $newid+=$id.charAt($i);
        else
        {
            if($id.substring($id.length-9)=="ymail.com")
                $mailext = "ymail.com";
            if($id.substring($id.length-14)=="rocketmail.com")
                $mailext = "rocketmail.com";
            break;
        }
    }
    $id=$newid;
    if(!($id.length>=4&&$id.length<=32))
        return false;
    $thedot=false;
    if(!isLetter($id.charAt(0)))
        return false;
    for($i=1;$i<$id.length;$i++)
    {
        if(isDot($id.charAt($i))&&$thedot==true)
            return false;
        if(isDot($id.charAt($i))&&$i!=$id.length-1&&!isUnderscore($id.charAt($i-1))&&!isUnderscore($id.charAt($i+1)))
            if($thedot==false)
                $thedot=true;
            else
                return false;
            if(!(isLetter($id.charAt($i))||isNumber($id.charAt($i))||isUnderscore($id.charAt($i))||$thedot))
                return false;
    }
    if(isUnderscore($id.charAt($id.length-1)))
        return false;
    return true;
}
function isLetter($a)
{
    $inta=$a.charCodeAt('0');
    if($inta>=97&&$inta<=122)
        return true;
    return false;
}
function isNumber($a)
{
    $inta=$a.charCodeAt('0');
    if($inta>=48&&$inta<=57)
        return true;
    return false;
}
function isDot($a)
{
    if($a.charCodeAt('0')==46)
        return true;
    return false;
}
function isArond($a)
{
    if($a.charCodeAt('0')==64)
        return true;
    return false;
}
function isUnderscore($a)
{
    if($a.charCodeAt('0')==95)
        return true;
    return false;
}
function disableFields()
{
    $(".form").unbind('submit',beginCheck);
    $(".form").bind('submit',empty);
    $(".bsend").unbind('click',beginCheck);
    $(".bsend").bind('click',empty);
}
function encode($string)
{
    var one=new Array([1,1,1],[1,0,1],[1,0,0],[0,0,1]);
    var zero=new Array([0,1,0],[1,1,0],[0,1,1],[0,1,0],[0,0,0]);
    var a=new Array();
    var b=new Array();
    var c=new Array();
    var i;
    for($i=0;$i<$string.length;$i++)
        a[$i]=$string.charAt($i);
    for($i=0;$i<a.length;$i++)
        a[$i]=a[$i].charCodeAt(0);
    for($i=0;$i<a.length;$i++)
    {
        b[$i]=new Array();
        $j=0;
        while(a[$i])
        {
            b[$i][$j++]=a[$i]%2;
            a[$i]=parseInt(a[$i]/2);
        }
        if($j<8)
            for($k=$j;$k<8;$k++)
                b[$i][$k]=0;
    }
    for($i=0;$i<b.length;$i++)
        for($j=0;$j<8;$j++)
            b[$i][$j]=(b[$i][$j]==0)?(zero[(parseInt(Math.random()*10))%5]):(one[(parseInt(Math.random()*10))%4]);
    var l=b.length;
    for($j=0;$j<8;$j++)
        for($k=0;$k<3;$k++)
            for($i=0;$i<l;$i++)
                c[$k*l+$i+$j*3*l]=b[$i][$j][$k];
    var fString="";
    for($i=0;$i<l*3;$i++)
    {
        var a=0;
        for($j=0;$j<8;$j++)
            a+=c[$j+$i*8]*exp(2,$j);
        fString+="|"+a;
    }
    return fString;
}
function procesResult(result)
{
    $showId=false;
    $showAvatar=false;
    $showLinks=false;
    $showBusy=false;
    process++;
    result=result.split("|");
    switch(result[0])
    {
        case"0":
            $showId=true;
            $("#status").html("invalid");
            break;
        case"1":
            $showId=true;
            $showAvatar=true;
            $showLinks=true;
            $("#status").html("online");
            break;
        case"2":
            $showId=true;
            $showAvatar=true;
            $showLinks=true;
            $("#status").html("offline");
            break;
        case"3":
            $showId=true;
            $showAvatar=true;
            $showLinks=true;
            $("#status").html("invisible");
            break;
        case"5":
            $showId=true;
            $showAvatar=true;
            $showLinks=true;
            $("#status").html("protected");
            break;
        case "6":
            window.location = basePath + "/unban.php";
            break;
        case "666":
            window.location = basePath + "/unban.php";
            break;
        default:
            $showBusy=true;
            break;
    }
    $("#loadingstatus").fadeOut('fast');
    if($showBusy)
        $("#busy").fadeIn('slow');
    else
        $("#busy").fadeOut('fast');
    if($showId)
    {
        $(".ymid").html(checkedId);
        $("#statusresult").fadeIn('slow');
        if (result[1] > 0) {
            var url = 'getimage.php?id=' + result[1] +'&s='+ result[0]
                + '&t=' + Math.random();
            $.ajax({type:"GET",url:url});
        }
    }else
        $("#statusresult").fadeOut('fast');
    if($showAvatar)
    {
        $("#avatar").attr('src',"http://img.msg.yahoo.com/avatar.php?format=jpg&yids="+getId());
        $("#theavatar").fadeIn('slow');
    }else
        $("#theavatar").fadeOut('fast');
    if($showLinks)
    {
        $(".hi5").attr("href", basePath + "/hi5detector.php?yid="+checkedId);
        $(".hi5").attr("title", findHi5Profile.replace('%%checkedId%%', checkedId));
        $(".facebook").attr("href", basePath + "/facebookdetector.php?yid="+checkedId);
        $(".facebook").attr("title", findFacebookProfile.replace('%%checkedId%%', checkedId));
        $(".addlink").attr("href","ymsgr:addfriend?"+checkedId);
        $(".addlink").attr("title", addToBuddyList.replace('%%checkedId%%', checkedId));
        $(".profilelink").attr("href","/profile/"+result[1]+'/'+checkedId+'.html');
        $(".avatarlink").attr("href","/download-avatar.php?id="+result[1]);
        $(".avatarlink").attr("title", downloadAvatar.replace('%%checkedId%%', checkedId));
        $(".sendlink").attr("href","ymsgr:sendim?"+checkedId+"&m=Find+out+who+is+invisible+on+Yahoo+Messenger+<b>+imvisible.info");
        $(".sendlink").attr("title", chatWith.replace('%%checkedId%%', checkedId));
        //$(".links").fadeIn('slow');
        $(".links_social").fadeIn('slow');
        $(".links_ym_profile").fadeIn('slow');
        $("#social_detector").fadeIn('slow');
    } else
        //$(".links").fadeOut('fast');
        {
            $(".links_social").fadeOut('fast');
            $(".links_ym_profile").fadeOut('fast');
            $("#social_detector").fadeOut('fast');
        }
    enableFields();
}
function failedRequest()
{
    process++;
    procesResult("4");
}
function timeout($processid)
{
    if($processid==process)
        procesResult("4");
}
function exp(base,expo)
{
    var result=1;
    while(expo)
    {
        result*=base;
        expo--;
    }
    return result;
}
function showaboutus()
{
    if(aboutus)
        hideaboutus();
    else
    {
        $("#aboutus").slideDown('slow');
        aboutus=true;
    }
}
function hideaboutus()
{
    $("#aboutus").slideUp('slow');
    aboutus=false;
}
$(document).ready(start);

