$(document).ready(function(){
	if(!tmb.readCookie('visited')){
		tmb.build();
		tmb.start();
	}
});
tmb = {
	heading : 'If you liked this article...', 
    copy : 'Get other LifeDev articles like this delivered straight into your email. The articles are 100% free and only come a few times a week.',
	finePrint : 'This form appears only once, and your email will never be shared.',
	nameH : 'your name', 
	emailH : 'your email', 
	submit : 'submit',
	delay : 10, // timein seconds before popup shows
	commentTrigger : true, // if set to true, popup will show when user scrolls to comments 
	
	/**
	* No need to change anything below here
	**/
	build : function(){
		hiddenElements = [
		{ i : 'popup_meta_web_form_id', n : 'meta_web_form_id', v : '38976223' }, 
		{ i : 'popup_meta_split_id', n : 'meta_split_id', v : '' }, 
		{ i : 'popup_unit', n : 'listname', v : 'lifedevrss' }, 
		{ i : 'redirect_92d907c999870a46ecf46e0352121d3f', n : 'redirect', v : 'http://lifedev.net/email-subscription-thanks/?popup' }, 
		{ i : 'popup_meta_redirect_onlist', n : 'meta_redirect_onlist', v : 'http://www.chrisguillebeau.com/3x5' }, 
		{ i : 'popup_meta_adtracking', n : 'meta_adtracking', v : 'LifeDev_popup' }, 
		{ i : 'popup_meta_message', n : 'meta_message', v : '1' }, 
		{ i : 'popup_meta_required', n : 'meta_required', v : 'name,email' },
		{ i : 'popup_meta_forward_vars', n : 'meta_forward_vars', v : '0' }
		];
		$('head')
			.append($('<link/>')
				.attr('rel', 'stylesheet')
				.attr('href', 'http://lifedev.net/wp-content/themes/thesis/custom/popup.css')
				.attr('type', 'text/css')
			);
				
		var popup = $('<div/>')
			.attr('id', 'tmb_popup')
			.hide()
			.prependTo('body');
		popup
			.append(
				$('<div/>')
					.attr('id', 'tmb_overlay')
			);
		var dialog = $('<div/>')
						.attr('id', 'tmb_dialog');
		popup.append(dialog);
		dialog
			.append(
				$('<div/>')
					.attr('id', 'tmb_popup-content')
					.append($('X<a/>')
						.attr('href', '#')
						.attr('id', 'tmb_popup-close')
						.click(
							function(){
									tmb.close();
							return false;
							}
						)
					)
					.append(
						$('<div/>')
						.attr('id', 'tmb_popup-heading')
					)
					.append(
						$('<p/>')
						.attr('id', 'tmb_popup-copy')
					)
					.append(
						$('<form/>')
							.attr('id', 'tmb_popup-form')
							.attr('action', 'http://www.aweber.com/scripts/addlead.pl')
							.attr('method', 'post')
							.append(
								$('<table/>')
									.append(
										$('<tr/>')
											.append(
												$('<th/>')
													.attr('id', 'tmb_popup-nameH')
											)
											.append(
												$('<td/>')
													.append(
														$('<input/>')
															.attr('type', 'text')
															.attr('name', 'name')
														)
											)
									)
									.append(
										$('<tr/>')
											.append(
												$('<th/>')
													.attr('id', 'tmb_popup-emailH')
											)
											.append(
												$('<td/>')
													.append(
														$('<input/>')
															.attr('type', 'text')
															.attr('name', 'email')
														)
											)
									)								
							)
							.append(
								$('<a/>')
									.attr('id', 'tmb_popup-submit')
									.attr('href', '#')
							)
					)
					.append(
						$('<div/>')
							.attr('id', 'tmb_popup-finePrint')
						)
			);
			for(i in hiddenElements){
				var elm = hiddenElements[i];
				$('#tmb_popup-form')
					.append($('<input/>')
						.attr('type', 'hidden')
						.attr('id', elm.i)
						.attr('name', elm.n)
						.val(elm.v)
					);
			}		
	},
	setup : function(){
		$('#tmb_popup-heading').html(tmb.heading);
		$('#tmb_popup-copy').html(tmb.copy);
		$('#tmb_popup-nameH').html(tmb.nameH);
		$('#tmb_popup-emailH').html(tmb.emailH);
		$('#tmb_popup-finePrint').html(tmb.finePrint);
		$('#tmb_popup-submit')
			.html(tmb.submit)
			.click(function(){
				$('#tmb_popup-form').submit();
				return false;
			});
		var win = tmb.windowDimensions();
		var pageHeight = win[1] + tmb.scrollHeight();
		var pageWidth = win[0];
		var pageCenter = pageWidth/2;
		var dialogWidth = '544';
		var dialogHeight = '410';
		var dialogLeft = pageCenter - (dialogWidth/2);
		var dialogTop = (win[1]/2) - (dialogHeight/2);
		if(tmb.isIE6){
			dialogTop = tmb.scrollY() + (win[1]/2) - (dialogHeight/2);
			$('#tmb_dialog').css('position', 'absolute');
		}
		
		$('#tmb_overlay')
			.css('height', pageHeight + 'px');
		$('#tmb_dialog')
			.css({
				'width' : dialogWidth + 'px',
				'height' : dialogHeight + 'px', 
				'top' : dialogTop + 'px', 
				'left' : dialogLeft + 'px'
			});
	},
	open : function()
	{
		tmb.setup();
		$('#tmb_popup').show();
		tmb.createCookie('visited', '1', 186);
	},
	close : function()
	{
		$('#tmb_popup').hide();
	},
	start : function(){			
		tmb.endY = $('#comments').offset().top;
		var win = tmb.windowDimensions();
		if(tmb.endY < win[1] || !tmb.commentTrigger){
			tmb.timedOpen(tmb.delay * 1000);
		}
		else{
			setTimeout('tmb.trigger()', 1000);
		}
	},
	trigger : function(){
		var scroll = tmb.scrollY();
		var win = tmb.windowDimensions();
		if(scroll > (tmb.endY - win[1])){ 
			tmb.open();
		}
		else{
			setTimeout('tmb.trigger()', 1000);
		}
	},
	timedOpen : function(s){
		setTimeout('tmb.open()', s);
	},
	createCookie : function(name, value, days){
		if(days){
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else{
			var expires = "";
		}
		value = escape(value);
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie : function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return(unescape(c.substring(nameEQ.length,c.length)));
		}
		return null;
	},
	scrollY : function(){
		var scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;
	},
	scrollHeight : function(){
		var scrOfY = 0;
		if( typeof( window.scrollMaxY ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.scrollMaxY;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollHeight;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollHeight;
		}
		return scrOfY;
	},
	windowDimensions : function(){
		var width = 0, height = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			width = window.innerWidth;
			height = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
		return [ width , height ];
	},
	endY : 0,
	isIE6 : navigator.userAgent.toLowerCase().indexOf('msie 6') != -1
}