$(document).ready(function() {
	$('a[rel*=facebox]').facebox();
	$('#lnkShowPosteditForm').click(function(){
		$('#frmPosteditHolder').slideDown();
		return false;
	});
	
	// if they click the post to facebook or post to twitter checkbox
	// then we have to make sure that the private checkbox is not checked
	// conversely if they check private, then facebook and twitter can't be checked
	$('#cboxFacebook').change(function(){
		if ($(this).attr('checked')){
			$('#cboxPrivate').attr('checked', false);
		}
	});
	$('#cboxTwitter').change(function(){
		if ($(this).attr('checked')){
			$('#cboxPrivate').attr('checked', false);
		}
	});
	$('#cboxPrivate').change(function(){
		if ($(this).attr('checked')){
			$('#cboxFacebook').attr('checked', false);
			$('#cboxTwitter').attr('checked', false);
		}
	});
	
	// do something when the more link is clicked
	$('.lnkMore').click(function(){
		// get our post id
		var postId = $(this).next('.postid').html();
		// hide our truncated post
		$('#postmsg_'+postId).hide();
		$('#postmsgfull_'+postId).show();
		return false;
	});
	
	$('#tagsCheckUncheck').click(function(){
		// loop through our form and check all checkboxes
		if ($(this).attr('checked')){
			$('form[name=frmReserveTags]').find('input[type=checkbox]').attr('checked', true);
		} else {
			$('form[name=frmReserveTags]').find('input[type=checkbox]').attr('checked', false);
		}
	});
	$('.txt_blur').focus(function(){
		if ($(this).attr('id') == 'txtUsername'){
			$(this).val('');
		}
		if ($(this).attr('id') == 'txtPassword'){
			$(this).val('');
		}
		$(this).removeClass('txt_blur').addClass('txt_focus');
	});
	$('.txtPost_blur').focus(function(){
		if ($(this).val() == 'What do you know?'){
			$(this).val('');
		}
		$(this).removeClass('txtPost_blur').addClass('txtPost_focus');
		// show our post form
		$('.innerForm').slideDown();
	});
	$('.txtSearch_blur').focus(function(){
		$(this).val('').removeClass('txtSearch_blur').addClass('txtSearch_focus');
	});
	
	$('.lnkShowRecentSearches').click(function(){
		$('.recentSearches').slideDown();
		$(this).hide();
		return false;
	});
	$('.lnkHideRecentSearches').click(function(){
		$('.recentSearches').slideUp(function(){
			$('.lnkShowRecentSearches').show();
		});
		return false;
	});
	
	// do character counting on our post form
	countPostChars();
	$('#txtPost').keyup(function(){
		countPostChars();
	});
	$('#txtPost').focus(function(){
		countPostChars();
	});
	$('#txtPost2').keyup(function(){
		countPostChars2();
	});
	$('#txtPost2').focus(function(){
		countPostChars2();
	});
	
	// add no follow to all feed links
	$('.feed-item').find('a').attr('rel', 'nofollow');
	$('.comment').find('a').attr('rel', 'nofollow');
});
function countPostChars(){
	if ($('#txtPost') != null){
		var value = $('#txtPost').val();
		if (value){
			var currentLength = $('#txtPost').val().length;
			if (value == 'What do you know?'){
				currentLength = 0;
			} else {
				if (currentLength > 500){
					$('#charCounterVal').addClass('formErrors');
				} else {
					$('#charCounterVal').removeClass('formErrors');
				}
			}
			$('#charCounterVal').html(currentLength);
		}
	}
}
function countPostChars2(){
	if ($('#txtPost2') != null){
		var value = $('#txtPost2').val();
		if (value){
			var currentLength = $('#txtPost2').val().length;
			if (value == 'What do you know?'){
				currentLength = 0;
			} else {
				if (currentLength > 500){
					$('#charCounterVal').addClass('formErrors');
				} else {
					$('#charCounterVal').removeClass('formErrors');
				}
			}
			$('#charCounterVal').html(currentLength);
		}
	}
}
function submitenter(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13){
		myfield.form.submit();
		return false;
	} else {
		return true;
	}
}
