<?php

// Получаем ссылку
if ($this->config['output'] != '') {
	$_field = explode('|', $this->config['output']);
	if (count($_field) == 3)
		$field_output = "xf_{$_field[0]}";
	elseif (count($_field) == 1)
		$field_output = $_field[0];	
}

// Получаем основные поля
if ($this->config['output_quality'] != '') {
	$_field = explode('|', $this->config['output_quality']);
	$field_output_quality = "xf_{$_field[0]}";
}

if ($this->config['output_season'] != '') {
	$_field = explode('|', $this->config['output_season']);
	$field_output_season = "xf_{$_field[0]}";
}

if ($this->config['output_episode'] != '') {
	$_field = explode('|', $this->config['output_episode']);
	$field_output_episode = "xf_{$_field[0]}";
}

if ($this->config['output_voice'] != '') {
	$_field = explode('|', $this->config['output_voice']);
	$field_output_voice = "xf_{$_field[0]}";
}

// Получаем поля Kinopoisk, IMDM, World-Art, ...
if ($this->config['title'] != '') {
	$_field = explode('|', $this->config['title']);
	if (count($_field) == 3)
		$field_title = "xf_{$_field[0]}";
	elseif (count($_field) == 1)
		$field_title = $_field[0];	
}

if ($this->config['kinopoisk_id'] != '') {
	if ($this->config['kinopoisk_id'] == 'pspvolt_id') 
		$field_kinopoisk = "pspvolt_id";
	else {
		$_field = explode('|', $this->config['kinopoisk_id']);
		$field_kinopoisk = "xf_{$_field[0]}";
	}
}

if ($this->config['imdb_id'] != '') {
	$_field = explode('|', $this->config['imdb_id']);
	$field_imdbid = "xf_{$_field[0]}";
}


if ($this->config['world_art_id'] != '') {
	$_field = explode('|', $this->config['world_art_id']);
	$field_worldart = "xf_{$_field[0]}";
}

if ($this->config['kinorium_id'] != '') {
	$_field = explode('|', $this->config['kinorium_id']);
	$field_kinorium = "xf_{$_field[0]}";
}

if ($this->config['tmdb_id'] != '') {
	$_field = explode('|', $this->config['tmdb_id']);
	$field_tmdb = "xf_{$_field[0]}";
}

if ($this->config['shikimori_id'] != '') {
	$_field = explode('|', $this->config['shikimori_id']);
	$field_shikimori = "xf_{$_field[0]}";
}

if ($this->config['mdl_id'] != '') {
	$_field = explode('|', $this->config['mdl_id']);
	$field_mdl = "xf_{$_field[0]}";
}

$autokpimdbdwa = $this->config['allow_autopaste'] ? 1 : 0;

// Получаем дополнительные поля
if ($this->config['output_title'] != '') {
	$_field = explode('|', $this->config['output_title']);
	if (count($_field) == 3)
		$field_output_title = "xf_{$_field[0]}";
	elseif (count($_field) == 1)
		$field_output_title = $_field[0];	
}
$title_rw = $this->config['output_title_rw'] ? 1 : 0;

if ($this->config['output_title_orig'] != '') {
	$_field = explode('|', $this->config['output_title_orig']);
	$field_output_title_orig = "xf_{$_field[0]}";
}
$title_orig_rw = $this->config['output_title_orig_rw'] ? 1 : 0;

if ($this->config['output_title_ua'] != '') {
	$_field = explode('|', $this->config['output_title_ua']);
	$field_output_title_ua = "xf_{$_field[0]}";
}
$title_ua_rw = $this->config['output_title_ua_rw'] ? 1 : 0;

if ($this->config['output_title_alt'] != '') {
	$_field = explode('|', $this->config['output_title_alt']);
	$field_output_title_alt = "xf_{$_field[0]}";
}
$title_alt_rw = $this->config['output_title_alt_rw'] ? 1 : 0;

if ($this->config['output_year'] != '') {
	$_field = explode('|', $this->config['output_year']);
	$field_output_year = "xf_{$_field[0]}";
}
$year_rw = $this->config['output_year_rw'] ? 1 : 0;

if ($this->config['output_duration'] != '') {
	$_field = explode('|', $this->config['output_duration']);
	$field_output_duration = "xf_{$_field[0]}";
}
$duration_rw = $this->config['output_duration_rw'] ? 1 : 0;

if ($this->config['output_ganre'] != '') {
	$_field = explode('|', $this->config['output_ganre']);
	$field_output_ganre = "xf_{$_field[0]}";
}
$ganre_rw = $this->config['output_ganre_rw'] ? 1 : 0;


if ($this->config['output_collections'] != '') {
	$_field = explode('|', $this->config['output_collections']);
	$field_output_collections = "xf_{$_field[0]}";
}
$collections_rw = $this->config['output_collections_rw'] ? 1 : 0;

if ($this->config['output_country'] != '') {
	$_field = explode('|', $this->config['output_country']);
	$field_output_country = "xf_{$_field[0]}";
}
$country_rw = $this->config['output_country_rw'] ? 1 : 0;

if ($this->config['output_actors'] != '') {
	$_field = explode('|', $this->config['output_actors']);
	$field_output_actors = "xf_{$_field[0]}";
}
$actors_rw = $this->config['output_actors_rw'] ? 1 : 0;

if ($this->config['output_producer'] != '') {
	$_field = explode('|', $this->config['output_producer']);
	$field_output_producer = "xf_{$_field[0]}";
}
$producer_rw = $this->config['output_producer_rw'] ? 1 : 0;

if ($this->config['output_voice_director'] != '') {
	$_field = explode('|', $this->config['output_voice_director']);
	$field_output_voice_director = "xf_{$_field[0]}";
}
$voice_director_rw = $this->config['output_voice_director_rw'] ? 1 : 0;

if ($this->config['output_translator'] != '') {
	$_field = explode('|', $this->config['output_translator']);
	$field_output_translator = "xf_{$_field[0]}";
}
$translator_rw = $this->config['output_translator_rw'] ? 1 : 0;

if ($this->config['output_writer'] != '') {
	$_field = explode('|', $this->config['output_writer']);
	$field_output_writer = "xf_{$_field[0]}";
}
$writer_rw = $this->config['output_writer_rw'] ? 1 : 0;

if ($this->config['output_operator'] != '') {
	$_field = explode('|', $this->config['output_operator']);
	$field_output_operator = "xf_{$_field[0]}";
}
$operator_rw = $this->config['output_operator_rw'] ? 1 : 0;

if ($this->config['output_composer'] != '') {
	$_field = explode('|', $this->config['output_composer']);
	$field_output_composer = "xf_{$_field[0]}";
}
$composer_rw = $this->config['output_composer_rw'] ? 1 : 0;

if ($this->config['output_design'] != '') {
	$_field = explode('|', $this->config['output_design']);
	$field_output_design = "xf_{$_field[0]}";
}
$design_rw = $this->config['output_design_rw'] ? 1 : 0;

if ($this->config['output_editor'] != '') {
	$_field = explode('|', $this->config['output_editor']);
	$field_output_editor = "xf_{$_field[0]}";
}
$editor_rw = $this->config['output_editor_rw'] ? 1 : 0;

if ($this->config['output_kinopoisk_rate'] != '') {
	$_field = explode('|', $this->config['output_kinopoisk_rate']);
	$field_output_kinopoisk_rate = "xf_{$_field[0]}";
}
$kinopoisk_rate_rw = $this->config['output_kinopoisk_rate_rw'] ? 1 : 0;

if ($this->config['output_kinopoisk_rate_votes'] != '') {
	$_field = explode('|', $this->config['output_kinopoisk_rate_votes']);
	$field_output_kinopoisk_rate_votes = "xf_{$_field[0]}";
}
$kinopoisk_rate_votes_rw = $this->config['output_kinopoisk_rate_votes_rw'] ? 1 : 0;

if ($this->config['output_imdb_rate'] != '') {
	$_field = explode('|', $this->config['output_imdb_rate']);
	$field_output_imdb_rate = "xf_{$_field[0]}";
}
$imdb_rate_rw = $this->config['output_imdb_rate_rw'] ? 1 : 0;

if ($this->config['output_imdb_rate_votes'] != '') {
	$_field = explode('|', $this->config['output_imdb_rate_votes']);
	$field_output_imdb_rate_votes = "xf_{$_field[0]}";
}
$imdb_rate_votes_rw = $this->config['output_imdb_rate_votes_rw'] ? 1 : 0;

if ($this->config['output_tmdb_rate'] != '') {
	$_field = explode('|', $this->config['output_tmdb_rate']);
	$field_output_tmdb_rate = "xf_{$_field[0]}";
}
$tmdb_rate_rw = $this->config['output_tmdb_rate_rw'] ? 1 : 0;

