
$(document).ready(function() {
	forum.init();
});

var forum = {

	init: function() {
		this.handleButtons();
		this.handleBBCbuttons();
		this.handleSmileys();
		this.handleUseredit();
		this.handlePM();
		this.handlePoll();
	},

	handleUseredit: function() {
		$('#filters-data div[class!=off]').click(function() {
			if (!$(this).hasClass("act")) {
				window.location.hash = $('a',this).attr("rel");
				$(this).parent().children().removeClass("act");
				$('#filter-results').css("opacity","0.5");
				$(this).addClass("act");
				$('.form-errors').hide();
				$.ajax({
					url: '/forums/ajax/user/edit',
					type: 'POST',
					data: 'filter='+$('a',this).attr("rel"),
					success: function (r) {
						$('#filter-results').html(r);
						$('#filter-results').fadeTo(300,1);
						//forum.handleBBCbuttons();
						//forum.handleSmileys();
					}
				});
			}
		});

		if (window.location.hash && $('#filters-data').length) {
			hash = window.location.hash.substr(1);
			if (!hash.length) { return false; }

			if (!$('a[rel='+hash+']').parent().hasClass("act")) {
				$('a[rel='+hash+']').parents('.filters').children().removeClass("act");
				$('#filter-results').css("opacity","0.5");
				$('a[rel='+hash+']').parent().addClass("act");
				$.ajax({
					url: '/forums/ajax/user/edit',
					type: 'POST',
					data: 'filter='+hash,
					success: function (r) {
						$('#filter-results').html(r);
						$('#filter-results').fadeTo(300,1);
						//forum.handleBBCbuttons();
						//forum.handleSmileys();
					}
				});
			}
		}
	},

	handlePM: function() {
		$('#pm-user-pick').click(function() {
			window.open('/forums/ajax/user/pick','userpick','width=650,height=470,scrollbars=1');
		});

		$('.userpick-do').click(function(e) {
			e.preventDefault();

			d = $(this).attr("rel").split(":");
			d[2] = d[2].replace("<","&lt;");
			d[2] = d[2].replace(">","&gt;");

			$('input[name=user_to]',window.opener.document).val(d[0]);
			$('#pm-user-to',window.opener.document).html('<a href="/forums/userinfo.php?id='+d[0]+'"'+(d[1]?' style="color:#'+d[1]+'" class="forum-user-colored"':'')+'>'+d[2]+'</a>');
			window.close();
		});
	},

	handleButtons: function() {
		$('.forum-button').hover(
			function() {
				$(this).addClass("hover");
			},
			function() {
				$(this).removeClass("hover");
			}
		);
	},

	putBBcode: function(tag) {
		element = $('#post_text').get(0);
		element.focus();

		tag0 = tag[0];
		tag1 = tag[1];
		single = false;
		if (tag.constructor.toString().indexOf("Array") == -1) {
			tag0 = "";
			tag1 = tag;
			single = true;
		}

		if (element.createTextRange) {
			sel = document.selection.createRange();
			sel.text = tag0 + sel.text + tag1;
			if (!single) {
				sel.moveStart('character',tag0.length);
				sel.moveEnd('character',tag1.length*1);
			}
		} else if ('selectionStart' in element) {
			startPos = element.selectionStart;
			endPos = element.selectionEnd;
			selText = element.value.substring(startPos, endPos);
			element.value = element.value.substring(0, startPos) + tag0 + selText + tag1 + element.value.substring(endPos, element.value.length);
			if (!single) { element.setSelectionRange(startPos+tag0.length,endPos+tag0.length); }
		} else {
			element.value += tag0 + tag1;
		}

		element.focus();
	},

	handleBBCbuttons: function() {
		$('button.bbcode-insert').live('click', function(e) {
			e.preventDefault();

			tag = $(this).val().split(':');
			forum.putBBcode(tag);
		});

		$('select.bbcode-insert').live('change', function() {
			tag = $(this).val().split(':');
			forum.putBBcode(tag);

			$(this).val(0);
		});

		$('div.bbcode-insert').live('click', function() {
			tag = $(this).attr("title").split(':');
			forum.putBBcode(tag);
		});
	},

	handleSmileys: function() {
		$('.insert-smiley').live('click', function(e) {
			e.preventDefault();

			forum.putBBcode($(this).attr("rel"));
		});
	},

	handlePoll: function() {
		$('.poll-vote').live('click',function(e) {
			e.preventDefault();

			d = $(this).attr("rel").split(":");

			$.ajax({
				url: '/forums/ajax/poll/' + d[0] + '/' + d[1],
				type: 'POST',
				success: function (r) {
					$('#poll-return').html(r);
				}
			});
		});

		$('.poll-revote').live('click',function(e) {
			e.preventDefault();

			$.ajax({
				url: '/forums/ajax/poll/revote/' + $(this).attr("rel"),
				type: 'POST',
				success: function (r) {
					$('#poll-return').html(r);
				}
			});
		});
	}

}

