var HH = new Object();

HH.editor = {

	e_attributes: Array(
	Array("Bold Text", "bold.gif", "[b]", "[/b]"),
	Array("Italicize Text", "italic.gif", "[i]", "[/i]"),
	Array("Underline Text", "underline.gif", "[u]", "[/u]"),
	false,
        Array("Justify Left", "left.gif", "[left]", "[/left]"),
        Array("Justify Center", "center.gif", "[center]", "[/center]"),
        Array("Justify Right", "right.gif", "[right]", "[/right]"),
	false,
	Array("Add Link", "link.gif", "link"),
        Array("Add Image", "image.gif", "image"),
        Array("Image Library", "imagelib.gif", "imagelib")
	),
	
	e_custom: Array(
	"image",
        "imagelib",
	"link"
	),
        
        e_buttons: "<label>\n%\n</label>",
	
	e_validate: function(){
	    if (!document.getElementById) HH.editor = false;
	},
	
	e_textarea: new String(),
	e_input_text: new String(),
    
	e_text: function(){
		return document.getElementById(HH.editor.e_textarea);	
	},
        
        e_load: function(textarea)
	{
		HH.editor.e_validate();
		
		HH.editor.e_textarea = textarea;
		
		var buttons = new String();
		
		for (i = 0; i < HH.editor.e_attributes.length; i += 1)
		{
			if (typeof(HH.editor.e_attributes[i]) == "object")
			{
				var tags = (HH.editor.e_attributes[i][3] == null) ? "'" + HH.editor.e_attributes[i][2] + "'" : "'" + HH.editor.e_attributes[i][2] + "', '" + HH.editor.e_attributes[i][3] + "'";
				
                                buttons += '<img src="http://habbohut.com/staff-hut/library/images/editor/' + HH.editor.e_attributes[i][1] + '" alt="' + HH.editor.e_attributes[i][0] + '" title="' + HH.editor.e_attributes[i][0] + '" onclick="HH.editor.e_tag(' + tags + '); return false;" />\n';
                                	
				//buttons += '<input title="' + HH.editor.e_attributes[i][0] + '" type="button" '
				//+ 'style="background-image:url(\'' + HH.editor.e_attributes[i][1] + '\');" '
				//+ 'onclick="HH.editor.e_tag(' + tags + '); return false;" />\n';
			}
			else
			{
				buttons += '</label>\n<label>\n';	
			}
		}
		
		HH.editor.e_buttons = HH.editor.e_buttons.replace(/%/, buttons);
	},
        
        e_tag: function(start, end){
		if (!start && !end) return false;
		
		for (i = 0; i < HH.editor.e_custom.length; i += 1)
		{
			if (start == HH.editor.e_custom[i])
			{
				HH.editor.e_tag_c(start);
				return false;
			}
		}
		
		if (!end)
			HH.editor.e_tag_s(start);
		else
			HH.editor.e_tag_w(start, end);
	},
        
        e_tag_c: function(start, end){
		switch (start){
			case "image":
			
				var ask = confirm("Do you wish to add an image?");
				if (!ask) return false;
				
				var builtImage = "[img";
				
				var url = prompt("Enter the url of the image", "");
				if (url == "" || url == null) return false;
				builtImage += "=" + url + "]";
                                
                                var ali = prompt("Type 'left' or 'right' to align the image. Leave this blank if you do not wish to align.", "");
                                builtImage += (ali == "" || ali == null) ? '' : ali;
				
				
				builtImage += "[/img]";
				
				return HH.editor.e_tag(builtImage);
				
			break;
			
			case "link":
			
				var ask = confirm("Do you wish to add a link?");
				if (!ask) return false;
				
				var href = prompt("Enter link url", "http://");
				if (href == "http://" || href == null) return false;
				
				var value = prompt("Enter link value (optional)", "");
				return HH.editor.e_tag((value != null && value != "") ? "[url=" + href + "]" + value + "[/url]" : "[url]" + href + "[/url]");
				
			break;
                    
                        case "imagelib":
                            $('overlay').toggle();
                            $('overlay').innerHTML = $('imagelib').innerHTML;
                        break;
		}
	},
        
        e_tag_s: function(start)
	{
		var textarea = HH.editor.e_text();

		textarea.focus();
		
		if (textarea.setSelectionRange)
		{		
			var cursorPos = textarea.selectionStart;
			
			var s1 = textarea.value.substr(0, cursorPos);
			var s2 = textarea.value.substr(cursorPos);
			
			cursorPos += start.length;
			
			return textarea.value = s1 + start + s2, textarea.selectionStart = cursorPos, textarea.selectionEnd = cursorPos;
		}
		
		if (textarea.isTextEdit)
		{
			var text = document.selection.createRange().text;
			return document.selection.createRange().text = start + text;
		}
		
		return textarea.value += start;
	},
        
        e_tag_w: function(start, end)
	{
		var textarea = HH.editor.e_text();
		
		textarea.focus();
		
		if (textarea.setSelectionRange)
		{
			var cursorStart = textarea.selectionStart;
			var cursorEnd = textarea.selectionEnd;

			var s1 = textarea.value.substr(0, cursorStart);
			var s2 = textarea.value.substr(cursorStart, (cursorEnd - cursorStart));
			var s3 = textarea.value.substr(cursorEnd);
			
			cursorStart += start.length + s2.length + end.length;
			
			return textarea.value = s1 + start + s2 + end + s3, textarea.selectionStart = cursorStart, textarea.selectionEnd = cursorStart;
		}
		
		if (textarea.isTextEdit)
		{
			var text = document.selection.createRange().text;
			return document.selection.createRange().text = start + text + end;
		}
		
		return textarea.value += start + end;
	},
        
        e_write: function()
	{
		document.write(HH.editor.e_buttons)
	},
        
        preview: function()
	{
		$('overlay').toggle();
                var url = 'http://habbohut.com/staff-hut/library/class.editor.preview.php';
// notice the use of a proxy to circumvent the Same Origin Policy.

new Ajax.Request(url, {
  method: 'post',
  parameters: $('acontent').serialize(true),
  
  onSuccess: function(obj)
        {
            $('overlay').innerHTML = obj.responseText;
        }
});
	}
	
};
