$JQ = jQuery.noConflict();

/* Define the base LIBRARY namespace */
LIBRARY = {
	getTotalWidth: function(theElement) {
		var totalWidth = theElement.width();
		totalWidth += parseInt(theElement.css('padding-left').replace('px'), '') + parseInt(theElement.css('padding-right').replace('px'), '');
		totalWidth += parseInt(theElement.css('margin-left').replace('px'), '') + parseInt(theElement.css('margin-right').replace('px'), '');
		if (!isNaN(parseInt(theElement.css('borderLeftWidth').replace('px'), '') + parseInt(theElement.css('borderRightWidth').replace('px'), ''))) {
			totalWidth += parseInt(theElement.css('borderLeftWidth').replace('px'), '') + parseInt(theElement.css('borderRightWidth').replace('px'), '');
		}
		return totalWidth;
	},

	getTotalHeight: function(theElement) {
		var totalHeight = theElement.height();
		totalHeight += parseInt(theElement.css('padding-top').replace('px'), '') + parseInt(theElement.css('padding-bottom').replace('px'), '');
		totalHeight += parseInt(theElement.css('margin-top').replace('px'), '') + parseInt(theElement.css('margin-bottom').replace('px'), '');
		if (!isNaN(parseInt(theElement.css('borderTopWidth').replace('px'), '') + parseInt(theElement.css('borderBottomWidth').replace('px'), ''))) {
			totalHeight += parseInt(theElement.css('borderTopWidth').replace('px'), '') + parseInt(theElement.css('borderBottomWidth').replace('px'), '');
		}
		return totalHeight;
	},

	moveHTML: function(theElement, targetElement) {
		var theHTML = LIBRARY.getHTML(theElement);
		theElement.remove();
		targetElement.append(theHTML);
	},
	
	getHTML: function(theElement) {
		var HTML = '<div class="' + theElement.attr('class') + '" id="' + theElement.attr('id') + '" style="' + theElement.attr('style') + '">' + theElement.html() + '</div>';
		return HTML;
	},

	setZindex: function(theElement, elementState) {
		theElement.parents().each(function() {
			var eachElement = $JQ(this);
			if (eachElement.css('position') == 'relative') {
				if (elementState == 1) {
					eachElement.css('zIndex','99999');
				} else {
					eachElement.css('zIndex','0');
				}
			}
		});
	},
	
	getBrowserDimensions: function() {
		var browserDimensions = new Array();
		browserDimensions = {
			width: 0,
			height: 0		
		}
		// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		if (typeof window.innerWidth != 'undefined') {
			browserDimensions.width = window.innerWidth,
			browserDimensions.height = window.innerHeight
		}
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
			browserDimensions.width = document.documentElement.clientWidth,
			browserDimensions.height = document.documentElement.clientHeight
		}
		// older versions of IE
		else {
			browserDimensions.width = document.getElementsByTagName('body')[0].clientWidth,
			browserDimensions.height = document.getElementsByTagName('body')[0].clientHeight
		}
		return browserDimensions;
	},

	positionFooter: function(elementContent, elementFooter) {
		elementContent.css('height', 'auto');
		var contentHeight = LIBRARY.getTotalHeight(elementContent);
		var contentPadding = contentHeight - elementContent.height();
		var footerHeight = LIBRARY.getTotalHeight(elementFooter);
		var browserDimensions = LIBRARY.getBrowserDimensions();
		var windowHeight = browserDimensions.height;
		if (parseInt(contentHeight) + parseInt(footerHeight) < windowHeight) {
			var newHeight = windowHeight - footerHeight - contentPadding;
			elementContent.css('height', newHeight + 'px');
		}
		elementFooter.css('visibility', 'visible');
	},
	
	prepopulateFieldsText: function(classPrefix) {
		classPrefix = classPrefix.toLowerCase()
		$JQ('input.fieldText').each(function() {
			var eachElement = $JQ(this);
						
			if (!eachElement.attr('readonly')) {
				if (eachElement.attr('class').toLowerCase().indexOf(classPrefix) >= 0) {
					var defaultFieldValue = LIBRARY.getDefaultFieldValue(eachElement, classPrefix)
					eachElement.val(defaultFieldValue);
	
					eachElement.unbind('focus');
					eachElement.bind('focus', function() {
						if (eachElement.val() == defaultFieldValue) {
							eachElement.val('');
						}
					});
	
					eachElement.unbind('blur');
					eachElement.bind('blur', function() {
						if (eachElement.val() == '') {
							eachElement.val(defaultFieldValue);
						}
					});
				}
			}
		});
	},
	
	getDefaultFieldValue: function(theElement, classPrefix) {
		var classNames = theElement.attr('class').split(' ');
		for (var i=0; i<classNames.length; i++) {
			if (classNames[i].toLowerCase().indexOf(classPrefix) >= 0) {
				var defaultFieldValue = classNames[i].substring(classPrefix.length).replace('_', ' ').replace('DOTS', '...');
				break;
			}
		}
		return defaultFieldValue;
	},

	createArrayOfFieldData: function(formElement, callback) {
		var parameterArray = {};
		LIBRARY.getFields('select', formElement, function(parameterIn) {
			$JQ.each(parameterIn, function(key, val) {
				parameterArray[key] = val;
			});
			LIBRARY.getFields('input', formElement, function(parameterIn) {
				$JQ.each(parameterIn, function(key, val) {
					parameterArray[key] = val;
				});
				LIBRARY.getFields('textarea', formElement, function(parameterIn) {
					$JQ.each(parameterIn, function(key, val) {
						parameterArray[key] = val.replace(/\n/g, '<br />');
					});
					if (callback) {
						callback.call(this, parameterArray);
					}
				});
			});
		});
	},
	
	getFields: function(fieldType, formElement, callback) {
		var fieldCount = 0;
		var fieldTotal = $JQ(fieldType, formElement).size();
		var parameterArray = {};
		$JQ(fieldType, formElement).each(function() {
			fieldCount++;
			var currentField = $JQ(this);
			parameterArray[currentField.attr('name')] = currentField.val();
			if (fieldCount == fieldTotal) {
				if (callback) {
					callback.call(this, parameterArray);
				}
			}
		});
		if (fieldTotal == 0) {
			if (callback) {
				callback.call(this, parameterArray);
			}
		}
	}

};
