var person;
var g_year;

function init()
{
    person = new Array();
    g_year = 2002;

    var content = document.getElementById("content1");
    var text = "";
    constructPersons();
    text += "<p><strong>The story so far</strong>: " + getPersonsInfo() + "</p>";

    content.innerHTML = text;

    var content = document.getElementById("content2");
    content.innerHTML = "";

    document.getElementById("dead").value = "";
    document.getElementById("married").value = "";
}

function showResults()
{
    var content = document.getElementById("content2");
    var text = "";
    text += "<p><strong>Recent events</strong>: " + getRelations("") + "</p>";
    text += "<p><strong>What happened then</strong>: " + getRelations("after") + "</p>";
    agePersons();
    g_year++;
    text += "<p><strong>Afterwards</strong>: A year passed and it's " + g_year + ". ";
    text += getPersonsInfo() + "</p>";
    text += "<p><strong>In the end</strong>: " + getRecapitulations() + "</p>";
    content.innerHTML = text;
}

function getRecapitulations()
{
    var text = "";
    var dead = countDead();
    if (dead > 0)
    {
        text += dead + " persons have died. ";
    }

    for (var i = 0; i < person.length; i++)
    {
        for (var j = 0; j < person.length; j++)
        {
            text += getRecapitulation(person[i], person[j]);
        }
    }

    if (text == "")
    {
        text = "Nothing happened.";
    }
    return text;
}

function countDead()
{
    var deads = 0;
    for (var i = 0; i < person.length; i++)
    {
        if (!person[i].alive)
        {
            deads++;
        }
    }
    return deads;
}

function getRecapitulation(a, b)
{
    var text = "";
    if (a != b && a.alive && b.alive)
    {
        if (a.likes == b.name && b.likes == a.name)
        {
            text += a.name + " is married to " + b.name + " and happily in love; ";
            text += a.getHeShe() + " had " + a.getHad() + ", ";
            text += "now " + a.getHeShe() + " has " + a.getHas() + ". ";
        }
    }
    return text;
}

function agePersons()
{
   for (var i = 0; i < person.length; i++)
   {
        if (person[i].alive)
        {
            person[i].age++;
            
            if ( person[i].age > 40 && getRandom(0, 100) > 75 )
            {
                person[i].dislikes = "";
            }
            if ( person[i].age > 65 && getRandom(0, 100) > 75 )
            {
                person[i].alive = false;
            }
        }
   }
}

function getRelations(sType)
{
    var text = "";
    for (var i = 0; i < person.length; i++)
    {
        for (var j = 0; j < person.length; j++)
        {
            if (sType == "after")
            {
                text += getRelationAfter(person[i], person[j]);
            }
            else
            {
                text += getRelation(person[i], person[j]);
            }
        }
    }

    if (text == "")
    {
        text = "Nothing happened.";
    }
    return text;
}

function getRelationAfter(a, b)
{
    var text = "";
    if (a != b && a.alive && b.alive)
    {
        if (a.likes != "" && a.likes == b.has)
        {
            if (Math.random() * 100 > 50)
            {
                text += a.name + " kills " + b.name + " because ";
                text += b.getHeShe() + " has " + b.has + " which ";
                text += a.getHeShe() + " likes too. ";
                b.alive = false;
            }
            else
            {
                text += a.name + " tries to kill " + b.name + " because ";
                text += b.getHeShe() + " has " + b.has + " which ";
                text += a.getHeShe() + " likes, but " + a.getHeShe();
                text += " fails and goes to jail. ";
                a.dislikes = b.name;
                a.location = "jail";
            }
        }
    }
    return text;
}

function getRelation(a, b)
{
    var text = "";

    if (a != b && a.alive && b.alive && a.location == b.location)
    {
        if (a.likes != "" && a.likes == b.has)
        {
            if (b.likes == "")
            {
                text += a.name + " likes " + b.has + " that " + b.name + " has. ";
                text += b.name + " looses " + b.getHisHer() + " " + b.has;
                text += " to " + a.getHimHer() + ". ";
                a.had = a.has;
                b.had = b.has;
                a.likes = b.name;
                b.likes = a.name;
                a.has = b.has;
                b.has = a.name;
            }
        }
        if (a.dislikes != "" && a.dislikes == b.has)
        {
            text += a.name + " dislikes " + b.name + " because ";
            text += b.getHeShe() + " has " + b.has + ". ";
            if (b.likes == a.name)
            {
                text += "That makes " + b.name + " sad. "
            }
        }
        if (a.likes == b.name)
        {
            text += a.name + " likes " + b.name + ". ";
        }
        if (a.likes != "" && a.likes == b.has)
        {
            text += a.name + " is jealous at " + b.name + " because ";
            text += a.getHeShe() + " likes " + a.likes + " too. ";
            if (b.likes == b.has)
            {
                text += "But " + b.name + " won't give it to " + a.getHimHer();
                text += " because " + b.getHeShe() + " likes it. ";
            }
        }
    }
    return text;
}

