$(document).ready(function() {
	bindFormDefaultValueInteraction();
	removeFirstListBorders();
	bindPrintLinkActions();
	applyPop();
	applyPrint();
});

function bindFormDefaultValueInteraction() {
	$("form input[type='text']").filter(':not(#contact-form input)').each(function(i) {
		$(this)
			.focus(function() {
				if ($(this).val() == $(this)[0].defaultValue) {
					$(this).val('').addClass('typing');
				}
			})
			.blur(function() {
				if ($(this).val() == '') {
					$(this).val($(this)[0].defaultValue).removeClass('typing');
				}
			});
	});
	
	$('#subscribe-form').submit(function(e) {
		if ($('#subscribe-name').val() == 'Enter email to subscribe') {
			e.preventDefault();
			alert('Please enter your email address.');
			return false;
		}
	});
}

function removeFirstListBorders() {
	$('.most-popular ul li ul li:first, .blogroll ul li ul li:first, .anchors li:first').addClass('first');
}

function applyPrint() {
	$('.go-print').click(function(e) {
		e.preventDefault();
		window.print();
		return false;
	});
}

function bindPrintLinkActions() {
	$('.print a').click(function(e) {
		e.preventDefault();
		launchPrintWindow($(this).get(0).href);
		return false;
	});
}

function launchPrintWindow(link) {
	window.open(link, 'printWindow', 'width=720,height=600,toolbar=0,status=0,resizable=0,scrollbars=1');
}

function share(customTitle, customLink) {
	SHARETHIS.addEntry({
		title: customTitle,
		url: customLink 
	}, {button:true, embeds:true});
}

function applyPop() {
	$('a')
		.each(function (index) {
			if ($(this).attr('href').match(/^\//i) == '/' || $(this).attr('href').match(/^http\:\/\/(?:www\.)?livingmaxwell\.com/i) !== null || $(this).attr('href').match(/^javascript/i) !== null || $(this).attr('href').match(/^#/i) !== null) {
				$(this).attr('rel', 'max-link');
			}
		});
	
	$('a:not(.OS_buy_link)')
		.filter(':not(a[rel="max-link"])')
		.attr('rel', 'seeit')
		.click(function(e) {
			e.preventDefault();
			window.open($(this).attr('href'), '_blank');
			return false;
		});
}
