var cleanPostFormHTML;
var flashVersion = '9.0.115.0';
var postFormHTML;

function photoCredits()
{
	$('.footerright .last').removeClass('last');
	$('.footerright ul').append('<li class="last"><a href="#">Photo credits</a></li>');
	$('.footerright .last a').click(function() {
		$('#photoCredits').toggle();
		return false;
	});
}

function profilePostForm()
{
	if ($('body.community').length == 0) return false;
	var a = (location.hash.indexOf('postForm') < 0) ? '<a class="tictac" href="#"></a>' : '<a class="tictac open" href="#"></a>';
	$('#postForm h2').wrapInner(a);
	$('#postForm h2 a').click(function() {
		$(this).toggleClass('open');
		$('#postForm .panelContent').slideToggle('fast');
		return false;
	});
	if (location.hash.indexOf('postForm') < 0)$('#postForm .panelContent').hide();
}

function profileMedia()
{
	$('#profileVideos .thumbnails a').click(function() {
		$("div#profileVideos .media").load($(this).attr("href"));
		return false;
	});
	
	$('#profilePhotos .thumbnails a').click(function() {
		$("div#profilePhotos .media").load($(this).attr("href"));
		return false;
	});
}

function discussPostForm()
{
	if ($('body.discuss').length == 0) return false;
	var a = (location.hash.indexOf('postForm') == -1 && location.search.indexOf('reply') == -1) ? '<a class="tictac" href="#"></a>' : '<a class="tictac open" href="#"></a>';
	$('#postForm h2').wrapInner(a);
	$('#postForm h2 a').click(discussAddCommentForm);
	postFormHTML = $('#postForm .panelContent').html();
	cleanPostFormHTML = cleanDiscussFormHTML(postFormHTML);
	if (location.search.indexOf('reply') == -1 && location.hash.indexOf('postForm') == -1) $('#postForm .panelContent').hide();
	$('.leftcol .replyPanel a').click(function() {
		discussAddReplyForm(this);
		return false;
	});
}

function cleanDiscussFormHTML(html)
{
	$('body').append('<div id="cleaningDiv">'+html+'</div>');
	$('#cleaningDiv .form_error').remove();
	$('#cleaningDiv input#reply').remove();
	$('#cleaningDiv label.error').removeClass('error');
	var p = $('#cleaningDiv p:first').text().replace('message', 'comment');
	$('#cleaningDiv p:first').text(p);
	var cleanHTML = $('#cleaningDiv').html();
	$('#cleaningDiv').remove();
	return cleanHTML;
}

function discussAddCommentForm()
{
	$(this).toggleClass('open');
	$('#postForm .panelContent').slideToggle('fast');
	if ($(this).hasClass('open') && $(this).parent().parent().children('.panelContent').length == 0)
	{
		$('.leftcol .replyPanel #replyForm').remove();
		$('.leftcol .replyPanel a').show();
		$(this).parent().parent().append('<div class="panelContent">'+postFormHTML+'</div>');
		$('#postForm .panelContent').hide().slideToggle('fast');
	}
	return false;
}

function discussAddReplyForm(e)
{
	var temp_array = $(e).attr('href').split('=');
	var message_title = $(e).parent().parent().children('h3').text();
	$('.leftcol .replyPanel #replyForm').remove();
	$('.leftcol .replyPanel a').show();
	$('#postForm h2 a').removeClass('open');
	$('#postForm .panelContent').remove();
	$(e).hide();
	$(e).parent().append('<div id="replyForm">'+cleanPostFormHTML+'</div>');
	$('#replyForm input[type=text]').val('');
	$('#replyForm textarea').val('');
	$('#replyForm form').prepend('<input type="hidden" name="reply" id="reply" value="'+temp_array[1]+'" />');
	$('#replyForm form #subject').attr('value', 'Re: '+message_title);
}

function donateFormValidation()
{
	if ($('form[name=creditcard]').length == 0) return false;
	$('form[name=creditcard]').submit(validateDonateForm);
	$('form[name=creditcard] #other2').focus(function(){
		$('form[name=creditcard] #cc_4').attr('checked', 'checked');
	});
}

function validateDonateForm()
{
	var numCheckedRadio = $('form[name=creditcard] input[type=radio]:checked').length;
	var valid = false;
	if (numCheckedRadio > 0)
	{
		if ($('input#cc_4:checked').length == 1)
		{
			if ($('input#other2').val() == '')
			{
				valid = false;
			}
			else
			{
				valid = true;
			}
		}
		else
		{
			valid = true;
		}
	}
	else
	{
		valid = false;
	}
	if (!valid) alert("Please enter a donation amount.");
	return valid;
}

function flashUpdate()
{
	$(this).children('.flashError').show();
	var url = String(location).split('?');
	url.splice(1,0,'?hasFlash=true&');
	url = url.join('');
	$('#flashErrorBypass')
		.attr('href', url)
		.click(flashUpdateBypass);
}

function flashUpdateBypass()
{
	var now = new Date();
	now.setDate(now.getDate()+365);
	document.cookie = "kingsvilleflashupdatebypass="+escape(true)+";expires="+now.toGMTString();
}

$(document).ready(function() {
	if (document.cookie.indexOf("kingsvilleflashupdatebypass=") != -1) flashVersion = '0';
	photoCredits();
	profilePostForm();
	profileMedia();
	discussPostForm();
	donateFormValidation();
});
$.fn.flash.update = flashUpdate;