// Submit the answer, but not before we check!
function answer_submit()
{
	var o_answer_form = $('#answer_form');
	if($.trim(o_answer_form[0].answertext.value) == 0)
	{
		$.popup.inline('Please add text to your answer');
		return false;
	}

}

function add_image() 
{
	if(!user.exists)
	{
		var error = '<div style="text-align:center"><b>Share your image!</b></div><br />\
		To upload an image, please <a href="/register?referer='+window.location.pathname+(window.location.search?window.location.search:'')+'"><b>sign up</b></a> for a free account or <a href="/login?return='+window.location.pathname+(window.location.search?window.location.search:'')+'"><b>sign in</b></a>.';
		$.popup.inline(error);
		return false;
	}
	else if(!user.confirmed)
	{
		$.popup.inline("You must confirm your account in order to upload.<br/><br />  Please check your email for confirmation instructions.");
		return false;
	}
	
	$('#image_answer_frame').show("normal");
}

function preview_images()
{
	var url = '/json/answer_images?question='+question_info.id+'&answer='+question_info.aid;
	$.getJSON(url, preview_images_handler);
}

function deleteimage(id, hash)
{			
	var url = '/json/delete_image?qid='+question_info.id+'&aid='+question_info.aid+'&image=' + id + "&hash=" + hash;
	$.getJSON(url, preview_images_handler);
}

function preview_images_handler(obj)
{
	if(obj.result)
		$('#image_answers').html(obj.result);
}

// Video handler
var embedTotal = 0;
var embed_limit = 12; // The maxium allowed embeds
var imageTotal = 1;
function moreEmbed()
{
	if(!user.exists)
	{
		var error = '<div style="text-align:center"><b>Share your video!</b></div><br />\
		To upload a video, please <a href="/register?referer='+window.location.pathname+(window.location.search?window.location.search:'')+'"><b>sign up</b></a> for a free account or <a href="/login?return='+window.location.pathname+(window.location.search?window.location.search:'')+'"><b>sign in</b></a>.';
		$.popup.inline(error);
		return false;
	}
	else if(!user.confirmed)
	{
		$.popup.inline("You must confirm your account in order to upload.<br/><br />  Please check your email for confirmation instructions.");
		return false;
	}
	
	var container = $('#video_answer');

	if(embedTotal > embed_limit)
	{
		$.popup.inline("You have reached the limit of "+embed_limit+" video answers.");
		return;
	}
		
	if(embedTotal == 0)
		container.html('<span class="attach">Attach a video:</span><br />');

	container.append(
		'<div class="videotd">'+
		'<input type="text" name="embedcode[]" size="40"/>&nbsp;<span style="cursor:pointer;" onclick="moreEmbed();">Add another</span>'+
		'</div>'
	);
	embedTotal++;
	
	$('#videotable').show('normal');
}

function preview()
{
	var options =
	{
		url: '/json/answer_preview',
		dataType: 'json',
		success: preview_handler
	};
	$('#answer_form').ajaxSubmit(options);
	$("#preview_notice").html('See Answer Preview Below<br />');
	return false;
}
function preview_handler(obj)
{
	var o_answer_preview = $('#answer_preview');
	var options = 
	{
		display: 'block',
		border: '1px solid #f00',
		padding: '5px',
		whiteSpace: obj.fixed_width ? 'pre' : 'normal'
	}
	o_answer_preview.css(options);
	o_answer_preview.html(obj.result);
}

