/* FCE_FLOORS_PLATES: for each floor, the name of the floorplate to use. Should ocrrespond to
* names of GIF images in the resources/ directory, and also the imagemaps
* names to be used with the floorplate */
var FCE_FLOORS_PLATES = {};
for (var i = 4; i <= 31; i++) {
    FCE_FLOORS_PLATES[i] = ['Merc_' + i + '_color'];
}

/* if certain floorplate images are different widths, we use position: relative
* to offset them by this amount so they will look centered or aligned or
* whatever. This hack avoids messing with all the other metrics going on */
//var FCE_FLOORPLATE_OFFSETS = { 2 : -40 };

/* make an Image object to pre-load each of the floor plates, and tack the obj
* onto the end of the array for further use. This has to be done here and not
* in document.ready() func because surprisingly that fires before the rest of
* the javascript is parsed. */
for (f in FCE_FLOORS_PLATES) {
    var preloader = new Image();
    preloader.src = 'resources/' + FCE_FLOORS_PLATES[f][0] + '.gif';
    FCE_FLOORS_PLATES[f].push(preloader);
}



var FCEAptPointers_4 = {
    "01": { "x": 99, "y": 50, "ord": "" },
    "02": { "x": 72, "y": 50, "ord": "" },
    "03": { "x": 50, "y": 50, "ord": "" },
    "04": { "x": 18, "y": 64, "ord": "" },
    "05": { "x": 13, "y": 131, "ord": "" },
    "06": { "x": 33, "y": 131, "ord": "" },
    "07": { "x": 53, "y": 131, "ord": "" },
    "08": { "x": 74, "y": 131, "ord": "" },
    "09": { "x": 96, "y": 131, "ord": "" },
    "10": { "x": 118, "y": 131, "ord": "" },
    "11": { "x": 140, "y": 131, "ord": "" },
    "12": { "x": 162, "y": 131, "ord": "" },
    "13": { "x": 183, "y": 131, "ord": "" },
    "14": { "x": 203, "y": 131, "ord": "" },
    "15": { "x": 223, "y": 131, "ord": "" },
    "16": { "x": 130, "y": 64, "ord": "" },
    "17": { "x": 130, "y": 50, "ord": "" },
    "18": { "x": 130, "y": 50, "ord": "" }
};

var FCEAptPointers_5_7 = {
    "01": { "x": 99, "y": 48, "ord": "" },
    "02": { "x": 75, "y": 54, "ord": "" },
    "03": { "x": 54, "y": 54, "ord": "" },
    "04": { "x": 24, "y": 76, "ord": "" },
    "05": { "x": 24, "y": 112, "ord": "" },
    "06": { "x": 53, "y": 126, "ord": "" },
    "07": { "x": 74, "y": 126, "ord": "" },
    "08": { "x": 96, "y": 126, "ord": "" },
    "09": { "x": 117, "y": 126, "ord": "" },
    "10": { "x": 140, "y": 126, "ord": "" },
    "11": { "x": 161, "y": 126, "ord": "" },
    "12": { "x": 183, "y": 126, "ord": "" },
    "13": { "x": 212, "y": 111, "ord": "" },
    "14": { "x": 212, "y": 75, "ord": "" },
    "15": { "x": 183, "y": 54, "ord": "" },
    "16": { "x": 161, "y": 54, "ord": "" }
};

var FCEAptPointers_8_13 = {
    "01": { "x": 86, "y": 74, "ord": "" },
    "02": { "x": 58, "y": 76, "ord": "" },
    "03": { "x": 17, "y": 80, "ord": "" },
    "04": { "x": 17, "y": 118, "ord": "" },
    "05": { "x": 52, "y": 118, "ord": "" },
    "06": { "x": 83, "y": 118, "ord": "" },
    "07": { "x": 118, "y": 118, "ord": "" },
    "08": { "x": 152, "y": 118, "ord": "" },
    "09": { "x": 186, "y": 118, "ord": "" },
    "10": { "x": 217, "y": 118, "ord": "" },
    "11": { "x": 211, "y": 79, "ord": "" },
    "12": { "x": 167, "y": 78, "ord": "" }
};

