/**
 * Clears a text form element when it has the style 'clear-default'
 */
function clickClear() {
	if ($('input.clear-default').size() > 0) {
		$('input.clear-default').each(function(index) {
			this.defaultValue = $(this).val();
			$(this).click(function() {
				if ($(this).val() == this.defaultValue) {
					$(this).val('');
				};
			})
			.focus(function() {
				if ($(this).val() == this.defaultValue) {
					$(this).val('');
				};
			})
			.blur(function() {
				if ($(this).val() == "") {
					$(this).val(this.defaultValue);
				};
			});
		});
		
		// Clear out form defaults on submit
		$('form').submit(function(event) {
			$('input.clear-default').each(function(index) {
				if ($(this).val() == this.defaultValue) {
					$(this).val('');
				};
			});
		});
	};
}

/**
 * Builds pull quote divs assuming you've wrappted your content with a span with the class: pullquote-left or pullquote-right
 */
function buildPullQuote () {
	$('span.pullquote-left, span.pullquote-right').each(function(index) {
		var contents = $.trim($(this).html());
		var firstCharacterCode = contents.charCodeAt(0);
		if (firstCharacterCode < 65 || firstCharacterCode > 96) {
			contents = '&hellip; ' + contents;
		};
		
		var lastCharacter = contents.charAt(contents.length - 1);
		if ("?!.".search(lastCharacter) < 0) {
			contents = contents + ' &hellip;';
		};
		var $parent = $(this).parent();
		var $pullquote = $('<div>').attr('class', $(this).attr('class')).html(contents);
		$parent.before($pullquote);
	});
}

$(document).ready(function() {
	clickClear();
	buildPullQuote();
});


// Delete above if using standard Javascript, delete below if using jQuery

/**
 * Clears a text form element when it has the style 'clear-default'
 */
function clickClear() {
	if(!document.getElementsByTagName) return false;
	
	var inputs = document.getElementsByTagName("INPUT");
	
	for ( var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].className.match("clear-default"))
		{
			inputs[i].onclick = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			};
			inputs[i].onfocus = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			};
			inputs[i].onblur = function() {
				if(this.value == "")
				{
					this.value = this.defaultValue;
				}
				return 0;
			};
		}
	}
	return 0;
}

addLoadEvent(clickClear);

/**
 * Gets DOM elements by class name
 * @param {String} searchClass The name of the class to search for
 * @param {Object} node Optional - The DOM element to search under
 * @param {String} tag Optional - The type of element to search for
 * @returns The list of elements that follow the searchClass, node and tag
 * @type Array
 */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/**
 * Determines if a DOM element has a specific class
 * @param {Object} ele The DOM element to check
 * @param {String} cls The class to search for
 * @returns True or false if it has the class
 * @type Boolean
 */
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

/**
 * Adds a class to a DOM element
 * @param {Object} ele The DOM element to add a class to
 * @param {String} cls The class to add
 */
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

/**
 * Removes a class from a DOM element
 * @param {Object} ele The DOM element to remove a class from
 * @param {String} cls The class to remove
 */
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

/**
 * Queues up the function to fire onload
 * @param {string} func The function name without parenthesis
 */
function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldOnLoad();
			func();
		};
	}
}