var http;
var worms = new Array();
var width, height;


function getMouseCoords(e)
{
    var mouseX = e.pageX - 
        document.getElementById("snakes").offsetLeft -
        document.getElementById("container").offsetLeft;
    var mouseY = e.pageY - 
        document.getElementById("snakes").offsetTop -
        document.getElementById("container").offsetTop;

   /* if(e.offsetX) {
        mouseX = e.offsetX;
        mouseY = e.offsetY;
    }
    else if(e.layerX) {
        mouseX = e.layerX;
        mouseY = e.layerY;
    }*/
    
    return {
        x : mouseX,
        y : mouseY
        };
}

function initSnakes(){
    document.getElementById("snakes").onmousemove = function(event)
    {
        var mouseCoords;

        mouseCoords = getMouseCoords(event);
        for(var i = 0; i < worms.length; i++){
            worms[i].setGoal(mouseCoords.x,
            mouseCoords.y)
        }
    }
    width = document.getElementById("snakes").width;
    height = document.getElementById("snakes").height;

    http=getHTTPObject();

    if(http!=null){
        http.open("GET", "get-search-strings.php");
        http.send(null);
        http.onreadystatechange=readSnakes;
    }

    /*worms[0] = new snake("hej", width, height, 1);
    worms[1] = new snake("bakpotatisgryta", width, height, .8);
    worms[2] = new snake("Hej! Jag är en text :D", width, height, .6);
    worms[3] = new snake(">: Om nom nom nom nom nom nom nom nom!", width, height, .4);
    worms[4] = new snake("<:==========================>>>>", width, height, .2);*/

    mainLoop();
}

function getHTTPObject(){
    if (window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
        return new XMLHttpRequest();
    else {
        alert("Your browser does not support AJAX.");
        return null;
    }
}

function readSnakes(){
    if(http!=null){
        if(http.readyState==4){
            var response = http.responseText.toString();
            var strings = response.split("\n");

            worms = new Array();
            for(var i = 0; i < strings.length; i++){
                worms[i] = new snake(strings[i], width, height, (i+1)/strings.length);
            }
        }
    }
}

function mainLoop()
{
    step();
    draw();
    setTimeout('mainLoop()', 1000/30);
}

function step(){
    for(var i = 0; i < worms.length; i++){
        worms[i].step();
    }
}

function draw(){
    var canvas = document.getElementById("snakes");
    var ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, width, height);
    for(var i = 0; i < worms.length; i++){
        worms[i].draw(ctx);
    }
}

function snake(text, worldWidth, worldHeight, age){
    this.age = age;
    this.text = text;
    this.length = text.length * 5;
    this.tailX = new Array(this.length);
    this.tailY = new Array(this.length);
    this.width = worldWidth;
    this.height = worldHeight;
    this.x = this.width;
    this.y = this.height;
    this.goalX = Math.random() * this.width;
    this.goalY = Math.random() * this.height;
    this.speed = 3;
    this.rotSpeed = -.01 + Math.random() * .02;
    this.direction = 3 * Math.PI/4;
    this.timer = 0;

    for(var i = 0; i < this.length; i++){
        this.tailX[i] = this.x;
        this.tailY[i] = this.y;
    }

    this.step = function(){
        var goalDir = this.pointDir(this.x, this.y, this.goalX, this.goalY);
        var dirDif = this.directionDifference(this.direction, goalDir);

        this.direction += this.rotSpeed;

        this.tailX[0] = this.x;
        this.tailY[0] = this.y;
        for(var i = this.length -1; i > 0; i--){
            this.tailX[i] = this.tailX[i-1];
            this.tailY[i] = this.tailY[i-1];
        }

        this.x += Math.cos(this.direction) * this.speed;
        this.y -= Math.sin(this.direction) * this.speed;

        if(this.pointDist(this.x, this.y, this.goalX, this.goalY) < 10){
            this.goalX = Math.random() * this.width;
            this.goalY = Math.random() * this.height;
        }

        this.timer ++;
        if(this.timer > 60 * Math.random()){
            this.rotSpeed = -dirDif * Math.random() * .1 - .1 + Math.random() * .2;

            if(Math.random() < .1)
                this.rotSpeed *= -1;

            this.timer = 0;
        }
    }

    this.draw = function(ctx){
        ctx.lineWidth = 20;

        ctx.fillStyle = "rgba(0, 0, 0, "+this.age+")";

        ctx.font = "20px Monospace"; //Times New Roman";

        for(var i = 0; i < this.length; i+=5){
            ctx.save();
            ctx.translate(this.tailX[i], this.tailY[i])
            ctx.rotate(-this.pointDir(this.tailX[i], this.tailY[i], this.tailX[i+4], this.tailY[i+4]));
            ctx.fillText(this.text.charAt(i/5), 0, 6);
            ctx.restore();
        }

        //ctx.fillRect(this.goalX - 3, this.goalY - 3, 6, 6);

    }

    this.pointDir = function(x1, y1, x2, y2){
        return Math.atan2(y1-y2, x2-x1);

    }

    this.pointDist = function(x1, y1, x2, y2){
        var a = x1 - x2;
        var b = y1 - y2;

        return Math.sqrt(a*a + b*b);
    }

    this.directionDifference = function(dir1, dir2){
        var difference = dir1 - dir2;
        while (difference < -Math.PI) difference += Math.PI*2;
        while (difference > Math.PI) difference -= Math.PI*2;
        return difference;

    }

    this.setGoal = function(x, y){
        this.goalX = x;
        this.goalY = y;
    }
}

