$(function(){
	$('.forum-selector-form select').change(function() {
		var id = parseInt($(this).val());
		if (id > 0) {
			var url = $('.forum-selector-form').attr('action');
			url += '?'+$('.forum-selector-form select').attr('name')+'='+id;
			document.location.href = url;
		}
	});
	
	$('.forum-navigator-form select').change(function() {
		var id = parseInt($(this).val());
		if (id > 0) {
			var url = $('.forum-navigator-form').attr('action');
			url += (url.indexOf('?') == -1 ? '?' : '&') + $('.forum-navigator-form select').attr('name')+'='+id;
			document.location.href = url;
		}
	});

	$('.forum-post a.reply').click(function() {
		if (document.getElementById('forum-post-form'))	{
			var href = $(this).attr('href');
			var pattern = /reply=([0-9]+)/;
			var matched = href.match(pattern);
			if (matched != null && parseInt(matched[1]) > 0) {
				return fillForm(matched[1], false);
			}
		}
	});

	$('.forum-post a.thumb').click(function() {
		var href = $(this).attr('href');
		var image = href.match(/image=([0-9]+)/);
		var image_id = image[1];
		var post_id = href.match(/#([0-9]+)/);
		post_id = post_id[1];
		
		var image_url = $('.forum-post a.thumb #post_thumb_' + image_id).attr('src');
		var url = image_url.match(/^.*\/([0-9]+x[0-9]+)\/.*$/);

		image_url = image_url.replace(url[1], '560x420');
		
		$('#forum-post-' + post_id + ' a.thumb').removeClass('selected');
		$(this).addClass('selected');
		$('.forum-post #full_image_' + post_id).attr('src', image_url);
		return false;
	});

	function fillForm(reply_id, quote) {
		setFormReplyToId(reply_id);
		setFormSubject(reply_id);
		$('#forum-post-form #form_message').val('');
		$('#forum-post-form #form_id').val('');
		$('#forum-post-form #form_email').val('');
		$('#forum-post-form #form_email_reply').attr('checked', false); 

		document.location.hash = 'forum_post_form';

		return false;
	}

	function setFormSubject(id) {
		var subject = parseHtmlEntities($('#forum-post-' + id + ' .message h4').html());
		if (subject.indexOf('Re:') != 0) {
			subject = 'Re: ' + subject;
		}
		$('#forum-post-form #form_subject').val(subject);
	}

	function setFormReplyToId(id) {
		$('#forum-post-form #form_replyto_id').val(id);
	}
	
	function parseHtmlEntities(text) {
		text = strip_tags(text);
		text.toString();
		text = text.replace(/&lt;/g, '<');
		text = text.replace(/&gt;/g, '>');
		text = text.replace(/&amp;/g, '&');
		text = text.replace(/&quot;/g, '"');
		text = text.replace(/&#039;/g, '\'');
		return text;
	}

	$('#thread_form #show_form').click(function() {
		$('#thread_form .hide').removeClass('hide');
		$(this).addClass('hide');
	});

	$('#forum_form #show_form').click(function() {
		$('#forum_form .hide').removeClass('hide');
		$(this).addClass('hide');
	});

	$('#group_form #show_form').click(function() {
		$('#group_form .hide').removeClass('hide');
		$(this).addClass('hide');
	});

	$('.list-form input[name=delete_thread_bt]').click(function() {
		if (!confirm(gettext('Are You sure to delete?'))) {
			return false;
		}
	});

	$('.list-form input[name=delete_forum_bt]').click(function() {
		if (!confirm(gettext('Are You sure to delete?'))) {
			return false;
		}
	});

	function strip_tags(str, allowed_tags) {
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: Luke Godfrey
		// +      input by: Pul
		// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   bugfixed by: Onno Marsman
		// +      input by: Alex
		// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
		// *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
		// *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
		// *     returns 2: '<p>Kevin van Zonneveld</p>'
		// *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
		// *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
	 
		var key = '', tag = '', allowed = false;
		var matches = allowed_array = [];
		var allowed_keys = {};
	 
		var replacer = function(search, replace, str) {
			return str.split(search).join(replace);
		};
	 
		// Build allowes tags associative array
		if (allowed_tags) {
			allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
		}
	  
		str += '';
	 
		// Match tags
		matches = str.match(/(<\/?[^>]+>)/gi);
	 
		// Go through all HTML tags
		for (key in matches) {
			if (isNaN(key)) {
				// IE7 Hack
				continue;
			}
	 
			// Save HTML tag
			html = matches[key].toString();
	 
			// Is tag not in allowed list? Remove from str!
			allowed = false;
	 
			// Go through all allowed tags
			for (k in allowed_array) {
				// Init
				allowed_tag = allowed_array[k];
				i = -1;
	 
				if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
				if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
				if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
	 
				// Determine
				if (i == 0) {
					allowed = true;
					break;
				}
			}
	 
			if (!allowed) {
				str = replacer(html, "", str); // Custom replace. No regexing
			}
		}
	 
		return str;
	}
});