$(document).ready(function() {
	//tagWidget
	//cookie?
	 user_id=readCookie('dgtf_userid');
	console.log("user--:"+user_id+"-----p_id:"+projekt_id);
	  if(user_id != null && projekt_id!=""){
	  	var url="../code/tags/get_suggest_tags.php";
	  	$.getJSON(url,{},function (suggest_tags) {
	  		//console.log("suggest_tags--:"+suggest_tags.join());
	  		projekt_tag_wdg=new Tag_Widget(suggest_tags,projekt_id);
	  		projekt_tag_wdg.init();
	  	});
  
	  }
	  else{
	  	projekt_tag_wdg=new Tag_Widget([],projekt_id);
	  	projekt_tag_wdg.init();
	  }
});

function Tag_Widget(usertags,content_id) {
	
	
	// server URLs
	this.get_tagsServiceURL = "../code/tags/get_tags_json.php";

	this.save_tagsServiceURL ="../code/tags/addtags.php";
	this.get_suggest_tagsServiceURL ="../code/tags/get_suggest_tags.php";
	// data values
	this.suggest_tags = usertags;
	this.contentToTag = content_id;
	//flags/helper
	this.tags_EnterFlag=true;

	// assign concrete elements
	this.tag_input =  $("#tags") ;
	this.tag_cloud =  $("#tagcloud") ;
	this.add_button =  $("#addTag_button") ;
	this.tag_input_container =  $("#tags_in") ;
	this.tagwdg =  $("#tagwdg") ;

	
}
Tag_Widget.prototype.init = function(){

	var t = this;
	///submit on Enter
	this.tag_input.keydown(function(event){
	
		if(event.keyCode ==13 && autocompleter_flag==false) {
		
			t.add_tags();
			t.tags_EnterFlag=false;
		}
	});
	//cookie?
	if(user_id ==null){
		this.tag_input_container.hide();
	}
	else{
		this.add_button.click(function() {
			t.add_tags();
			return false;
		});
		this.init_suggestions();
	}
	this.build_tagcloud();
}

Tag_Widget.prototype.init_suggestions= function(){

	
	var w=this.tag_input.width();
	var t = this;
	
	this.tag_input.autocomplete(this.suggest_tags, {

		width: w,
		minChars: 1,
		matchContains: false,
		max: 50,

		// format to lookup/search
		formatItem: function(row, i, max) {
			t.tags_EnterFlag=false;
			autocompleter_flag=true;
			return row.tag;
		},

		// format to show in suggestion list
		formatItemToShow: function(row, i, max) {
			return row.tag +"("+row.use+")";
		}

	});
	this.tag_input.bind("result", function(event, data, formatted) {
		console.log("autocompleter---result");
	});
}
	
	//
Tag_Widget.prototype.build_tagcloud= function(){

  	var o={};
  	o.p_id=this.contentToTag;
	
	var t = this;

  	$.getJSON(this.get_tagsServiceURL,o,function (tagObj) {
		if(tagObj.tags!=0){
			t.tag_cloud.html("");
			$.each(tagObj, function(i,obj) {

				t.tag_cloud.append(	"<span class='"+tagObj[i].stil+"'> "+tagObj[i].name+"</span>");
				//	t.tag_cloud.append(	"<a href='pattern.php?0,0,82,0,0,82&qs="+tagObj[i].name+"&first=1' class='"+tagObj[i].stil+"'> "+tagObj[i].name+"</a>");
				t.tag_input.val("");
			});
		}
		else{
			if(user_id == null)
				t.tagwdg.css({display: 'none'});
				
		}
		
	});
}
//
Tag_Widget.prototype.add_tags= function(){
	console.log("add_tags--"+this.contentToTag);
  	var o={};
  	o.p_id=this.contentToTag;
	o.tags=this.tag_input.val();

	var t = this;
	
  	$.getJSON(this.save_tagsServiceURL,o,function (feedback) {
		if(feedback.ok){
			t.build_tagcloud();
			$("#keywords").hide();
		}
		else
			t.tag_input.val(feedback.msg);
	});
}