var FCEAptPointers_14 = {
    "02": { "x": 76, "y": 125, "ord": "" },
    "03": { "x": 113, "y": 125, "ord": "" },
    "04": { "x": 155, "y": 125, "ord": "" },
    "05": { "x": 166, "y": 82, "ord": "" }
};

var FCEAptPointers_15_27 = {
    "01": { "x": 79, "y": 73, "ord": "" },
    "02": { "x": 80, "y": 115, "ord": "" },
    "03": { "x": 116, "y": 115, "ord": "" },
    "04": { "x": 153, "y": 115, "ord": "" },
    "05": { "x": 160, "y": 79, "ord": "" }
};

var FCEAptPointers_28 = {
    "01": { "x": 87, "y": 115, "ord": "" },
    "02": { "x": 139, "y": 114, "ord": "" }
};

var FCEAptPointers_29 = {
    "01": { "x": 116, "y": 109, "ord": "" }
};

var FCEAptPointers_30 = {
    "01": { "x": 95, "y": 109, "ord": "" },
    "02": { "x": 143, "y": 109, "ord": "" }
};

var FCEAptPointers_31 = {
    "01": { "x": 120, "y": 85, "ord": "" }
};


var FCEAptPointerPositioning = {

    pointsByFloor: {
        "default": FCEAptPointers_15_27,
        "4": FCEAptPointers_4,
        "5": FCEAptPointers_5_7,
        "6": FCEAptPointers_5_7,
        "7": FCEAptPointers_5_7,
        "8": FCEAptPointers_8_13,
        "9": FCEAptPointers_8_13,
        "10": FCEAptPointers_8_13,
        "11": FCEAptPointers_8_13,
        "12": FCEAptPointers_8_13,
        "13": FCEAptPointers_8_13,
        "14": FCEAptPointers_14,
        "28": FCEAptPointers_28,
        "29": FCEAptPointers_29,
        "30": FCEAptPointers_30,
        "31": FCEAptPointers_31
    },

    /* get the position for the given apartment */
    getPosByApt: function(floor, aptNum) {
        var points = this.pointsByFloor[floor] || this.pointsByFloor['default'];
        return points[aptNum];
    },

    /* get all points for the whole floor */
    getPosByFloor: function(floor) {
        var points = this.pointsByFloor[floor] || this.pointsByFloor['default'];
        return points;
    },

    positionDialog: function(elem, floor, aptnum) {
        var pos = this.getPosByApt(floor, aptnum);
        if (!pos) {
            $(elem).hide();
        }
        else {
            $(elem).show();

            // position the dialog according to the x and y given + any offset
            // given in css for aptInfoDialogOffset
            var offsetX = parseInt($(elem + 'Offset').css('left'));

            var imageWidth = FCE_FLOORS_PLATES[floor][1].width;

            /* since floor plate <img> is centered, try to find the offset
            * from the left edge of the thing we are actually relative to. */
            var dWidth = (parseInt($('#aptFloorPlateW').css('width')) - imageWidth) / 2;

            if (jQuery.browser.msie && jQuery.browser.version < 7) { // msie6 is buggy but this is actually much easier.
                dWidth = 0;
            }

            //console.log('W %s - img %s', $('#aptFloorPlateW').css('width'), imageWidth);
            //console.log('posX: %s, posY: %s, dWidth: %s, offsetX: %s', pos.x, pos.y, dWidth, offsetX);
            $(elem + 'W').css('left', pos.x + dWidth + offsetX)
                                 .css('top', pos.y + parseInt($(elem + 'Offset').css('top')))
                                 .show();

            $(elem).addClass(pos.ord);
        }

    },

    // puts little black dots at the coordinates on top of the floorplate
    debug: function(floor) {
        var points = this.getPosByFloor(floor);
        for (p in points) {
            $('#aptFloorPlate').append('<div style="height: 1px; width: 1px; background: red; position: absolute; left: ' + points[p].x + 'px; top: ' + points[p].y + 'px;">' + p + '</div>');
        }
    }

};

