var board,planchette;
var boardY,boardY;
var nextX,nextY;
var saveX,saveY;
var posX,posY;
var offsetX=70;
var offsetY=70;
var inReply=false;
var inContact=false;
var answer;
var nextChar;
var step=5;
var timeout=50;
var pause=2000;
var angle;
var timerID;
var chars="abcdefghijklmnopqrstuvwxyz1234567890!~^_ ";
posX=new Array(105,134,171,204,240,270,305,340,372,389,420,452,484,
114,151,182,217,249,274,300,331,365,399,436,466,492,
154,184,217,251,281,314,346,378,410,441,
137,465,300,300,300);
posY=new Array(182,161,145,134,126,123,124,125,130,140,146,155,175,
223,202,190,181,174,171,169,172,174,185,199,211,231,
280,280,280,280,280,280,280,280,280,280,
054,054,350,140,215);
window.onload=init;
function init(){
var i;
board=document.getElementById("board");
boardX=getPageOffsetLeft(board);
boardY=getPageOffsetTop(board);
planchette=document.getElementById("planchette");
i=chars.indexOf("_");
moveElTo(planchette,boardX+posX[i]-offsetX,boardY+posY[i]-offsetY);
planchette.style.visibility="visible";
}
function handsOn(){
inContact=true;
}
function handsOff(){
inContact=false;
if(inReply){
saveX=getElLeft(planchette);
saveY=getElTop(planchette);
shakePlanchette();
}
}
function startReply(){
answer +="_";
if(!inContact){
saveX=getElLeft(planchette);
saveY=getElTop(planchette);
shakePlanchette();
}
if(getNext()){
inReply=true;
movePlanchette();
}
else
inReply=false;
}
function endReply(){
nextChar="";
inReply=false;
inContact=false;
}
function getNext(){
var i;
if(answer=="")
return false;
nextChar=answer.substr(0,1)
answer=answer.substr(1);
for (i=0; i< chars.length; i++)
if(chars.charAt(i)==nextChar){
nextX=posX[i];
nextY=posY[i];
return true;
}
return false;
}
function movePlanchette(){
var i,dx,dy,last,theta;
if(!inReply)
return;
if(inContact){
dx=boardX+nextX-getElLeft(planchette)-offsetX;
dy=boardY+nextY-getElTop(planchette)-offsetY;
theta=Math.round(Math.atan2(-dy,dx) * 180 / Math.PI);
if(theta< 0)
theta +=360;
if(Math.abs(dx)< step && Math.abs(dy)< step){
moveElBy(planchette,dx,dy);
last=nextChar;
if(getNext()){
if(nextChar==last){
angle=0;
timerID=window.setTimeout('circlePlanchette()',pause);
}
else
timerID=window.setTimeout('movePlanchette()',pause);
return;
}
else{
endReply();
return;
}
}
else if(theta>23 && theta<=68)
moveElBy(planchette,step,-step);
else if(theta>68 && theta<=113)
moveElBy(planchette,0,-step);
else if(theta>113 && theta<=158)
moveElBy(planchette,-step,-step);
else if(theta>158 && theta<=203)
moveElBy(planchette,-step,0);
else if(theta>203 && theta<=248)
moveElBy(planchette,-step,step);
else if(theta>248 && theta<=293)
moveElBy(planchette,0,step);
else if(theta>293 && theta<=338)
moveElBy(planchette,step,step);
else
moveElBy(planchette,step,0);
}
timerID=window.setTimeout('movePlanchette()',timeout);
}
function circlePlanchette(){
var x,y;
if(!inContact){
timerID=window.setTimeout('circlePlanchette()',timeout);
return;
}
x=getElLeft(planchette)+step * Math.cos(angle);
y=getElTop(planchette)-step * Math.sin(angle);
moveElTo(planchette,x,y);
angle +=Math.PI / 10;
if(angle< 2 * Math.PI)
timerID=window.setTimeout('circlePlanchette()',timeout);
else
movePlanchette();
}
function shakePlanchette(){
var dx,dy,x,y;
if(inContact)
return;
dx=Math.floor(Math.random() * 5)-2;
dy=Math.floor(Math.random() * 5)-2;
x=saveX+dx;
y=saveY+dy;
moveElTo(planchette,x,y);
timerID=window.setTimeout('shakePlanchette()',timeout);
}
var none=new Array("pregunta","preguntame","aqui estoy","no temas","habla",
"hablame","escucho","espero","que quieres");
var days=new Array("domingo","lunes","martes","miercoles","jueves",
"viernes","sabado","ni puta idea","quien sabe","quizas","es posible","no lo creo","lo sabes tu");
var months=new Array("enero","febrero","marzo","abril","mayo","junio",
"julio","agosto","septiembre","octubre","noviembre",
"diciembre","ni puta idea","quien sabe","quizas","es posible","no lo creo","lo sabes tu","yo que se");
var seasons=new Array("primavera","verano","otono","invierno","ni puta idea","quien sabe","quizas","es posible","no lo creo","lo sabes tu");
var times=new Array("nunca","pronto","muy pronto","manana","proxima semana","ni puta idea",
"ahora no","mas tarde","en su hora","no listo","quien sabe","quizas","es posible","no lo creo","lo sabes tu","yo que se");
var people=new Array("un amigo","un extrano","tu mismo","nadie",
"un cercano","quien sabe","quizas","es posible","no lo creo","lo sabes tu","yo que se");
var places=new Array("lejos","cerca","muy cerca","un lugar nuevo","ni puta idea",
"aqui no","quien sabe","quizas","es posible","no lo creo","lo sabes tu","yo que se");
var other=new Array("no puedo decir","no lo se","quizas","pregunta mas tarde","ni puta idea",
"no entiendo","no he visto","quizas despues","oscuro",
"nublado","no es claro","confuso","quien sabe","quizas","es posible","no lo creo","lo sabes tu","yo que se");
var names=new Array("no puedo decir","no lo se","alberto","jose","ni puta idea",
"maria","roberto","julio","mario",
"antonio","marco","lina","claudia","lo he olvidado");
var nosi=new Array("nosi");
var fumas=new Array("camel","marihuana","no fumo","fumo buena mierda","tengo un cancer");
var drogas=new Array("me pico la vena","fumo canutos","me meto coca","todo tipo de drogas","pastis");
var morir=new Array("de cancer","paro cardiaco","de sobredosis","coma etilico","te suicidaras","sobredosis de cocaina");

