(function() {
	var Z = {
		Config : {
			sHTMLtag : "can-has-js"
			,sSelPNG : "p.lede img"
		}

		/*
			STOP EDITING HERE
		*/
		,init : function() {
			if (top != self) {
				top.location.replace(self.location.href);
			}

			var c = Z;
			var p = c.Project;
			var u = c.Utility;

			u.addDOMLoadEvent(function() {
				p.tagIt();
				p.ttt();
				p.contact();
				p.tweets();

				if (typeof DD_belatedPNG != "undefined") {
					DD_belatedPNG.fix(c.Config.sSelPNG);
				}
			});
		}
		,Project : {
			ajaxContact : function() {
				$('#contact-error').remove();
				
				var $form = $('form#contact-form');
				var data = {
					'name': $('input[name="name"]').val()
					,'company': $('input[name="company"]').val()
					,'email': $('input[name="email"]').val()
					,'phone': $('input[name="phone"]').val()
					,'comments': $('textarea[name="comments"]').val()
				};
				
				$.ajax({
					'type': 'POST',
					'url': $form.attr('action'),
					'data': data,
					'complete': function(xhr)
					{
						var $data = $(xhr.responseText);
						$data.hide();
						$form.before($data);
						
						if (xhr.status == '200')
						{
							$form.fadeOut();
						}
						
						else
						{
							Recaptcha.reload();
						}
						
						$data.slideDown();
					}
				});
				return false;
			}
			,contact : function() {
				var c = Z;
				var u = c.Utility;
				var oForm = document.getElementById("contact-form");
				if (oForm) {
					var oScope = oForm.getElementsByTagName("fieldset")[0], aLabels = oScope.getElementsByTagName("label"), ceil = aLabels.length, did = false;
					if (ceil) {
						for (var i = 0; i < ceil; i++) {
							var oLabel = aLabels[i];
							var oTarget = document.getElementById(oLabel.htmlFor);
							if (oTarget) {
								var sText = oLabel.innerHTML;

								oTarget.className = u.safeAppend(oTarget.className, "default");
								oTarget.setAttribute("defaultVal", sText);
								oTarget.value = sText;

								var focus = function(e) {
									var oTarget = window.event ? window.event.srcElement : e ? e.target : null;
									if (!oTarget) return;

									if (oTarget.value == oTarget.getAttribute("defaultVal")) {
										oTarget.value = "";
										oTarget.className = "";
									} else {
										oTarget.select();
									}
								}
								var blur = function(e) {
									var oTarget = window.event ? window.event.srcElement : e ? e.target : null;
									if (!oTarget) return;

									if (oTarget.value == "" && oTarget.getAttribute("defaultVal")) {
										oTarget.value = oTarget.getAttribute("defaultVal");
									}
								}

								u.addEvent(oTarget, "focus", focus);
								u.addEvent(oTarget, "blur", blur);
								oTarget.onfocus = focus;
								did = true;
							}
						}
					}
					if (did) {
						oForm.className = u.safeAppend(oForm.className, "did-run");
					}
					oForm.onsubmit = function() {
						var doit = true, sMsg = "";
						var oName = document.getElementById("name");
						var oEmail = document.getElementById("email");
						var bEmail = oEmail.value.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/gi);

						if (oName.value === "Your Name" || oName.value.length === 0) {
							sMsg += 'you didn&#8217;t supply <label for="name">a name</label>';
						}
						if (bEmail === null || oEmail.value.length === 0) {
							if (sMsg.length) {
								sMsg += ', and ';
							}
							sMsg += 'we need <label for="email">a valid email address</label>';
						}

						if (sMsg.length > 0) {
							var sHTML = '<em>Oops!</em> A slight error occurred: ' + sMsg + '.';
							var oP = document.getElementById("contact-error");
							if (!oP) {
								oP = document.createElement("p");
								oP.setAttribute("id", "contact-error");
								oP.className = "note error";
							}
							oP.innerHTML = sHTML;
							oForm.parentNode.insertBefore(oP, oForm)
							doit = false;
						}
						
						if (doit) {
							Z.Project.ajaxContact();
							return false;
						}
						
						return doit;
					}
				}
			}
			,ttt : function() {
				var $oTable = $("table.ttt");
				$oTable.find("a").each(function() {
					var $oLink = $(this);
					var $oImg = $oLink.find("img");
					var sSrc = $oImg.attr("src");
					if (sSrc.indexOf("-blank") !== -1) {
						$oLink.addClass("b");
					}
				});
			}
			,tagIt : function() {
				var c = Z;
				var u = c.Utility;

				var oHtml = document.getElementsByTagName("html")[0];

				if (oHtml) {
					oHtml.className = u.safeAppend(oHtml.className, c.Config.sHTMLtag);
				}
			}
			,tweets : function() {
			$("#sub").prepend('<div class="tweet"><h2>Recently from <a href="http://twitter.com/happycog">@HappyCog</a></h2></div>');
				$(".tweet").tweet({
				            count: 5,
				            loading_text: "loading tweets&hellip;",
				            filter_replies: "true",
				            query: "from:happycog"
				        });
			}
		}
		,Utility : {
			// From http://www.thefutureoftheweb.com/blog/adddomloadevent
			addDOMLoadEvent : (function(){
				// create event function stack
				var load_events = [],
					load_timer,
					script,
					done,
					exec,
					old_onload,
					init = function () {
						done = true;

						// kill the timer
						clearInterval(load_timer);

						// execute each function in the stack in the order they were added
						while (exec = load_events.shift())
							exec();

						if (script) script.onreadystatechange = '';
					};

				return function (func) {
					// if the init function was already ran, just run this function now and stop
					if (done) return func();

					if (!load_events[0]) {
						// for Mozilla/Opera9
						if (document.addEventListener)
							document.addEventListener("DOMContentLoaded", init, false);

						// for Internet Explorer
						/*@cc_on @*/
						/*@if (@_win32)
							document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
							script = document.getElementById("__ie_onload");
							script.onreadystatechange = function() {
								if (this.readyState == "complete")
									init(); // call the onload handler
							};
						/*@end @*/

						// for Safari
						if (/WebKit/i.test(navigator.userAgent)) { // sniff
							load_timer = setInterval(function() {
								if (/loaded|complete/.test(document.readyState))
									init(); // call the onload handler
							}, 10);
						}

						// for other browsers set the window.onload, but also execute the old window.onload
						old_onload = window.onload;
						window.onload = function() {
							init();
							if (old_onload) old_onload();
						};
					}

					load_events.push(func);
				}
			})()
			,addEvent : function(oEl, sType, fFunc, bCapture) {
				var bCapture = (bCapture) ? bCapture : false;
				if (oEl.addEventListener) {
					oEl.addEventListener(sType, fFunc, bCapture);
					return true;
				} else if (oEl.attachEvent) {
					var r = oEl.attachEvent('on' + sType, fFunc);
					return r;
				} else {
					return false;
				}
			}
			,safeAppend : function(target, str) {
				target += (target.length > 0 ? " ": "") + str;
				return target;
			}
		}
	}
	Z.init();
})();