function countdown() {
	if(seconds - 1 == -1) {
		if(minutes - 1 == -1) {
			if(hours - 1 == -1) {
				minutes = 0;
			} else {
				hours--;
			}
			
			minutes = 59;
		} else {
			minutes--;
		}
		
		seconds = 59;	
	} else {
		seconds--;
	}
	
	var strHours = String('0' + hours).substr(-2);
	var strMinutes = String('0' + minutes).substr(-2);
	var strSeconds = String('0' + seconds).substr(-2);
	
	$('#countdown').html(strHours + ':' + strMinutes + ':' + strSeconds);
	
	if(hours + minutes + seconds > 0) {
		setTimeout(countdown, 1000);
	}
}

function edit(e) {
	e.preventDefault();
	var id = parseInt(e.currentTarget.id.substr(4));
	$('#postBody' + id).load('/main/edit/' + id, '', function() {
		$('#submit' + id).click(submitFormEdit);
	});
}

function highlight(id) {
	$.scrollTo('#post' + id, 500, {onAfter: function() {
		$('#post' + id).animate({opacity: 0}, 500);
		$('#post' + id).animate({opacity: 1}, 500);	
	}});
}

function reply(author_name, id) {
	$.scrollTo(0, 500, {onAfter: function() {
		$('#body').focus().val('@' + author_name + ' #' + id + ': ');
	}});
	
	return false;
}

function resizeImage(e) {
	var img = e.currentTarget;
	var maxWidth = 448;

	if(img.width > maxWidth) {
		img.height = parseInt(img.height * (maxWidth / img.width));
		img.width = maxWidth;
	}
}

function setNSFW(img) {
	img.css({'opacity': 0.15}).hover(
		function() {
        	$(this).animate({opacity: 1}, 'fast');
      	}, 
      	function() {
			$(this).animate({opacity: 0.15}, 'fast');
		}
	);
}

function submitForm(e) {
	e.preventDefault();
	var error = false;
	$('#authorEmail, #authorName, #body').removeClass('error');
	
	if(!$.trim($('#body').val()).length) {
		$('#body').addClass('error').focus();
		error = true;
	}
	
	if(!$.trim($('#authorEmail').val()).length) {
		$('#authorEmail').addClass('error').focus();
		error = true;		
	}
	
	if(!$.trim($('#authorName').val()).length) {
		$('#authorName').addClass('error').focus();
		error = true;
	}
	
	if(!error) {
		$('#fail').hide();
		$('#form').submit();
	} else {
		$('#fail').fadeIn();
	}
}

function submitFormEdit(e) {
	e.preventDefault();
	var error = false;
	var id = parseInt(e.currentTarget.id.substr(6));
	$('#body' + id).removeClass('error');
	
	if(!$.trim($('#body' + id).val()).length) {
		$('#body' + id).addClass('error').focus();
		error = true;
	}
	
	if(!error) {
		$(document).css({cursor: 'wait'});
		$('#fail' + id).hide();
		$('#submit' . id).unbind('click');
		
		$.post('/main/edit/' + id, {body: $('#body' + id).val(), is_nsfw: $('#isNSFW' + id + ':checked').val() || 0}, function(html) {
			$(document).css({cursor: 'auto'});
			$('#postBody' + id).html(html);
			$('#postBody' + id + ' a.nsfw img.image').each(function(img) { setNSFW($(this)) });
			$('#postBody' + id + ' img.image').load(resizeImage);
		}, 'html');
	} else {
		$('#fail' + id).fadeIn();
	}
}

function init() {
	$('#submit').click(submitForm);
	$('#posts a.nsfw img.image').each(function(img) { setNSFW($(this)) });
	$('#posts img.image').load(resizeImage);
	$('a[id^=edit]').click(edit);
	countdown();
}