function consult(){
var question,words,r;
var i,today;
if(timerID)
clearTimeout(timerID);
answer="";
inContact=false;
question=document.forms[0].elements[0].value;
question=clean(question);
words=question.split(" ");
r=Math.random();
if(question==""){
answer=pickFromList(none);
startReply();
return false;
}
if(words.length>=3)
for (i=0; i< words.length-3; i++)
if(words[i+1]=="o"){
if(r< .5)
answer=words[i];
else
answer=words[i+2];
startReply();
return false;
}
if(r< .1)
answer=pickFromList(other);
else if(inList(words,"dia")||inList(words,"dias")){
if(r< .7)
answer=pickFromList(days);
else
answer=pickFromList(times);
}
else if(inList(words,"mes")||inList(words,"meses")){
if(r< .7)
answer=pickFromList(months);
else
answer=pickFromList(times);
}
else if(inList(words,"hora")||inList(words,"horas"))
answer=Math.floor(Math.random() * 12)+1;
else if(inList(words,"cuando")){
if(r< .3)
answer=pickFromList(days);
if(r< .4)
answer=pickFromList(months);
if(r< .5)
answer=pickFromList(seasons);
else
answer=pickFromList(times);
}
else if(inList(words,"cuanta") || inList(words,"cuanto") || inList(words,"cuantos") || inList(words,"cuantas")){
answer=Math.round(Math.random() * 10)+1;
if(answer !=10 && r< .5)
answer +=Math.round(Math.random() * 10);
}
else if(inList(words,"quien"))
answer=pickFromList(people);
else if(inList(words,"fumas") || inList(words,"fuma"))
answer=pickFromList(fumas);
else if(inList(words,"drogas") || inList(words,"droga") || inList(words,"estupefaciente"))
answer=pickFromList(drogas);
else if(inList(words,"sino"))
answer=pickFromList(nosi);
else if(inList(words,"morir") || inList(words,"morira"))
answer=pickFromList(morir);
else if(inList(words,"llamas") || inList(words,"llama"))
answer=pickFromList(names);
else if(inList(words,"donde"))
answer=pickFromList(places);
else if(inList(words,"que"))
answer=pickFromList(other);
else if(inList(words,"soy") ||
inList(words,"es") ||
inList(words,"eres") ||
inList(words,"son") ||
inList(words,"hacen") ||
inList(words,"puedo") ||
inList(words,"va a") ||
inList(words,"voy a") ||
inList(words,"sere") ||
inList(words,"tendre") ||
inList(words,"puede") ||
inList(words,"podria") ||
inList(words,"estan") ||
inList(words,"deberia") ||
inList(words,"deberian") ||
inList(words,"sabes")){
if(r< .5)
answer="!";
else
answer="~";
}
if(answer=="")
answer=pickFromList(other);
startReply();
}
function clean(s){
var i,c,t;
var letters="abcdefghijklmnopqrstuvwxyz";
t=s.toLowerCase();
s="";
for (i=0; i< t.length; i++){
if(t.substr(i,2)=="'s"){
s +=" is";
i++;
}
else if(t.substr(i,2)=="'t"){
s +=" not";
i++;
}
else if(t.substr(i,3)=="'ll"){
s +=" will";
i +=2;
}
else
s +=t.substr(i,1);
}
t="";
for (i=0; i< s.length; i++){
c=s.substr(i,1);
if(letters.indexOf(c)>=0)
t +=c;
else
t +=" ";
}
return t;
}
function inList(list,word){
var i;
for (i=0; i< list.length; i++)
if(list[i]==word)
return true;
return false;
}
function pickFromList(list){
var r;
r=Math.floor(Math.random() * list.length);
return list[r];
}
function getPageOffsetLeft(el){
return el.offsetLeft+(el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
}
function getPageOffsetTop(el){
return el.offsetTop+(el.offsetParent ? getPageOffsetTop(el.offsetParent) : 0);
}
function getElLeft(el){
return parseInt(el.style.left,10);
}
function getElTop(el){
return parseInt(el.style.top,10);
}
function moveElTo(el,x,y){
el.style.left=x+"px";
el.style.top=y+"px";
}
function moveElBy(el,dx,dy){
el.style.left=(getElLeft(el)+dx)+"px";
el.style.top=(getElTop(el)+dy)+"px";
}
