// set form examples
function setExamples(){
	$('form dl.examples dt').hide();
	$('form dl.examples dd input[type="text"], form dl.examples dd input[type="password"], form dl.examples dd textarea, #aside form p input[type="text"]').each(function(){
		if($(this).val().length <= 0){
			$(this).example(function(){
				
				var label = $('label', $(this).parents('dd').prev('dt')).text().replace(':', '');
				
				if (label != '')
				{
					return label;
				}
				else{
					return $(this).attr('title');
				}
			});
		}
	});
}

// tests for valid email
function is_email(s){
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return reg.test(s);
}

// tests for valid url
function is_url(s){
	var reg = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	
	return reg.test(s);
}

// handles AJAX form validation
function validate(formData, jqForm, options){ 
	$(jqForm).addClass('active');
	$('body').addClass('wait');

	var regemail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var errors = '';

	for (var i=0; i < formData.length; i++){
		if(!formData[i].value){
			if(formData[i].name == 'name'){
				errors = errors + '<li>The Name field is required</li>';
			}

			if(formData[i].name == 'email'){
				errors = errors + '<li>The Email field is required</li>';
			}

			if(formData[i].name == 'comment'){
				errors = errors + '<li>The Comment field is required</li>';
			}
		}
		else if(formData[i].name == 'email' && !is_email(formData[i].value)){
			errors = errors + '<li>A valid Email is required</li>';
		}
		else if(formData[i].name == 'url' && !is_url(formData[i].value)){
			errors = errors + '<li>The URL field must be in the form of "http://domain.com"</li>';
		}
	}
	
	if(errors.length > 0){
		$('form.active').removeClass('active').prev('.messages').css('display', 'block').hide().html('<ul class="errors">' + errors + '</ul>').fadeIn(300);
		setExamples();
		$(jqForm).removeClass('active');
		$('body').removeClass('wait');

		return false;
	}
	else{
		return true;
	}
}

function commentResponse(responseText, statusText){
	if(responseText.search('<div id="content">') != -1){
		errors = responseText.substring(responseText.indexOf('<div id="content">') + '<div id="content">'.length);
		errors = errors.substring(0, errors.indexOf('</div>'));
		errors = errors.substring(errors.indexOf('<ul>') + '<ul>'.length);
		errors = errors.substring(0, errors.indexOf('</ul>'));

		if(errors != null){
			if(errors.length > 0){
				$('form.active').removeClass('active').prev('.messages').css('display', 'block').hide().html('<ul class="errors">' + errors + '</ul>').fadeIn(300);
				setExamples();
			}
			else{
				window.location.reload();
			}
		}
		else{
			window.location.reload();
		}
	}
	else{
		window.location.reload();
	}

	$('body').removeClass('wait');
}

$(document).ready(function(){
	// set form examples
	setExamples();

	// handle comment form submission via AJAX
	$('form#comment_form').each(function(){
		// rewrite comment form actions to be handled via AJAX without error
		var newAction = $(this).attr('action').toString().replace(window.location.protocol + '//' + window.location.hostname, '');

		$(this).attr('action', newAction).ajaxForm({
			beforeSubmit: validate,
			success: commentResponse
		});
	})

	$('.post .content p').each(function(){
		var length = $.trim($(this).html()).length;

		if(length <= 0){
			$(this).remove();
		};
	});

	$('.post .content p img').each(function(){
		if ($(this).width() <= 250 && $(this).width() > 50 && $(this).height() > 25)
		{
			$(this).wrap('<span class="inline"></span>');
		}
		else if ($(this).height() > 25)
		{
			$(this).wrap('<span class="block"></span>');
		}
	});

	$('#link_list ul').each(function(){
		var listItems = $('li', this);

		listItems.each(function(i){
			if (i > 9)
			{
				$(this).addClass('inactive');
			}
		});

		if (listItems.length > 9)
		{
			$(this).append('<li class="more"><a href="#">view all &raquo;</a></li>')
		}

		$('li.more a', this).click(function(){
			$('li.inactive', $(this).parents('ul')).removeClass('inactive')

			$(this).parents('li.more').remove();

			return false;
		});
	});

	// create AddThis button
	$('.posted a.addthis').hover(function(){
		var url = $('.info h2 a', $(this).parents('.post')).attr('href');
		var title = $('.info h2 a', $(this).parents('.post')).text();

		return addthis_open(this, '', url, title);
	}, function(){
		addthis_close();
	}).click(function(){
		return addthis_sendto();
	});

	// add custom style to account for addthis style bug in safari
	if ($.browser.safari)
	{
		$('.posted a.addthis').css({'display': 'inline-block', 'margin-top': '-3px'})
	}
});