if ($this->config['output_tmdb_rate_votes'] != '') {
	$_field = explode('|', $this->config['output_tmdb_rate_votes']);
	$field_output_tmdb_rate_votes = "xf_{$_field[0]}";
}
$tmdb_rate_votes_rw = $this->config['output_tmdb_rate_votes_rw'] ? 1 : 0;

if ($this->config['output_director'] != '') {
	$_field = explode('|', $this->config['output_director']);
	$field_output_director = "xf_{$_field[0]}";
}
$director_rw = $this->config['output_director_rw'] ? 1 : 0;

if ($this->config['output_age'] != '') {
	$_field = explode('|', $this->config['output_age']);
	$field_output_age = "xf_{$_field[0]}";
}
$age_rw = $this->config['output_age_rw'] ? 1 : 0;

if ($this->config['output_mpaa'] != '') {
	$_field = explode('|', $this->config['output_mpaa']);
	$field_output_mpaa = "xf_{$_field[0]}";
}
$mpaa_rw = $this->config['output_mpaa_rw'] ? 1 : 0;

if ($this->config['output_premier_mir'] != '') {
	$_field = explode('|', $this->config['output_premier_mir']);
	$field_output_premier_mir = "xf_{$_field[0]}";
}
$premier_mir_rw = $this->config['output_premier_mir_rw'] ? 1 : 0;

if ($this->config['output_premier_rf'] != '') {
	$_field = explode('|', $this->config['output_premier_rf']);
	$field_output_premier_rf = "xf_{$_field[0]}";
}
$premier_rf_rw = $this->config['output_premier_rf_rw'] ? 1 : 0;

if ($this->config['output_budget'] != '') {
	$_field = explode('|', $this->config['output_budget']);
	$field_output_budget = "xf_{$_field[0]}";
}
$budget_rw = $this->config['output_budget_rw'] ? 1 : 0;

if ($this->config['output_feels_usa'] != '') {
	$_field = explode('|', $this->config['output_feels_usa']);
	$field_feels_usa = "xf_{$_field[0]}";
}
$feels_usa_rw = $this->config['output_feels_usa_rw'] ? 1 : 0;

if ($this->config['output_feels_world'] != '') {
	$_field = explode('|', $this->config['output_feels_world']);
	$field_feels_world = "xf_{$_field[0]}";
}
$feels_world_rw = $this->config['output_feels_world_rw'] ? 1 : 0;

if ($this->config['output_feels_russia'] != '') {
	$_field = explode('|', $this->config['output_feels_russia']);
	$field_feels_russia = "xf_{$_field[0]}";
}
$feels_russia_rw = $this->config['output_feels_russia_rw'] ? 1 : 0;

if ($this->config['output_slogan'] != '') {
	$_field = explode('|', $this->config['output_slogan']);
	$field_output_slogan = "xf_{$_field[0]}";
}
$slogan_rw = $this->config['output_slogan_rw'] ? 1 : 0;

if ($this->config['output_trivia'] != '') {
	$_field = explode('|', $this->config['output_trivia']);
	$field_output_trivia = "xf_{$_field[0]}";
}
$trivia_rw = $this->config['output_trivia_rw'] ? 1 : 0;

if ($this->config['output_poster'] != '') {
	$_field_poster = explode('|', $this->config['output_poster']);
	$field_output_poster = "xf_{$_field_poster[0]}";
}
$poster_no_upload = $this->config['output_poster_no_upload'] ? 1 : 0;

if ($this->config['output_screen'] != '') {
	$_field_screen = explode('|', $this->config['output_screen']);
	$field_output_screen = "xf_{$_field_screen[0]}";
}

$not_fullstory = $this->config['output_not_fullstory'] ? 1 : 0;

// Shablons
$cdnm_shab_date = $this->config['cdnm_shab_date'] ? date($this->config['cdnm_shab_date']) : '';
$cdnm_shabs = $this->config['cdnm_metatitle'].'#SEP#'.$this->config['cdnm_metadescription'].'#SEP#'.$this->config['cdnm_title'].'#SEP#'.$this->config['cdnm_chpu'];

$cdnm_shab_metatitle = $this->config['cdnm_metatitle_onlyparse'] ? 0 : 1;
$cdnm_shab_metadescription = $this->config['cdnm_metadescription_onlyparse'] ? 0 : 1;
$cdnm_shab_title = $this->config['cdnm_title_onlyparse'] ? 0 : 1;
$cdnm_shab_chpu = $this->config['cdnm_chpu_onlyparse'] ? 0 : 1;

$cdnm_shab_metatitle_movie = $this->config['cdnm_metatitle_movie'] ? 1 : 0;
$cdnm_shab_metadescription_movie = $this->config['cdnm_metadescription_movie'] ? 1 : 0;
$cdnm_shab_title_movie = $this->config['cdnm_title_movie'] ? 1 : 0;
$cdnm_shab_chpu_movie = $this->config['cdnm_chpu_movie'] ? 1 : 0;

?>

<!-- CSS -->
<link type="text/css" href="/engine/skins/cdnm/css/button.css" rel="stylesheet" />

<!-- Buttons -->
<div class="cdnm">
	<button type="button" id="cdnmFindButton" class="cdnm-btn cdnm-btn-sm cdnm-btn-primary" style="float: left;">Поиск на <b>CDN Movie</b></button>
	<button type="button" id="cdnmClearFindButton" class="cdnm-btn cdnm-btn-sm cdnm-btn-danger" style="float: left; display: none; margin-left: 4px">Очистить поиск</button>
	<button type="button" id="cdnmClearOutputButton" class="cdnm-btn cdnm-btn-sm cdnm-btn-warning" style="float: left; display: none; margin-left: 4px">Стереть вставленную ссылку</button>
	<button type="button" id="cdnmPreviewOutputButton" class="cdnm-btn cdnm-btn-sm cdnm-btn-info" style="float: left; display: none; margin-left: 4px">Предпросмотр</button>
	<div style="clear: both"></div>
	<div id="cdnmFindResults"></div>
</div>

<!-- Подключаем jQuery для старых версий скрипта -->
<script type="text/javascript">
<!--
	if (!window.jQuery)
		document.write(unescape("<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.11.1.min.js\">%3C/script%3E"));
-->
</script>

