/**
 * Project:        M.VuiZ.Net
 * Author:         nampt.me <admin@vuiz.net>
 * Copyright:      © 2025 vuiz.net All rights reserved.
 */
/**
 * Copyright https://nam.name.vn/ All Rights Reserved.
 * Permission to use, copy, modify, and redistribute this software and its
 * documentation for personal, non-commercial use is hereby granted provided that
 * this copyright notice appears in all copies.
 * Date: Sun 12/22/2019
 */

//zingmp3,chiasenhac,facebook,keeng,mixcloud,nhac.vn,nhaccuatui,soundcloud,zingtv,xvideo//
$(document).ready(function() {
	function result(rs) {
		if (-1 != rs.indexOf("jwplayer"))
		{
			setTimeout(function(){
				$('#result').hide().empty().html(rs).slideDown('slow');
				$("#inputForm").prop('disabled', false),$("#link").prop('disabled', false);
			}, 3000);
		}
		else{
			$('#result').hide().empty().html(rs).slideDown('slow');
			$("#inputForm").prop('disabled', false),$("#link").prop('disabled', false);
		}
		$("#link").attr("placeholder", $("#link").val()).val("");
	}
	function error(nofi) {
		$('#result').hide().empty().html('<div class="gmenu" style="text-align: center;"><i style="color:red">'+nofi+'</i></div>').slideDown('slow');
		$("#inputForm").prop('disabled', false),$("#link").prop('disabled', false);
	}
	function errorServer() {
		$('#result').hide().empty().html('<div class="gmenu" style="text-align: center;"><i style="color:red"><b>Máy chủ đang bận. Vui lòng thử lại !</b></i></div>').slideDown('slow');
		$("#inputForm").prop('disabled', false),$("#link").prop('disabled', false);
	}
	$("#inputForm").click(function() {
		var n = $("#link").val().trim();
		if (!n) 
			return alert("Chưa nhập link thím ơi !"), !1;
		$("#result").hide().html('<div class="gmenu" style="text-align: center;"><img src="/template/images/loading.svg" /></div>').slideDown('slow'), $("#inputForm").prop('disabled', true),$("#link").prop('disabled', true);
		if (-1 != n.indexOf("video") || -1 != n.indexOf("tiktok.com") || -1 != n.indexOf("facebook"))
			$("head").append('<script async src="https://cdn.jwplayer.com/libraries/rlx7bqvD.js"></script>');
		try {
			if (-1 != n.indexOf("mp3.zing.vn") || -1 != n.indexOf("zingmp3.vn")) 
				$.post("getlink/mp3zing/apizing.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("chiasenhac.")) 
				$.post("getlink/chiasenhac/api-csn.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("facebook.com/")) 
				$.post("getlink/facebookvideo/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("keeng.vn/")) 
				$.post("getlink/keeng.vn/keengapi.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("mixcloud.com/")) 
				$.post("getlink/mixcloud/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("nhac.vn/")) 
				$.post("getlink/nhac.vn/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("nhaccuatui.com/")) 
				$.post("getlink/nhaccuatui/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("soundcloud.com/")) 
				$.post("getlink/soundcloud/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("tv.zing.vn")) 
				$.post("getlink/tv.zing.vn/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("xvideos") || -1 != n.indexOf("xv") || -1 != n.indexOf("xnxx")) 
				$.post("getlink/xvideos/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							result(n.success);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("mediafire")) 
				$.post("getlink/mediafire/api.php", {
					mediafire: n,
				}, function(n) {
					try {
						if (n.success) {
							result('<div class="menu">Download: <a class="download" rel="noreferrer noopener" target="_blank" href="'+n.success.url+'">'+n.success.name+'</a><br/><input type="text" value="'+n.success.url+'"></div>');
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("tiktok.com")) 
				$.post("getlink/tiktokdownloader/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							var html = '';
							var download = ' <a rel="noreferrer noopener" target="_blank" href="'+n.success['link']+'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download Video </button></a> ';
							var play = n.success['link'];
							if (n.success['audio']) {
								download += ' <a rel="noreferrer noopener" target="_blank" href="'+n.success['audio']+'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Audio only </button></a> ';
							}
							$("head").append('<meta name="referrer" content="no-referrer" />');
							html = '<div class="menu"><div id="video"><div id="myvideo"></div></div> <script> var playerInstance = jwplayer("myvideo"); playerInstance.setup({file:"'+play+'",type:"mp4", image:"'+n.success['thumbnail']+'" }); <\/script> <div style="text-align: center; padding-top:15px;"> '+download+' </div></div>';
							result(html);
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
					}
				}, "json").fail(function() {
					errorServer();
				});
			else if (-1 != n.indexOf("youtube.com") || -1 != n.indexOf("youtu")) 
				$.post("getlink/youtube/api.php", {
					link: n,
				}, function(n) {
					try {
						if (n.success) {
							var data = n.success;
							$("#submit").removeClass('disabled');
							var html = '<div class="menu" style="text-align:center;"><div style="padding-bottom:15px;font-size: 16px; font-weight: bold;">'+data.info.title+' - '+data.info.channelTitle+'</div><div id="player" style="padding-bottom:15px;"><img src="'+data.info.thumbnail+'" width="600px" title="'+data.info.title+'" /></div> <div><button id="PlayVideoButton"><i class="fa fa-youtube-play"></i> Play Video</button> <button id="PlayAudioButton"><i class="fa fa-music"></i> Play Audio</button></div>';

							if(data.downloads.video) {
								html += '<p style="font-weight: bold;">Video With Audio</p>';
								$.each(data.downloads.video, function(index, video) {
									html += '<a rel="noreferrer noopener" target="_blank" href="'+video.url+'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download '+video.qualityLabel+'</button></a> <br />';
								});

								$("#PlayVideoButton").click(function () {
									playvideo(data.info.title, data.info.thumbnail, data.downloads.video[0].url);
								});
							}
							if(data.downloads.videoNoAudio) {
								html += '<p style="font-weight: bold;">Video No Audio</p>';
								$.each(data.downloads.videoNoAudio, function(index, video) {
										html += '<a rel="noreferrer noopener" target="_blank" href="'+video.url+'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download '+video.qualityLabel+'</button></a> <br />';
								});
							}
							if(data.downloads.audio) {
								html += '<p style="font-weight: bold;">Audio Only</p>';
								$.each(data.downloads.audio, function(index, audio) {
									html += '<a rel="noreferrer noopener" target="_blank" href="'+audio.url+'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download m4a</button></a> <br />';
								});
								$("#PlayAudioButton").click(function () {
									playaudio(data.info.title, data.info.thumbnail, data.downloads.audio[0].url);
								});
							}
							html += '</div>';

							result(html);
							$("head").append('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.6.8/plyr.min.css" /><script src="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.6.8/plyr.min.js" type="text/javascript"><\/script>');

							if(data.downloads.video) {
								$("#PlayVideoButton").click(function () {
									playvideo(data.info.title, data.info.thumbnail, data.downloads.video);
								});
							}
							if(data.downloads.audio) {
								$("#PlayAudioButton").click(function () {
									playaudio(data.info.title, data.info.thumbnail, data.downloads.audio[0].url);
								});
							}
						} else {
							error(n.error);
						}
					} catch (t) {
						errorServer();
						console.log("try");
					}
				}, "json").fail(function() {
					errorServer();
					console.log("json");
				});
			else {
				error("<b>Link bạn nhập lỗi hoặc không được hỗ trợ !</b>");
			}
		} catch (t) {
			errorServer();
		}
		return !1
	});

	// Function Youtube
	function playvideo(title, image, videos) {
		var playsource = videos.map(function (video) {
			return {
				src: video.url,
				type: 'video/mp4',
				size: video.height
			}
		});
		$("#player").hide().html('<video id="videoplayer" playsinline controls></video>');
		const player = new Plyr('#videoplayer');
		player.source = {
			type: 'video',
			title: title,
			poster: image,
			sources: playsource
		};
		$("button.plyr__control--overlaid").css({"height":"auto", "margin":"none"});
		$("#player").slideDown('slow');
	}

	function playaudio(title, image, audio) {
		$("#player").hide().html('<div style="max-width:600px;margin:0 auto;"><img src="'+image+'" width="600px" title="'+title+'" /><audio id="videoplayer" controls"></audio></div>');
		const player = new Plyr('#videoplayer');
		player.source = {
			type: 'audio',
			title: title,
			poster: image,
			sources: [{
				src: audio,
				type: 'audio/mp4'
			}]
		};
		player.play();
		$("#player").slideDown('slow');
	}
});

function showcomment() {
	$('#fb-show-comment').html('<script> window[\'CboxReady\'] = function (Cbox) {Cbox(\'button\', \'6-857482-8iLaK1\'); } </script> <script src="https://static.cbox.ws/embed/2.js" async></script>');
}