$(document).ready(function(){

});

function showNotification(msg)
{
	$('#box').html(msg);
	$('#box').bounceBox();
	$('#box').bounceBoxShow();
	tn = setTimeout('hideNotification()', 4000);
}

function hideNotification()
{
	clearTimeout(tn)
	$('#box').bounceBoxHide();
}

function showWorking()
{
	$('#working').bounceBox();
	$('#working').bounceBoxShow();
}

function hideWorking()
{
	$('#working').bounceBoxHide();
}

function submitComment(id)
{
	var button = $('#comment' + id + ' button');
	var name = $('#comment' + id + ' #name').val();
	var email = $('#comment' + id + ' #email').val();
	var website = $('#comment' + id + ' #website').val();
	var comment = $('#comment' + id + ' #comment').val();
	var post_id = $('#comment' + id + ' #post_id').val();

	button.html('Working...');
	button.attr('disabled', 'disabled');

	if (name == '' || name == 'name' || email == '' || email == 'email' || comment == '' || comment == '...') {
		showNotification('Oops! You need to fill out all the required fields.');
		button.html('Post');
		button.removeAttr('disabled');
	} else if (!validateEmail(email)) {
		showNotification('Oops! Your email address is not valid.');
		button.html('Post');
		button.removeAttr('disabled');
	} else {
		showWorking();
		var timeout = setTimeout('abortComment()', 10000);
		$.post('/ajax/blog/new-comment/', {name: name, email: email, website: website, comment: comment, post_id: post_id}, function(data){
			if (data.status == -1) {
				abortComment();
			} else if (data.status == 1) {
				hideWorking();
				displayComment(id, data)
			}
			clearTimeout(timeout);
		}, 'json');
	}
}

function abortComment()
{
	hideWorking();
	showNotification('There was an error submitting your comment. Please try again later.');
	button.html('Post');
	button.removeAttr('disabled');
}

function emailSubscribe()
{
	var email = $('#email-subscribe').val();
	var button = $('#email-subscribe .submit');

	button.attr('disabled', 'disabled');

	if (!validateEmail(email)) {
		showNotification('Oops! Your email address is not valid.');
		button.removeAttr('disabled');
	} else {
		showWorking();
		$.post('/ajax/blog/subscribe/', {email: email}, function(data){
			if (data.status == -1) {
				hideWorking();
				showNotification('There was an unknown error. Please try later.');
				button.removeAttr('disabled');
			} else if (data.status == 1) {
				hideWorking();
				hideSubscribeForm();
			}
		}, 'json');
	}
}

function hideSubscribeForm()
{
	$('#subscribe-form').hide(600, function(){
		$('.panel-articles').append('<p style="display:none;" class="sub-msg">Thank you for subscribing to Billie Monster! Please check your email for a confirmation link.</p>');
		$('.panel-articles .sub-msg').show(600);
	});
}


function displayComment(id, data) {
	comments = $('#comments-' + id);
	if (data.website == '' || data.website == 'website' || data.website == 'http://') {
		name = data.name
	} else {
		name = '<a href="' + data.website + '" target="_blank">' + data.name + '</a>';
	}
	comment = '<div class="comment" id="comment-' + data.id + '" style="display: none;"><strong>' + name + '</strong> (' + data.created + ')<p>' + data.comment + '</p></div>';
	comments.append(comment);
	$('#comments-' + id + ' .form').hide(600, function(){
		$('#comment-' + data.id).show(600);
	});
}

function emailLink(name, domain, ext, text, subject)
{
	email = name + '@' + domain + '.' + ext;
	if (text == '') text = email;
	htmlLink = '<a href="mailto:' + email + '?subject='+subject+'">'+text+'</a>';
	document.write(htmlLink);
}

function validateEmail(email)
{
 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
 return email.match(re)
}
