/**
 * tagCloud component.
 *	
 */

function TagCloud($element,resource_ID,user_ID) {
	TagCloud.baseConstructor.call(this, $element, resource_ID, user_ID);

	this.initDOMElems();
	
	// needed?
	this.resultSelectEvent = new YAHOO.util.CustomEvent("searchResultSelect", this);
	this.tagSelectEvent = new YAHOO.util.CustomEvent("tagSelect", this);
	
	if(resource_ID!="")
		this.resource_ID=resource_ID;
	if(user_ID!=""){
		this.user_ID=user_ID;
		this.getTagsURL = MACEConstants.componentsURL+"tagCloud/php/getUserTags.php";
	}
	else{
		this.getTagsURL = MACEConstants.componentsURL+"tagCloud/php/getPopularTags.php";
	}
	this.selectedTags=[];
	this.buildTagCloud();
}

TagCloud.extend(Component);

TagCloud.prototype.buildTagCloud = function(){

	var _this = this;
	var o={};
	if(this.resource_ID)
		o.resource_ID=this.resource_ID;
	if(this.user_ID)
		o.user_ID=this.user_ID;
	
	// TODO: get selected tags from app and mark them as selected on startup!
	
  	$.getJSON(this.getTagsURL,o,function (tagObj) {

			if(tagObj.length ==0){
		  		_this.messageBox.showMessage("No tags yet!");
			}
			
			else{
		  		_this.messageBox.hide();
		  		
		  		
				$.each(tagObj, function(i,obj) {

					_this.$element.append(	"<li><a id='popTag_"+i+"' href='' class='"+tagObj[i].stil+"'>"+tagObj[i].name+"</a></li>");
					$("#popTag_"+i).bind("click",	function() {
						if(_this.user_ID){
							if($(this).hasClass("selected")){
								$(this).removeClass("selected");
								app.myContentComponent.reset();
							} else {
								_this.$element.find("li a").removeClass("selected");
								$(this).addClass("selected");
								app.myContentComponent.filterResultsByTag(tagObj[i].name);					
							}
							
						}
						else{
							//ist das übertragbar?
							// -> risky, dann muss jede App, die das verwendet, auch ein doKeywordSearch etc. haben!
							// app.doKeywordSearch(tagObj[i].name);
							// app.$keywordInputField.val(tagObj[i].name);
						}
						// besser: events benutzen!
						console.log("onTagSelect");
						_this.tagSelectEvent.fire($(this));
						return false;
					});
				});
				
				_this.selectTagsByNames(_this.selectedTags);
			}
	});
}

// takes an array of tag names and marks tags with the respective labels as selected

TagCloud.prototype.selectTagsByNames=function(names){
	console.log("TagCloud.prototype.selectTagsByNames", typeof (names), names);
	try{				
		this.selectedTags=names;
		$("a", this.$element).removeClass("selected");
		
		$("a", this.$element).each(function(){
			if(Array.contains(names, $(this).text())){	
				console.log("selecting", $(this).text());
				$(this).addClass("selected");
				return;
			}
		});
	} catch (e){
		console.warn("error selecting tags by names", this.selectedTags);
	}
}

/*

	EVENTS 

*/

/**
 * Returns resultSelectEvent as events this component will broadcast.
 */

TagCloud.prototype.getBroadcastEvents = function() {
	console.log("TagCloud broadcasts: resultSelectEvent, tagSelectEvent");
	return [this.resultSelectEvent, this.tagSelectEvent];
}



TagCloud.prototype.initDOMElems = function(){
	this.messageBox = new MessageBox(this.$element.find("#messageBox"), "", MessageBox.TYPE_NONE, true);
	this.messageBox.showWorking("Loading tags...");
}

