
	
	$j.scf.behavior.add("smlItemComments", {
	"attach" : function(el, settings, jq) {
		var sml = $j.scf.smartlet.get(el);
		var sbm = $j("#" + el.id + " input[type=submit][name=" + sml.id('save') + "]");
		var prv = $j("#" + el.id + " input[type=submit][name=" + sml.id('prv') + "]");
		var cnl = $j("#" + el.id + " input[type=submit][name=" + sml.id('cancel') + "]");
		
		var commentBox = $j("#" + sml.id("txt"));
		var editOriginal;
		
		var frm = $j("#" + sml.id() + " form > .fieldset");
		frm.hide();
		
		var toggle = $j("<button type=\"button\"></button>");
		toggle.text("Add comment");
		
		toggle.click(function(){
			sml.set('commentParent',0);
			sml.set('editComment',0);
			
			//frm.appendTo(jq);
			toggle.after(frm);
			
			frm.show();
			frm[0].scrollIntoView();
			frm.find("textarea")[0].focus();
			toggle.hide();
		});
		frm.before(toggle);
		
		function initToggles(ch) {
			if (!ch) ch = $j("div.childcomment");
			ch.each(function(i){
				var chEach = $j(this);
				var togCh = $j("<a></a>");
				togCh.attr("title","Hide replies");
				
				togCh.addClass("smlTogHide");
				togCh.click(function(){
					var jThis = $j(this);
					if(jThis.hasClass("smlTogHide")) {
						jThis.removeClass("smlTogHide");
						jThis.addClass("smlTogShow");
						chEach.hide("normal");
						var replyCount = chEach.find("div.smlComment").length;
						jThis.attr("title","Show replies (" + replyCount + ")");
					} else {
						jThis.removeClass("smlTogShow");
						jThis.addClass("smlTogHide");
						
						// DSW; HACK: IE doesn't antialias properly after show.
						if(jQuery.browser.msie){
							chEach.show("normal", function(){this.style.removeAttribute('filter');});
						}else{
							chEach.show("normal");
						}
						jThis.attr("title","Hide replies");
					}
				});
				chEach.parent().children(".smlCommentSource").prepend(togCh);
			});
		}
		initToggles();
		
		function initButtonbar(el){
			el.find("span.smlButtons button[name='" + sml.id("commentParent") + "']").click(function(event){
				event.preventDefault();
				cancelComment();
				
				var parent = $j(this).scf.form.val();
				sml.set("commentParent", parent);
				sml.set("editComment", 0);
				
				$j("#comment" + parent + " > div.smlCommentBody").after(frm);
				frm.show();
				frm.find("textarea")[0].focus();
				toggle.show();
				$j("#comment0").remove();
			});
			
			el.find("span.smlButtons button[name='" + sml.id("editComment") + "']").click(function(event){
				event.preventDefault();
				cancelComment();
				
				$j("#" + sml.id("editorNote")).show();
				$j("label[for='" + sml.id("editorNote") + "']").show();
				
				var edt = $j(this).scf.form.val();
				sml.set("editComment", edt);
				sml.set("commentParent", 0);
				
				editOriginal = $j("#comment" + edt + " > div.smlCommentBody").html();
				
				$j("#comment" + edt + " > div.smlCommentBody").after(frm);
				frm.show();
				frm.find("textarea")[0].focus();
				toggle.show();
				
				sml.ajax({
					busy : frm,
					ready: function(o) {
						commentBox.val(sml.get("commentBody"));
					}
				});
				
			});
			
			el.find("span.smlButtons button[name='" + sml.id("activate") + "']").click(function(event){
				event.preventDefault();
				cancelComment();
				
				var btn = this;
				var comment = $j(this).scf.form.val();
				sml.set('activate',comment);
				sml.ajax({
					busy : $j("#comment" + comment),
					ready: function(o) {
						$j(btn).remove();
						$j("#comment" + comment + " .smlButtons strong").text("Active");
						$j("#comment" + comment).removeClass("smlInactive");
					}
				});
			});
			
			el.find("span.smlButtons button[name='" + sml.id("deactivate") + "']").click(function(event){
				event.preventDefault();
				cancelComment();
				
				var btn = this;
				var comment = $j(this).scf.form.val();
				sml.set('deactivate',comment);
				sml.ajax({
					busy : $j("#comment" + comment),
					ready: function(o) {
						$j(btn).remove();
						if(sml.get("showInactive")){
							$j("#comment" + comment + " .smlButtons strong").text("Inactive");
							$j("#comment" + comment).addClass("smlInactive");
						} else $j("#comment" + comment).hide();
					}
				});
			});
			
			el.find("span.smlButtons button[name='" + sml.id("delete") + "']").click(function(event){
				event.preventDefault();
				cancelComment();
				
				if(confirm("Delete this comment?")) {
					var comment = $j(this).scf.form.val();
					sml.set("delete", comment);
					sml.ajax({
						busy : $j("#comment" + comment),
						ready: function(o) {
							$j("#comment" + comment).hide();
						}
					});
				}
			});
		}
		
		initButtonbar(jq);
		
		var addBtn = $j("<button type=\"button\">" + sbm.val() + "</button>");
		var cnlBtn = $j("<button type=\"button\">" + cnl.val() + "</button>");
		var prvBtn = $j("<button type=\"button\">" + prv.val() + "</button>");
		
		sbm.replaceWith(addBtn);
		prv.replaceWith(prvBtn);
		cnl.replaceWith(cnlBtn);
		
		function cancelComment() {
			toggle.show();
			$j("#" + sml.id("editorNote")).hide();
			$j("#" + sml.id("editorNote")).val("");
			$j("label[for='" + sml.id("editorNote") + "']").hide();
			
			frm.hide();
			commentBox.val("");
			sml.set("commentBody", "");
			$j("#comment0").remove();
			$j(".smlPreview").removeClass("smlPreview");
			if(editOriginal!=null) {
				if(sml.get("editComment") != 0) {
					$j("#comment" + sml.get("editComment") + " > div.smlCommentBody").html(editOriginal);
				}
				editOriginal = null;
			}
		}
		
		cnlBtn.click(function() {
			cancelComment();
		});
		
		var postBack = function(preview) {
			sml.set("preview", preview);
			var comment = jQuery.trim(commentBox.val());
			if (comment.length == 0) return;
			
			var noteBox = $j("#" + sml.id("editorNote") + ":visible");
			if (noteBox.length != 0){
				var note = jQuery.trim(noteBox.val());
				if (note.length == 0 && !preview) {
					alert("Please enter a reason for the edit in the note field");
					return;
				}
				sml.set("editorNote", note);
			}
			
			sml.set("commentBody", comment);
			
			sml.ajax({
				busy : frm,
				ready: function(o) {
					if (o.lastError){
						alert(o.lastErrorMessage);
						return;
					}
					
					if(!preview){
						cancelComment();
					}
					
					if (sml.get("editComment") == 0) {
						$j("#comment0").remove();
						
						if (preview){
							var nw = $j("<div />");
							nw.addClass("smlComment");
							
							nw.addClass("smlPreview");
							nw.attr("id","comment" + sml.get("commentId"));
							
							nw.append("<div />");
							bd=nw.find("div");
							bd.addClass("smlCommentBody");
							bd.append(sml.get("commentBody"));
							
							var hd = $j("<div />");
							hd.addClass("smlCommentSource");
							nw.prepend(hd);
							var src = $j("<span />");
							
							src.addClass("smlCommentSource");
							src.text(sml.get("userName") + " - " + sml.get("addDate"));
							hd.append(src);
						
							// frm.before(nw);
							if(sml.get("commentParent") == 0){
								toggle.before(nw);
							} else {
								nw.addClass("childcomment");
								frm.before(nw);
							}
							
						} else {
							var nw = $j(sml.get("commentResult"));
							initButtonbar(nw);
							
							if(sml.get("commentParent") == 0) {
								toggle.before(nw);
							} else {
								$j("#comment" + sml.get("commentParent") + " button[name='" +  sml.id("delete") + "']").hide();
								
								var chld = $j("#comment" + sml.get("commentParent") + " > div.childcomment");
								if(chld.size() == 0) {
									chld = $j("<div class=\"childcomment\">");
									$j("#comment" + sml.get("commentParent")).append(chld);
									initToggles(chld);
								}
								chld.append(nw);
							}
							nw[0].scrollIntoView();
						}
					} else {
						if(preview) {
							
							// TODO: translate
							var edHist = $j("<div class=\"smlEditHistory\"><div class=\"smlTimestamp\"></div><div class=\"smlNote\"></div></div>");
							
							var s = "Edited by [pl_user] on [pl_date]";
							s = s.replace(/\[pl_user\]/, sml.get("userName"));
							s = s.replace(/\[pl_date\]/, sml.get("addDate"));
							edHist.find(".smlTimestamp").html(s);
							edHist.find(".smlNote").text(sml.get("editorNote"));
							
							$j("#comment" + sml.get("editComment")).addClass("smlPreview");
							var bd = $j("#comment" + sml.get("editComment") + " > div.smlCommentBody");
							bd.html(sml.get("commentBody"));
							bd.append(edHist);
						} else {
							var edcm = $j("#comment" + sml.get("editComment"));
							edcm.removeClass("smlPreview");
							if (sml.get("newCommentStatus") != "ACTIVE") edcm.addClass("smlInactive");
							
							var nw = $j(sml.get("commentResult")).find(" > div.smlCommentBody");
							edcm = edcm.find(" > div.smlCommentBody");
							edcm.html(nw.html());
						}
					}
				}
			});
		}
		
		prvBtn.click(function() {
			postBack(true);
		});
		
		addBtn.click(function() {
			postBack(false);
		});
	}
});

