
function console_log(l) {
	if(typeof console != 'undefined')
		console.log(l);
};


jQuery.fn.inputFocus = function() {

	this.bind('focus', function() {
		var t = $(this);
		if (t.val() == t.attr('title')) {
			t.val('');
			t.removeClass("empty");
		}

		window.inputHasFocus = true;
	}).bind('blur', function() {
		var t = $(this);
		if (t.val() == "") {
			t.val(t.attr('title'));
			t.addClass("empty");
		}

		window.inputHasFocus = false;
	});

	return this;
};


$(document).ready(function(){
    $(".forwardForm").click(function() {
		 if ($("#"+$(this).attr("id")+'-form').is(":hidden")) {
		 	$("#"+$(this).attr("id")+'-form').slideDown("slow");
      	} else {
		 	$("#"+$(this).attr("id")+'-form').slideUp("slow");
      }
      });


    $(".forward-link").click(function() {
	 if ($("#"+$(this).attr("id")+'-form').is(":hidden")) {
		 	$("#"+$(this).attr("id")+'-form').slideDown("slow");
      	} else {
		 	$("#"+$(this).attr("id")+'-form').slideUp("slow");
      }
      return false;
      });

	$('#commentform').submit(function() {
		return inputValidation();
	});

	// debug
	var debug = $('#debug');
	if(debug.length > 0) {
		var toggleDebug = $('<a href="">debug</a>').click(function() {
			debug.toggle();
			return false;
		});
		$('#signinout p:eq(0)').append(' [').append(toggleDebug).append(']');
	}

});


function attachCommentForm(commentId, rgt, depth)
{
	$('#commentID').val(commentId);
	$('#parentID').val(rgt);

	if (commentId == '0') {
		$('#cancel-reply').hide();
		$('#comments-form-title').text('Add a comment');
		//$('#comments-add .comment-preview').removeClass().addClass('comment-preview');
	} else {
		$('#cancel-reply').show();
		$('#comments-form-title').text('Reply to '+$('#n'+commentId).text());
		//if(depth > 0) $('#comments-add .comment-preview').addClass('reply'+depth);
	}
	$('#comments-add').insertAfter($('#c'+commentId));
};



function forwardValidation(id)
{
	var validEmail = 0;
	$('#meua-'+id).val(navigator.userAgent);
	var errors = false;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test($('#ForwardEmail'+id).val())) {
		validEmail = 1;
	} else {
		$('#ForwardEmail'+id).addClass('error');
		errors = true;
	}
	if (errors == false) {
		$('#ForwardEmail'+id).removeClass('error');
		return true;
	}
	return false;
};

function inputValidation()
{
	var validName = 0;
	var validEmail = 0;
	var validComments = 0;
	var validPassword = 0;
	var errors = false;
	var userType = $('#userType').val();
	
	$('#meua').val(navigator.userAgent);
	$('#AuthorPassInput').removeClass('error');
	$('#AuthorEmailInput').removeClass('error');
	$('#AuthorNameInput').removeClass('error');
	$('#CommentsInput').removeClass('error');
	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var passValid = /[\w\s]{3,}/;
	if((userType=='member' || userType=='Anonymous') && passValid.test($('#C_Comments').value)) {
		$('#commentform').attr('action','#'+$('#comments-add').parent().attr('id'));
		return true;
	}
	if($('#LoggedIn').val() == 0) {
		if($('#AuthorName').val() != '') {
			validName = 1;
		}
		if (filter.test($('#AuthorEmail').val())) {
			validEmail = 1;
		}
		if (passValid.test($('#AuthorPass').val())) {
			validPassword = 1;
		}
		if($('#AuthorPassInput').css('display') != 'none' && validPassword == 0) {
			$('#AuthorPassInput').addClass('error');
			errors = true;
		}
		if ($('#AuthorNameInput').css('display') != 'none' && validName == 0) {
			$('#AuthorNameInput').addClass('error');
			errors = true;
		}
		if (validEmail == 0) {
			$('#AuthorEmailInput').addClass('error');
			errors = true;
		}
	}
	if (passValid.test($('#C_Comments').val())) {
		validComments = 1;
	}
	if (validComments == 0) {
		$('#CommentsInput').addClass('error');
		errors = true;
	}

	$('#commentform').attr('action','#'+$('#comments-add').parent().attr('id'));

	if (errors == false) {
		return true;
	}
	return false;
};

