
//================== singleton class BSpatialSelect ==================================
/* class that represents the result of a spatial select action on the map, either of type point, line, rectangle or polygon */

//------------------ static fields 
BSpatialSelect.instance = null;
BSpatialSelect.POINT = 0;
BSpatialSelect.LINE = 1;
BSpatialSelect.RECTANGLE = 2;
BSpatialSelect.POLYGON = 3;
BSpatialSelect.INVALID = -1;

//------------------ static methods 
/**
 * returns the singleton instance
 */
BSpatialSelect.getInstance = function() {
    return BSpatialSelect.instance;
}

/**
 * sets the singleton instance
 */
BSpatialSelect.setInstance = function(ss) {
    BSpatialSelect.instance = ss;
}

//------------------ constructor and instance methods
/**
 * @param type integer that indicates the type
 * @param x array of type double that holds x coordinates
 * @param y array of type double that holds y coordinates
 */
function BSpatialSelect(type, x, y)  {
    //alert("BSpatialSelect constructor");

    this.type = type;
    this.x = x;
    this.y = y;
    this.numPoints = 0;

    //basic validation only, doesn't check coordinate values
    if (x != null && BUtil.typeOf(x) == 'array' && y != null && BUtil.typeOf(y) == 'array'
            && x.length == y.length
            && ((type == BSpatialSelect.POINT && x.length == 1) 
            || (type == BSpatialSelect.LINE && x.length >= 2)
            || (type == BSpatialSelect.RECTANGLE && x.length == 4)
            || (type == BSpatialSelect.POLYGON &&  x.length >= 3)
            )) {
        this.numPoints = x.length;
        //alert("BSpatialSelect constructor OK");
    }
    else {
        this.type = BSpatialSelect.INVALID;
        this.x = null;
        this.y = null;
        //alert("BSpatialSelect constructor NOK");
    }
    
    //------------------ instance methods
    
    /**
     * Returns the type of this object.
     * @return Possible values are the static final variables BSpatialSelect.POINT, BSpatialSelect.LINE, BSpatialSelect.RECTANGLE, BSpatialSelect.POLYGON.
     */
    this.getType = function () { return this.type; }

    /**
     * Returns the x coordinates.
     * @return array of type double
     */
    this.getX = function () { return this.x; }

    /**
     * Returns the y coordinates.
     * @return array of type double
     */
    this.getY = function () { return this.y; }

    /**
     * Returns the y coordinates.
     * @return array of type double
     */
    this.getNumPoints = function () { 
        return this.numPoints; 
    }
}




//================== static class BMap ==================================
/* class that allows scripting of the map */

function BMap() {
}

BMap.initialised = false;

/**
 * @return true if the map is ready to receive javascript calls
 */
BMap.isInitialised = function() {
    return BMap.initialised;
}

/**
 * initialises the map to receive javascript calls
 * @return true if succesful
 */
BMap.initialise = function() {
    if (!BMap.initialised) {
        if (bridgeMethodInitialise) {
            bridgeMethodInitialise();
            BMap.initialised = true;
        }
    }
    return BMap.initialised;
}

/**
 * zooms the map to a specified extent, values are in decimal degrees.
 * @return true if succesful
 */
BMap.zoomToExtent = function(left, bottom, right, top) {
    if (!BMap.initialise()) {
        return false;
    }
    if(!bridgeMethodZoomToExtent) {
        return false;
    }
    bridgeMethodZoomToExtent(left, bottom, right, top);
    return true;
}

/**
 * zooms the map to a specified coordinate
 * @param longitude
 * @param latitude
 * @param scaleFactor optional, use a negative value if using the default
 * @param showMarker true if a marker should be shown on the map
 * @param label the label to show on the marker
 * @return true if succesful
 */
BMap.zoomToCoordinate = function(longitude, latitude, scaleFactor, showMarker, markerLabel) {
    if (!BMap.initialise()) {
        return false;
    }
    if(!bridgeMethodZoomToCoordinate) {
        return false;
    }
    bridgeMethodZoomToCoordinate(longitude, latitude, scaleFactor, showMarker, markerLabel);
    return true;
}




//================== static class BUtil ==================================

function BUtil() {
}

//private
//This is a workaround from http://javascript.crockford.com/remedial.html
//'instanceof Array' will only recognize arrays that are created in the same context (or window or frame). 
BUtil.typeOf = function(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (typeof value.length === 'number' &&
                    !(value.propertyIsEnumerable('length')) &&
                    typeof value.splice === 'function') {
                s = 'array';
            }
        } 
        else {
            s = 'null';
        }
    }
    return s;
}

/**
 * focuses a window opened via an html <a> link
 * @param linkObject
 * @param windowFeatures string
 * @return false
 */
BUtil.focusPopup = function(linkObject, windowFeatures) {
    //alert("BUtil.focusWindowFromLink");
    BUtil.focusPopupMain(linkObject.getAttribute('href'), linkObject.getAttribute('target') || '_blank', windowFeatures);
    return false;
}

//private
BUtil.focusPopupMain = function(url, target, features) {
      if (!(features)) {
        features = "";
      }
      if (!(target)) {
        target = "_blank";
      }
      //alert("BUtil.focusPopupMain" + ", " + url +  ", " + target +  ", " + features);
      var theWindow;
      try {
          if (target != "_blank") {// && theWindow.focus) {
              theWindow = window.open("", target, features);
          }
          else {
              theWindow = window.open(url, target, features);
          }
          if (!theWindow) {
              alert("Your browser seems to block popup windows. Please enable popup windows to see the requested page.");
          }
          if (theWindow && target != "_blank") {// && theWindow.focus) {
              //alert("BUtil.focusPopup theWindow.focus");
              //theWindow.focus();//does not work with tabbed browsing?
              theWindow.close();
              theWindow = window.open(url, target, features);
          }
      }
      catch (e) {
        alert("BUtil.focusPopupMain error: " + e.name + ": " + e.message); 
      } 
      return theWindow;
}