function getPersonsInfo()
{
    var text = "";
    for (var i = 0; i < person.length; i++)
    {
        text += person[i].getInfo();
    }
    return text;
}

Person.prototype.getInfo = function()
{
    var text = "";
    if (this.alive)
    {
        text += this.name + " is a " + this.age + " year old " + this.gender + ", ";
        text += "living in " + this.location + "; ";
        text += this.getHeShe() + " likes " + this.getLikes() + ", and ";
        text += this.getHeShe() + " dislikes " + this.getDislikes() + ". ";
    }
    else
    {
        text += this.name + " died at the age of " + this.age + " in ";
        text += this.location + ". ";
    }
    return text;
}

Person.prototype.getLikes = function()
{
    return (this.likes == "") ? "nothing in particular" : this.likes;
}

Person.prototype.getHas = function()
{
    return (this.has == "") ? "nothing" : this.has;
}

Person.prototype.getHad = function()
{
    return (this.had == "") ? "nothing" : this.had;
}

Person.prototype.getDislikes = function()
{
    return (this.dislikes == "") ? "nothing in particular" : this.dislikes;
}

Person.prototype.getHeShe = function()
{
    return (this.gender == "male") ? "he": "she";
}

Person.prototype.getHimHer = function()
{
    return (this.gender == "male") ? "him": "her";
}

Person.prototype.getHisHer = function()
{
    return (this.gender == "male") ? "his": "her";
}

function constructPersons()
{
    var i = -1;
    var townA = getRandomChoice("Chicago", "San Francisco");
    var townB = getRandomChoice("New York", "Hollywood");
    var likeA = getRandomChoice("money", "fame");
    var likeB = getRandomChoice("work", "friends");
    var likeC = getRandomChoice("computer", "politics");
    var likeD = getRandomChoice("car", "sport");

    person[++i] = new Person();
    person[i].name = getRandomChoice("Jill", "Betty");
    person[i].gender = "female";
    person[i].age = getRandom(20, 30);
    person[i].likes = getRandomChoice(likeB, likeA);
    person[i].dislikes = getRandomChoice("", likeC);
    person[i].has = "";
    person[i].location = getRandomChoice(townA, townB);

    person[++i] = new Person();
    person[i].name = getRandomChoice("Jack", "James");
    person[i].gender = "male";
    person[i].age = getRandom(22, 32);
    person[i].likes = person[i-1].name;
    person[i].dislikes = getRandomChoice(likeD, "");
    person[i].has = likeC;
    person[i].location = getRandomChoice(townA, townB);

    person[++i] = new Person();
    person[i].name = getRandomChoice("Max", "John");
    person[i].gender = "male";
    person[i].age = getRandom(24, 34);
    person[i].likes = "";
    person[i].dislikes = "";
    person[i].has = getRandomChoice(likeA, likeC);
    person[i].location = getRandomChoice(townA, townB);

    person[++i] = new Person();
    person[i].name = getRandomChoice("Ferdinand", "Brian");
    person[i].gender = "male";
    person[i].age = getRandom(28, 38);
    person[i].likes = likeC;
    person[i].dislikes = "";
    person[i].has = getRandomChoice(likeA, likeB);;
    person[i].location = townA;

    if ( getRandom(0, 100) > 50 )
    {
        person[++i] = new Person();
        person[i].name = getRandomChoice("Sarah", "Jennifer");
        person[i].gender = "female";
        person[i].age = getRandom(25, 35);
        person[i].likes = likeB;
        person[i].dislikes = likeC;
        person[i].has = "";
        person[i].location = getRandomChoice(townA, townB);
    }

    if ( getRandom(0, 100) > 50 )
    {
        person[++i] = new Person();
        person[i].name = getRandomChoice("Mary", "Ann");
        person[i].gender = "female";
        person[i].age = getRandom(24, 34);
        person[i].likes = getRandomChoice(likeB, likeC);
        person[i].dislikes = "";
        person[i].has = getRandomChoice(likeB, likeC);
        person[i].location = townB;
    }
}

function getRandomChoice(a, b)
{
    return ( getRandom(0, 100) > 50 ) ? a : b;
}

function Person()
{
    this.alive = true;
    this.name = "";
    this.gender = "";
    this.age = 0;
    this.likes = "";
    this.dislikes = "";
    this.has = "";
    this.location = "";
    this.had = "";
}

function getRandom(min, max)
{
    var randInt = Math.random() * (max - min) + min;
    return Math.floor(randInt);
}

