/* 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 : ['Element_floor_2'],
        3 : ['Element_floor_3'],
        4 : ['Element_floor_4'],
        5 : ['Element_floor_5'],
        6 : ['Element_floor_6'],
        7 : ['Element_floor_7'],
        8 : ['Element_floor_8'],
        9 : ['Element_floor_9'],
        10 : ['Element_floor_10'],
        11 : ['Element_floor_11'],
        12 : ['Element_floor_12'],
        13 : ['Element_floor_13'],
        14 : ['Element_floor_14'],
        15 : ['Element_floor_15']
    };

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

/* x/y coords for the center of each apartment in the floorplates */
var FCEAptPointers_def = {
            "50" : {"x":20, "y":18, "ord":""},
            "51" : {"x":51, "y":18, "ord":""},
            "52" : {"x":79, "y":18, "ord":""},
            "53" : {"x":105, "y":18, "ord":""},
            "54" : {"x":134, "y":18, "ord":""},
            "55" : {"x":164, "y":18, "ord":""},
            "56" : {"x":162, "y":56, "ord":""},
            "57" : {"x":127, "y":56, "ord":""},
            "58" : {"x":105, "y":56, "ord":""},
            "59" : {"x":77, "y":56, "ord":""},
            "60" : {"x":20, "y":56, "ord":""}
          };

var FCEAptPointers_2 = {
            "50" : {"x":161, "y":30, "ord":""},
            "51" : {"x":191, "y":30, "ord":""},
            "52" : {"x":216, "y":30, "ord":""},
            "53" : {"x":239, "y":30, "ord":""},
            "54" : {"x":267, "y":30, "ord":""},
            "55" : {"x":296, "y":30, "ord":""},
            "56" : {"x":296, "y":70, "ord":""},
            "57" : {"x":263, "y":70, "ord":""},
            "58" : {"x":241, "y":70, "ord":""},
            "59" : {"x":216, "y":70, "ord":""},
            "60" : {"x":161, "y":70, "ord":""}
          };
var FCEAptPointers_10_14 = {
            "50" : {"x":20, "y":18, "ord":""},
            "51" : {"x":51, "y":18, "ord":""},
            "52" : {"x":79, "y":18, "ord":""},
            "53" : {"x":105, "y":18, "ord":""},
            "54" : {"x":134, "y":18, "ord":""},
            "55" : {"x":164, "y":18, "ord":""},
            "56" : {"x":164, "y":63, "ord":""},
            "57" : {"x":130, "y":63, "ord":""},
            "58" : {"x":105, "y":63, "ord":""},
            "59" : {"x":77, "y":63, "ord":""},
            "60" : {"x":20, "y":63, "ord":""}
          };
var FCEAptPointers_15 = {
            "50" : {"x":20, "y":18, "ord":""},
            "51" : {"x":51, "y":18, "ord":""},
            "52" : {"x":79, "y":18, "ord":""},
            "53" : {"x":105, "y":18, "ord":""},
            "54" : {"x":134, "y":18, "ord":""},
            "55" : {"x":164, "y":18, "ord":""},
            "56" : {"x":164, "y":57, "ord":""},
            "57" : {"x":105, "y":57, "ord":""},
            "58" : {"x":77, "y":57, "ord":""},
            "59" : {"x":20, "y":57, "ord":""}
          };

          var FCEAptPointerPositioning = {

              pointsByFloor: {
                  "default": FCEAptPointers_def,
                  "2": FCEAptPointers_2,
                  "10": FCEAptPointers_10_14,
                  "11": FCEAptPointers_10_14,
                  "12": FCEAptPointers_10_14,
                  "13": FCEAptPointers_10_14,
                  "14": FCEAptPointers_10_14,
                  "15": FCEAptPointers_15
              },

              /* 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 subunit = aptnum.replace(/^.*(\d\d[A-Z]?)$/, '$1');
                  var pos = this.getPosByApt(floor, subunit)
                  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.
                          //Double hack.  setting dWidth =0 does not work for floor 2.  On those floors, dwidth is (maybe appropriately) negative.
                          if (dWidth < 0) {
                            dWidth = -45;
                          } else {
                            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;"></div>');
                  }
              }

          };

