$(document).ready(function ()
{
	var UID = $('#activity_wire').data("id");
	var wiretype = $('#activity_wire').data("wiretype");
	var wire_url = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=activity_wire&UID=' + UID + '&wiretype=' + wiretype;
	$("#hold_wire_content").hide();
	$("#spinner").show();
	$('#pulling_wire').val(1);

	//console.log(wire_url);

	var myxsl = '/assets/css/activitywire.xsl';
	var formData = {
		xslurl: myxsl,
		xmlurl: wire_url,
		wiretype: wiretype,
		UID: UID
	};
	var xslurl = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=xslt';
	//console.log('hi');
	//console.log('xslurl' + xslurl);
	//console.log('hi2');
	//console.log(formData);	

	$.ajax(
	{
		url: xslurl,
		type: "POST",
		data: formData,
		success: function (data, textStatus, jqXHR)
		{
			process_wire(data);
		},
		error: function (jqXHR, textStatus, errorThrown)
		{
			createGrowl('Error displaying wire.', 'Error Notice', 'qtip-red qtip-rounded qtip-shadow');
		}
	});
	display_comment_box(UID, wiretype);
});

$(window).scroll(function ()
{
	if ($(window).scrollTop() == $(document).height() - $(window).height())
	{
		// run our call for pagination

		if ($("#hold_wire_content").attr('data-last_id'))
		{
			var if_pulling = $('#pulling_wire').val();
			if (if_pulling == 0)
			{
				var UID = $('#activity_wire').data("id");
				var last_id = $('#hold_wire_content').data("last_id");
				var wiretype = $('#activity_wire').data("wiretype");
				var wire_url = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=activity_wire&UID=' + UID + '&wiretype=' + wiretype + '&last_id=' + last_id;
				$("#hold_wire_content").hide();
				$("#spinner").show();
				$('#pulling_wire').val(1);

				var myxsl = '/assets/css/activitywire.xsl';

				var formData = {
					xslurl: myxsl,
					xmlurl: wire_url
				};

				console.log(formData);

				var scroll_url = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=xslt&UID=' + UID + '&wiretype=' + wiretype + '&last_id=' + last_id;

				$.ajax(
				{
					url: scroll_url,
					type: "POST",
					data: formData,
					success: function (data, textStatus, jqXHR)
					{
						process_wire(data);
					},
					error: function (jqXHR, textStatus, errorThrown)
					{
						createGrowl('Error displaying wire.', 'Error Notice', 'qtip-red qtip-rounded qtip-shadow');
					}
				});
			}
		}
	}
});

function process_wire(data)
{
	var newmarkup = data;
	var newmarkup2 = decode_entities(newmarkup);
	var newmarkup3 = newmarkup2.replace("<body>", " ").replace("<html>", " ").replace("</html>", " ").replace("</body>", " ");

	if (newmarkup.length > 32)
	{
		$('#feedul').append(newmarkup3);
		$("#spinner").hide();
		$('#pulling_wire').val(0);
		$('#hold_wire_content').empty();
		var last_id = $("#feedul").children().last().data('feed_id');
		var mydata = $("#hold_wire_content").data();
		mydata.last_id = last_id;
		$("#hold_wire_content").attr("data-last_id", last_id);
		$('.comment_area_wire').autosize();
		$('.main_comment_area').autosize();
		$('#nomoreresults').hide();
	}
	else
	{
		var utype = $('#activity_wire').data('wiretype');

		if ($("#feedul > .item").length > 0)
		{
			$('#nomoreresults').show().addClass('alert alert-warning mb20 text-center').html('No more items available in this wire.');
		}
		else
		{
			if (utype == 'member')
			{
				$('#nomoreresults').show().addClass('alert alert-warning mb20 text-center').html('No items have been posted to this wire or you are not connected to this member. <b class="text-danger">Click the follow button</b> under their name to send a connection request.');
			}
			else
			{
				$('#nomoreresults').show().addClass('alert alert-warning mb20 text-center').html('No items have been posted or you do not have permission to view this wire.');
			}
		}

		$("#spinner").hide();
	}
}

var decode_entities = (function ()
{
	// Remove HTML Entities
	var element = document.createElement('div');

	function decode_HTML_entities(str)
	{

		if (str && typeof str === 'string')
		{

			// Escape HTML before decoding for HTML Entities
			str = escape(str).replace(/%26/g, '&').replace(/%23/g, '#').replace(/%3B/g, ';');

			element.innerHTML = str;
			if (element.innerText)
			{
				str = element.innerText;
				element.innerText = '';
			}
			else
			{
				// Firefox support
				str = element.textContent;
				element.textContent = '';
			}
		}
		return unescape(str);
	}
	return decode_HTML_entities;
})();

function display_comment_box(UID, wiretype)
{
	var comment_box_url = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=activity_wire_comment_box&UID=' + UID + '&wiretype=' + wiretype;
	$.ajax(
	{
		url: comment_box_url,
		type: "GET",
		success: function (data, textStatus, jqXHR)
		{
			console.log('hi' + data);
			$("#main_comment_box_container").html(data);
			$('.comment_area_wire').autosize();
			$('.main_comment_area').autosize();
		}
	});
}

