var defaultFieldText="Introdu Id-ul aici!";
var procesing=false;
var process=1;
var checkedId="";
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);
	}
}
function loadHistory()
{
	for($i=0;$i<=50;$i++)
	{
		if($.cookie("history["+$i+"]")!=null)
			addToHistory($i);
		else
			break;
	}
}
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 enableFields()
{
	$(".form").unbind('submit',empty);
	$(".form").bind('submit',beginCheck);
	$(".bsend").unbind('click',empty);
	$(".bsend").bind('click',beginCheck);
	$(".idfield").focus();
}
function disableFields()
{
	$(".form").unbind('submit',beginCheck);
	$(".form").bind('submit',empty);
	$(".bsend").unbind('click',beginCheck);
	$(".bsend").bind('click',empty);
}
function empty(){return false;}
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 isValidId($id)
{
	$newid="";
	$mailext="";
	for($i=0;$i<$id.length;$i++)
	{
		if(!isArond($id.charAt($i)))
			$newid+=$id.charAt($i);
		else
		{
			if($id.substr(-9,9)=="ymail.com")
				$mailext = "ymail.com";
			if($id.substr(-14,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 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 exp(base,expo){var result=1;while(expo){result*=base;expo--;}return result;}
function openResult()
{
	if(getId()==""||getId()==defaultFieldText)
		return false;
	$id=getId();
	if(!isValidId($id))
		return false;
	checkedId=$id;
	disableFields();
	$(".result").slideDown(1000);
	$(".toptext-new").animate({top:"265px",height:"0px"},1000,function(){$(".toptextposition").hide();});
	$(".checker-holder").animate({marginBottom:"5px"},1000);
	$(".loading").fadeIn('fast');
	$(".avatar").fadeOut('fast');
	$(".htext").fadeOut('fast');
	$(".links").fadeOut('fast');
	$(".busy").fadeOut('fast');
	process++;
	addToCookie(getId());
	$.ajax({type:"GET",url:"getstatus2.php?id="+encode(getId())+"&t="+getRandom(),success:procesResult,error:failedRequest});
	setTimeout('timeout('+process+')',14000);
	return false;
}
function addToCookie($id)
{
	$found = false;
	for($i=0;$i<=50;$i++)
	{
		if($.cookie("history["+$i+"]")!=null)
			if($.cookie("history["+$i+"]") == $id)
			{
				$found = true;
				break;
			} else {}
		else
			break;
	}
	if(!$found)
	{
		$.cookie("history["+$i+"]",$id, { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
		$.cookie("cout["+$i+"]",'1', { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
		addToHistory($i);
	} else {
		$inc = ($.cookie("cout["+$i+"]")/1)+1;
		$.cookie("cout["+$i+"]",$inc, { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
		checkCounts();
	}
}
function checkCounts()
{
	for($i=50;$i>0;$i--)
		if($.cookie("cout["+$i+"]")!=null)
		{
			$j = $i-1;
			if(parseInt($.cookie("cout["+$i+"]"))>parseInt($.cookie("cout["+$j+"]")))
				hisReverse($i,$j);
		}
}
function hisReverse($i,$j)
{
	$.cookie("history[tmp]", $.cookie("history["+$i+"]"), { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	$.cookie("history["+$i+"]", $.cookie("history["+$j+"]"),  { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	$.cookie("history["+$j+"]", $.cookie("history[tmp]"), { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	$.cookie("history[tmp]", null, { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	
	$.cookie("cout[tmp]", $.cookie("cout["+$i+"]"), { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	$.cookie("cout["+$i+"]", $.cookie("cout["+$j+"]"),  { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	$.cookie("cout["+$j+"]", $.cookie("cout[tmp]"), { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	$.cookie("cout[tmp]", null, { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	
	updateHistory($i);
	updateHistory($j);
}
function addToHistory($i)
{
	$par = ($i%2)?"odd":"even";
	$("#history").append("<p class='hist "+$par+"' id='phis"+$i+"'><img src='/imgs2/remove-button2.gif' alt='"+$i+"' id='remhis"+$i+"'/><span id='his" + $i + "' >"+$.cookie("history["+$i+"]")+"</span><div class='clearing'></div></p>");
	$("#his"+$i).bind('click',hisCheck);
	$("#remhis"+$i).bind('click',remHis);
}
function remHis(par)
{
	$id = $(this).attr('alt');
	$.cookie('history['+$id+']',null);
	for($i=$id;$i<=50;$i++)
	{
		$j = ($i/1)+1;
		if($.cookie('history['+$j+']')!=null)
		{
			$.cookie('history['+$i+']',$.cookie('history['+$j+']'),{ expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
			$.cookie('cout['+$i+']',$.cookie('cout['+$j+']'),{ expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
			updateHistory($i);
		} else
			break;
	}
	$.cookie("history["+$i+"]", null, { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	$.cookie("cout["+$i+"]", null, { expires: 356, path: '/', domain: 'imvisible.ro', secure: false });
	removeHistory($i);
}
function updateHistory($i)
{
	$("#his"+$i).html($.cookie("history["+$i+"]"));
}
function removeHistory($id)
{
	$("#phis"+$id).remove();
}
function hisCheck(par)
{
	$("#yahooid").attr('value',$(this).html());
	beginCheck();
}
function failedRequest()
{
	process++;
	procesResult("4");
}
function procesResult(result)
{
	$showId=false;
	$showAvatar=false;
	$showLinks=false;
	$showBusy=false;
	process++;
	result=result.split("|");
	switch(result[0])
	{
		case"6":	$(".text").html("<b>Ati atins limita de verificari pe astazi</b>");
				window.location = "/";
					break;
		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("invizibil");
					break;
		case"5":	$showId=true;
					$showAvatar=false;
					$showLinks=true;
					$(".status").html("protejat");
					break;
		default:	$showBusy=true;
					break;
	}
	$(".loading").fadeOut('fast');
	if($showBusy)
		$(".busy").fadeIn('slow');
	else
		$(".busy").fadeOut('fast');
	if($showId)
	{
		$(".id").html(checkedId);
		$(".htext").fadeIn('slow');
	} else
		$(".htext").fadeOut('fast');
	if($showAvatar)
	{
		$(".avatar").attr('src',"/imgs2/pixel.gif");
		$(".avatar").fadeIn('slow');
		setTimeout('procesImage()',300);	
		if($('#acord').attr('checked') == true)
			$acord = "1";
		else
			$acord = "0";
		$.ajax({type:"GET",url:"getimage.php?id="+result[1]+"&jProcess="+process+"&s="+result[0]+"&acord="+$acord+"&t="+getRandom()});
		// avatar code here
	} else
		$(".avatar").fadeOut('fast');
	if($showLinks)
	{
		$(".add").attr("href","ymsgr:addfriend?"+checkedId);
		$(".profile").attr("href","/profile/"+result[1]);
		$(".savatar").attr("href","/salveaza-avatar/"+result[1]);
		$(".sendim").attr("href","ymsgr:sendim?"+checkedId+"&m=Afla+cine+e+invizibil+pe+Yahoo+messenger:+<b>+www.imvisible.ro+");
		$(".links").fadeIn('slow');
	} else
		$(".links").fadeOut('fast');
	enableFields();
}
function getRandom()
{
	return Math.floor(Math.random()*1000000000);
}
function procesImage($res)
{
	$(".avatar").attr('src',"http://img.msg.yahoo.com/avatar.php?format=png&yids="+getId()+"&rand="+getRandom());
}
function failedImage($res)
{
	$(".avatar").attr('src',"/imgs2/img.png");
}
function timeout($processid)
{
	if($processid==process)
		procesResult("4");
}
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.substr(-9,9)=="ymail.com")
					$mailext = "ymail.com";
				if($id.substr(-14,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 resetFieldText()
{
	$(".idfield").attr('value',defaultFieldText);
}
function checkInputField()
{
	if(getId()==defaultFieldText)
		clearFieldText();
}
function resetInputField()
{
	if(getId()=='')
		resetFieldText();
}
$(document).ready(start);
