﻿// HTML/String Manipulation

function cleanHtml(value) {
	if (typeof (value) == 'object')
		value.html(cleanString(value.html()));
	else
		value = cleanString(value);

	return value;
}

function cleanString(value) {
	return value.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"');
}

function cleanHtmlScript(value) {
	if (typeof (value) == 'object')
		value.html(cleanStringScript(value.html()));
	else
		value = cleanStringScript(value);

	return value;
}

function cleanStringScript(value) {
	return value.replace(/&#39;/g, "\\&#39;");
}

function trimHtml(value) {
	if (typeof (value) == 'object')
		value.html(trimString(value.html()));
	else
		value = trimString(value);

	return value;
}

function trimString(value) {
	return value.replace(/\r/g, '').replace(/\n/g, '').replace(/\t/g, '').replace(/&nbsp;/g, '');
}



// Element Manipulation

(function($) {
	$.fn.extend({
		gat_center: function(container, height) {
			var containerHeight = null;

			// If container specified, use its height for calculating container height, else use first parent's height
			if (container) {
				containerHeight = $(container).height() > 0 ? $(container).height() : $(container).css('height');
			}
			else {
				containerHeight = $(this).parent().height() > 0 ? $(this).parent().height() : $(this).parent().css('height');
			}

			containerHeight = parseInt(containerHeight);

			return $(this).each(function() {
				var element = $(this);

				// If height specified, use it for centering, else calculate against element's height
				if (!height) {
					height = $(element).height() > 0 ? $(element).height() : $(element).css('height');
				}

				height = parseInt(height);
				$(element).css('margin-top', ((containerHeight - height) / 2) + 'px');
			});
		}
	});
})(jQuery);