<!-- JavaScript -->
<script type="text/javascript">
<!--
	var cdnm_rdata = false;

	function preview_cdnm(url) {
        $('#dlepopup').remove();
        $('body').append("<div id='dlepopup' title='Предпросмотр видео' style='display:none'><iframe src=\""+url+"\" width=\"640\" height=\"480\" frameborder=\"0\" allowfullscreen></iframe></div>");
        $('#dlepopup').dialog({
          autoOpen: true,
          width: 680,
          resizable: false,
          close: function( event, ui ) { $(this).dialog('destroy'); }
        });
	};

	function cdnm_show_loading(message) {
		if (message) {
			$("#cdnm-loading").html(message);
		}
		var setX = ($(window).width() - $("#cdnm-loading").width()) / 2;
		var setY = ($(window).height() - $("#cdnm-loading").height()) / 2;
		$("#cdnm-loading").css({
			left: setX + "px",
			top: setY + "px",
			position: "fixed",
			zIndex: "99",
		});
		$("#cdnm-loading").fadeTo("slow", 1.0);
	};

	function cdnm_hide_loading() {
		$("#cdnm-loading").fadeOut("slow");
	};

	$("body").append("<div id=\"cdnm-loading\">Пожалуйста, подождите...</div>");

	function cdnm_view_output(url) {
		$(".cdnm-find-url").val(url);
		$(".cdnm-find-url-orig").val(url);

		$("#cdnmFindUrlModal").modal("show");
	};

	$(".cdnm-find-url").click(function() {
		$(this).select();
	});

	function serials_format(type, format, data, format_def, plus) {
	  if (format_def != 0 && format == '') format = format_def;
	  if (format != '' && format > 0 && data != '') {
		//season
		if (type == 'season') {
			text_season = '';
			season = data;
			switch (format) {
			  case '1': //1 сезон, 2 сезон, 3 сезон
				text_season = season; break; 
			  case '2': //1 сезон, 1-2 сезон, 1-3 сезон
				text_season = season > 1 ? '1-'+season:season; break; 
			  case '3': //1 сезон, 1,2 сезон, 1,2,3 сезон, 1,2,3,4 сезон
				for (i = 1; i <= season ; i++) {
					text_season += i + ",";
				}
				text_season = text_season.substr(0, text_season.length-1);
			}
			
			return text_season + ' сезон';
		}
		//episode
		if (type == 'episode') {
			if (plus == 1) data++;
			
			text_episod = '';
			ep = data;
			switch (format) {
			  case '1': //1 серия, 2 серия, 3 серия
				text_episod = ep; break; 
			  case '2': //1 серия, 1-2 серия, 1-3 серия
				text_episod = ep > 1 ? '1-'+ep:ep; break;
			  case '3': //1 серия, 1,2 серия, 1,2,3 серия, 1,2,3,4 серия
				for (i = 1; i <= ep ; i++) {
					text_episod += i + ",";
				}
				text_episod = text_episod.substr(0, text_episod.length-1);
				break;
			  case '4': //1 серия, 1,2 серия, 1,2,3 серия, 2,3,4 серия
				if (ep > 2 ) text_episod = (ep - 2) +','+ (ep - 1) +','+ ep;
				else if (ep == 2 ) text_episod = (ep - 1) +','+ ep;
				else text_episod = ep;
				break;
			  case '5': //1,2 серия, 1,2,3 серия, 1,2,3,4,5 серия, 1-5,6,7 серия
				if (ep >= 6 ) text_episod = '1-' + (ep - 2) +','+ (ep - 1) +','+ ep;
				else {
					for (i = 1; i <= ep ; i++) {
						text_episod += i + ",";
					}
					text_episod = text_episod.substr(0, text_episod.length-1);
				}
			}
			return text_episod + ' серия';
		}
		  
	  } else return data;
	}
	
	function cdnm_set_link(url) {
		cdnm_show_loading();

		var field_output = "#<?=$field_output?>";
		// added

		$(field_output).val(url);

		$("#cdnmClearOutputButton,#cdnmPreviewOutputButton").show();

		cdnm_hide_loading();
	};

	function cdnm_set_output(url, this_item, numitem) {
		cdnm_show_loading();

		var fields_arr = $(this_item).parents('li#cdnm_item_'+numitem);		
		var nid_item = $(fields_arr).data("nid");
		var nid_arr = cdnm_rdata[nid_item].material_data;
		var type = $(fields_arr).data("type");
		
		var field_output = "#<?=$field_output?>";

		var out_quality = $(fields_arr).data("quality");
		var out_voice = $(fields_arr).data("voice");
		var out_season = $(fields_arr).data("season");
		var out_episode = $(fields_arr).data("episode");
		var out_season_orig = $(fields_arr).data("season_orig");
		var out_episode_orig = $(fields_arr).data("episode_orig");
   		
		var autokpimdbwa = <?=$autokpimdbdwa?>;
		var out_kinopoisk_id = $(fields_arr).data("kinopoisk_id");
		var out_imdbid = $(fields_arr).data("imdbid");
		var out_waid = $(fields_arr).data("world_art_id");
		var out_kinoriumid = $(fields_arr).data("kinorium_id");
		var out_tmdbid = $(fields_arr).data("tmdb_id");
		var out_shikimoriid = $(fields_arr).data("shikimori_id");
		var out_mdlid = $(fields_arr).data("mdl_id");

		var out_title = $(fields_arr).data("title");
		var out_title_orig = $(fields_arr).data("title_orig");
		var out_title_ua = $(fields_arr).data("title_ua");
		var out_title_alt = $(fields_arr).data("title_alt");
		
		var out_year = $(fields_arr).data("year");
		var out_duration = $(fields_arr).data("duration");
		var out_ganre = $(fields_arr).data("ganre");
		var out_collections = $(fields_arr).data("collections");
		var out_country = $(fields_arr).data("country");
		
		var out_director = $(fields_arr).data("director");
		var out_actors = $(fields_arr).data("actors");
		var out_producer = $(fields_arr).data("producer");
		var out_voice_director = $(fields_arr).data("voice_director");
		var out_translator = $(fields_arr).data("translator");
		var out_writer = $(fields_arr).data("writer");
		var out_operator = $(fields_arr).data("operator");
		var out_composer = $(fields_arr).data("composer");
		var out_design = $(fields_arr).data("design");
		var out_editor = $(fields_arr).data("editor");
		
		var out_kinopoisk_rate = $(fields_arr).data("kinopoisk_rate");
		var out_kinopoisk_rate_votes = $(fields_arr).data("kinopoisk_rate_votes");
		var out_imdb_rate = $(fields_arr).data("imdb_rate");
		var out_imdb_rate_votes = $(fields_arr).data("imdb_rate_votes");
		var out_tmdb_rate = $(fields_arr).data("tmdb_rate");
		var out_tmdb_rate_votes = $(fields_arr).data("tmdb_rate_votes");
		
		var title_rw = <?=$title_rw?>;
		var title_orig_rw = <?=$title_orig_rw?>;
		var title_ua_rw = <?=$title_ua_rw?>;
		var title_alt_rw = <?=$title_alt_rw?>;
		var year_rw = <?=$year_rw?>;
		var duration_rw = <?=$duration_rw?>;
		var ganre_rw = <?=$ganre_rw?>;
		var collections_rw = <?=$collections_rw?>;
		var country_rw = <?=$country_rw?>;
		
		var director_rw = <?=$director_rw?>;
		var actors_rw = <?=$actors_rw?>;
		var producer_rw = <?=$producer_rw?>;
		var voice_director_rw = <?=$voice_director_rw?>;
		var translator_rw = <?=$translator_rw?>;
		var writer_rw = <?=$writer_rw?>;
		var operator_rw = <?=$operator_rw?>;
		var composer_rw = <?=$composer_rw?>;
		var design_rw = <?=$design_rw?>;
		var editor_rw = <?=$editor_rw?>;
		var kinopoisk_rate_rw = <?=$kinopoisk_rate_rw?>;
		var kinopoisk_rate_votes_rw = <?=$kinopoisk_rate_votes_rw?>;
		var imdb_rate_rw = <?=$imdb_rate_rw?>;
		var imdb_rate_votes_rw = <?=$imdb_rate_votes_rw?>;
		var tmdb_rate_rw = <?=$tmdb_rate_rw?>;
		var tmdb_rate_votes_rw = <?=$tmdb_rate_votes_rw?>;
		var poster_no_upload = <?=$poster_no_upload?>;
    
		//
		var out_poster = $(fields_arr).data("poster");
		var out_screen = $(fields_arr).data("screen");
		var out_age = $(fields_arr).data("age");
		var out_mpaa = $(fields_arr).data("mpaa");
		var out_premier_rf = $(fields_arr).data("premier_rf");
		var out_premier_mir = $(fields_arr).data("premier_mir");

		var out_budget = $(fields_arr).data("budget");
		var out_feels_usa = $(fields_arr).data("feels_usa");
		var out_feels_world = $(fields_arr).data("feels_world");
		var out_feels_russia = $(fields_arr).data("feels_russia");

		var out_trivia = $(fields_arr).data("trivia");
		var out_slogan = $(fields_arr).data("slogan");
		var out_description = $(fields_arr).data("description");
		var out_description_short = $(fields_arr).data("description_short");
		
		var age_rw = <?=$age_rw?>;
		var mpaa_rw = <?=$mpaa_rw?>;
		var premier_mir_rw = <?=$premier_mir_rw?>;
		var premier_rf_rw = <?=$premier_rf_rw?>;
		var budget_rw = <?=$budget_rw?>;
		var feels_usa_rw = <?=$feels_usa_rw?>;
		var feels_world_rw = <?=$feels_world_rw?>;
		var feels_russia_rw = <?=$feels_russia_rw?>;
		var slogan_rw = <?=$slogan_rw?>;
		var trivia_rw = <?=$trivia_rw?>;
		var not_fullstory = <?=$not_fullstory?>;
		
		var out_cats = String($(fields_arr).data("cats"));
		if (out_cats.indexOf(',') > 0)
			out_cats = out_cats.split(',');
		else
			out_cats = [out_cats];
		
		$('[data-rel=links]').tokenfield('destroy');
		// Запись в поля
		if (out_cats.length >= 1) {
			for (i = 0;i < out_cats.length; i++) {
				if (out_cats[i] == '' || out_cats[i] == '-') continue;
				$("#category option[value="+out_cats[i]+"]").prop("selected", true).trigger("chosen:updated");
			}
			$("#category").trigger("chosen:updated");
			$("#category").trigger("liszt:updated");			
			$("#").trigger("chosen:updated");
			$("#").trigger("liszt:updated");
		}
			 
		if(url != 'undefined') $(field_output).val(url);

		if($("#<?=$field_output_quality?>") && out_quality != 'undefined') 
			$("#<?=$field_output_quality?>").val(out_quality);
		
		if(out_episode != 'undefined' && out_episode != '' && out_season != 'undefined' && out_season != '') {
		  if($("#<?=$field_output_season?>")) $("#<?=$field_output_season?>").val(out_season);
		  if($("#<?=$field_output_episode?>")) $("#<?=$field_output_episode?>").val(out_episode);
		} else {
		  if($("#<?=$field_output_season?>")) $("#<?=$field_output_season?>").val('');
		  if($("#<?=$field_output_episode?>")) $("#<?=$field_output_episode?>").val('');
		}
		
		if($("#<?=$field_output_voice?>") && out_voice != 'undefined') 
			$("#<?=$field_output_voice?>").val(out_voice);

		if(autokpimdbwa) {
		  if('<?=$field_kinopoisk?>' != 'pspvolt_id' && $("#<?=$field_kinopoisk?>") && out_kinopoisk_id && out_kinopoisk_id != 'undefined') $("#<?=$field_kinopoisk?>" ).val(out_kinopoisk_id);
		  if($("#<?=$field_imdbid?>") && out_imdbid && out_imdbid != 'undefined') $("#<?=$field_imdbid?>").val(out_imdbid);		   
		  if($("#<?=$field_worldart?>") && out_waid && out_waid != 'undefined') $("#<?=$field_worldart?>").val(out_waid);
		  if($("#<?=$field_kinorium?>") && out_kinoriumid && out_kinoriumid != 'undefined') $("#<?=$field_kinorium?>").val(out_kinoriumid);
		  if($("#<?=$field_tmdb?>") && out_tmdbid && out_tmdbid != 'undefined') $("#<?=$field_tmdb?>").val(out_tmdbid);
		  if($("#<?=$field_shikimori?>") && out_shikimoriid && out_shikimoriid != 'undefined') $("#<?=$field_shikimori?>").val(out_shikimoriid);
		  if($("#<?=$field_mdl?>") && out_mdlid && out_mdlid != 'undefined') $("#<?=$field_mdl?>").val(out_mdlid);
		}
		
		if($("#<?=$field_output_title?>") && out_title != 'undefined' ) 
			if ($("#<?=$field_output_title?>").val() == '' || ($("#<?=$field_output_title?>").val() != '' && title_rw)) 
				$("#<?=$field_output_title?>").val(out_title);
		
		if($("#<?=$field_output_title_orig?>") && out_title_orig != 'undefined' ) 
			if ($("#<?=$field_output_title_orig?>").val() == '' || ($("#<?=$field_output_title_orig?>").val() != '' && title_orig_rw)) 
				$("#<?=$field_output_title_orig?>").val(out_title_orig);
		
		if($("#<?=$field_output_title_ua?>") && out_title_ua != 'undefined' ) 
			if ($("#<?=$field_output_title_ua?>").val() == '' || ($("#<?=$field_output_title_ua?>").val() != '' && title_ua_rw)) 
				$("#<?=$field_output_title_ua?>").val(out_title_ua);
			
		if($("#<?=$field_output_title_alt?>") && out_title_alt != 'undefined' ) 
			if ($("#<?=$field_output_title_alt?>").val() == '' || ($("#<?=$field_output_title_alt?>").val() != '' && title_alt_rw)) 
				$("#<?=$field_output_title_alt?>").val(out_title_alt);
		
		if($("#<?=$field_output_year?>") && out_year != 'undefined' ) 
			if ($("#<?=$field_output_year?>").val() == '' || ($("#<?=$field_output_year?>").val() != '' && year_rw)) 
				$("#<?=$field_output_year?>").val(out_year);
			
		if($("#<?=$field_output_duration?>") && out_duration != 'undefined' ) 
			if ($("#<?=$field_output_duration?>").val() == '' || ($("#<?=$field_output_duration?>").val() != '' && duration_rw)) 
				$("#<?=$field_output_duration?>").val(out_duration);
		
		if($("#<?=$field_output_ganre?>") && out_ganre != 'undefined' ) 
			if ($("#<?=$field_output_ganre?>").val() == '' || ($("#<?=$field_output_ganre?>").val() != '' && ganre_rw)) 
				$("#<?=$field_output_ganre?>").val(out_ganre);
			
		if($("#<?=$field_output_collections?>") && out_collections != 'undefined' ) {
			if ($("#<?=$field_output_collections?>").val() == '' ) 
				$("#<?=$field_output_collections?>").val(out_collections);
			else if ($("#<?=$field_output_collections?>").val() != '' && collections_rw) { 
				old_collections = $("#<?=$field_output_collections?>").val();
				old_collections = old_collections + ',' + out_collections;
				old_collections_arr = new Set(old_collections.split(', '));
				out_collections = Array.from(old_collections_arr).join(', ');
				$("#<?=$field_output_collections?>").val(out_collections);
			}
		}
			
		if($("#<?=$field_output_country?>") && out_country != 'undefined' ) 
			if ($("#<?=$field_output_country?>").val() == '' || ($("#<?=$field_output_country?>").val() != '' && country_rw)) 
				$("#<?=$field_output_country?>").val(out_country);
			
		if($("#<?=$field_output_director?>") && out_director != 'undefined' ) 
			if ($("#<?=$field_output_director?>").val() == '' || ($("#<?=$field_output_director?>").val() != '' && director_rw)) 
				$("#<?=$field_output_director?>").val(out_director);
			
		if($("#<?=$field_output_actors?>") && out_actors != 'undefined' ) 
			if ($("#<?=$field_output_actors?>").val() == '' || ($("#<?=$field_output_actors?>").val() != '' && actors_rw)) 
				$("#<?=$field_output_actors?>").val(out_actors);
			
		if($("#<?=$field_output_producer?>") && out_producer != 'undefined' ) 
			if ($("#<?=$field_output_producer?>").val() == '' || ($("#<?=$field_output_producer?>").val() != '' && producer_rw)) 
				$("#<?=$field_output_producer?>").val(out_producer);
			
		if($("#<?=$field_output_voice_director?>") && out_voice_director != 'undefined' ) 
			if ($("#<?=$field_output_voice_director?>").val() == '' || ($("#<?=$field_output_voice_director?>").val() != '' && voice_director_rw)) 
				$("#<?=$field_output_voice_director?>").val(out_voice_director);
			
		if($("#<?=$field_output_translator?>") && out_translator != 'undefined' ) 
			if ($("#<?=$field_output_translator?>").val() == '' || ($("#<?=$field_output_translator?>").val() != '' && translator_rw)) 
				$("#<?=$field_output_translator?>").val(out_translator);
			
		if($("#<?=$field_output_writer?>") && out_writer != 'undefined' ) 
			if ($("#<?=$field_output_writer?>").val() == '' || ($("#<?=$field_output_writer?>").val() != '' && writer_rw)) 
				$("#<?=$field_output_writer?>").val(out_writer);
			
		if($("#<?=$field_output_operator?>") && out_operator != 'undefined' ) 
			if ($("#<?=$field_output_operator?>").val() == '' || ($("#<?=$field_output_operator?>").val() != '' && operator_rw)) 
				$("#<?=$field_output_operator?>").val(out_operator);
			
		if($("#<?=$field_output_composer?>") && out_composer != 'undefined' ) 
			if ($("#<?=$field_output_composer?>").val() == '' || ($("#<?=$field_output_composer?>").val() != '' && composer_rw)) 
				$("#<?=$field_output_composer?>").val(out_composer);
			
		if($("#<?=$field_output_design?>") && out_design != 'undefined' ) 
			if ($("#<?=$field_output_design?>").val() == '' || ($("#<?=$field_output_design?>").val() != '' && design_rw)) 
				$("#<?=$field_output_design?>").val(out_design);
		
		if($("#<?=$field_output_editor?>") && out_editor != 'undefined' ) 
			if ($("#<?=$field_output_editor?>").val() == '' || ($("#<?=$field_output_editor?>").val() != '' && editor_rw)) 
				$("#<?=$field_output_editor?>").val(out_editor);
			
		if($("#<?=$field_output_kinopoisk_rate?>") && out_kinopoisk_rate != 'undefined' ) 
			if ($("#<?=$field_output_kinopoisk_rate?>").val() == '' || ($("#<?=$field_output_kinopoisk_rate?>").val() != '' && kinopoisk_rate_rw)) 
				$("#<?=$field_output_kinopoisk_rate?>").val(out_kinopoisk_rate);
			
		if($("#<?=$field_output_kinopoisk_rate_votes?>") && out_kinopoisk_rate_votes != 'undefined' ) 
			if ($("#<?=$field_output_kinopoisk_rate_votes?>").val() == '' || ($("#<?=$field_output_kinopoisk_rate_votes?>").val() != '' && kinopoisk_rate_votes_rw)) 
				$("#<?=$field_output_kinopoisk_rate_votes?>").val(out_kinopoisk_rate_votes);
			
		if($("#<?=$field_output_imdb_rate?>") && out_imdb_rate != 'undefined' ) 
			if ($("#<?=$field_output_imdb_rate?>").val() == '' || ($("#<?=$field_output_imdb_rate?>").val() != '' && imdb_rate_rw)) 
				$("#<?=$field_output_imdb_rate?>").val(out_imdb_rate);
			
		if($("#<?=$field_output_imdb_rate_votes?>") && out_imdb_rate_votes != 'undefined' ) 
			if ($("#<?=$field_output_imdb_rate_votes?>").val() == '' || ($("#<?=$field_output_imdb_rate_votes?>").val() != '' && imdb_rate_votes_rw)) 
				$("#<?=$field_output_imdb_rate_votes?>").val(out_imdb_rate_votes);
		
		if($("#<?=$field_output_tmdb_rate?>") && out_tmdb_rate != 'undefined' ) 
			if ($("#<?=$field_output_tmdb_rate?>").val() == '' || ($("#<?=$field_output_tmdb_rate?>").val() != '' && tmdb_rate_rw)) 
				$("#<?=$field_output_tmdb_rate?>").val(out_tmdb_rate);
			
		if($("#<?=$field_output_tmdb_rate_votes?>") && out_tmdb_rate_votes != 'undefined' ) 
			if ($("#<?=$field_output_tmdb_rate_votes?>").val() == '' || ($("#<?=$field_output_tmdb_rate_votes?>").val() != '' && tmdb_rate_votes_rw)) 
				$("#<?=$field_output_tmdb_rate_votes?>").val(out_tmdb_rate_votes);
		
		///
		///
		<?php if (!empty($field_output_poster)) : ?>
		if($("#<?=$field_output_poster?>") && out_poster != 'undefined' ) {
			if ($("#<?=$field_output_poster?>").val() == '') {
				if (poster_no_upload && '<?=$_field_poster[2]?>' == 'text')
					$("#<?=$field_output_poster?>").val(out_poster);
				else {
					var upload_url = "/engine/ajax/cdnm.php?action=upload&image_url="+encodeURIComponent(out_poster)+"&author="+$("input[name=new_author]").val()+"&post_id="+$("input[name=id]").val();
					$.ajax({
						url: upload_url,
						dataType: "json",
						cache: false,
						success: function(data) {
							if (data.link != "") {
								if ('<?=$_field_poster[2]?>' == 'image') {
									data.link = data.link.replace('/uploads/posts/', '');

									var upload_image = '<div id="xf_'+ data.md5 +'" class="uploadedfile" data-id="'+ data.link +'" data-alt="">'+
										'<div class="info">'+ data.filename +'</div>'+
										'<div class="uploadimage"><img style="width:auto;height:auto;max-width:100px;max-height:90px;" src="/uploads/posts/'+ data.link +'"></div>'+
										'<div class="info"><a href="#" onclick="xfaddalt(\''+ data.md5 +'\', \'poster\');return false;">Описание</a>'+
										'<br><a href="#" onclick="xfimagedelete(\'poster\',\''+ data.link +'\');return false;">Удалить</a></div>'+
										'</div>';
									$('#uploadedfile_<?=$_field_poster[0]?>').html(upload_image);
									$('#xfupload_<?=$_field_poster[0]?> .qq-upload-button input').prop('disabled', true);
								}
								$("#<?=$field_output_poster?>").val(data.link);
							}
						}
					});
				}
			}
		} else if( $("#<?=$field_output_poster?>") && out_poster == 'undefined' && '<?=$_field_poster[2]?>' == 'text') {
				if ('<?=$this->config['output_poster_no']?>' != '') $("#<?=$field_output_poster?>").val(out_poster) = '<?=$this->config['output_poster_no']?>';
		}
		<?php endif;?>
		<?php if (!empty($field_output_screen)) : ?>
		if($("#<?=$field_output_screen?>") && out_screen != 'undefined' ) {
			if ($("#<?=$field_output_screen?>").val() == '') {
					var upload_url = "/engine/ajax/cdnm.php?action=upload&image_type=screen&image_url="+encodeURIComponent(out_screen)+"&author="+$("input[name=new_author]").val()+"&post_id="+$("input[name=id]").val();
					$.ajax({
						url: upload_url,
						dataType: "json",
						cache: false,
						success: function(data) {
							if (data.links.length > 0) {
								var upload_image = '';
								var xf_md5 = '<?=md5($field_output_screen)?>';
								$.each(data.links, function(key, item) {
									upload_image = upload_image +'<div class="file-preview-card uploadedfile" id="xf_'+ item.link_md5 +'" data-id="'+ item.link +'" data-alt=""><div class="active-ribbon"><span><i class="mediaupload-icon mediaupload-icon-ok"></i></span></div><div class="file-content"><img src="/uploads/posts/'+ item.link +'" class="file-preview-image"></div><div class="file-footer"><div class="file-footer-caption"><div class="file-caption-info" rel="tooltip" title="'+ item.filename +'">'+ item.filename +'</div><div class="file-size-info"> ()</div></div><div class="file-footer-bottom"><div class="file-preview"><a onclick="xfaddalt(\''+ item.link_md5 +'\', \'screens\');return false;" href="#" rel="tooltip" title="Описание"><i class="mediaupload-icon mediaupload-icon-edit"></i></a></div><div class="file-delete"><a class="file-delete-link" onclick="xfimagegalerydelete_'+ xf_md5 +'(\'screens\',\''+ item.link +'\', \''+ item.link_md5 +'\');return false;" href="#"><i class="mediaupload-icon mediaupload-icon-trash"></i></a></div></div></div></div>';

								});
								$('#uploadedfile_<?=$_field_screen[0]?>').html(upload_image);
								$("#<?=$field_output_screen?>").val(data.links_str);
								
							}
						}
					});
				
			}
		}
		<?php endif;?>
			
		if($("#<?=$field_output_age?>") && out_age != 'undefined' ) 
			if ($("#<?=$field_output_age?>").val() == '' || ($("#<?=$field_output_age?>").val() != '' && age_rw)) 
				$("#<?=$field_output_age?>").val(out_age);
			
		if($("#<?=$field_output_mpaa?>") && out_mpaa != 'undefined' ) 
			if ($("#<?=$field_output_mpaa?>").val() == '' || ($("#<?=$field_output_mpaa?>").val() != '' && mpaa_rw)) 
				$("#<?=$field_output_mpaa?>").val(out_mpaa);
		
		if($("#<?=$field_output_premier_mir?>") && out_premier_mir != 'undefined' ) 
			if ($("#<?=$field_output_premier_mir?>").val() == '' || ($("#<?=$field_output_premier_mir?>").val() != '' && premier_mir_rw)) 
				$("#<?=$field_output_premier_mir?>").val(out_premier_mir);
			
		if($("#<?=$field_output_premier_rf?>") && out_premier_rf != 'undefined' ) 
			if ($("#<?=$field_output_premier_rf?>").val() == '' || ($("#<?=$field_output_premier_rf?>").val() != '' && premier_rf_rw)) 
				$("#<?=$field_output_premier_rf?>").val(out_premier_rf);
			
		if($("#<?=$field_output_budget?>") && out_budget != 'undefined' ) 
			if ($("#<?=$field_output_budget?>").val() == '' || ($("#<?=$field_output_budget?>").val() != '' && budget_rw)) 
				$("#<?=$field_output_budget?>").val(out_budget);
			
		if($("#<?=$field_output_feels_usa?>") && out_feels_usa != 'undefined' ) 
			if ($("#<?=$field_output_feels_usa?>").val() == '' || ($("#<?=$field_output_feels_usa?>").val() != '' && feels_usa_rw)) 
				$("#<?=$field_output_feels_usa?>").val(out_feels_usa);
		
		if($("#<?=$field_output_feels_world?>") && out_feels_world != 'undefined' ) 
			if ($("#<?=$field_output_feels_world?>").val() == '' || ($("#<?=$field_output_feels_world?>").val() != '' && feels_world_rw)) 
				$("#<?=$field_output_feels_world?>").val(out_feels_world);

		if($("#<?=$field_output_feels_russia?>") && out_feels_russia != 'undefined' ) 
			if ($("#<?=$field_output_feels_russia?>").val() == '' || ($("#<?=$field_output_feels_russia?>").val() != '' && feels_russia_rw)) 
				$("#<?=$field_output_feels_russia?>").val(out_feels_russia);

		if($("#<?=$field_output_slogan?>") && out_slogan != 'undefined' ) 
			if ($("#<?=$field_output_slogan?>").val() == '' || ($("#<?=$field_output_slogan?>").val() != '' && slogan_rw)) 
				$("#<?=$field_output_slogan?>").val(out_slogan);
			
		if($("#<?=$field_output_trivia?>") && out_trivia != 'undefined' ) 
			if ($("#<?=$field_output_trivia?>").val() == '' || ($("#<?=$field_output_trivia?>").val() != '' && trivia_rw)) 
				$("#<?=$field_output_trivia?>").val(out_trivia);
			
		if($("#full_story") && out_description != 'undefined' && !not_fullstory) {
			if ($("#full_story").val() == '') {
				$("#full_story").val(out_description);
				
				// var full_tiny = tinymce.get('full_story_ifr'); 
				// if (full_tiny) 
					// full_tiny.setContent(out_description); 
			}
		}
		
		if($("#short_story") && out_description_short != 'undefined' ) {
			if ($("#short_story").val() == '') {
				$("#short_story").val(out_description_short);
				
				// var short_tiny = tinymce.get('short_story_ifr'); 
				// if (short_tiny) 
					// short_tiny.setContent(out_description_short); 
			}
		}
			
			
		// Shablons
		var cdnm_shab_metatitle = <?=$cdnm_shab_metatitle?>;
		var cdnm_shab_metadescription = <?=$cdnm_shab_metadescription?>;
		var cdnm_shab_title = <?=$cdnm_shab_title?>;
		var cdnm_shab_chpu = <?=$cdnm_shab_chpu?>;
		
		var cdnm_shab_metatitle_movie = <?=$cdnm_shab_metatitle_movie?>;
		var cdnm_shab_metadescription_movie = <?=$cdnm_shab_metadescription_movie?>;
		var cdnm_shab_title_movie = <?=$cdnm_shab_title_movie?>;
		var cdnm_shab_chpu_movie = <?=$cdnm_shab_chpu_movie?>;
			
		// SHAB
		var cdnm_shab_date = "<?=$cdnm_shab_date?>";			
		var cdnm_shabs = "<?=$cdnm_shabs?>"; //console.log(cdnm_shabs);

		cdnm_shabs = cdnm_shabs.replace(/\{title\}/g,out_title);
		cdnm_shabs = cdnm_shabs.replace(/\{year\}/g,out_year);
		cdnm_shabs = cdnm_shabs.replace(/\{date\}/g,cdnm_shab_date);
		cdnm_shabs = cdnm_shabs.replace(/\{description\}/g,out_description);

		cdnm_shabs = cdnm_shabs.replace(/\{xfvalue_(.+?)\}/g, function(xf){
			var tmp = xf.replace(/\{xfvalue_(.+?)\}/g, '$1');
			var result_input = $('#xf_'+tmp).val();
			var result_select = $('select[name="xfield['+tmp+']"] option:selected').text();
			if (result_input) return result_input;
			else if (result_select) return result_select;
			else return '';
		});
		if (type == 'movie') {
			cdnm_shabs = cdnm_shabs.replace(/\{if_movie\}(.+?)\{\/if_movie\}/g, '$1');
			cdnm_shabs = cdnm_shabs.replace(/\{if_serial\}(.+?)\{\/if_serial\}/g, '');
		} else {
			cdnm_shabs = cdnm_shabs.replace(/\{if_movie\}(.+?)\{\/if_movie\}/g, '');
			cdnm_shabs = cdnm_shabs.replace(/\{if_serial\}(.+?)\{\/if_serial\}/g, '$1');
		}
		
		//console.log(cdnm_shabs);
		cdnm_shabs = cdnm_shabs.split('#SEP#');
		
		if (cdnm_shabs[0] != '' && (type != 'movie' || cdnm_shab_metatitle_movie) && cdnm_shab_metatitle) {
			cdnm_shabs[0] = cdnm_shabs[0].replace(/\{season\}/g, serials_format("season", "<?=$this->config['uf_metatitle_season_format']?>", out_season_orig, "<?=$this->config['output_season_format']?>", 0));
			cdnm_shabs[0] = cdnm_shabs[0].replace(/\{episode\}/g, serials_format("episode", "<?=$this->config['uf_metatitle_episode_format']?>", out_episode_orig, "<?=$this->config['output_episode_format']?>", "<?=$this->config['uf_metatitle_plus']?>"));
			$('input[name="meta_title"]').val(cdnm_shabs[0]);
			//console.log('metatitle - '+cdnm_shabs[0]);
		}
		if (cdnm_shabs[1] != '' && (type != 'movie' || cdnm_shab_metadescription_movie) && cdnm_shab_metadescription) {
			cdnm_shabs[1] = cdnm_shabs[1].replace(/\{season\}/g, serials_format("season", "<?=$this->config['uf_description_season_format']?>", out_season_orig, "<?=$this->config['output_season_format']?>", 0));
			cdnm_shabs[1] = cdnm_shabs[1].replace(/\{episode\}/g, serials_format("episode", "<?=$this->config['uf_description_episode_format']?>", out_episode_orig, "<?=$this->config['output_episode_format']?>", "<?=$this->config['uf_description_plus']?>"));
			$('input[name="descr"]').val(cdnm_shabs[1]);
			//console.log('description - '+cdnm_shabs[1]);
		}
		if (cdnm_shabs[2] != '' && (type != 'movie' || cdnm_shab_title_movie) && cdnm_shab_title) {
			cdnm_shabs[2] = cdnm_shabs[2].replace(/\{season\}/g, serials_format("season", "<?=$this->config['uf_title_season_format']?>", out_season_orig, "<?=$this->config['output_season_format']?>", 0));
			cdnm_shabs[2] = cdnm_shabs[2].replace(/\{episode\}/g, serials_format("episode", "<?=$this->config['uf_title_episode_format']?>", out_episode_orig, "<?=$this->config['output_episode_format']?>", "<?=$this->config['uf_title_plus']?>"));
			$('#title').val(cdnm_shabs[2]);
			//console.log('title - '+cdnm_shabs[2]);
		}
		if (cdnm_shabs[3] != '' && (type != 'movie' || cdnm_shab_chpu_movie) && cdnm_shab_chpu) {
			cdnm_shabs[3] = cdnm_shabs[3].replace(/\{season\}/g, serials_format("season", "<?=$this->config['uf_chpu_season_format']?>", out_season_orig, "<?=$this->config['output_season_format']?>", 0));
			cdnm_shabs[3] = cdnm_shabs[3].replace(/\{episode\}/g, serials_format("episode", "<?=$this->config['uf_chpu_episode_format']?>", out_episode_orig, "<?=$this->config['output_episode_format']?>", "<?=$this->config['uf_chpu_plus']?>"));
			cdnm_shabs[3] = cdnm_shabs[3].replace(/,/g, '-');
			$('input[name="alt_name"]').val(cdnm_shabs[3]);
			//console.log('chpu - '+cdnm_shabs[3]);
		}
			
		// SHAB end

		$("#cdnmClearOutputButton,#cdnmPreviewOutputButton").show();
		
		$('[data-rel=links]').tokenfield({createTokensOnBlur:true});

		cdnm_hide_loading();
	};

	if ($("#<?=$field_output?>").val()) {
		$("#cdnmClearOutputButton,#cdnmPreviewOutputButton").show();
	};

	$("#<?=$field_output?>").keyup(function() {
		if ($(this).val())
			$("#cdnmClearOutputButton,#cdnmPreviewOutputButton").show();
		else
			$("#cdnmClearOutputButton,#cdnmPreviewOutputButton").hide();
	});

	$("#cdnmFindButton").click(function() {
		cdnm_show_loading();

		var array_fields = [];

		var output = '';
		if($("#<?=$field_output?>").val()) output = $("#<?=$field_output?>").val();
		if($("#<?=$field_kinopoisk?>").val()) array_fields['kinopoisk_id'] = $("#<?=$field_kinopoisk?>").val();
		if($("#<?=$field_imdbid?>").val()){
			var tt = '';
			if ($("#<?=$field_imdbid?>").val().indexOf('tt') == -1) tt = 'tt';
			else array_fields['imdb_id'] = tt + $("#<?=$field_imdbid?>").val();
		}
		if($("#<?=$field_worldart?>").val()) array_fields['worldart_id'] = $("#<?=$field_worldart?>").val();
		if($("#<?=$field_kinorium?>").val()) array_fields['kinorium_id'] = $("#<?=$field_kinorium?>").val();
		if($("#<?=$field_shikimori?>").val()) array_fields['shikimori_id'] = $("#<?=$field_shikimori?>").val();
		if($("#<?=$field_mdl?>").val()) array_fields['mdl_id'] = $("#<?=$field_mdl?>").val();
		if($("#<?=$field_tmdb?>").val()) array_fields['tmdb_id'] = $("#<?=$field_tmdb?>").val();

		var find_url = "/engine/ajax/cdnm.php?action=find";

		$("#cdnmClearFindButton").hide();
		$("#cdnmFindResults").html("");

		if ( Object.keys(array_fields).length > 0 ) {
			var out_base = ''
			
			for (var key in array_fields) {
			  var sep = '';
			  if (out_base != '') sep = ',';
			
			  out_base += sep + key + '=' + array_fields[key];
			}
			find_url += "&out_base=" + out_base;
			
			if (output != 'undefined' && output != '') find_url += "&output=" + encodeURIComponent(output);

				$.ajax({
					url: find_url,
					dataType: "json",
					cache: false,
					success: function(data) {

						if (data.error == 'Content not found!' || (data.status == "ok" && data.result.length == 0) ) {
							$("#cdnmFindResults").html("<ul class=\"cdnm-list-group\"><li class=\"cdnm-list-group-item\">По вашему запросу ничего не найдено.</li></ul>");
						} else if (data.status == "error" || typeof data.error != 'undefined') {
							$("#cdnmFindResults").html("<ul class=\"cdnm-list-group\"><li class=\"cdnm-list-group-item\">Ошибка запроса к апи. ("+data.error+")</li></ul>");
						} else {
							var results = "<ul class=\"cdnm-list-group\">";

							cdnm_rdata = data.result;
							
							console.log(data.result);

							$.each(data.result, function(key, item) {

								var num = key + 1;

								if (item.type == 'movie') {
									var type = "<span class=\"cdnm-label cdnm-label-blue\">"+item.content_type+"</span>";
								} else if (item.type == 'serial') {
									var type = "<span class=\"cdnm-label cdnm-label-green\">"+item.content_type+" S" + item.seasons_orig + "E" + item.episodes_orig + "</span>";
								} else {
									var type = "<span class=\"cdnm-label cdnm-label-red\" title=\"Нет в базе\">Нет в базе</span>";
								}
								
								var hd_video = "";
								if (item.quality) {
									var hd_video = "<span class=\"cdnm-label cdnm-label-gray\" title=\"Качество\">" + item.quality + "</span>";
								}

								var translations = "";
								if (item.translate) {
									translations_arr = item.translate.split(', ');
									translations = "<span class=\"cdnm-label cdnm-label-purple\" title=\"Озвучка\">";
									translations += translations_arr.join('</span><span class="cdnm-label cdnm-label-purple" title="Озвучка">');
									translations += "</span>";
								}
								
								var kinopoisk_id = "";
								if (item.kinopoisk_id) {
									kinopoisk_id = "<a title=\"КиноПоиск\" class=\"cdnm-label\" style=\"background: #FE800A;\" target=\"_blank\" href=\"https://www.kinopoisk.ru/film/" + item.kinopoisk_id + "/\">KP: " + item.kinopoisk_id + "</a>";
								}
								var imdb_id = "";
								if (item.imdb_id) {
									tt = '';
									if (item.imdb_id.indexOf('tt') == -1) tt = 'tt';
									
									imdb_id = "<a title=\"IMDb\" class=\"cdnm-label\" style=\"background: #FE800A;\" target=\"_blank\" href=\"https://www.imdb.com/title/" + tt + item.imdb_id + "/\">IMDB: " + tt + item.imdb_id + "</a>";
								}
								var world_art_id = "";
								if (item.world_art_id) {
									world_art_id = "<a title=\"World Art\" class=\"cdnm-label\" style=\"background: #FE800A;\" target=\"_blank\" href=\"http://www.world-art.ru/animation/animation.php?id=" + item.world_art_id + "/\">WA: " + item.world_art_id + "</a>";
								}
								var tmdb_id = "";
								if (item.tmdb_id) {
									if (item.content_type == '1' || item.content_type == '3')
										tmdb_type = 'movie/';
									else 
										tmdb_type = 'tv/';
									
									tmdb_id = "<a title=\"TMDB\" class=\"cdnm-label\" style=\"background: #FE800A;\" target=\"_blank\" href=\"https://www.themoviedb.org/" + tmdb_type + item.tmdb_id + "\">TMDB: " + item.tmdb_id + "</a>";
								}
								var kinorium_id = "";
								if (item.kinorium_id) {
									tmdb_id = "<a title=\"Kinorium\" class=\"cdnm-label\" style=\"background: #FE800A;\" target=\"_blank\" href=\"https://ru.kinorium.com/" + item.kinorium_id + "/\">Kinorium: " + item.kinorium_id + "</a>";
								}
								var mdl_id = "";
								if (item.mdl_id) {
									mdl_id = "<a title=\"MyDrammaList\" class=\"cdnm-label\" style=\"background: #FE800A;\" target=\"_blank\" href=\"https://mydramalist.com/" + item.mdl_id + "\">MDL: " + item.mdl_id + "</a>";
								}
								var shikimori_id = "";
								if (item.shikimori_id) {
									shikimori_id = "<span title=\"Shikimori\" class=\"cdnm-label\" style=\"background: #FE800A;\">Shikimori: " + item.shikimori_id + "</span>";
								}
								
								var year = '';
								if (item.year) year = "<span class=\"cdnm-year\">(" + item.year + ")</span>";
								
								var season_orig = '';
								var episodes_orig = '';
								if (item.seasons_orig) season_orig = item.seasons_orig;
								if (item.episodes_orig) episodes_orig = item.episodes_orig;
								
								results += '<li id="cdnm_item_' + num + '" class="cdnm-list-group-item" data-nid="' + (num - 1) + 
								'" data-vid="' + num + 
								'" data-type="' + item.type + 
								'" data-quality="' + item.quality + 
								'" data-voice="' + item.translate + 
								'" data-season="' + item.seasons + 
								'" data-episode="' + item.episodes + 
								'" data-season_orig="' + season_orig + 
								'" data-episode_orig="' + episodes_orig + 
								'" data-cdnm_id="' + item.id + 
								'" data-kinopoisk_id="' + item.kinopoisk_id + 
								'" data-imdbid="' + item.imdb_id + 
								'" data-world_art_id="' + item.world_art_id + 
								'" data-kinorium_id="' + item.kinorium_id + 
								'" data-tmdb_id="' + item.tmdb_id + 
								'" data-shikimori_id="' + item.shikimori_id + 
								'" data-mdl_id="' + item.mdl_id + 
								'" data-title="' + item.title + 
								'" data-title_orig="' + item.title_orig + 
								'" data-title_ua="' + item.title_ua + 
								'" data-title_alt="' + item.title_alt + 
								'" data-year="' + item.year + 
								'" data-duration="' + item.duration + 
								'" data-collections="' + item.collections + 
								'" data-ganre="' + item.ganre + 
								'" data-country="' + item.country + 
								'" data-director="' + item.director + 
								'" data-actors="' + item.actors + 
								'" data-producer="' + item.producer + 
								'" data-voice_director="' + item.voice_director + 
								'" data-translator="' + item.translator + 
								'" data-writer="' + item.writer + 
								'" data-operator="' + item.operator + 
								'" data-composer="' + item.composer + 
								'" data-design="' + item.design + 
								'" data-editor="' + item.editor + 
								'" data-kinopoisk_rate="' + item.kinopoisk_rate + 
								'" data-kinopoisk_rate_votes="' + item.kinopoisk_rate_votes + 
								'" data-imdb_rate="' + item.imdb_rate + 
								'" data-imdb_rate_votes="' + item.imdb_rate_votes + 
								'" data-tmdb_rate="' + item.tmdb_rate + 
								'" data-tmdb_rate_votes="' + item.tmdb_rate_votes + 
								'" data-poster="' + item.poster + 
								'" data-screen="' + item.screen.join(',') + 
								'" data-age="' + item.age + 
								'" data-mpaa="' + item.mpaa + 
								'" data-premier_rf="' + item.premier_rf + 
								'" data-premier_mir="' + item.premier_mir + 
								'" data-budget="' + item.budget + 
								'" data-feels_world="' + item.feels_world + 
								'" data-feels_usa="' + item.feels_usa + 
								'" data-feels_russia="' + item.feels_russia + 
								'" data-trivia="' + item.trivia + 
								'" data-slogan="' + item.slogan + 
								'" data-description="' + item.description + 
								'" data-description_short="' + item.description_short + '"' + 
								'" data-cats="' + item.ganres_cats + '">';

								results += "<button type=\"button\" class=\"cdnm-btn cdnm-btn-xs cdnm-btn-default cdnm-btn-right\" onclick=\"cdnm_set_output('" + item.iframe_src + "', this, " + num + ")\" >" + (item.type != 'not-found'? "Заполнить поля" : "Спарсить данные" ) + "</button>";
								if (item.type != 'not-found') {
									results += "<button type=\"button\" class=\"cdnm-btn cdnm-btn-xs cdnm-btn-default cdnm-btn-right\" onclick=\"cdnm_set_link('" + item.iframe_src + "')\"> Вставить только ссылку</button>";
								}
								
								results += type + "<span class=\"cdnm-title\">" + item.title + (item.title_orig ? " / " + item.title_orig + " " : " ") + year + hd_video +"<br>"+ kinopoisk_id + imdb_id + world_art_id + tmdb_id + kinorium_id + mdl_id + shikimori_id +"<br>"+ translations + "</li>";
							});
							results += "</ul>";

							$("#cdnmFindResults").html(results);
							$("#cdnmClearFindButton").show();
						}

						setTimeout(function() {
							cdnm_hide_loading();
						}, 0);
					}

				});
				
				setTimeout(function() {
					cdnm_hide_loading();
				}, 30000);
				
		} else {
			cdnm_hide_loading();
			DLEalert("Вы не заполнили ни одного поля или не выбрали поля по которым необходимо производить поиск контента в админпанели модуля.", "Ошибка");
		}
	});

	$("#cdnmClearFindButton").click(function () {
		cdnm_show_loading();

		$("#cdnmFindResults").html("");
		$(this).hide();

		cdnm_hide_loading();
	});

	$("#cdnmClearOutputButton").click(function () {
		cdnm_show_loading();

		var field_output = "#<?=$field_output?>";

		$(field_output).val("");
		$(this).hide();
		$("#cdnmPreviewOutputButton").hide();

		cdnm_hide_loading();
	});

	$("#cdnmPreviewOutputButton").click(function () {
		var field_output = "#<?=$field_output?>";
		preview_cdnm($(field_output).val());
		return false;
	});

	if (!$.fn.modal) {

		/*!
		 * Bootstrap v3.2.0 (http://getbootstrap.com)
		 * Copyright 2011-2014 Twitter, Inc.
		 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
		 */

		/*!
		 * Generated using the Bootstrap Customizer (http://getbootstrap.com/customize/?id=23203afa2fca48b8eccd)
		 * Config saved to config.json and https://gist.github.com/23203afa2fca48b8eccd
		 */
		if (typeof jQuery === "undefined") { throw new Error("Bootstrap's JavaScript requires jQuery") }

		/* ========================================================================
		 * Bootstrap: modal.js v3.2.0
		 * http://getbootstrap.com/javascript/#modals
		 * ========================================================================
		 * Copyright 2011-2014 Twitter, Inc.
		 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
		 * ======================================================================== */


		+function ($) {
		  'use strict';

		  // MODAL CLASS DEFINITION
		  // ======================

		  var Modal = function (element, options) {
			this.options        = options
			this.$body          = $(document.body)
			this.$element       = $(element)
			this.$backdrop      =
			this.isShown        = null
			this.scrollbarWidth = 0

			if (this.options.remote) {
			  this.$element
				.find('.modal-content')
				.load(this.options.remote, $.proxy(function () {
				  this.$element.trigger('loaded.bs.modal')
				}, this))
			}
		  }

		  Modal.VERSION  = '3.2.0'

		  Modal.DEFAULTS = {
			backdrop: true,
			keyboard: true,
			show: true
		  }

		  Modal.prototype.toggle = function (_relatedTarget) {
			return this.isShown ? this.hide() : this.show(_relatedTarget)
		  }

		  Modal.prototype.show = function (_relatedTarget) {
			var that = this
			var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })

			this.$element.trigger(e)

			if (this.isShown || e.isDefaultPrevented()) return

			this.isShown = true

			this.checkScrollbar()
			this.$body.addClass('modal-open')

			this.setScrollbar()
			this.escape()

			this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))

			this.backdrop(function () {
			  var transition = $.support.transition && that.$element.hasClass('fade')

			  if (!that.$element.parent().length) {
				that.$element.appendTo(that.$body) // don't move modals dom position
			  }

			  that.$element
				.show()
				.scrollTop(0)

			  if (transition) {
				that.$element[0].offsetWidth // force reflow
			  }

			  that.$element
				.addClass('in')
				.attr('aria-hidden', false)

			  that.enforceFocus()

			  var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })

			  transition ?
				that.$element.find('.modal-dialog') // wait for modal to slide in
				  .one('bsTransitionEnd', function () {
					that.$element.trigger('focus').trigger(e)
				  })
				  .emulateTransitionEnd(300) :
				that.$element.trigger('focus').trigger(e)
			})
		  }

		  Modal.prototype.hide = function (e) {
			if (e) e.preventDefault()

			e = $.Event('hide.bs.modal')

			this.$element.trigger(e)

			if (!this.isShown || e.isDefaultPrevented()) return

			this.isShown = false

			this.$body.removeClass('modal-open')

			this.resetScrollbar()
			this.escape()

			$(document).off('focusin.bs.modal')

			this.$element
			  .removeClass('in')
			  .attr('aria-hidden', true)
			  .off('click.dismiss.bs.modal')

			$.support.transition && this.$element.hasClass('fade') ?
			  this.$element
				.one('bsTransitionEnd', $.proxy(this.hideModal, this))
				.emulateTransitionEnd(300) :
			  this.hideModal()
		  }

		  Modal.prototype.enforceFocus = function () {
			$(document)
			  .off('focusin.bs.modal') // guard against infinite focus loop
			  .on('focusin.bs.modal', $.proxy(function (e) {
				if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
				  this.$element.trigger('focus')
				}
			  }, this))
		  }

		  Modal.prototype.escape = function () {
			if (this.isShown && this.options.keyboard) {
			  this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
				e.which == 27 && this.hide()
			  }, this))
			} else if (!this.isShown) {
			  this.$element.off('keyup.dismiss.bs.modal')
			}
		  }

		  Modal.prototype.hideModal = function () {
			var that = this
			this.$element.hide()
			this.backdrop(function () {
			  that.$element.trigger('hidden.bs.modal')
			})
		  }

		  Modal.prototype.removeBackdrop = function () {
			this.$backdrop && this.$backdrop.remove()
			this.$backdrop = null
		  }

		  Modal.prototype.backdrop = function (callback) {
			var that = this
			var animate = this.$element.hasClass('fade') ? 'fade' : ''

			if (this.isShown && this.options.backdrop) {
			  var doAnimate = $.support.transition && animate

			  this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
				.appendTo(this.$body)

			  this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
				if (e.target !== e.currentTarget) return
				this.options.backdrop == 'static'
				  ? this.$element[0].focus.call(this.$element[0])
				  : this.hide.call(this)
			  }, this))

			  if (doAnimate) this.$backdrop[0].offsetWidth // force reflow

			  this.$backdrop.addClass('in')

			  if (!callback) return

			  doAnimate ?
				this.$backdrop
				  .one('bsTransitionEnd', callback)
				  .emulateTransitionEnd(150) :
				callback()

			} else if (!this.isShown && this.$backdrop) {
			  this.$backdrop.removeClass('in')

			  var callbackRemove = function () {
				that.removeBackdrop()
				callback && callback()
			  }
			  $.support.transition && this.$element.hasClass('fade') ?
				this.$backdrop
				  .one('bsTransitionEnd', callbackRemove)
				  .emulateTransitionEnd(150) :
				callbackRemove()

			} else if (callback) {
			  callback()
			}
		  }

		  Modal.prototype.checkScrollbar = function () {
			if (document.body.clientWidth >= window.innerWidth) return
			this.scrollbarWidth = this.scrollbarWidth || this.measureScrollbar()
		  }

		  Modal.prototype.setScrollbar = function () {
			var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
			if (this.scrollbarWidth) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
		  }

		  Modal.prototype.resetScrollbar = function () {
			this.$body.css('padding-right', '')
		  }

		  Modal.prototype.measureScrollbar = function () { // thx walsh
			var scrollDiv = document.createElement('div')
			scrollDiv.className = 'modal-scrollbar-measure'
			this.$body.append(scrollDiv)
			var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
			this.$body[0].removeChild(scrollDiv)
			return scrollbarWidth
		  }


		  // MODAL PLUGIN DEFINITION
		  // =======================

		  function Plugin(option, _relatedTarget) {
			return this.each(function () {
			  var $this   = $(this)
			  var data    = $this.data('bs.modal')
			  var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)

			  if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
			  if (typeof option == 'string') data[option](_relatedTarget)
			  else if (options.show) data.show(_relatedTarget)
			})
		  }

		  var old = $.fn.modal

		  $.fn.modal             = Plugin
		  $.fn.modal.Constructor = Modal


		  // MODAL NO CONFLICT
		  // =================

		  $.fn.modal.noConflict = function () {
			$.fn.modal = old
			return this
		  }


		  // MODAL DATA-API
		  // ==============

		  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
			var $this   = $(this)
			var href    = $this.attr('href')
			var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) // strip for ie7
			var option  = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())

			if ($this.is('a')) e.preventDefault()

			$target.one('show.bs.modal', function (showEvent) {
			  if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
			  $target.one('hidden.bs.modal', function () {
				$this.is(':visible') && $this.trigger('focus')
			  })
			})
			Plugin.call($target, option, this)
		  })

		}(jQuery);

	}
-->
</script>