//FIXME: Add distinct logger for this file?

function StationPoint(id, x, y, symbol) {
    this._id = id;
    this._x=x;
    this._y=y;
    this._symbol=symbol;
    this._fieldCount = 0;
    this._maxFieldNameLength = 0;
    this._maxFieldLength = 0;
    this._httpLink = null;
    this._imageLink = null;
    this._imageW = null;
    this._imageH = null;
}

StationPoint.prototype.getId = function() {
    return this._id;
}

StationPoint.prototype.getX = function() {
    return this._x;
}

StationPoint.prototype.getY = function() {
    return this._y;
}

StationPoint.prototype.getSymbol = function() {
    return this._symbol;
}

StationPoint.prototype.getFieldCount = function() {
    return this._fieldCount;
}

StationPoint.prototype.getMaxFieldLength = function() {
    return this._maxFieldLength;
}

StationPoint.prototype.getMaxFieldNameLength = function() {
    return this._maxFieldNameLength;
}

StationPoint.prototype.getLink = function() {
    return this._httpLink;
}

StationPoint.prototype.processHtmlRow = function(title, value, xPos, yPos) {
    var html;
    if (title != null && title != "") {
        var cleanTitle = removeNamespace(title);
        if (title.toUpperCase()==uniquePtField.toUpperCase()) {	
            //Depending on the station interactivity mode, whether http, or selection...
            if (stationClickMode==2) {
                //mapLog.debug("modifySelection: " + value + ":" + this.getId());
                this._httpLink="javascript:modifySelection("+value+"" +","+ xPos +","+yPos+","+ this.getX() +","+ this.getY()+");";
            }
        } else if ((cleanTitle.toUpperCase()=="HYPERLINK") || (cleanTitle.toUpperCase()=="LINK") || (cleanTitle.toUpperCase()=="URL")) {
            if (stationClickMode==1) {
                this._httpLink=value;
            }
        } else if (title.toUpperCase().indexOf("SYMBOLFIELD") > -1 ) {
            // By convention, we will not display fields of this type
        } else if (title.length > 13 && title.substr(0,14) == "overview_image" && value && value != "") {
            //Currently ignore overview_image_w overview_image_h
            if ("overview_image_url" == title) {
                //html='<tr><td>' + value + '</td></tr>';
                this._imageLink = value;
            }
        } else if (value != null && value != "") {
            if (cleanTitle.length>this._maxFieldNameLength) this._maxFieldNameLength = cleanTitle.length;
            if (value.length>this._maxFieldLength) this._maxFieldLength=value.length;
            //Start writing the table with attribute field names, and values, and also get the Hyperlink value as well
            html='<tr> <td id="description"> '+ cleanTitle +'</td>';
            this._fieldCount++;
            if ("ok" == value.toLowerCase() || "nominal" == value.toLowerCase()) {
                html+='<td id="value" class="ok">'+value+'</td></tr>\n';
            } else if ("warning" == value.toLowerCase()) {
                html+='<td id="value" class="nominal">'+value+'</td></tr>\n';
            } else if ("failure" == value.toLowerCase() || "bad" == value.toLowerCase()) {
                html+='<td id="value" class="failure">'+value+'</td></tr>\n';
            } else {
                html+='<td id="value">'+value+'</td></tr>\n';
            }
        }
    }
    return html;
}

GMLPoint.prototype = new StationPoint();

function GMLPoint(id, x, y, symbol, node) {
    StationPoint.call(this, id, x, y, symbol);
    this._node = node;
}

GMLPoint.prototype.getHTML = function(xPos, yPos) {
    this._maxFieldNameLength = 0;
    this._maxFieldLength = 0;
    this._httpLink="";
    this._fieldCount = 0;
    var html = '<table cellpadding="0" cellspacing="0" class="descriptiontable">';
    //html += '<tr><td id="overviewImage"></td></tr>';
    html += '<table cellpadding="0" cellspacing="0" class="descriptiontable">\n';
    var featureAttributeNodes = this.getNode().childNodes;

    for (j=0;j<featureAttributeNodes.length;j++) {
        //If it is a regular node (mozilla needs this) (and not the geometry node)
        if ((featureAttributeNodes[j].nodeType != 3)&&(featureAttributeNodes[j].nodeType != 8)&&(featureAttributeNodes[j].nodeName.toUpperCase()!=addNamespace(POINT_TAG).toUpperCase())) {
            //populate a variable which we will use for dynamic positioning of the attribute table later
            var displayName=featureAttributeNodes[j].nodeName;
            var value = Sarissa.getText(featureAttributeNodes[j]);
            var rowHtml = this.processHtmlRow(displayName, value, xPos, yPos);
            if (rowHtml!=null) {
                html += rowHtml;
            }
        }//end if node is neither "text" nor "comment" nodeType
    }//end for each point attribute node
    html+='</table>';
    //The current html table is complete
    html+='</td></tr></table>';
    return html;
}

GMLPoint.prototype.getNode = function() {
    return this._node;
}

//Case insensitive for now.
GMLPoint.prototype.getAttribute = function(key) {
    var attrValue = null;
    if (key != null && key != '') {
        key = key.toUpperCase();
        var featureAttributeNodes = this.getNode().childNodes;

        for (j=0;j<featureAttributeNodes.length;j++) {
            if (featureAttributeNodes[j].nodeName.toUpperCase() == key) {
                attrValue = Sarissa.getText(featureAttributeNodes[j]);
                break;
            }
        }
    }
    return attrValue;
}

CSVPoint.prototype = new StationPoint();

function CSVPoint(id, x, y, symbol, attrs, keys) {
    StationPoint.call(this, id, x, y, symbol);
    this._attrs = attrs;
    this._keys = keys;
}

CSVPoint.prototype.getHTML = function(xPos, yPos) {
    this._maxFieldNameLength = 0;
    this._maxFieldLength = 0;
    this._httpLink="";
    this._fieldCount = 0;
    var html = '<table cellpadding="0" cellspacing="0" class="transback">';
    html += '<tr><td>';
    html += '<table cellpadding="0" cellspacing="0" class="descriptiontable">\n';
    if (this._attrs != null) {
        if (this._keys != null) {
            for(var i=0; i < this._keys.length; i++) {
                var key = this._keys[i];
                if (key != null) {
                    var attr = this._attrs[i];
                    var rowHtml = this.processHtmlRow(key, attr, xPos, yPos);
                    if (rowHtml != null) {
                        html += rowHtml;
                    }
                }
            }
        }
    }
    html += '</table>';
    if (this._imageLink) {
        html+='<td valign="top"><img src="' + this._imageLink + '" class="descriptiontable"></td>';
    }
    html += '</tr></table>';
    return html;
}

CSVPoint.prototype.getAttribute = function(key) {
    var attrValue = null;
    if (key != null && key != '') {
        if (this._attrs != null) {
           if (this._keys != null) {
               key = key.toUpperCase();
               for(var i=0; i < this._keys.length; i++) {
                   if (this._keys[i].toUpperCase() == key) {
                       attrValue = this._attrs[i];
                       break;
                   }
               }
           }
        }
    }
    return attrValue
}

