$.fn.moveToEnd = function() {
  return this.each(function() {
		if ($(this).get(0).setSelectionRange) {
		  var pos = $(this).val().length;
	    $(this).get(0).setSelectionRange(pos, pos);
	  } else if ($(this).get(0).createTextRange) {
	    var range = $(this).get(0).createTextRange();
	    var pos = $(this).val().length;
	    range.collapse(true);
	    range.moveEnd('character', pos);
	    range.moveStart('character', pos);
	    range.select();
	  }
	});
}

/*
* Forum.js - basic forum functionalities
*/
var Forum = {
	
	staff: ['milton', 'Colleen', 'Chris', 'linda', 'lionel', 'erika', 'rishi', 'Ben', 'erik.beeson', 'tyl', 'Bram', 'Erik', 'john', 'yuenlin', 'kenny'],
	
	render: function() {
		
		//Rounded corners
		$('.category').corner('5px');
		$('.breadcrumbs').corner('5px');
		$('.post').corner('5px');
		$('.posts').corner('bottom 5px');
		$('.post-topic-title').corner('top 5px');
		$('.reply-form').corner('5px');
		
		//Make topic rows clickable
		$('.topic-row').click(function() {
			window.location = $(this).children('td').children('a').attr('href');
		}).mouseenter(function() {
			$(this).children('td').children('a').addClass('hover');
			$(this).addClass('hover');
		}).mouseleave(function() {
			$(this).children('td').children('a').removeClass('hover');
			$(this).removeClass('hover');
		});
		
		$('.author').each(function() {
			if (Forum.isStaff($(this).children('.by').html())) {
				//$(this).children('.by').addClass('staff');
				$(this).parent().addClass('staff');
			}
		});
		
		$('.flashup').click(function() {
			
			var flasher = $(this).siblings('.flash');
			flasher.show();
			
			//Exclude the button quote, so it won't scroll up
			if (!$(this).hasClass('.button-quote')) $('#login-vseeid').focus();
			setTimeout(function() {
				flasher.fadeOut(1000);
			}, 1000);
			
			return false;
		});
		
		// //Do these after posts loaded and jumped
		// $(document).ready(function() {
		// 	//Check the address. If there is #pxxxx, trying to highlight the post
		// 	var loc = window.location.toString();
		// 	if (loc.match(/#p\d+/)) {
		// 
		// 		var postId = loc.substring(loc.indexOf('#')+2);
		// 		var thePost = $('#post-'+postId);
		// 		var originalColor = thePost.css('backgroundColor');
		// 
		// 		//Make it yellow!
		// 		thePost.css('backgroundColor', '#ffa');
		// 
		// 		thePost.animate({'backgroundColor':originalColor}, 2000);
		// 	}
		// });
		
		//Quotations:
		$('.button-quote').click(function() {
			
			if ($(this).hasClass('flashup')) return false;
			
			var author = $(this).parent().siblings('.author').children('.by').html();
			var body = $.trim($(this).siblings('div.post-body').html());
			body = body.replace(/\n/g, "");
			//Other quotes
			body = body.replace(/<div class="quote"><div class="header">Quoting /gi, "[quote:");
			body = body.replace(/:<\/div><div class="body">/gi, "]");
			body = body.replace(/<\/div><\/div>/gi, "[/quote]");
			body = body.replace(/<a href="(.*)">.*<\/a>/gi, "$1");
			var quote = "[quote:"+author+"]"+body+"[/quote]\n";
			
			var originalValue = $('#reply-body').val();
			if (originalValue != '') originalValue += "\n";
			
			var finalQuote = originalValue + quote;
			finalQuote = finalQuote.replace(/<br>/gi, "\n");
			
			$('#reply-body').val(finalQuote).moveToEnd().scrollTop(90000);
			
			
			var flasher = $(this).siblings('.flash');
			flasher.html('Message quoted. Scroll down to reply now.');
			flasher.show();
			setTimeout(function() {
				flasher.fadeOut(1000);
			}, 1000);
		});
		
	},
	
	isStaff: function(staff) {
		var isStaff = false;
		$.each(Forum.staff, function(i, o) {
			if (o == staff) {
				isStaff = true;
				return false;
			}
		});
		return isStaff;
	}
};
