var Utils = {
	ExternalLinks: function () {
		/* XHTML Compliant Open Window */
		$('a.external').each(function () {
			$(this).attr("target", "_blank");
		});
	},
	FixMobileViewport: function () {
		/* Fix Rotation Viewport Bug on iOS Devices */
		if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
			var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
			if (viewportmeta) {
				viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
				document.body.addEventListener('gesturestart',
		        function () {
		        	viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
		        },
		        false);
			}
		}
	},
	PreloadImages: function () {
		/* Image Pre-Loader */
		if (document.images) {
			if (typeof (document.preload) == 'undefined') {
				document.preload = new Object();
			}

			document.preload.loadedimages = new Array();
			var arglength = arguments.length;

			for (arg = 0; arg < arglength; arg++) {
				document.preload.loadedimages[arg] = new Image();
				document.preload.loadedimages[arg].src = arguments[arg];
			}
		}
	},
	SetDefaultFormFields: function () {
		var items = $("input[type='text'], textarea");

		$(items).focusin(function () {
			$.data(document.body, "defaultValues", $(this).val());
			$(this).val("");
		});

		$(items).focusout(function () {
			if ($(this).val().length == 0)
				$(this).val($.data(document.body, "defaultValues"));
		});
	},
	WriteSafeEmail: function (username, hostname, tld) {
		// Generate SPAM Safe E-Mails
		var atSign = "&#64;";
		var appearance = "";

		/* Optional 4th argument for appearance */
		if (arguments[3] == null) {
			appearance = username + atSign + hostname + "." + tld;
		}
		else {
			appearance = arguments[3];
		}

		var addr = username + atSign + hostname + "." + tld;
		document.write("<" + "a" + " " + "href=" + "mail" + "to:" + addr + " \/>" + appearance + "<\/a>");
	},
	TwitterWindows: function () {
		$(".twitter-window").each(function () {
			$(this).attr("onclick", "window.open('" + $(this).attr("href") + "', 'Twitter Share', 'width=600,height=400,resizable=no,scrollbars=no,titlebar=no'); return false;");
		});
	}
};

$(function () {
	Utils.ExternalLinks();
});
