// 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');
}

$(function(){
	// set form examples
	setExamples();

	// open external links in new window
	$('a[href^=http]').click(function(){
		if (!$(this).is('[href*=' + self.document.location.hostname + ']'))
		{
			return !window.open(this.href);
		}
	});

	// trim empty paragraphs in posts
	$('.post .content p').each(function(){
		var length = $.trim($(this).html()).length;

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

	// add view all link for link lists
	$('#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;
		});
	});
	
	// 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
		});
	});
	
	$('#comments #comment_form p.submit button').hover(function(){
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

	$('img#btn-sample').parents('a').each(function(index){
		var mp3 = $(this).attr('href');

		$(this).before('<span id="player"></span>');

		$(this).prev('#player').flash({
			swf: '/discover/_assets/swf/player.swf',
			height: 48,
			width: 137,
			params: {
				wmode: 'transparent',
				menu: 'false',
				flashvars: {
					bgAudio: mp3
				}
			}
		}).end().remove();
	});
});