String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,''); }

YAHOO.util.Event.onDOMReady(function() {	
	var inputs = document.getElementsByTagName("input");
	// set up date pickers for date fields
	for(var i = 0; i < inputs.length; i++) {
		var input = inputs[i];
		// if the browser natively supports "date" types, don't use the JS date picker
		if(input.getAttribute("type") == "date" && input.type == "text" && typeof YAHOO.widget.Calendar != "undefined") {
			var id = input.id;
			var holderid = "holder_" + id;
			input._holder = document.getElementById(holderid);
			input._holder.style.display = "none";
			// create the YAHOO calendar object
			input._calendar = new YAHOO.widget.Calendar(false, holderid, { close: true, navigator: true, iframe: true });
			input._calendar._input = input;
			input._calendar.selectEvent.subscribe(function(type, dates, calendar) {
				var dates = calendar.getSelectedDates();
				var date = dates[0];
				var yearstring = date.getFullYear();
				var monthstring = date.getMonth() + 1;
				if(monthstring < 10) monthstring = "0" + monthstring;
				var datestring = date.getDate();
				if(datestring < 10) datestring = "0" + datestring;
				calendar._input.value = yearstring + "-" + monthstring + "-" + datestring;
				calendar.hide();
				calendar._input._holder.setAttribute("_stamp", date.getTime());
				calendar._input._holder.style.display = "none";
			}, input._calendar);
			input._button = document.createElement("input");
			input._button.type = "button";
			input._button.value = "Choose...";
			input.parentNode.appendChild(input._button);
			input._button._input = input;
			YAHOO.util.Event.addListener(input._button, "click", function(event, obj) {
				this._input._holder.style.display = "block";
				this._input._holder.style.position = "absolute";
				if(this._input._holder.getAttribute("_stamp")) { 
					var date = new Date(parseInt(this._input._holder.getAttribute("_stamp"))); 
				} else { 
					var date = new Date(); 
				}
				this._input._calendar.cfg.setProperty("pagedate", (date.getMonth()+1) + "/" + date.getFullYear());
				this._input._calendar.cfg.setProperty("selected", (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear());
				this._input._calendar.render();	
			}, input._button, input._button); 
			
		}
	}
	
	var tas = document.getElementsByTagName("textarea");
	for(var i = 0; i < tas.length; i++) {
		var ta = tas[i];
		if(ta.getAttributeNode("_html")) {
			if(typeof YAHOO.widget.Editor != "undefined") {
				ta.value = ta.getAttribute("_html");
				ta.editor = new YAHOO.widget.Editor(ta.id, {
					height: parseInt(ta.style.height) > 0 ? ta.style.height : (ta.rows ? ta.rows + "em" : "350px"),
					width: "100%",
					toolbar: {
						/*titlebar: 'My Editor',*/
						buttons: [
						{ group: 'textstyle', label: "Formatting", 
							buttons: [
							{ type: 'push', label: 'Bold', value: 'bold' },
							{ type: 'push', label: 'Italic', value: 'italic' },
							{ type: 'separator' }
							]
						}, 
						{ group: 'lists', label: "Lists", 
							buttons: [
							{ type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' }, 
							{ type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' }, 
							{ type: 'separator' }
							]
						}, 
						{ group: 'insertlink', label: "Insert Link", 
							buttons: [
							{ type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: true }]
						}
						]
					},
					animate: true,
					extracss: "html, body { margin: 0; padding: 0; height: 100%; border: 0; }  " +
						"body { " +
						" height: 99%; " +
						"margin: 0; " +
						"padding: 1px !important; /* 1px padding prevents collapsing margins */ " +
						"background: white; " +
						"font-size: 10pt; " +
						"font-family: Helvetica, Arial, sans-serif; " +
						"} " +
						"ol ol li { list-style-type: upper-alpha; } " +
						"ol ol ol li { list-style-type: lower-roman; } " +
						"ol ol ol ol li { list-style-type: lower-alpha; } " +
						"img { display: block; margin: 1em auto; border: 1px solid black; }  ",
					handleSubmit: true
				});
				ta.editor._ta = ta;
				ta.editor._dropImage = function(event, editorDocument) {
					var imgs = editorDocument.getElementsByTagName("img");
					for(var i = 0; i < imgs.length; i++) {
						var img = imgs[i];
						var parent = img;
						while(parent.parentNode != editorDocument.body) {
							parent = parent.parentNode;
						}
		// 				if(parent != img) {
		// 					// move the img to after the parent
		// 					if(parent.nextSibling) { 
		// 						parent.parentNode.insertBefore(img, parent.nextSibling);
		// 					} else {
		// 						parent.parentNode.appendChild(img);
		// 					}
		// 				}
						var src = img.src;
						src = src.replace(/\d+$/, 350);
						img.src = src;
					}
				}
				ta.editor.on("editorContentLoaded", function(event, editor) {
					doc = editor._getDoc();
				// 	doc.addEventListener("click", foo, false);
					// alert(editor._ta);
					document.getElementById(editor._ta.id + "_html").value = 1;
					YAHOO.util.Event.addListener(doc, "click", editor._dropImage, doc);
					YAHOO.util.Event.addListener(doc, "drop", editor._dropImage, doc);
					YAHOO.util.Event.addListener(doc, "dragdrop", editor._dropImage, doc);
					YAHOO.util.Event.addListener(doc, "paste", editor._dropImage, doc);
					YAHOO.util.Event.addListener(doc, "beforeeditfocus", editor._dropImage, doc);
					doc._somethingChanged = function(event, doc) {
						// this only needs to be fired once to prove that something has changed
						window._needsConfirmation = true;
						YAHOO.util.Event.removeListener(doc, "keypress", doc._somethingChanged);
					}
					YAHOO.util.Event.addListener(doc, "keypress", doc._somethingChanged, doc);
					
				}, ta.editor);
				if(ta.name == "body") { // only do this for the editor in the edit post screen
					ta.editor.on("toolbarLoaded", function() {
						this.toolbar.addButtonGroup({ group: 'insertimage', label: "Images",
							buttons: [
								{ 
								type: 'push', label: 'How to insert images', value: 'insertimage'
								}
							]
						});
					
						var button = this.toolbar.getButtonByValue("insertimage");
						var element = button.get("element");
						button._tooltip = new YAHOO.widget.Panel("insertimagePanel", {
							width: "400px",
							context: [element, "tl", "bl", ["beforeShow"]], 
							visible: false, 
							iframe: true, 
							close: true,
							constraintoviewport: true
						});
						button._tooltip.setHeader("Inserting Images");
						button._tooltip.setBody("<p>To upload images for this post, click <b>Browse</b> under <b>Add a Photo</b> on the right.</p><p>After you have uploaded images, you can drag images from the <b>Photo Gallery</b> list on the right into the body of your post.  The images will automatically be resized and centered.  To edit more information about each image, click the <b>Edit Photo Gallery</b> tab above</p>");
						button._tooltip.render();
						
						this.toolbar.on("insertimageClick", function() {
							var button = this.getButtonByValue("insertimage");
							if(button._tooltip) {
								if(button._tooltip.cfg.getProperty("visible")) {
									button._tooltip.hide();
								} else {
									button._tooltip.show();
								}
							}
							return false;
						});
					}, ta.editor, true);
				}
				
				
				ta.editor.render();
				
				ta.editor.on("removeImage", function(event, editor) {
					var uniqid = event.id;
					var editorDocument = editor._getDoc();
					var imgs = editorDocument.getElementsByTagName("img");
					var runagain = false;
					var imagearray = [];
					for(var i = 0; i < imgs.length; i++) { imagearray.push(imgs[i]); }
					for(var i = 0; i < imagearray.length; i++) {
						var img = imagearray[i];
						if(img.getAttribute("_uniqid") == uniqid) {
							imagearray[i] = img.parentNode.removeChild(img);
							runagain = true;
						}
					}
					if(runagain) editor.fireEvent("removeImage", { id: uniqid });
				}, ta.editor);	
			}
		}
	}
	showLinkButtons();
});

function showLinkButtons(contextel) {
	if(!contextel) contextel = document.body;
	var links = YAHOO.util.Dom.getElementsByClassName("link-button", "a", contextel);
	for(var i = 0; i < links.length; i++) {
		var link = links[i];
		if(!link._button) {
			link._button = new YAHOO.widget.Button(link, { type: "link" });
		}
	}
	
}

