
	var Bl = {

		init : function() {

			Bl.comment_form();
			Bl.faves();
			Bl.faves_manage();
			Bl.rater();
			//Bl.thread_reply();
			Bl.tweaks();

		},	//	init : function()



		comment_form : function() {
			if ($('.comment_form').get(0)) {
				$('.comment_form input:submit').removeAttr("disabled");
				$('.comment_form').submit(function() {
					val_comment = $('.comment_form textarea').val();
					if (!val_comment) {
						$('.comment_form textarea').css({ borderColor: '#f66' });
						if ('.choothar') $('.choothar').remove();
						$('<span class="choothar">Your comment can\'t be empty!</span>').insertAfter('#response_form input:submit');
						return false;
					} else {
						$('.comment_form textarea').css({ borderColor: '#5e9dc9' });
						if ('.choothar') $('.choothar').remove();
						$('<img src="/img/icons/ajax_indicator.gif" class="ajax_activity" alt="Please wait&hellip;" />').insertAfter('.comment_form input:submit');
						$('.comment_form input:submit').attr("disabled", "disabled");
						$.ajax({
							type : 'POST',
							url  : '/incs/actions/comment.php',
							data : 'comment=' + val_comment + '&ajax=true',
							success : function(response) {
								response = $(response);
								comment = response.find('comment');

								switch (comment.attr('status')) {
									case 'win' :
										c_content    = comment.find('content').text().replace(/([^>]?)\n/g, '$1'+ "<br />" +'\n');
										c_datetime   = comment.find('datetime').text();
										c_user_id    = comment.find('user_id').text();
										c_user_name  = comment.find('user_name').text();
										c_user_query = comment.find('user_query').text();

										$('<div class="comment"><div class="user_info"><div class="avatar"><img src="/img/thumbs/user' + c_user_id + '.jpg" width="30" height="30" alt="' + c_user_name + '" /></div><div class="details"><h5>Posted by <a href="/users/' + c_user_query + '/">' + c_user_name + '</a></h5><span class="date"> Today <span> - ' + c_datetime + ' <abbr title="Greenwich Mean Time">GMT</abbr></span></span></div></div><p>' + c_content + '</p></div>')
											.appendTo('.comments_list')
											.effect('highlight');

										$('.comment_form')
											.animate({ opacity : 0 }, 500, 'linear', function() {
												$('.comment_form').remove();
											});
									break;

									case 'not_logged_in' :
										$('.ajax_activity').remove();
										$('.comment_form input:submit').removeAttr("disabled");
										if ('.choothar') $('.choothar').remove();
										$('<span class="choothar">You are not logged in!</span>').insertAfter('.comment_form input:submit');
									break;

									default :
										$('.ajax_activity').remove();
										$('.comment_form input:submit').removeAttr("disabled");
										if ('.choothar') $('.choothar').remove();
										$('<span class="choothar">An error occured! Please try again&hellip;</span>').insertAfter('.comment_form input:submit');
									break;

								}
							},

							dataType : 'xml'
						});
					}

					return false;
				});
			}
		},



		faves : function() {
			function perform_ajax(action) {
				$.ajax({
					type : 'GET',
					url  : '/incs/actions/favourite.php',
					data : 'action=' + action + '&ajax=true',
					success : function(response) {
						if (response == 'fail') {
							alert('You must be logged in to add or remove favourites!');
						} else {
							$('.favourites p').animate({
								opacity: 0
							}, 250, 'swing', function() {
								$(this).empty().append(response).animate({ opacity: 1 }, 250);
								prep_links();
							});
						}
					}
				});
			}

			function prep_links() {
				actions = ['add', 'remove'];
				$(actions).each(function() {
					prep_link(this);
				});
			}

			function prep_link(action) {
				$('.fave_'+action).click(function() {
					perform_ajax(action);
					return false;
				});
			}

			if ($('.favourites').get(0)) prep_links();
		},



		faves_manage : function() {
			if ($('.faves_list').get(0)) {
				$('.faves_list li:odd').css({ backgroundColor : '#dceffb' });
				$('.faves_list span').css({ display : 'none' });
				
				$('.faves_list span a').each(function() {

					$(this).click(function() {
						this_li = $(this).parents('li');

						url_array = $(this).attr('href').split('/');

						item_type = url_array[url_array.length - 2];
						item_id   = url_array[url_array.length - 1];

						this_li.effect('highlight', { color : '#f66'}, 250)
							.animate({ opacity : 0 }, 250)
							.effect('blind', {}, 250, function() {
								this_li.remove();

								$('.faves_list li').css({ backgroundColor : '#fff' });
								$('.faves_list li:odd').css({ backgroundColor : '#dceffb' });
							});
						
						$.ajax({
							type    : 'GET',
							url     : '/incs/actions/remove_favourite.php',
							data    : 'item_type=' + item_type + '&id=' + item_id + '&ajax=true',
							success : function(response) {
								if (response == 'win') {
									
								}
								console.log(response);
							}
						});

						return false;
					});
				});

				$('.faves_list li').hover(
					function() {
						$(this).find('span').css({ display : 'block' });
					},
					function() {
						$(this).find('span').css({ display : 'none' });
					}
				);
			}
		},



		thread_reply : function() {
			if ($('.thread_reply_form').get(0)) {
				$('.thread_reply_form input:submit').removeAttr("disabled");
				$('.thread_reply_form').submit(function() {
					val_response = $('.thread_reply_form textarea').val();
					if (!val_response) {
						$('.thread_reply_form textarea').css({ borderColor: '#f66' });
						if ($('.choothar').get(0)) $('.choothar').remove();
						$('<span class="choothar">Your comment can\'t be empty!</span>').insertAfter('#response_form input:submit');
						return false;
					} else {
						$('.thread_reply_form textarea').css({ borderColor: '#5e9dc9' });
						if ($('.choothar').get(0)) $('.choothar').remove();
						$('<img src="/img/icons/ajax_indicator.gif" class="ajax_activity" alt="Please wait&hellip;" />').insertAfter('#response_form input:submit');
						$('.thread_reply_form input:submit').attr("disabled", "disabled");
						$.ajax({
							type : 'POST',
							url  : '/incs/actions/post_forum_reply.php',
							data : 'response=' + val_response + '&ajax=true',
							success : function(response) {
								response = $(response);
								reply = response.find('reply');
								switch (reply.attr('status')) {
									case 'win' :
										r_content    = reply.find('content').text().replace(/([^>]?)\n/g, '$1'+ "<br />" +'\n');
										r_datetime   = reply.find('datetime').text();
										r_user_id    = reply.find('user_id').text();
										r_user_name  = reply.find('user_name').text();
										r_user_query = reply.find('user_query').text();

										$.ajax({
											dataType : 'html',
											data     : { ajax : true, uid : r_user_id },
											src      : '/incs/actions/get_avatar.php',
											success  : function(response) {
												console.log(response);
												avatar = response;
											},
											type     : 'GET'
										});

										reply_text = '<div class="clearfix thread_reply"><div class="user_info"><div class="avatar">'+avatar+'</div><div class="details"><h3>Posted by <a href="/users/'+r_user_query+'/">'+r_user_name+'</a></h3><span class="date"> Today <span> - '+r_datetime+' <abbr title="Greenwich Mean Time">GMT</abbr></span></span></div><div class="content_block">'+r_content+'</div></div></div>';
										
										$(reply_text).appendTo('.replies_list');

										$('div.thread_reply').removeClass('alt');
										$('div.thread_reply:even').addClass('alt');
										$('div.thread_reply:last').effect('highlight');
										$('.thread_reply_form textarea')
											.animate({ opacity : 0 }, 500, 'linear', function() {
												$('.thread_reply_form').remove();
											});
									break;
									case 'not_logged_in' :
										$('.ajax_activity').remove();
										$('.thread_reply_form input:submit').removeAttr("disabled");
										if ('.choothar') $('.choothar').remove();
										$('<span class="choothar">You are not logged in!</span>').insertAfter('#response_form input:submit');
									break;
									default :
										$('.ajax_activity').remove();
										$('.thread_reply_form input:submit').removeAttr("disabled");
										if ('.choothar') $('.choothar').remove();
										$('<span class="choothar">An error occured! Please try again&hellip;</span>').insertAfter('#response_form input:submit');
									break;
								}
							},
							dataType : 'xml'
						});
					}
					return false;
				});
			}
		},



		rater : function() {
			if ($('.rater').get(0)) {
				$('.rater input:checked').parent().parent().addClass('checked');
				$('.rater li').click(function() {
					if ($(this).hasClass('checked')) return;
					$('.rater ul').addClass('loading');
					$('.rater input').removeAttr('checked')
					$(this).find('input').attr('checked', 'checked');
					$('.rater li').removeClass('checked');
					$('.rater input:checked').parent().parent().addClass('checked');
					rating = $('.rater input:checked').val();
					module = $('.rater input#module').val();
					unique = $('.rater input#unique').val();
					$.post('/incs/actions/rater.php', { ajax : true, rating : rating, module : module, unique : unique }, function (response) {
						num_useful = parseFloat($('.rater .num_useful').html());
						num_total  = parseFloat($('.rater .num_total').html());
						flash_useful = true;
						flash_total  = true;
					//	alert(response);
						if (response) {
							//	First time rating this video
							if (rating == 'y') {
								num_useful++;
							} else {
								flash_useful = false;
							}
							num_total++;
						} else {
							//	Changing their flippin' tune
							num_useful  = rating == 'y' ? num_useful + 1 : num_useful - 1;
							flash_total = false;
						}
						$(this).delay(1000, function() {
					//		$('.rater ul').removeClass('loading');
							$('.rater span.did_you').fadeOut();
							$('.rater ul').fadeOut();	// temp (we'll get it allow changing of minds later)
							if (flash_useful) $('.rater .num_useful').html(num_useful).effect('highlight');
							if (flash_total)  $('.rater .num_total ').html(num_total ).effect('highlight');
						});
					});	//	$.post()
				});	//	$('.rater li').click()
			}	//	if ($('.rater').get(0))
		},	//	rater : function()



		tweaks : function () {
			//	TWEAK CSS AND THAT
			$('.date span').css({ display : 'none' }).parent().hover(
				function() { $(this).find('span').css({ display : 'inline' }); },
				function() { $(this).find('span').css({ display : 'none' }); }
			);
			$('.thread_reply .details span.date span').css({ display : 'none' }).parent().hover(
				function() { $(this).find('span').css({ display : 'inline' }); },
				function() { $(this).find('span').css({ display : 'none' }); }
			);
			$('div.comment:even').addClass('alt');
			$('div.thread_reply:even').addClass('alt');
			$('.breadcrumb li:last').css({ background : 'none', paddingRight : 0 });
			$('.forum_content tbody tr:even').css({ backgroundColor : '#ecf6fd' });

			//	Hide stuff
			$('.hide_inner').each(function() { $(this).removeClass('hide_inner').wrapInner('<span class="hide_this"></span>'); });

			//	FIREBUG - Load it up, load it up real good!
			if ($.isFunction(window.loadFirebugConsole))
				window.loadFirebugConsole();
//			else
//				$('<script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script>').appendTo('head');
		}	//	tweaks : function ()

	};	//	var Bl = {}

	//	Do it up, DO IT UP!
	$(function() { Bl.init(); });
