/* 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 = {
        2 : ['plate_2'],
        3 : ['plate_3'],
        4 : ['plate_4'],
        5 : ['plate_5'],
        6 : ['plate_6-7'],
        7 : ['plate_6-7'],
        8 : ['plate_8'],
        9 : ['plate_9-12'],
        10 : ['plate_9-12'],
        11 : ['plate_9-12'],
        12 : ['plate_9-12']
    };


/* 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);
}

/* x/y coords for the center of each apartment in the floorplates */
var FCEAptPointers_2 = {
            "01" : {"x":192, "y":41, "ord":""},
            "02" : {"x":192, "y":67, "ord":""},
            "03" : {"x":192, "y":90, "ord":""},
            "04" : {"x":192, "y":119, "ord":""},
            "05" : {"x":192, "y":153, "ord":""},
            "06" : {"x":23, "y":98, "ord":""},
            "07" : {"x":23, "y":67, "ord":""},
            "08" : {"x":24, "y":40, "ord":""},
            "09" : {"x":49, "y":40, "ord":""},
            "10" : {"x":68, "y":40, "ord":""},
            "11" : {"x":89, "y":40, "ord":""},
            "12" : {"x":107, "y":40, "ord":""},
            "13" : {"x":127, "y":40, "ord":""},
            "14" : {"x":146, "y":40, "ord":""},
            "15" : {"x":165, "y":40, "ord":""}
          };

var FCEAptPointers_3 = {
            "01" : {"x":198, "y":47, "ord":""},
            "02" : {"x":198, "y":94, "ord":""},
            "03" : {"x":178, "y":125, "ord":""},
            "04" : {"x":178, "y":141, "ord":""},
            "05" : {"x":178, "y":162, "ord":""},
            "06" : {"x":23, "y":158, "ord":""},
            "07" : {"x":23, "y":134, "ord":""},
            "08" : {"x":24, "y":89, "ord":""},
            "09" : {"x":24, "y":46, "ord":""},
            "10" : {"x":70, "y":37, "ord":""},
            "11" : {"x":115, "y":46, "ord":""},
            "12" : {"x":122, "y":81, "ord":""},
            "13" : {"x":167, "y":84, "ord":""},
            "14" : {"x":166, "y":59, "ord":""},
            "15" : {"x":165, "y":33, "ord":""}
          };
var FCEAptPointers_4 = {
            "01" : {"x":196, "y":48, "ord":""},
            "02" : {"x":196, "y":90, "ord":""},
            "03" : {"x":201, "y":144, "ord":""},
            "04" : {"x":184, "y":144, "ord":""},
            "05" : {"x":166, "y":144, "ord":""},
            "06" : {"x":149, "y":144, "ord":""},
            "07" : {"x":133, "y":144, "ord":""},
            "08" : {"x":115, "y":144, "ord":""},
            "09" : {"x":70, "y":153, "ord":""},
            "10" : {"x":44, "y":153, "ord":""},
            "11" : {"x":15, "y":153, "ord":""},
            "12" : {"x":15, "y":122, "ord":""},
            "13" : {"x":17, "y":83, "ord":""},
            "14" : {"x":17, "y":52, "ord":""},
            "15" : {"x":50, "y":31, "ord":""},
            "16" : {"x":47, "y":57, "ord":""},
            "17" : {"x":47, "y":82, "ord":""},
            "18" : {"x":90, "y":81, "ord":""},
            "19" : {"x":90, "y":57, "ord":""},
            "20" : {"x":104, "y":40, "ord":""},
            "21" : {"x":126, "y":58, "ord":""},
            "22" : {"x":123, "y":83, "ord":""},
            "23" : {"x":165, "y":82, "ord":""},
            "24" : {"x":165, "y":57, "ord":""},
            "25" : {"x":163, "y":34, "ord":""}
          };
