function Zone(_name, _width, _height, _scale, _xCar, _zCar, _longName) {
    this.name   = _name;
    this.width  = _width;
    this.height = _height;
    this.scale  = _scale;
    this.xCar   = _xCar;
    this.zCar   = _zCar;
    this.longName = _longName;
    
    var select = document.getElementById('zoneSelect');
    var option = document.createElement('OPTION');
    option.value = this.name;
    option.appendChild(document.createTextNode(this.longName));
    select.appendChild(option);
    
    Zone.prototype.pxToPos = function(x, z) {
        var xPos = Math.round((x - this.xCar) * this.scale);
        var zPos = Math.round((z - this.zCar) * this.scale) * -1;
        return new Array(xPos, zPos);
    };

    Zone.prototype.posToPx = function(x, z) {
        var xPix = Math.round(x / this.scale) + this.xCar;
        var zPix = Math.round(z * -1 / this.scale) + this.zCar;
        return new Array(xPix, zPix);
    };
}

function MLoc() {
    this.formID = "locator";

    this.currentZone = false;
    this.zones = new Array();
    this.xPos = 0;
    this.zPos = 0;
    this.xPix = 0;
    this.zPix = 0;

    MLoc.prototype.addZone = function(_name, _width, _height, _scale, _xCar, _zCar, _longName) {
        this.zones[_name] = new Zone(_name, _width, _height, _scale, _xCar, _zCar, _longName);
        return this.zones[_name];
    };

    MLoc.prototype.selectZone = function(zone) {
        this.currentZone = this.zones[zone] != undefined ? this.zones[zone] : this.zones['nubul'];
        this.xPos = 0;
        this.zPos = 0;
        this.locate(0, 0);
    };

    MLoc.prototype.pickUp = function(event) {
        var pix = clickedPix(event);
        this.xPix = pix[0];
        this.zPix = pix[1];

        var pos = this.currentZone.pxToPos(this.xPix, this.zPix);
        this.xPos = pos[0];
        this.zPos = pos[1];

        this.renew();
    };

    MLoc.prototype.locate = function(x, z) {
        var form = document.forms.locator;

        if (x != undefined && z != undefined) {
            this.xPos = x;
            this.zPos = z;
        }
        else if (form.xpos.value != undefined && form.zpos.value != undefined) {
            this.xPos = form.xpos.value;
            this.zPos = form.zpos.value;
        }
        else {
            this.xPos = 0;
            this.zPos = 0;
        }

        var pix = this.currentZone.posToPx(this.xPos, this.zPos);
        this.xPix = pix[0];
        this.zPix = pix[1];

        this.renew();
    };

    MLoc.prototype.renew = function() {
        var image = document.getElementById('image');
        image.src = this.currentZone.name + '.png';
        image.width = this.currentZone.width;
        image.height = this.currentZone.height;
        document.getElementById('locator').style.marginTop = (this.currentZone.height + 24) + 'px';

        var uri = 'http://syntacticsugar.net/moeloc/?' + this.currentZone.name + ';' + this.xPos + ';' + this.zPos;
        document.forms.locator.xpos.value = this.xPos;
        document.forms.locator.zpos.value = this.zPos;
        document.forms.locator.uri.value = uri;
        tweetURI.set(uri, '#MasterOfEpic ' + this.currentZone.longName + ' X:' + this.xPos + ', Z:' + this.zPos);
        
        var select = document.getElementById('zoneSelect');
        for (i in select.childNodes) {
            if (select.childNodes[i].tagName == 'OPTION') {
                select.childNodes[i].selected = (select.childNodes[i].value == this.currentZone.name) ? true : false;
            }
        }

        var sight = document.getElementById('sight');
        sight.style.top = this.zPix - 7 + 15 + 'px';
        sight.style.left = this.xPix - 7 + 15 + 'px';

        var scale = document.getElementById('scale');
        scale.firstChild.nodeValue = '(1px ≒ ' + Math.round(this.currentZone.scale * 10) / 10 + 'm)';
    };

    MLoc.prototype.initialize = function() {
        var params = location.search.substr(1).split(';');
        this.selectZone(params[0]);
        this.locate(params[1], params[2]);
    };
} 

function clickedPix(event) {
    var xScroll = 0;
    var zScroll = 0;

    if (document.documentElement.scrollLeft || document.documentElement.scrollTop) {
        xScroll = document.documentElement.scrollLeft;
        zScroll = document.documentElement.scrollTop;
    }
    else if (window.pageXOffset || window.pageYOffset) {
        xScroll = window.pageXOffset;
        zScroll = window.pageYOffset;
    }

    return new Array(event.clientX + xScroll - 15, event.clientY + zScroll - 15);
}

var ML;
function initialize() {
    tweetURI.bind(document.getElementById('tweet-it'));
    
    ML = new MLoc();
    ML.addZone('nubul', 502, 456, 1.346,  219, 293, 'Nubul');
    ML.addZone('alvis', 502, 456, 2.870,  239, 200, 'Alvis Forest');
    ML.addZone('garm',  502, 456, 2.017,  312, 436, 'Garm Corridor');
    ML.addZone('lexur', 604, 456, 3.750,  266, 169, 'Lexur Hills');
    ML.addZone('ips',   502, 456, 5.870,  253, 236, 'Ips Canyon');
    ML.addZone('mirim', 502, 456, 3.405,  214, 257, 'Mi-Rim Coast');
    ML.addZone('irvana',502, 456, 4.720,  244, 241, 'Ilvana Valley');
    ML.addZone('elvin', 571, 668, 2.500,  260, 324, 'Elvin Valley');
    ML.addZone('elvinm', 502, 456, 2.947,  82, 118, 'Elvin Mountain');
    ML.addZone('neok',  502, 456, 5.481,  245, 245, 'Neok Highland');
    ML.addZone('neokm', 502, 456, 0.868,  182, 248, 'Neok Mountain');
    ML.addZone('drgne', 502, 593, 2.173,  460, 65,  'Dragon Edge');
    ML.addZone('bsqnw', 502, 456, 0.900,  287, 289, 'Bisque NorthWest');
    ML.addZone('bsqw',  502, 456, 0.900,  257, 275, 'Bisque West');
    ML.addZone('bsqc',  502, 456, 1.545,  190, 281, 'Bisque Central');
    ML.addZone('bsqp',  502, 456, 1.800,  306, 227, 'Bisque Port');
    ML.addZone('bsqe',  502, 456, 1.120,  261, 329, 'Bisque East');
    ML.addZone('mutumc', 502, 456, 1.038,  209, 30, 'Mutum Catacomb');
    ML.addZone('mutum', 502, 456, 1.165,  249, 236, 'Mutum Cemetery');
    ML.initialize();
}



