(function($){
	$.fn.ATTagging = function(){
		return this.each(function(){
			$(this).bind("click",function(){ShowTaggingWindow(this);return false;});

			function ShowTaggingWindow(cnt){
				var url = jQuery(cnt).attr("href");
				try{
					window.radopen(url, "ATCCTaggingWindow");
				}catch(e){alert(e)}
                return false;
			}
		});
	};
})(jQuery);

(function($){
	$.fn.ATTagEditor = function(suggestSelector, options){
		return this.each(function(){
			var t = new $.ATTagEditor(this, suggestSelector, options)
		});
	}
	
	$.ATTagEditor = function(textbox, suggestSelector, options){
		if(!options){ options = {};}
		if(!options.sepText){ options.sepText = ","; }
		if(!options.usedTagClass){ options.usedTagClass = "UsedTag"; }
		if(!options.usedTagDecorator){ options.usedTagDecorator = usedTagDecorator;}
		
		var $textbox = $(textbox);
		var $suggestions = $(suggestSelector);
		
		$textbox.bind("change", textboxChanged)
				.bind("keyup", textboxChanged)
				.bind("keypress", textboxKeyPress);
		$suggestions.bind("click", suggestionClick);

		$textbox.trigger("change");

		function textboxKeyPress(e){
			if( e.keyCode && e.keyCode == 13){
				if(e.stopPropagation) e.stopPropagation()
				$(".ButtonSave").each(function(){
					if( this.tagName == "A" ){
						var href = $(this).attr("href");
						if(href.indexOf("javascript:") > -1 ){
							href = href.substring("javascript:".length);
							try{eval(href);}catch(e){alert(e)}
						}
					}
				});
				return false;
			}
		}

		function textboxChanged(e){
			var tags = $textbox.val().toLowerCase().split(options.sepText);
			for( i=0; i < tags.length;  i++)
			{
				tags[i] = $.trim(tags[i]);
			}
			$suggestions.each(function(){
				var $this = $(this);

				var text = $this.text().toLowerCase();
				options.usedTagDecorator(this, true);

				if($.inArray(text, tags) != -1)
					options.usedTagDecorator(this, false);
			});
		};

		function suggestionClick(){
			var tagText = $textbox.val().toLowerCase();
			var tags = tagText.split(options.sepText);
			for( i=0; i < tags.length;  i++)
			{
				tags[i] = $.trim(tags[i]);
			}
			$this = $(this);
			var text = $this.text().toLowerCase();
			if($.inArray(text, tags) != -1){
				tagText = GetTagsString(tags, text, "");
			}else{
				tagText = GetTagsString(tags, "", text);
			}

			$textbox.val(tagText);
			$textbox.trigger("change");
		};
		
		function GetTagsString(tags, removeTag, addTag){
			var retVal="", i=0;
			for(i=0; i < tags.length; i++){
				var tag = tags[i];
				if(tag != "" && tag != removeTag) retVal += tag + options.sepText;
			}
			if(addTag != "") retVal += addTag + options.sepText;
			return retVal;
		}

		function usedTagDecorator(target, remove){
			if(remove == true){$(target).removeClass(options.usedTagClass); return;}
			$(target).addClass(options.usedTagClass);
		}
	}
})(jQuery);

/*"Tag this item" link in detail view*/
jQuery(document).ready(function(){
	jQuery('.ATCCTags').ATTagging();
});

/*Tag cloud process*/
jQuery(document).ready(function(){
	var minVal = -2;
	var maxVal = 0;
	jQuery(".CloudContainer a").each(function(){
		var temp = parseInt(jQuery(this).attr("count"));
		if (minVal === -2) {minVal = temp;};
		if (temp < minVal) {minVal = temp;}else if(temp > maxVal){maxVal = temp;}
	}).each(function(){
		var weight = 2;

		if( maxVal != minVal ){
			var diff = (maxVal - minVal)/5;
			if( isNaN(diff) || diff == 0) diff = 1;
			var temp = parseInt(jQuery(this).attr("count"));
			try{
				weight = parseInt((temp - minVal) / diff);
			}catch(e){weight = 0}
		}
		jQuery(this).addClass("CloudLevel" + weight);
	});
});

function ATCCTagRefreshPage(){
	document.location = document.location;
}