

/*  OBJECT EXTENSIONS  */

String.prototype.trimLeft = function() { return this.replace(/^\s+/,'') }
String.prototype.trimRight = function() { return this.replace(/\s+$/,'') }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,'') }
String.prototype.insert = function(idx,value) { return this.slice(0,idx) + value + this.slice(idx) }

Object.prototype.registerEvent = function(sEvent, fListener, bUseCapture) {
	var result;
	if (this) {
		if ((typeof(this.addEventListener) == 'function') && (typeof(fListener) == 'function')) {
			this.addEventListener(sEvent, fListener, !!bUseCapture);
			result = true;
		}
		else if ((typeof(this.attachEvent) == 'function') && (typeof(fListener) == 'function')) {
			result = this.attachEvent("on" + sEvent, fListener);
		}
		else {
			this["on" + sEvent] = fListener;
			result = (this["on" + sEvent] == fListener);
		}
	}
	return result;
}



//  PROTOTYPE.JS EXTENSIONS

Object.extend(Element, {
	toggleClassName: function(element,className1, className2) {
		if (Element.hasClassName(element, className1)) {
			Element.removeClassName(element, className1);
			if (className2) {
				Element.addClassName(element, className2);
			}
		}
		else {
			Element.addClassName(element, className1);
			if (className2) {
				Element.removeClassName(element, className2);
			}
		}
	}
})



//  SCRIPTACULOUS EXTENSIONS

//  add markdown syntax links to edit in place text areas
Object.extend(Ajax.InPlaceEditor.prototype, {
	createForm: function() {
	  this.form = document.createElement("form");
	  this.form.id = this.options.formId;
	  Element.addClassName(this.form, this.options.formClassName);
	  this.form.onsubmit = this.onSubmit.bind(this);

	  this.createEditField();

	  if (this.options.textarea) {
	    var br = document.createElement("br");
	    this.form.appendChild(br);
	  }

	  if (this.options.instructions) { // mods: add documentation links
	    documentation = document.createElement("div");
	    documentation.className = "documentation";
	    documentation.appendChild(document.createElement("span").appendChild(document.createTextNode("This field uses Markdown for formatting. ")));
	    cheatLink = document.createElement("a");
	    cheatLink.appendChild(document.createTextNode("cheat sheet"));
	    cheatLink.href = "javascript:popup('/Mackay/content/markdown_help');";
//	    cheatLink.setAttribute("onclick", "popup('/Mackay/content/markdown_help');return false;");
	    documentation.appendChild(cheatLink);
	    documentation.appendChild(document.createElement("span").appendChild(document.createTextNode(" | ")));
	    fullLink = document.createElement("a");
	    fullLink.appendChild(document.createTextNode("documentation"));
	    fullLink.href = "http://daringfireball.net/projects/markdown/syntax";
	    fullLink.target = "_blank";
	    documentation.appendChild(fullLink);
	    this.form.appendChild(documentation);
	  }
	  if (this.options.okButton) {
	    okButton = document.createElement("input");
	    okButton.type = "submit";
	    okButton.value = this.options.okText;
	    okButton.className = 'editor_ok_button';
	    this.form.appendChild(okButton);
	  }

	  if (this.options.cancelLink) {
	    cancelLink = document.createElement("a");
	    cancelLink.href = "#";
	    cancelLink.appendChild(document.createTextNode(this.options.cancelText));
	    cancelLink.onclick = this.onclickCancel.bind(this);
	    cancelLink.className = 'editor_cancel';      
	    this.form.appendChild(cancelLink);
	  }
	}
})

// allow html in in place editor
Object.extend(Ajax.InPlaceEditor.prototype, {
	onLoadedExternalText: function(transport) {
	  Element.removeClassName(this.form, this.options.loadingClassName);
	  this.editField.disabled = false;
	  this.editField.value = transport.responseText;
	  Field.scrollFreeActivate(this.editField);
	}
})



/*  / OBJECT EXTENSIONS  */


//  popup markdown cheat sheet
popup = function(path, width, height) {
	if (path == undefined)   alert('no path specified to popup');
	if (width == undefined)  width = 450;
	if (height == undefined) height = 400;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('" + path + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height + "');");
}




/*  FORM HELPERS  */

// takes a string, looks for a div id'd as form_status, blinds it up, swaps the text and blinds it down. if no str, doesn't blind down. if alert == false, will not set the text to class="alert"
set_status = function(str, add_alert_class) {
	var bdown = function() { return };  // callback used if str is undefined
	if (add_alert_class == undefined) add_alert_class = true;
	if (str != undefined) { // wrap the text in a p
		if (add_alert_class) {
			form_status_str = '<p class="alert">' + str + '</p>';
		} else {
			form_status_str = '<p>' + str + '</p>';
		}
		bdown = function() { // callback function to show the text
			Element.update('form_status', form_status_str);
			Effect.BlindDown('form_status', {duration: 0.3})
		}
	}
	if (Element.visible('form_status')) { // there's currently text showing, so hide it
		Effect.BlindUp('form_status', {duration: 0.3, afterFinish: bdown})
	} else {
		bdown();
	}
}

// adds a class to all fields passed in field_names array, adding a prefix if defined
highlight_form_fields = function(field_names, field_prefix, css_class) {
	if (css_class == undefined) {
		css_class = "alert";
	}
	if ((field_prefix != undefined) && (field_prefix != ''))
		field_prefix += "_";
	field_names = eval(field_names);
	for (var i=0; i<field_names.length; i++) {
		Element.addClassName(field_prefix + field_names[i], css_class);
		Element.addClassName(field_prefix + field_names[i] + "_label", css_class);
	}
}

// clears all fields with the given class, of that class, optional prefix
clear_classes = function(class_name, el_prefix) {
	els = document.getElementsByClassName(class_name);
	if (el_prefix == undefined) 
		el_prefix = '';
	for (var i=0; i < els.length; i++) {
		if (els[i].id.substr(0, el_prefix.length) == el_prefix)
			Element.removeClassName(els[i], class_name);
	}
}


/*  / FORM HELPERS  */
