jQuery(document).ready(function($){
	var receiver = '';
	var trend = '';
	var userid = '';
	$(".messages").animate({ scrollTop: $(document).height() }, "fast");

	$("#profile-img").click(function() {
		$("#status-options").toggleClass("active");
	});

	$(".expand-button").click(function() {
	  $("#profile").toggleClass("expanded");
		$("#contacts").toggleClass("expanded");
	});

	$("#status-options ul li").click(function() {
		$("#profile-img").removeClass();
		$("#status-online").removeClass("active");
		$("#status-away").removeClass("active");
		$("#status-busy").removeClass("active");
		$("#status-offline").removeClass("active");
		$(this).addClass("active");
		
		if($("#status-online").hasClass("active")) {
			$("#profile-img").addClass("online");
		} else if ($("#status-away").hasClass("active")) {
			$("#profile-img").addClass("away");
		} else if ($("#status-busy").hasClass("active")) {
			$("#profile-img").addClass("busy");
		} else if ($("#status-offline").hasClass("active")) {
			$("#profile-img").addClass("offline");
		} else {
			$("#profile-img").removeClass();
		};
		
		$("#status-options").removeClass("active");
	});

	function newMessage() {
		message = $(".message-input input").val();
		if($.trim(message) == '') {
			return false;
		}
		if($.trim(receiver) == '') {
			return false;
		}
		var action = 'new_chat';
		$.ajax({
 
		  	url:"contents/plugins/messaging/ajax.php",
		  	method:"POST",
		  	data:{action: action, new_message: message, receiver: receiver},
		  	dataType:"json",
		  	success:function(data)
		  	{
		  		refreshMessages(data.trend, data.id);
		  		$('.contact.active .preview').html('<span>You: </span>'+message);
			}
		 
		});	
		$('<li class="sent"><img src="images/default-user.png" alt="" /><p><em>sending...</em></p></li>').appendTo($('.messages ul'));
		$('.message-input input').val(null);
		$('.contact.active .preview').html('<span>You: </span><em>sending...</em>');
		$(".messages").animate({ scrollTop: $(document).height() }, "fast");
	};

	$('.submit').click(function() {
	  newMessage();
	});

	$(window).on('keydown', function(e) {
	  if (e.which == 13) {
	    newMessage();
	    return false;
	  }
	});

	$(".contact").click(function(){
		$(".contact").removeClass("active");
		$(this).addClass("active");
		trend = $(this).data('trend');
		userid = $(this).data('id');
		refreshMessages(trend, userid);
	});

	function refreshMessages(trend, id) {
		var action = "refresh_messages";

		$.ajax({
 
		  	url:"contents/plugins/messaging/ajax.php",
		  	method:"POST",
		  	data:{action: action, trend: trend},
		  	dataType:"json",
		  	success:function(data)
		  	{
		  		var updated = false;
		  		var html = '<ul>';
			   	for (var i in data.messages) {
			   		if(!updated) {
			   			if(id == data.messages[i].sender) {
			   				getImage(data.messages[i].sender, 'sent');
			   				getImage(data.messages[i].receiver, 'replies');
				   			getImage(data.messages[i].receiver, 'contact-profile');
				   			getFullname(data.messages[i].receiver, "fullName");
				   			receiver = data.messages[i].receiver;
				   		} else {
			   				getImage(data.messages[i].receiver, 'sent');
			   				getImage(data.messages[i].sender, 'replies');
				   			getImage(data.messages[i].sender, 'contact-profile');
				   			getFullname(data.messages[i].sender, "fullName");
				   			receiver = data.messages[i].sender;
				   		}
			   			updated = true;
			   		}
			   		html += '<li class="';
			   		if(id == data.messages[i].sender) {
			   			html += 'sent';
			   		} else html += 'replies';
			   		html += '">';
			   		html += '<img src="';
			   		html +='" alt="pix" />';
			   		html += '<p>'+data.messages[i].message+'</p>';
			   		html += '</li>';
			   	}
			   	html += '</ul>';
			   	$(".messages").html(html);
			   	$(".messages").animate({ scrollTop: $(document).height() }, "fast");
			}
		 
		});
	}

	function getImage(id, c) {
		var action = 'get_user_image';
		$.ajax({
 
		  	url:"contents/plugins/messaging/ajax.php",
		  	method:"POST",
		  	data:{action: action, id: id},
		  	dataType:"json",
		  	success:function(data)
		  	{
		  		$("."+c+" > img").attr("src", data.image);
			}
		 
		});	
	}

	function getFullname(id, c) {
		var action = 'get_fullname';
		$.ajax({
 
		  	url:"contents/plugins/messaging/ajax.php",
		  	method:"POST",
		  	data:{action: action, id: id},
		  	dataType:"json",
		  	success:function(data)
		  	{
		  		$("."+c).html(data.fullname);
			}
		 
		});	
	}

	if($('.contact').length) {
		$(".contact")[0].click();
	}

	function refreshLastMessages() {
		var action = 'get_last_messages';
		$.ajax({
			url:"contents/plugins/messaging/ajax.php",
		  	method:"POST",
		  	data:{action: action},
		  	dataType:"json",
		  	success:function(data)
		  	{
		  		console.log(data);
		  		var html = '';
		  		for(var i in data.lastmessages) {
		  			html += '<li class="contact" data-trend="'+data.lastmessages[i].trend+'"' 
		  			html += 'data-id="'+data.lastmessages[i].me+'">';
					html += '<div class="wrap">';
					html += '<span class="contact-status online"></span>';
					html += '<img src="'+data.lastmessages[i].image+'" alt="pix" />';
					html += '<div class="meta">';
					html += '<p class="name">'+data.lastmessages[i].fullname+'</p>';
					html += '<p class="preview">';
					if(data.lastmessages[i].sender == data.lastmessages[i].me) {
						html += 'You: ';
					}
					html += data.lastmessages[i].message + '</p>';
					html += '</div></div></li>';
		  		}
		  		$("#contacts ul").html(html);
			}
		})
	}

		// refreshLastMessages();
	// setInterval(function(){ refreshMessages(trend, userid); }, 10000);
})