/*
Last updated - February 15, 2007
Copyright - Pramati Technologies.
*/

var xmlTagCloudDataSource = Class.create();

Object.extend(xmlTagCloudDataSource.prototype,Object.extend(wTagCloudDataSource.prototype,{
	initialize:function(xmlUrl,urlExpression,dioId){
		this.options = Object.extend({
			tagsLimit: -1
		}, arguments[2], {});

		this.urlExpression = urlExpression;
		this.dioId = (dioId) ? dioId : null;
		this.tags = new Array();
		this.url = xmlUrl;
	},

	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 datasource = this;
		var sUrl = this.url;
		var theDate = new Date();
		var getMostFrequentlyUsed = new Ajax.Request(
				sUrl,
		{
			method: 'post',
			parameters: '',
			asynchronous: false,
			onComplete: function(originalRequest){
				//check for response type - text or xml DOM
				if(originalRequest.responseText.length > 0){
					var responseXML = null;
                    // code for IE
					if (window.ActiveXObject)
					{
						responseXML = new ActiveXObject("Microsoft.XMLDOM");
						responseXML.async="false";
						responseXML.loadXML(originalRequest.responseText);
                    }
					// code for Mozilla, Firefox, Opera, etc.
					else
					{
						var parser=new DOMParser();
						responseXML=parser.parseFromString(originalRequest.responseText,"text/xml");
					}
				}else{
					var responseXML = originalRequest.responseXML;
				}

				var xmlTags = $A(responseXML.getElementsByTagName("tag"));
				var data = new Array();
				var strCreated = '';

				xmlTags.each(function(tag,index){
					strCreated = tag.getAttribute('created');
					if(trim(strCreated) != '')
						theDate = parseInt(strCreated);
					else
						theDate = new Date().getTime();
					
					datasource.tags[datasource.tags.length] = new oTag(tag.getAttribute('tagtitle'),tag.getAttribute('freq'),datasource.getUrl(tag.getAttribute('tagtitle')),theDate,tag.getAttribute('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;
	}
}));