function killthread(myid)
{
	var UID = $('#activity_wire').data("id");
	var wiretype = $('#activity_wire').data("wiretype");
	var remove_url = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=feed_remove&UID=' + UID + '&wiretype=' + wiretype + '&site_use_feed_id=' + myid;
	$.ajax(
	{
		type: "POST",
		url: remove_url,
		success: function (data)
		{
			if (data.length)
			{
				alert_confirm('Only the original poster can remove this thread.');
			}
			else
			{
				$("#item-" + myid).fadeOut(1000, function ()
				{
					$(this).remove();
				});
			}
		}
	});
	return false;
}

function killcomment(myid)
{
	var UID = $('#activity_wire').data("id");
	var wiretype = $('#activity_wire').data("wiretype");
	var remove_url = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=comment_remove&UID=' + UID + '&wiretype=' + wiretype + '&comment_id=' + myid;
	//console.log(remove_url);
	$.ajax(
	{
		type: "POST",
		url: remove_url,
		success: function (data)
		{
			{
				if (data.length)
				{
					alert_confirm('Only the original poster can remove this thread.');
				}
				else
				{
					$("#comment-" + myid).fadeOut(1000, function ()
					{
						$(this).remove();
					});
					$("#cid" + myid).fadeOut(1000, function ()
					{
						$(this).remove();
					});
				}
			}
		}
	});
	return false;
}

$(document).on("click", ".thread_delete", function ()
{
	var myid = $(this).data('id');
	killbootstrap();
	var dialog =
		new BootstrapDialog(
		{
			title: '<h3>Please Confirm</h3>',
			message: 'Are you sure you want to delete this thread?',
			buttons: [
			{
				label: 'Close',
				action: function (dialog)
				{
					dialog.close();
				}
			},
			{
				label: 'I wish to continue',
				action: function (dialog)
				{
					dialog.close();
					killthread(myid);
				}
			}]
		}).open();
	event.preventDefault();
});

$(document).on("click", ".comment_delete", function ()
{
	var myid = $(this).data('id');
	killbootstrap();
	var dialog =
		new BootstrapDialog(
		{
			title: '<h3>Please Confirm</h3>',
			message: 'Are you sure you want to delete this comment?',
			buttons: [
			{
				label: 'Close',
				action: function (dialog)
				{
					dialog.close();
				}
			},
			{
				label: 'I wish to continue',
				action: function (dialog)
				{
					dialog.close();
					killcomment(myid);
				}
			}]
		}).open();
	event.preventDefault();

});

// code for fade in element by element
$.fn.fadeInWithDelay = function ()
{
	var delay = 0;
	return this.each(function ()
	{
		$(this).delay(delay).animate(
		{
			opacity: 1
		}, 200);
		delay += 100;
	});
};

$(document).on("click", "#main_post_button .btn", function ()
{
	var UID = $('#activity_wire').data("id");
	var wiretype = $('#activity_wire').data("wiretype");
	var feed_update_url = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=feed_update&UID=' + UID + '&wiretype=' + wiretype;
	var main_post_content = $("#main_post textarea").val();
	var len_main_post_content = main_post_content.length;
	if (typeof main_post_content === 'undefined' || len_main_post_content == 0)
	{
		alert_confirm('Please enter your message.');
	}
	else
	{

		var formData = {
			'wire_post': main_post_content
		};
		//console.log(feed_update_url);
		$.ajax(
		{
			type: "POST",
			url: feed_update_url,
			data: formData,
			success: function (data)
			{
				if (data.length)
				{
					$("#feedul").prepend(data);
					$('.comment_area_wire').autosize();
					$('.main_comment_area').autosize();
					$("#main_post textarea").val('');
				}
				else
				{
					alert_confirm('Duplicate post found or post is too short. Please try again.');
				}
			}
		});
		return false;
	}
});

$(document).on("keyup", ".comment_area_wire", function (e)
{
	if (e.which == 13 && e.shiftKey)
	{
		var content = this.value;
		var caret = getCaret(this);
		this.value = content.substring(0, caret) + "\n" + content.substring(parent, content.length - 1);
		e.preventDefault();
	}
	else if (e.which == 13)
	{
		var comment_post_content = $(this).val();
		var len_comment_post_content = comment_post_content.trim().length;
		var comment_type = $(this).data('comment-type');
		var id = $(this).data('id');
		if (typeof comment_post_content === 'undefined' || len_comment_post_content == 0)
		{
			alert_confirm('Please enter your comment.');
		}
		else
		{
			var UID = $('#activity_wire').data("id");
			var wiretype = $('#activity_wire').data("wiretype");
			var formData = {
				'comment_post': comment_post_content
			};
			var myurl = window.location.protocol + '//' + document.domain + '/ajax_results.cfm?ajax_type=comment_update&comment_type=' + comment_type + '&content_id=' + id + '&UID=' + UID + '&wiretype=' + wiretype;
			$.ajax(
			{
				type: "POST",
				url: myurl,
				data: formData,
				success: function (data)
				{
					if (data.length)
					{
						$("#comments_" + id).prepend(data).show();
						$('.comment_area_wire').autosize();
						$("#comment_area_" + id).val('');
					}
					else
					{
						alert_confirm('Duplicate post found or post is too short. Please try again.');
					}
				}
			});
			return false;
		}
	}
});

$(document).on("click", ".comments_thread", function ()
{
	var id = $(this).data('id');
	$("#comments_" + id).toggle();
});

$(document).on("click", ".logged_out_comments_thread", function ()
{
	var id = $(this).data('id');
	$("#comments_" + id).toggle();
	$("#comments_container_" + id).toggle();
});

$(document).on("click", "#main_post", function ()
{
	$("#main_post_button").show();
});