/* ============================================================================
* style.lib.js, v1.0.003, 2008-07-27
*
* Author:	Scott Park (scott@firefallpro.com), except where noted.
*
* Desc.:	Provides CSS/inline style related functions
*
* Contents:	trim()
*			ltrim()
*			rtrim()
*			show()
*			hide()
*			showByClass()
*			hideByClass()
*			changeClassByElement()
*			changeClassByClass()
*
* Requires: n/a
*
* http://www.firefallpro.com
*
* Copyright 2008 Firefall Pro, LLC, unless otherwise noted. The contents of
* this file may not be reused, resold or redistributed, in part or in whole,
* without the expressed permission of the respective copyright holders.
* ========================================================================== */

// trim(), ltrim(), and rtrim(): Credit: http://www.somacon.com/p355.php
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// Shows an element, element ID or series of comma separated element IDs
function show(id) {
	var element;
	
	// Makes the element visible
	function showElement(e) {
		
		// Block Level Tags
		if (e.tagName.toLowerCase() in {"p":"","div":"","hr":"","h1":"","h2":"","h3":"","h4":"","h5":"","h6":"","address":"","blockquote":"","center":"","del":"","ins":"","noscript":"","pre":""}) {
			e.style.display = "block";
		
		// Table
		} else if (e.tagName.toLowerCase() == "table") {
			try {
				e.style.display = "table-row";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Table Row
		} else if (e.tagName.toLowerCase() == "tr") {
			try {
				e.style.display = "table-row";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Table Cell
		} else if (e.tagName.toLowerCase() == "td") {
			try {
				e.style.display = "table-cell";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Inline Tags
		} else {
			e.style.display = "inline";
		}
	}
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in show() or does not exist");
		return false;
	}
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\"specified in show() does not exist");
			} else {
				showElement(element);
			}
		}

	// Object
	} else {
		showElement(id);
	}
	return true;
}

// Hides an element, element ID or series of comma separated element IDs
function hide(id) {
	var element;
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in hide() or does not exist");
		return false;
	}
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\" specified in hide() does not exist");

			// Hide
			} else {
				element.style.display = "none";
			}
		}
		
	// Object
	} else {
		id.style.display = "none";
	}
	return true;
}

// Shows a tag by a class
function showByClass(tag,c,within) {
	var elements;
	var d;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in showByClass()");
		return false;
	}
	if (!c) {
		alert("A class has not been specified in showByClass()");
		return false;
	}
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in showByClass() does not exist");
		return false;
	}
	
	// Find elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Show matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == c) {
			show(elements[i]);
		}
	}
	return true;
}


// Hides a tag by a class
function hideByClass(tag,c,within) {
	var elements;
	var d;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in hideByClass()");
		return false;
	}
	if (!c) {
		alert("A class has not been specified in hideByClass()");
		return false;
	}
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in hideByClass() does not exist");
		return false;
	}
	
	// Find elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Hide matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == c) {
			elements[i].style.display = "none";
		}
	}
	return true;
}

// Changes the class of an element, element ID or series of comma separated element IDs
function changeClassByElement(id,c) {
	var element;
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in changeClassByElement() or does not exist");
		return false;
	}
	if (!c) c = "";
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\"specified in changeClassByElement() does not exist");
			} else {
				element.className = c;
			}
		}

	// Object
	} else {
		element = id;
		element.className = c;
	}
	return true;
}

// Changes the class of a tag by a class
function changeClassByClass(tag,find,replace,within) {
	var elements;
	var d;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in changeClassByClass()");
		return false;
	}
	if (!find) {
		alert("A class to find has not been specified in changeClassByClass()");
		return false;
	}
	if (!replace) replace = "";
	
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in changeClassByClass() does not exist");
		return false;
	}
	
	// Find Elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Hide matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == find) {
			elements[i].className = replace;
		}
	}
	return true;
}