$j.scf.behavior.add("bbCodeToolbar", {
	"autoAttach" : "form.CoolForm span.smlBBCoder",
	"attach" : function(el, settings, jq) {
	
		$j("a.smlPopup").click(function(event){
			event.preventDefault();
			
			var href = this.href;
			var options = {};
			options.html = "<iframe width=\"100%\" height=\"100%\" src=\"" + href + "\" />";
			options.width = 640;
			options.height = 480;
			$j(this).scf.popup(options);
		});
		
		jq.show();
		
		var jTxt = $j("#" + $j.scf.smartlet.get($j("div.sml_itemcomments").get(0)).id("txt"));
		var txt = jTxt.get(0);
		
		function enumProps(o) {
			var s = "";
			for(var p in o) {
				s += p + " -\t" + o[p] + "\n";
			}
			return s;
		}
		
		if (document.selection) {
			txt.onclick = function() {
				this.oldSelection = document.selection.createRange();
			}
			txt.onkeyup = function() {
				this.oldSelection = document.selection.createRange();
			}
		}
		
		txt.surroundSelection =  function(before,after) {
			this.replaceSelection(before + this.getSelection() + after);
		}
		
		txt.replaceSelection = function(newVal) {
			if (document.selection) { // IE
				var tr = document.selection.createRange();
				
				if (tr.parentElement && tr.parentElement() == this) {
					tr.text = newVal;
				} else {
					if (this.oldSelection) {
						this.oldSelection.text = newVal;
					} else {
						this.value += newVal;
					}
				}
			} else if (this.selectionStart || this.selectionStart == '0') {
				var startPos = this.selectionStart;
				var endPos = this.selectionEnd;
				var s = this.value.substring(0, startPos)
					+ newVal
					+ this.value.substring(endPos, this.value.length);
				this.value = s;
			} else { // non-IE
				this.value += newVal;
			}
		}
		
		txt.getSelection = function() {
			if (document.selection) { // IE
				var tr = document.selection.createRange();
				if (tr.parentElement && tr.parentElement() == this) {
					return tr.text;
				}
			} else if (this.selectionStart || this.selectionStart == '0') {
				var startPos = this.selectionStart;
				var endPos = this.selectionEnd;
				return this.value.substring(startPos, endPos);
			}
			return "";
		}
		
		jq.find("img").css("border","2px outset white");
		jq.find(".bbBold").click(function(){
			txt.surroundSelection("[b]","[/b]");
		});
		jq.find(".bbItal").click(function(){
			txt.surroundSelection("[i]","[/i]");
		});
		jq.find(".bbUrl").click(function(){
			var url = prompt("Please enter a URL","http://");
			if (url.length != 0 && url != "http://") {
				if (txt.getSelection().length == 0){
					txt.surroundSelection("[url]" + url + "[/url]","");
				} else {
					txt.surroundSelection("[url=" + url + "]","[/url]");
				}
			}
		});
		jq.find(".bbList").click(function(){
			var sel = txt.getSelection();
			sel = sel.replace(/\n/g, "\n[*]");
			txt.replaceSelection("[list]\n[*]" + sel + "\n[/list]");
		});
		jq.find(".bbQuot").click(function(){
			var quot = "";
			if(document.selection) {
				quot = document.selection.createRange().text;
			} else if (window.getSelection) {
				quot = window.getSelection();
			} else if (document.getSelection) {
				quot = document.getSelection().toString();
			}
			txt.surroundSelection("[quote]" + quot + "[/quote]","");
		});
		jq.find(".bbCode").click(function(){
			txt.surroundSelection("[code]","[/code]");
		});
	}
});

