$(document).ready(function() {
	$("a").filter(function() {
    	return this.hostname && this.hostname !== location.hostname;
	}).addClass('external');
});

$(document).ready(function() {
	var searchInput = document.getElementById('s');
	if (searchInput) {
		searchInput.onfocus = function() {
			this.defaultValue = this.defaultValue || this.value;
			if (this.defaultValue == this.value) this.value = '';
		}
		searchInput.onblur = function() {
			if (!this.value) this.value = this.defaultValue;
		}
	}
});

$(document).ready(function() {
	var h1 = document.getElementById('categoryTitle');
	if (h1) {
		h1.innerHTML = h1.innerHTML.replace(/↑/g, '<a href="'+window.location.pathname+'">');
		h1.innerHTML = h1.innerHTML.replace(/\s→\s/g, '</a>&nbsp;<img alt="" style="margin-right: 10px;" src="/wp-content/themes/dapple/img/arrow.png"/>');
	}
});

$(document).ready(function() {
	var links = $('#ctrlNav a');
	if (links && links.length > 0) {
		var link;
		document.onkeydown = function(e) {
			if (!e) {
				var e = window.event;
				e.target = e.srcElement;
			}
			if (e.ctrlKey && (('39' == e.keyCode) || ('37' == e.keyCode))) {
				if ('39' == e.keyCode) {
					link = $('#ctrlNav a:last');
				}
				else {
					link = $('#ctrlNav a:first');	
				}
				if (link.attr('href')) window.location.href = link.attr('href');
			}
		}
	}
	else {
		if ($('#ctrlNav')) $('#ctrlNav').hide();
	}
});


function getCount(postcount, taglist) {
	var postCount = $(postcount);
	var taglist = $(taglist);
	if (postCount && taglist) {
		var currentTag = taglist.filter(function() {
    		return this.href == window.location.href;
		});
		if (currentTag && currentTag.attr('title')) {
			$('.' + currentTag.attr('class')).addClass('currentTag').removeAttr('href');
			var count = currentTag.attr('title').replace(/[^0-9]+/, '');
			postCount.html(count);
		}
	}
}

$(document).ready(function() {
	getCount('#postCount', '#tagCloud a');
	getCount('#postCountTags', 'div.tags>a');
});

$(document).ready(function() {
	var replyLink = $('.comment-reply-link');
	var resp = $('#respond');
	var currentReply;
	
	replyLink.click(function() {
		tinyMCE.rmC('comment_area');
		$(this).hide();
		if (currentReply) $(currentReply).show();
		currentReply = this;
		tinyMCE.addC('comment_area');
	});
	
	$('#cancel-comment-reply-link').click(function() {
		tinyMCE.rmC('comment_area');
		$(currentReply).show();
		setTimeout(function() {tinyMCE.addC('comment_area');}, 500);
	});
});

$(document).ready(function() {
	$('.current-cat a, .current_page_item a, a.current_page_item').removeAttr('href');
});

$(document).ready(function() {
	if (typeof tinyMCE == 'object') {
		tinyMCE.rmC = function(id) {
			this.execCommand('mceRemoveControl',false, id);
		}
		tinyMCE.addC = function(id) {
			this.execCommand('mceAddControl',false, id);
		}
	}
});

$(document).ready(function() {
	var preview = $('#ajax-comment-preview');
	var editForm = $('#addCommentBlock>*:not(#commentform-submit)');
	var editButton = $('#edit');
	$('#acp-preview').click(function() {
		editForm.hide();
		preview.show();
		editButton.show();
	});
	editButton.click(function() {
		editForm.show();
		preview.hide();
		$(this).hide();
	});
});
