/*
Last updated - February 14, 2007
Copyright - Pramati Technologies.
*/

var jsonTagCloudDataSource = Class.create();

Object.extend(jsonTagCloudDataSource.prototype,Object.extend(wTagCloudDataSource.prototype,{
		initialize:function(jsonArray,urlExpression,dioId){
			this.options = Object.extend({
				tagsLimit: -1
			}, arguments[2], {});

			this.urlExpression = urlExpression;
			this.dioId = (dioId) ? dioId : null;
			this.tags = new Array();
			this.datasource = jsonArray;
		},

		getTags:function(){
			/*  Get tags for the current digital object (collection / photo)
				pageTagCloud    - Object - object reference for oTagCloud - page instance
				dioId           - Integer - digital object id
			*/
			var theDate = new Date();
			var data = new Array();
			var strCreated = '';
            var datasource=this;
			this.datasource.each(function(tag,index){
				strCreated = tag.created;
				if(trim(strCreated) != '')
						theDate = parseInt(strCreated);
					else
						theDate = new Date().getTime();

				datasource.tags[datasource.tags.length] = new oTag(tag.tagtitle,tag.freq,datasource.getUrl(tag.tagtitle),theDate,tag.rank);
			});
		},

		getUrl:function(tagName){
			return this.urlExpression.replace("#@@#", tagName);
		},

		/*  Adding tags to the cloud
			newTags     - Array - input value (tags to be added) - without trailing or leading spaces
		*/
		addTags:function(newTags){
			var tagArray = newTags.split(',');
			var datasource = this;

			//script to add tags to the database
		},

		addSingleTag:function(newTag){
			this.tags[this.tags.length] = newTag;
		},

		/*  Delete tags
			selectedTags    - Array - list of selected tags
		*/
		deleteTags:function(tagsToDelete){
			//script to delete tags from the database
		},

		renameTag:function(oldTagName,newTagName){
			//script to rename a tag on server side
		},

		mergeTags:function(oldTags,newTagName){
			var datasource = this;

			//script to merge tags on the server side
		},

		getTagByName: function(tagName){
			var theTags = this.tags;
			var matchingTag = false;

			matchingTag = theTags.find(function(tag,index){
				return trim(tag.tagtitle) == trim(tagName);
			});

			return matchingTag;
		}
}));
