</div>
	</div>
<?php include "layout/sidebar.php"; ?>
	</div>
</div>
<?php
$query = http_build_query(array_filter(array(
	//'instance' => ($instance == true ? $instance : false) ,
	'user' => (isset($tl['user']) == true ? $tl['user'] : false) ,
	'mode' => $tl['mode'],
	//'explicit' => ($nocookies == true ? $user_settings['explicit'] : false) ,
)));
?>
	<div class="mobile" style="display:block; height:50px;"></div>
	<script src="vendor/jquery.min.js"></script>
	<script src="vendor/jqueryui.js"></script>
	<script src="js/scripts.js"></script>
	<script type="text/javascript">
	
	window.setInterval(function() {
		$( ".post > .replies_container" ).each(function() {
		  var len = $(this).find('.reply').length;
		  if(len > 0){
			  var id = $(this).attr('id');
			  var last = $(this).find('.reply').last().attr('id');
			  $.get("action.php?a=true&replies=" + id + "&since="+last, function(data) {
				if (data !== ''){  
					$( ".post > #"+id+".replies_container" ).append(data);
				}
		    });
		  }
		});
	}, 15000);

	window.setInterval(function() {
		var thread = getUrlParameter('thread');
		if (typeof thread == 'undefined') {
			return false;
		}
		$.get("action.php?a=true&notes=" + thread, function(data) {
			thread = getUrlParameter('thread');
			if (typeof thread == 'undefined') {
				return false;
			}
			$( ".post_notes" ).html(data);
		});
	}, 15000);

	<?php if ($logedin): ?>

	$('body').on('click', '.replyform', function(e) {
		e.preventDefault;
		$("#replyform").remove();
		$(".element").removeClass("element_pad");
		$(this).closest(".element").addClass("element_pad");
	    var id = $(this).parents().eq(1).attr('id');
	    var type = $(this).parents().eq(3).attr('mode').trim().replace(" ",".");
		console.log(type);
	    $('#replyform').remove();
	    var data = JSON.parse(document.getElementById('data-' + id).innerHTML);
	    $.get("action.php?a=true&themefile=templates/replyform.txt", function(result) {
	        result = result.replace(":content:", data.mentions)
	        result = result.replace(":id:", data.id)
	        
			if(type == 'post'){
				$('.'+type).find('#' + id+'.replies_container').prepend(result);
			} else {
				$('#' + id+'.'+type).after(result);
			}
	        $('#replyform #' + data.scope).prop('selected', true);
	        $('#' + id).fadeIn(400);
	    });
	});
	
	$('body').on('click', '.compose', function(e) {
	    $('#postform').toggle();
	});
	
	$('body').on('click', '.polloption:not(.fixed)', function(e) {
		if (typeof $(this).parent().attr('multiple') == 'undefined'){
			$('.polloption').removeClass('voted');
		}
	    $(this).toggleClass('voted');
	});
	
	$('body').on('click', '.vote', function(e) {
	    var id = $(this).attr('id');
		var c = 0;
		var choice = [];
		$('#'+id+'.poll .polloption').each(function(a){
			if ($(this).hasClass('voted')){
				console.log(c);
				choice.push(c);
			}
			c++;
		});
		var choices = choice.join(',');
		console.log(choices);
		$.get("action.php?a=true&vote="+id+"&choices="+choices, function(result) {
			$('#'+id+'.poll').html(result);
	    });
	});
	
	$('body').on('click', '.searchform', function(e) {
		$(this).prev().toggle();
	});

	$('body').on('click', '.searchmobiletoggle', function(e) {
		$('#usermenu').off('hover');
		$('#usermenu').off('active');
		$('#searchmobile').toggle();
	});

	$("body").on("keydown", "form", function (e) {
	  if (e.ctrlKey && e.keyCode === 13) {
		$(this).find('input[type="submit"]').click();
	  }
	});

   $('.tlicon').click(function(){
     $('.tlicon').each(function(a){
       $( this ).removeClass('tiselected')
     });
     $( this ).addClass('tiselected');
   });

	$('body').on('click', '#send', function() {
		var form = $(this).closest('.form');
		$(form).find(".status").html('<span id="loading" class="animate-spin fontello">&#xe838;</span>');
		var status = $(form).find('textarea').val()
		var sensitive = $(form).find('input[name=sensitive]').prop('checked');
		var spoiler = $(form).find('input[name=spoiler]').val();
	    var scope = $(form).find('select[name=scope]').val();
		var thread = $(form).find('input[name=thread]').val();
        var IDs = [];
	    $(form).find(".uploadedImage").each(function(){ IDs.push(this.id); });
        var uploaded = IDs.join("|");
	    $.post("action.php", {
	            status: status,
	            scope: scope,
				thread: thread,
				uploaded: uploaded,
				spoiler: spoiler,
				sensitive: sensitive
	        },
	        function(data) {
				$(form).closest('.element').find(".picker").css("display","none");
				if($(form).closest('.element').attr('id') == 'postform'){
					$(form).find('textarea').val('');
					$(form).find('.uploadBox').remove();
					$(form).find('input[name=sensitive]').prop('checked', false);
					$(form).find(".status").html('');
					$(form).find('input[name=spoiler]').val('');
					newPosts();
				} else {
					var parent = $(form).closest('.element').find('.post').first().attr('id');
					$('#replyform').remove();
					$('#'+parent+'.replies_container').append(data);
				}
	        });
	});
	
	$('body').on('click', '.quickreply', function(e) {
		$(".notif.form").remove();
		$(this).closest('.notif').after('<div class="notif form"><div class="notifContents"><input type="hidden" name="thread" value="'+$(this).attr('id')+'"><input type="hidden" name="mentions" value="'+$(this).attr('data-mentions')+'"><textarea style="width:100%; height100%; border: 1px solid #ddd;"></textarea><input type="submit" id="quicksend" value="Send"></div></div>');
	});
	
	$('body').on('click', '#quicksend', function() {
		
		var form = $('#quicksend').closest('.form');
		var thread = $(form).find('input[name=thread]').val();
		var mentions = $(form).find('input[name=mentions]').val();
	    var status = mentions + $(form).find('textarea').val();
		$(".notif.form").html('<span id="loading" class="animate-spin fontello">&#xe838;</span>');

	    $.post("action.php", {
	            status: status,
				thread: thread,
	        },
	        function(data) {
				$(".notif.form").remove();
				notificaton('Reply Sent!');
	        });
	});

	$('body').on('click', '.badge', function() {
	    $('#notifications').toggle();
	    var notif = $('#hiddenside').children().first().attr('id');
	    localStorage.setItem("notif", notif);
	    $('#notifications').find('.container').children().removeClass('new');
	    $('.alert').css('display','none');
	    var title = document.title;
	    document.title = title.replace("(*)", "");
		$("#favicon").attr("href","favicon.ico");
	});
	
	$('body').on('click', '#usermenu', function() {
	    $('.mobilemenu').toggle();
	});
	
	$('body').on('click', '.link', function() {
	    $('.mobilemenu').hide();
	});
	
	$('body').on('click', '.account', function() {
	    $('.switcher').toggle();
		$('.account').toggle();
	});
	
	$('body').on('click', '.notif .ldr', function(e) {
		$('#notifications').find('.container').children().removeClass('new');
		$('#notifications').css('display','none');
		$('.alert').css('display','none');
		var title = document.title;
	    document.title = title.replace("(*)", "");
		$("#favicon").attr("href","favicon.ico");
	});
	
	$('body').on('click', '.nloadmore', function() {
	    var n = $('#notifications').find('.container').children().last().attr('id');
            $('#notifications').find('.container').children().last().after('<span id="loading" class="animate-spin fontello" style="color:black;">&#xe838;</span>');
	    $.get("action.php?a=true&max=true&notif=" + n, function(data) {
            $('#loading').remove();
		    $('#notifications').find('.container').append(data);
	    });
	});
	
	window.setInterval(function() {
	    var n = localStorage.getItem("notif");
		var f = $("#notifications").find('.container').children().first().attr('id');
		var nf;
	    if (n < f) {
	        nf = n;
			//$("#notifications").html('');
	    } else {
			nf = f;
		}
	    $.get("action.php?a=true&notif=" + f, function(data) {
	        if (data) {
	            var title = document.getElementsByTagName("title")[0].innerHTML;
	            if (title.indexOf("(*)") < 0) {
	                document.getElementsByTagName("title")[0].innerHTML = "(*) " + title;
					$("#favicon").attr("href","nfavicon.ico");
	            }
	            $('#notifications').find('.container').prepend(data);
	            var cnt = $('#notifications .container div.new').length;
		    var notif = $('#hiddenside').children().first().attr('id');
	            localStorage.setItem("notif", notif);
	            $.when($('#hiddenside').html('')).then(function() {
					$('.alert').css('display','block');
	                $('#hiddenside').append(data);
	                $('#hiddenside').children().each(function(index) {
	                    $(this).delay(4000 + (1000 * index)).fadeOut("slow");
	                });
	            });
	        }
	    });
	}, 15000);

	function notificaton(message){
		var notif = "<div class='notif error' style='border-left:3px solid red; background-color:pink;'> 	<div class='notifContents'> <div style='flex: 0 0 60px; background-size:cover; border-radius:5px;'></div> 					<div style='flex: 1; padding-left:5px; padding-right:5px; word-break: break-all; overflow:hidden;'> <span>"+message+"</span> </div> </div> </div>";
		$('#hiddenside').append(notif);
		$('#hiddenside').children().each(function(index) {
			$(this).delay(4000 + (1000 * index)).fadeOut("slow");
		});
		return false;
	};

	$('body').on('click', '.fav', function(e) {
		e.preventDefault;
	    var id = $(this).parents().eq(1).attr('id');
	    $("#"+id+".post_buttons .fav").switchClass("fav","unfav",200);
	    $.get("action.php?a=true&mode=on&fav=" + id, function(data) {
	        if (data == 'error') {
				$("#"+id+".post_buttons .unfav").switchClass("unfav","fav",200);
	        } else {
	            $("#"+id+".post_buttons .unfav").children().html(data);
	        }
	    });
	});

	$('body').on('click', '.unfav', function(e) {
		e.preventDefault;
	    var id = $(this).parents().eq(1).attr('id');
	    $("#"+id+".post_buttons .unfav").switchClass("unfav","fav",200);
	    $.get("action.php?a=true&mode=off&fav=" + id, function(data) {
	        if (data == 'error') {
				$("#"+id+".post_buttons .fav").switchClass("fav","unfav",200);
	        } else {
	            $("#"+id+".post_buttons .fav").children().html(data);
	        }
	    });
	});
	
	$('body').on('click', '.reblog', function(e) {
		e.preventDefault;
	    var id = $(this).parents().eq(1).attr('id');
	    $("#"+id+".post_buttons .reblog").switchClass("reblog","unreblog",200);
	    $.get("action.php?a=true&mode=on&reblog=" + id, function(data) {
	        if (data == 'error') {
				$("#"+id+".post_buttons .unreblog").switchClass("unreblog","reblog",200);
	        } else {
	            $("#"+id+".post_buttons .unreblog").children().html(data);
	        }
	    });
	});

	$('body').on('click', '.unreblog', function(e) {
		e.preventDefault;
	    var id = $(this).parents().eq(1).attr('id');
	    $("#"+id+".post_buttons .unreblog").switchClass("unreblog","reblog",200);
	    $.get("action.php?a=true&mode=off&reblog=" + id, function(data) {
	        if (data == 'error') {
				$("#"+id+".post_buttons .reblog").switchClass("reblog","unreblog",200);
	        } else {
	            $("#"+id+".post_buttons .reblog").children().html(data);
	        }
	    });
	});
	
	$('body').on('click', '.mute', function() {
		var id = $(this).attr('id');
		$.get("action.php?a=true&mode=true&mute=" + id, function(data) {
			$('#' + id + '.mute').switchClass("mute","unmute",200);
			$('#' + id + '.unmute').html('Unmute');
		});
	});
	
	$('body').on('click', '.softmute', function() {
		var id = $(this).attr('id');
		$.get("action.php?a=true&mode=true&softmute=" + id, function(data) {
			notificaton('User added to soft mute list!');
		});
	});

	$('body').on('click', '.unmute', function() {
		var id = $(this).attr('id');
		$.get("action.php?a=true&mode=off&mute=" + id, function(data) {
			$('#' + id + '.unmute').switchClass("unmute","mute",200);
			$('#' + id + '.mute').html('Mute');
		});
	});
	
	$('body').on('click', '.muteconv', function() {
		var id = $(this).attr('id');
		$.get("action.php?a=true&mode=true&thread=true&mute=" + id, function(data) {
			$('#' + id + '.muteconv').switchClass("muteconv","unmuteconv",200);
			$('#' + id + '.unmuteconv').html('Unmute Thread');
			console.log(data);
		});
	});

	$('body').on('click', '.unmuteconv', function() {
		var id = $(this).attr('id');
		$.get("action.php?a=true&mode=off&thread=true&mute=" + id, function(data) {
			$('#' + id + '.muteconv').switchClass("unmuteconv","muteconv",200);
			$('#' + id + '.muteconv').html('Mute Thread');
			console.log(data);
		});
	});

	$('body').on('click', '.menu', function(e) {
		$(this).next('span').slideToggle('left');
	});

	$('body').on('click', '.delete', function(e) {
		e.preventDefault;
	    var id = $(this).attr('id');
	    var post = $('#'+id+'.post').parent();
		var reply = $('#'+id+'.reply');
	    $.get("action.php?a=true&delete=" + id, function(data) {
	        if (data == '0') {
				post.css('opacity','0.5');
				reply.css('opacity','0.5');
	        } else {
	            $.when(post.fadeOut('slow')).then(function(){
					post.remove();
					reply.remove();
				});
	        }
	    });
	});
	
	$('body').on('click', '.delete', function(e) {
		e.preventDefault;
	    var id = $(this).attr('id');
	    $.get("action.php?a=true&bookmark=" + id, function(data) {
			notificaton('Post bookmarked!');
	    });
	});
	
	$('body').on('click', '.quote', function(e) {
		e.preventDefault;
	    var id = $(this).closest('.post').attr('id');
		console.log(id);
		var url = $("#"+id+".post .original").attr("href");
		console.log(url);
		$("#postform").find('textarea').val("> "+url+"\n\n");
		if ($("#postform").is(':visible')) {
			location.hash = "#top";
		} else {
			$('#postform').toggle();
		}
	});
	
	$('body').on('click', '.delpic', function(e) {
		var count = $(this).closest('#files').children().length;
		if (count == 1){
			$(this).closest('.uploadBox').remove();
		} else {
			count = count - 1;
			$(this).parent().fadeOut();
			$(this).parent().remove();

		}
	});


	/* paste to upload */
	document.onpaste = function(event){

	  var items = (event.clipboardData || event.originalEvent.clipboardData).items;

	  for (var i = 0 ; i < items.length ; i++) {

		var item = items[i];

		if (item.type.indexOf("image") != -1) {

		  var file = item.getAsFile();
		  upload_file_with_ajax(file,file_uploaded);
		}
	  }
	}

	function upload_file_with_ajax(file, ajax_callback){
	  $("#postform .status").html('<span id="loading" class="animate-spin fontello">&#xe838;</span>');
		  $.get("action.php?a=true&info=true", function(data) {
		  const info = JSON.parse(data);  
		var formData = new FormData();
		  formData.append('file', file);
		  $.ajax("https://"+info[1]+"/api/v2/media" , {
            
			type: 'POST',
			headers: {'Authorization': 'Bearer '+info[0]},
			contentType: false,
			processData: false,
			data: formData,
			error: function() {
			  console.log('There was a problem uploading this file. Retrying with legacy mode.');
			   $.ajax("upload.php" , {
				type: 'POST',
				headers: {'Authorization': 'Bearer '+info[0]},
				contentType: false,
				processData: false,
				data: formData,
				error: function() {
				  notificaton('There was a problem uploading this file.');
				  $("#postform .status").html('');
				},
				success: function (response) {
						if (typeof ajax_callback == 'function') {
							ajax_callback(JSON.parse(response));
						} else if (typeof ajax_callback == 'string') {
							if (ajax_callback != '') {
								eval(ajax_callback + '(response)');
							}
						}
					}
			  });
			},
			success: function (response) {
					if (typeof ajax_callback == 'function') {
						ajax_callback(response);
					} else if (typeof ajax_callback == 'string') {
						if (ajax_callback != '') {
							eval(ajax_callback + '(response)');
						}
					}
				}
		  });
	});
	}
	

	function file_uploaded(data) {
		//data = JSON.parse(data);
		if ($("#postform .uploadBox").length < 1){
			$("#postform #status").before('<div class="uploadBox"><ul id="files" style="margin:0px;"></ul></div>');
			 $( function() {
				$( "#files" ).sortable();
				$( "#files" ).disableSelection();
			} );
		}
		let file;
		switch (data.type) {
		  case 'image':
			file = data.preview_url;
			break;
		  case 'audio':
			file = 'img/aud.png';
			break;
		  case 'video':
			file = 'img/vid.png';
			break;
		  default:
			file = 'img/doc.png';
			break;
		}
		var nfiles = $("#postform .uploadBox").children().length;	
		$("ul[id=files]").append('<li class="uploadedImage" id="'+data.id+'" style="position:relative; background-image:url('+file+')"><div class="fontello delpic">&#xe813;</div></li>');	    
		$("#postform .status").html('');
	}



	function upload_files() {
		$("#postform .status").html('<span id="loading" class="animate-spin fontello">&#xe838;</span>');
		$.get("action.php?a=true&info=true", function(data) {
			const info = JSON.parse(data);
			var fileUpload = $("#files_input_field").get(0);
			var files = fileUpload.files;
			if (files.length == 0) return;

			var data;
			for (var i = 0; i < files.length; i++) {
				data = new FormData();
				data.append('file', files[i]);
			
				$.ajax({
					type: "POST",
					headers: {'Authorization': 'Bearer '+info[0]},
					url: "https://"+info[1]+"/api/v2/media",
					contentType: false, //default: 'application/x-www-form-urlencoded; charset=UTF-8'
					processData: false, //default: data, other DOMDocument
					data: data, //Type: PlainObject or String or Array
					success: function(data) {
							console.log(data);
							if ($("#postform .uploadBox").length < 1){
								$("#postform #status").before('<div class="uploadBox"><ul id="files" style="margin:0px;"></ul></div>');
								$( function() {
									$( "#files" ).sortable();
									$( "#files" ).disableSelection();
								} );
							}
							let file;
							switch (data.type) {
							  case 'image':
								file = data.preview_url;
								break;
							  case 'audio':
								file = 'img/aud.png';
								break;
							  case 'video':
								file = 'img/vid.png';
								break;
							  default:
								file = 'img/doc.png';
								break;
							}
							
							console.log(data.id);
							$("ul[id=files]").append('<li class="uploadedImage" id="'+data.id+'" style="position:relative; background-image:url('+file+')"><div class="fontello delpic">&#xe813;</div></li>');
							if(i === files.length){
								$("#postform .status").html('');
							}
					},
					error: function(jqXHR, e) {
						console.log(jqXHR);
						console.log('There was a problem uploading this file, trying again with legacy upload.');
						$("#postform .status").html('');

						// Attempt the upload again with upload.php
						$.ajax({
							type: "POST",
							headers: {'Authorization': 'Bearer '+info[0]},
							url: "upload.php",
							contentType: false,
							processData: false,
							data: data,
							success: function(data) {
								console.log(data);
								data = JSON.parse(data);
								if ($("#postform .uploadBox").length < 1){
									$("#postform #status").before('<div class="uploadBox"><ul id="files" style="margin:0px;"></ul></div>');
									$( function() {
										$( "#files" ).sortable();
										$( "#files" ).disableSelection();
									} );
								}
								let file;
								switch (data.type) {
								  case 'image':
									file = data.preview_url;
									break;
								  case 'audio':
									file = 'img/aud.png';
									break;
								  case 'video':
									file = 'img/vid.png';
									break;
								  default:
									file = 'img/doc.png';
									break;
								}
								
								console.log(data.id);
								$("ul[id=files]").append('<li class="uploadedImage" id="'+data.id+'" style="position:relative; background-image:url('+file+')"><div class="fontello delpic">&#xe813;</div></li>');
								if(i === files.length){
									$("#postform .status").html('');
								}
							},
							error: function(jqXHR, e) {
								console.log(jqXHR);
								notificaton('There was a problem uploading this file.');
							}
						});
					}
				});
			
			}
			
		});
	}
	
	function upload_files_reply() {
		$("#replyform .status").html('<span id="loading" class="animate-spin fontello">&#xe838;</span>');
		$.get("action.php?a=true&info=true", function(data) {
			const info = JSON.parse(data);
			var fileUpload = $("#replyform #files_input_field").get(0);
			var files = fileUpload.files;
			if (files.length == 0) return;

			var data;
			for (var i = 0; i < files.length; i++) {
				data = new FormData();
				data.append('file', files[i]);
			
				$.ajax({
					type: "POST",
					headers: {'Authorization': 'Bearer '+info[0]},
					url: "https://"+info[1]+"/api/v2/media",
					contentType: false, //default: 'application/x-www-form-urlencoded; charset=UTF-8'
					processData: false, //default: data, other DOMDocument
					data: data, //Type: PlainObject or String or Array
					success: function(data) {
							console.log(data);
							if ($("#replyform .uploadBox").length < 1){
								$("#replyform #status").before('<div class="uploadBox"><ul id="files" style="margin:0px;"></ul></div>');
								 $( function() {
									$( "#files" ).sortable();
									$( "#files" ).disableSelection();
								} );
							}
							let file;
							switch (data.type) {
							  case 'image':
								file = data.preview_url;
								break;
							  case 'audio':
								file = 'img/aud.png';
								break;
							  case 'video':
								file = 'img/vid.png';
								break;
							  default:
								file = 'img/doc.png';
								break;
							}
							
							console.log(data.id);
							$("#replyform #files").append('<li class="uploadedImage" id="'+data.id+'" style="position:relative; background-image:url('+file+')"><div class="fontello delpic">&#xe813;</div></li>');
							if(i === files.length){
								$("#postform .status").html('');
							}
					},
					error: function(jqXHR,e) {
						console.log(jqXHR);
						notificaton('There was a problem uploading this file.');
						$("#replyform .status").html('');
					}
				});
			
			}
			
		});
	}

	<?php endif; ?>	
    </script>
		
	</body>