var FCEAptPointers_5 = {
            "01" : {"x":196, "y":48, "ord":""},
            "02" : {"x":196, "y":90, "ord":""},
            "03" : {"x":191, "y":147, "ord":""},
            "04" : {"x":160, "y":147, "ord":""},
            "05" : {"x":141, "y":147, "ord":""},
            "06" : {"x":120, "y":147, "ord":""},
            "07" : {"x":70, "y":153, "ord":""},
            "08" : {"x":44, "y":153, "ord":""},
            "09" : {"x":15, "y":153, "ord":""},
            "10" : {"x":15, "y":122, "ord":""},
            "11" : {"x":17, "y":83, "ord":""},
            "12" : {"x":17, "y":52, "ord":""},
            "13" : {"x":50, "y":31, "ord":""},
            "14" : {"x":47, "y":57, "ord":""},
            "15" : {"x":47, "y":82, "ord":""},
            "16" : {"x":90, "y":81, "ord":""},
            "17" : {"x":90, "y":57, "ord":""},
            "18" : {"x":104, "y":40, "ord":""},
            "19" : {"x":126, "y":58, "ord":""},
            "20" : {"x":123, "y":83, "ord":""},
            "21" : {"x":165, "y":82, "ord":""},
            "22" : {"x":165, "y":57, "ord":""},
            "23" : {"x":163, "y":34, "ord":""}
          };
var FCEAptPointers_6_7 = {
            "01" : {"x":196, "y":48, "ord":""},
            "02" : {"x":196, "y":90, "ord":""},
            "03" : {"x":90, "y":126, "ord":""},
            "04" : {"x":55, "y":158, "ord":""},
            "05" : {"x":16, "y":152, "ord":""},
            "06" : {"x":16, "y":122, "ord":""},
            "07" : {"x":19, "y":85, "ord":""},
            "08" : {"x":19, "y":49, "ord":""},
            "09" : {"x":50, "y":50, "ord":""},
            "10" : {"x":48, "y":85, "ord":""},
            "11" : {"x":93, "y":85, "ord":""},
            "12" : {"x":93, "y":61, "ord":""},
            "13" : {"x":106, "y":43, "ord":""},
            "14" : {"x":125, "y":51, "ord":""},
            "15" : {"x":123, "y":85, "ord":""},
            "16" : {"x":166, "y":85, "ord":""},
            "17" : {"x":166, "y":50, "ord":""}
          };
var FCEAptPointers_8 = {
            "01" : {"x":197, "y":50, "ord":""},
            "02" : {"x":196, "y":90, "ord":""},
            "03" : {"x":90, "y":126, "ord":""},
            "04" : {"x":55, "y":158, "ord":""},
            "05" : {"x":16, "y":152, "ord":""},
            "06" : {"x":18, "y":90, "ord":""},
            "07" : {"x":19, "y":53, "ord":""},
            "08" : {"x":50, "y":70, "ord":""},
            "09" : {"x":95, "y":70, "ord":""},
            "10" : {"x":122, "y":70, "ord":""},
            "11" : {"x":167, "y":70, "ord":""}
          };

var FCEAptPointers_9_12 = {
            "01" : {"x":86, "y":128, "ord":""},
            "02" : {"x":54, "y":157, "ord":""},
            "03" : {"x":18, "y":136, "ord":""}
          };



var FCEAptPointerPositioning = {

    pointsByFloor : {
                      "2": FCEAptPointers_2,
                      "3": FCEAptPointers_3,
                      "4": FCEAptPointers_4,
                      "5": FCEAptPointers_5,
                      "6" : FCEAptPointers_6_7,
                      "7" : FCEAptPointers_6_7,
                      "8": FCEAptPointers_8,
                      //"3": FCEAptPointers_2_3,
                      //"15": FCEAptPointers_15,
                      "default": FCEAptPointers_9_12
                    },

    /* 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: #f99; position: absolute; left: '+points[p].x+'px; top: ' +points[p].y+ 'px;">'+p+'</div>'); 
        }
    }

};
