
// TagCount object  (file: TagCount.js.php)
// Version 1.1, modified by Jon to add Array contains method to fix IE balking at .indexOf.
Array.prototype.contains = function (element) {
	for (var elementCounter = 0; elementCounter < this.length; elementCounter++) {
		if (this[elementCounter] == element) {
			return true;
		}
	}
	return false;
}
function TagCount() {

  // private variables
  var keepZeros  = false;       // bool

  // public variables
  this.tags       = new Array;
  this.lexias     = new Object;  // format: this.lexias['tagName'] = ##
  this.documents  = new Object;  // format: this.documents['tagName'] = ##
  this.idFromTag  = new Object;  // format: this.idFromTag['tagName'] = ##

  // public functions

  this.addTag = function (tagName, numLexias, numDocuments, tagId) {
  	if ( this.tags.contains(tagName) ) return;  // no duplicates
  	this.tags.push(tagName);
    if (keepZeros) {
      this.lexias[tagName] = (null == numLexias) ? 0 : numLexias;
      this.documents[tagName] = (null == numDocuments) ? 0 : numDocuments;
    } else {
      if (null != typeof(numLexias) && numLexias > 0) this.lexias[tagName] = numLexias;
      if (null != typeof(numLexias) && numLexias > 0) this.documents[tagName] = numDocuments;
    }
    if (typeof(tagId) == 'number') {
    	tagId = parseInt(tagId);
    	this.idFromTag[tagName] = tagId;
    }
 	if (keepZeros || this.lexias[tagName] || this.documents[tagName]) return true;
    return false;
  }

  this.zeros = function (bool) {
    keepZeros = (true == bool) ? true : false;
    var x;
    if (!keepZeros) {
      for (x in this.lexias) {
        if (this.lexias[x] == 0) delete this.lexias[x];
      }
      for (x in this.documents) {
        if (this.documents[x] == 0) delete this.documents[x];
      }
    }
  }

  this.makeAlphabetical = function() {
  	 this.tags.sort(function(x,y){
      var a = String(x).toUpperCase();
      var b = String(y).toUpperCase();
      if (a > b)
         return 1
      if (a < b)
         return -1
      return 0;
     });  // case insensitive
  }

  this.getNumLexias = function (tagName) {
    return (this.lexias[tagName]) ? this.lexias[tagName] : false;
  }

  this.getNumDocuments = function (tagName) {
    return (this.documents[tagName]) ? this.documents[tagName] : false;
  }

  this.getTagFromId = function (tagId) {
  	 tagId = parseInt(tagId);
  	 for (var a in this.idFromTag) {
  	 	if (parseInt(this.idFromTag[a]) == tagId) {
  	 		return a;
  	 	}
  	 }
  	 return false;
  }

  this.getIdFromTag = function (tagName) {
  	 return (typeof(this.idFromTag[tagName]) != 'undefined') ? this.idFromTag[tagName] : false;
  }

  this.addAmountToLexias = function (tagName, myNum) {
    if (this.lexias[tagName] || this.addTag(tagName)) {
      if (null == myNum) myNum = 1;
      if (this.lexias[tagName]) this.lexias[tagName] = this.lexias[tagName] + myNum;
    }
  }

  this.addAmountToDocuments = function (tagName, myNum) {
    if (this.documents[tagName] || this.addTag(tagName)) {
      if (null == myNum) myNum = 1;
      if (this.documents[tagName]) this.documents[tagName] = this.documents[tagName] + myNum;
    }
  }

} // end object

// add content to object
var numTaggedWith = new TagCount();

numTaggedWith.addTag('1973', 1, 1, 1362);
numTaggedWith.addTag('aggression', 5, 1, 1213);
numTaggedWith.addTag('anthology', 2, 1, 1305);
numTaggedWith.addTag('appropriate', 1, 1, 1308);
numTaggedWith.addTag('attributes', 1, 1, 1215);
numTaggedWith.addTag('beating', 1, 1, 1216);
numTaggedWith.addTag('behaviour', 4, 1, 1214);
numTaggedWith.addTag('communicative', 1, 1, 1225);
numTaggedWith.addTag('drives', 1, 1, 1218);
numTaggedWith.addTag('editing', 1, 1, 1307);
numTaggedWith.addTag('essay', 1, 1, 354);
numTaggedWith.addTag('feminist', 1, 1, 1306);
numTaggedWith.addTag('forcing', 1, 1, 1219);
numTaggedWith.addTag('foregrounds', 1, 1, 1311);
numTaggedWith.addTag('freedom', 1, 1, 1220);
numTaggedWith.addTag('humans', 1, 1, 1098);
numTaggedWith.addTag('husbands', 1, 1, 1217);
numTaggedWith.addTag('immoral', 1, 1, 1223);
numTaggedWith.addTag('legitimacy', 1, 1, 1221);
numTaggedWith.addTag('legitimate', 1, 1, 1224);
numTaggedWith.addTag('marginalized', 1, 1, 1309);
numTaggedWith.addTag('perspectives', 1, 1, 1310);
numTaggedWith.addTag('sociality', 1, 1, 1226);
numTaggedWith.addTag('tribal', 1, 1, 662);
numTaggedWith.addTag('violence', 7, 1, 731);
numTaggedWith.addTag('violent', 1, 1, 1222);
