/*----------------------------------------------------------------------------------
Copyright © 2004 Ian Davis & James Carlyle
This software is provided 'as-is', without any express or implied warranty. In no 
event will the author(s) be held liable for any damages arising from the use of this 
software.
Permission is granted to anyone to use this software for any purpose, including 
commercial applications, and to alter it and redistribute it freely, subject to the 
following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that 
you wrote the original software. If you use this software in a product, an 
acknowledgment (see the following) in the product documentation is required.
Portions Copyright © 2004 Ian Davis & James Carlyle
2. Altered source versions must be plainly marked as such, and must not be 
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------------*/
function Res(cls, addLabel) {
  this.prop = new Object();
  this.cls = cls;
  if (addLabel) {
    this.addProp(cls.model.getProp("rdfs:label"), "", false);
  }
  this.index = null;
  this.uri = null;
}

function addProp(prop, value, readOnly) {
  if (this.prop[prop.toString()] == null) {
    this.prop[prop.toString()] = new Array();
  }
  var resProp = new ResProp(prop, value, readOnly);
  var index = this.prop[prop.toString()].push(resProp);
  resProp.setIndex(index);
  return resProp;
}
Res.prototype.addProp = addProp;

function setProp(prop, index, value) {
  var resProp = this.prop[prop.toString()][index];
  resProp.value = value;
}
Res.prototype.setProp = setProp;

function getProp(prop) {
  return this.prop[prop.toString()];
}
Res.prototype.getProp = getProp;

function getPropByName(propName) {
  return this.prop[propName];
}
Res.prototype.getPropByName = getPropByName;

function removeProp(prop, index) {
  delete this.prop[prop.toString()][index];
}
Res.prototype.removeProp = removeProp;

function serialiseRdfXml () {
  var s = "<" + this.cls.toString();
  var about = this.prop["rdf:about"];
  if (about != undefined) {
    s += " " + about[0].prop.toString() + "=\"" + escEntity(about[0].value) + "\">\r\n";
  } else {
    s += " rdf:nodeID=\"" + this.getSafeId() + "\">\r\n";
  }
  for (propName in this.prop) {
    if (propName != "rdf:about") {
      var propArray = this.prop[propName];
      for (var i = 0; i < propArray.length; i++) {
        var resProp = propArray[i];
        if (resProp != undefined) {
          s += resProp.serialiseRdfXml();
        }
      }
    }
  }
  s += "</" + this.cls.toString() + ">\r\n";
  return s;
}
Res.prototype.serialiseRdfXml = serialiseRdfXml;

function serialiseJsRes() {
  return "var " + this.getSafeId() + " = resMgr.createRes(model.getCls(\"" + this.cls.toString() + "\"), false);";  
}
Res.prototype.serialiseJsRes = serialiseJsRes;

function serialiseJsProp() {
  var s = "";
  for (propName in this.prop) {
    var propArray = this.prop[propName];
    for (var i = 0; i < propArray.length; i++) {
      var resProp = propArray[i];
      if (resProp != undefined) {
        s += this.getSafeId() + resProp.serialiseJs(i);
      }
    }
  }
  return s;
}
Res.prototype.serialiseJsProp = serialiseJsProp;

function setIndex(index) {
  this.index = index;
}
Res.prototype.setIndex = setIndex;

function toString() {
  if (this.prop["rdfs:label"] != undefined && this.prop["rdfs:label"][0] != undefined && this.prop["rdfs:label"][0].value != "") {
    return this.prop["rdfs:label"][0].value;
  } else {
    return this.getId();
  }
}
Res.prototype.toString = toString;

function getId() {
  return this.cls.toString() + "#" + this.index;
}
Res.prototype.getId = getId;

function getSafeId() {
  return this.cls.getSafeId() + "_" + this.index;
}
Res.prototype.getSafeId = getSafeId;

