//$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', '')))) {
			totalWidth += parseInt(theElement.css('borderLeftWidth').replace('px', ''));
		}
		if (!isNaN(parseInt(theElement.css('borderRightWidth').replace('px', '')))) {
			totalWidth += 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', '')))) {
			totalHeight += parseInt(theElement.css('borderTopWidth').replace('px', ''));
		}
		if (!isNaN(parseInt(theElement.css('borderBottomWidth').replace('px', '')))) {
			totalHeight += 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 = $(this);
			if (eachElement.css('position') == 'relative') {
				if (elementState == 1) {
					eachElement.css('zIndex','99999');
				} else {
					eachElement.css('zIndex','0');
				}
			}
		});
	},
	
	getDimensionsBrowser: function() {
		var dimensionsBrowser = new Array();
		dimensionsBrowser = {
			width: 0,
			height: 0		
		}
		// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		if (typeof window.innerWidth != 'undefined') {
			dimensionsBrowser.width = window.innerWidth,
			dimensionsBrowser.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) {
			dimensionsBrowser.width = document.documentElement.clientWidth,
			dimensionsBrowser.height = document.documentElement.clientHeight
		}
		// older versions of IE
		else {
			dimensionsBrowser.width = document.getElementsByTagName('body')[0].clientWidth,
			dimensionsBrowser.height = document.getElementsByTagName('body')[0].clientHeight
		}
		return dimensionsBrowser;
	},

	positionFooter: function(elementContent, elementFooter) {
		elementContent.css('height', 'auto');
		var contentHeight = LIBRARY.getTotalHeight(elementContent);
		var contentPadding = contentHeight - elementContent.height();
		var footerHeight = LIBRARY.getTotalHeight(elementFooter);
		var dimensionsBrowser = LIBRARY.getDimensionsBrowser();
		var windowHeight = dimensionsBrowser.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();
		$('input.fieldText, textarea.fieldArea').each(function() {
			var eachElement = $(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);
						}
					});
				}
			}
		});
	},

	bindFieldsRequired: function(classPrefix, classRequired, fieldRequiredColour, fieldDefaultColour) {
		classPrefix = classPrefix.toLowerCase();
		$('.' + classRequired).each(function() {
			var eachElement = $(this);
			if(eachElement.attr('tagName').toLowerCase() == 'select') {
				eachElement.unbind('change');
				eachElement.bind('change', function() {
					if (eachElement.attr('selectedIndex') != 0) {
						eachElement.css('color', fieldDefaultColour);
					} else {
						eachElement.css('color', fieldRequiredColour);
					}
				});
			}

			if(eachElement.attr('tagName').toLowerCase() == 'input') {
				eachElement.unbind('change');
				eachElement.bind('change', function() {
					var defaultFieldValue = LIBRARY.getDefaultFieldValue(eachElement, classPrefix);
					if ((eachElement.val() != defaultFieldValue) && (eachElement.val() != '')) {
						eachElement.css('color', fieldDefaultColour);
					} else {
						eachElement.css('color', fieldRequiredColour);
					}
				});
			}
		});
	},

	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(/_/g, ' ').replace('DOTS', '...');
				break;
			}
		}
		return defaultFieldValue;
	},

	createArrayOfFieldData: function(formElement, callback) {
		var parameterArray = {};
		LIBRARY.getFields('select', formElement, function(parameterIn) {
			$.each(parameterIn, function(key, val) {
				parameterArray[key] = val;
			});
			LIBRARY.getFields('input', formElement, function(parameterIn) {
				$.each(parameterIn, function(key, val) {
					parameterArray[key] = val;
				});
				LIBRARY.getFields('textarea', formElement, function(parameterIn) {
					$.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 = $(fieldType, formElement).size();
		var parameterArray = {};
		$(fieldType, formElement).each(function() {
			fieldCount++;
			var currentField = $(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);
			}
		}
	},
	
	activateBlockedText: function(blockedClass, widthClass) {
		$(blockedClass).each(function() {
			var eachElement = $(this);
			var eachWord = eachElement.html().split(" ");
			var eachLine = new Array();
			var elementWidth = eachElement.parent().width();
			var elementClasses = eachElement.attr('class').split(" ");
			var lineCount = 0;
			eachElement.css('visibility','hidden');
			eachElement.css('float','left');
			eachElement.css('white-space','nowrap');
			eachElement.html('');
			for (var countClasses=0; countClasses<elementClasses.length; countClasses++){
				if (elementClasses[countClasses].indexOf(widthClass.substring(1)) >= 0) {
					elementWidth = elementClasses[countClasses].substring(widthClass.length-1)
				}
			}
			for (var countWords=0; countWords<eachWord.length; countWords++ ){
				var oldLine = '';
				if (eachLine[lineCount]) {
					oldLine = eachLine[lineCount];
					eachLine[lineCount] = eachLine[lineCount] + eachWord[countWords] + " ";
				} else {
					eachLine[lineCount] = eachWord[countWords] + " ";
				}
				var newLine = '<span>' + eachLine[lineCount].substring(0,eachLine[lineCount].length-1) + '</span>';
				eachElement.html(newLine);
				if (eachElement.width() >= elementWidth) {
					eachLine[lineCount] = oldLine;
					lineCount++;
					eachLine[lineCount] = eachWord[countWords] + " ";
				}

				if (countWords == eachWord.length-1) {
					eachElement.html('');
					for (var countLines=0; countLines<eachLine.length; countLines++) {
						eachElement.html(eachElement.html() + '<span>' + eachLine[countLines].substring(0,eachLine[countLines].length-1) + '</span>');
						if (countLines == eachLine.length-1) {
							eachElement.css('visibility','visible');
						}
					}
				}
			}
		});
	},
	
	getHighest: function(theElements) {
		var dimensionsHighest = new Array();
		dimensionsHighest = {
			heightPlain: 0,
			heightTotal: 0
		}
		theElements.each(function() {
			var eachElement = $(this);
			if ((eachElement.attr('class').indexOf('hide') < 0) && (eachElement.css('display').toLowerCase() != 'none')) {
				var heightPlain = eachElement.height();
				if (dimensionsHighest.heightPlain < heightPlain) {
					dimensionsHighest.heightPlain = heightPlain;
				}
				var heightTotal = LIBRARY.getTotalHeight(eachElement);
				if (dimensionsHighest.heightTotal < heightTotal) {
					dimensionsHighest.heightTotal = heightTotal;
				}
			}
		});
		return dimensionsHighest;
	},

	getWidest: function(theElements) {
		var dimensionsWidest = new Array();
		dimensionsWidest = {
			widthPlain: 0,
			widthTotal: 0
		}
		theElements.each(function() {
			var eachElement = $(this);
			if ((eachElement.attr('class').indexOf('hide') < 0) && (eachElement.css('display').toLowerCase() != 'none')) {
				var widthPlain = eachElement.width();
				if (dimensionsWidest.widthPlain < widthPlain) {
					dimensionsWidest.widthPlain = widthPlain;
				}
				var widthTotal = LIBRARY.getTotalWidth(eachElement);
				if (dimensionsWidest.widthTotal < widthTotal) {
					dimensionsWidest.widthTotal = widthTotal;
				}
			}
		});
		return dimensionsWidest;
	},

	getWidthSiblings: function(theElement) {
		var widthSiblings = 0;
		var numberElements = theElement.siblings().size();
		if (numberElements > 0) {
			theElement.siblings().each(function() {
				var eachElement = $(this);
				if ((eachElement.attr('class').indexOf('hide') < 0) && (eachElement.css('display').toLowerCase() != 'none')) {
					widthSiblings = widthSiblings + LIBRARY.getTotalWidth(eachElement);
				}
			});
		}
		return widthSiblings;
	},


	getHeightSiblings: function(theElement) {
		var heightSiblings = 0;
		var numberElements = theElement.siblings().size();
		if (numberElements > 0) {
			theElement.siblings().each(function() {
				var eachElement = $(this);
				if ((eachElement.attr('class').indexOf('hide') < 0) && (eachElement.css('display').toLowerCase() != 'none')) {
					heightSiblings = heightSiblings + LIBRARY.getTotalHeight(eachElement);
				}
			});
		}
		return heightSiblings;
	},

	addTargetAttribute:function(targetClass, targetAction, targetTitle) {
		$('.' + targetClass).each(function() {
			var eachElement = $(this);
			eachElement.attr('target', targetAction);
			if (targetTitle != '') {
				if (eachElement.attr('title') == '') {
					eachElement.attr('title', targetTitle);
				} else {
					eachElement.attr('title', eachElement.attr('title') + ' - ' + targetTitle);
				}				
			}
		});
	}
};