function previewComments() {
	$('#C_Preview').html($('#C_Comments').val().replace(/\n/g,'<br />'));
};

function makeSlug(str, allow_end_space) {
  var theSlug = '';
	var decodedSlug = '';
	theSlug = str.substring(0,255);
	theSlug = theSlug.toLowerCase();
	for (var n = 0; n < theSlug.length; n++) {
		var c = theSlug.charCodeAt(n);
		if (c <= 127) {
			decodedSlug += String.fromCharCode(c);
		}
		if ((c >=224 && c <=229) || (c>=192 && c<=198) || (c>=281 && c<=286)) {
			decodedSlug += 'a';
		} else if ((c >=232 && c<=235) || (c>=200 && c<=203)) {
			decodedSlug += 'e';
		} else if ((c>=236 && c<=239) || (c>=204 && c<=207)) {
			decodedSlug += 'i';
		} else if ((c>=242 && c<=248) || (c>=210 && c<=216)) {
			decodedSlug += 'o';
		} else if ((c>=249 && c<=252) || (c>=217 && c<=220)) {
			decodedSlug += 'u';
		} else if (c==253 || c==255 || c==221 || c==376) {
			decodedSlug += 'y';
		} else if (c==230 || c==198) {
			decodedSlug += 'ae';
		} else if (c==338 || c==339) {
			decodedSlug += 'oe';
		} else if (c==199 || c==231) {
			decodedSlug += 'c';
		} else if (c==209 || c==241) {
			decodedSlug += 'n';
		} else if (c==352 || c==353) {
			decodedSlug += 's';
		} else if (c==208 || c==240) {
			decodedSlug += 'eth';
		} else if (c==223) {
			decodedSlug += 'sz';
          } else if ((c>=8219 && c<=8223) || c==8242 || c==8243 || c==8216 || c==8217 || c==168 || c==180 || c==729 || c==733) {
              //all the strange curly single and double quotes
		} else if  (c>127) {
			decodedSlug += '-';
		}
	}
	theSlug = decodedSlug;
	theSlug = theSlug.replace(/\'/gi, '');
	theSlug = theSlug.replace(/&/gi, '-and-');
	theSlug = theSlug.replace(/[^a-zA-Z0-9\-]/gi,'-');
	theSlug = theSlug.replace(/\-+/gi, '-');
	if (allow_end_space == undefined) {
  	theSlug = theSlug.replace(/\-$/gi, '');
  }
	theSlug = theSlug.replace(/^\-/gi, '');
	return theSlug;
}

function swapTab(arg) {
  $('#commenterrors').hide();
	$('#commentType').val(arg);
	$('#userType').val(arg);
	if (arg == 'Email') {
		$('#AuthorNameInput').show();
		$('#AuthorURLInput').show();
		$('#AuthorPassInput').hide();
		$('#AuthorEmailInput').show();
		$('#AuthorNotifyHeading').show();
		$('#AuthorNotifyInput').show();
		$('#tabEmail').removeClass().addClass('chosen');
		$('#tabPass').removeClass();
		$('#tabAnonymous').removeClass();
		$('#N_Preview').text(($('#AuthorName').val()==''?'Your Name':$('#AuthorName').val()));
	} else if (arg == 'Pass') {
		$('#AuthorNameInput').hide();
		$('#AuthorURLInput').show();
		$('#AuthorPassInput').show();
		$('#AuthorEmailInput').show();
		$('#AuthorNotifyHeading').show();
		$('#AuthorNotifyInput').show();
		$('#tabEmail').removeClass();
		$('#tabPass').removeClass().addClass('chosen');
		$('#tabAnonymous').removeClass();
		$('#N_Preview').text((window.userName ?'Your Name':window.userName));
	} else {
		$('#AuthorNameInput').hide();
		$('#AuthorURLInput').hide();
		$('#AuthorPassInput').hide();
		$('#AuthorEmailInput').hide();
		$('#AuthorNotifyHeading').hide();
		$('#AuthorNotifyInput').hide();
		$('#tabEmail').removeClass();
		$('#tabPass').removeClass();
		$('#tabAnonymous').removeClass().addClass('chosen');
		$('#N_Preview').text('Anonymous');
	}
};
