{capture name=tabs}
    <li><a href="index.php?module=BlogAdmin">Блог</a></li>
    <li class="active"><a href="index.php?module=ArticlesAdmin">Статьи</a></li>
	<li><a href="index.php?module=ArticleCategoriesAdmin">Категории статей</a></li>
	<li><a href="{url module=MarkasAdmin id=null page=null}">Марки и модели</a></li> 
{/capture}

{if $article->id}
{$meta_title = $article->name scope=parent}
{else}
{$meta_title = 'Новая статья' scope=parent}
{/if}

{* Подключаем Tiny MCE *}
{include file='tinymce_init.tpl'}

{* On document load *}
{literal}
<script src="design/js/jquery/datepicker/jquery.ui.datepicker-ru.js"></script>
<script src="design/js/autocomplete/jquery.autocomplete-min.js"></script>
<style>
.autocomplete-w1 {position:absolute; top:0px; left:0px; margin:6px 0 0 6px; /* IE6 fix: */ _background:none; _margin:1px 0 0 0; }
.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; overflow-x:auto; min-width: 300px; overflow-y: auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px;  _margin:0; _overflow-x:hidden; }
.autocomplete .selected { background:#F0F0F0; }
.autocomplete div { padding:2px 5px; white-space:nowrap; }
.autocomplete strong { font-weight:normal; color:#3399FF; }
</style>

<script>
$(function() {
	// Удаление изображений
	$(".images a.delete").click( function() {
		$("input[name='delete_image']").val('1');
		$(this).closest("ul").fadeOut(200, function() { $(this).remove(); });
		return false;
	});


	// Добавление категории
	$('#product_categories .add').click(function() {
		$("#product_categories ul li:last").clone(false).appendTo('#product_categories ul').fadeIn('slow').find("select[name*=categories]:last").focus();
		$("#product_categories ul li:last span.add").hide();
		$("#product_categories ul li:last span.delete").show();
		return false;
	});

	// Удаление категории
	$("#product_categories .delete").live('click', function() {
		$(this).closest("li").fadeOut(200, function() { $(this).remove(); });
		return false;
	});

    // Удаление связанноq статьи
    $(".related_articles a.delete").live('click', function() {
         $(this).closest("div.row").fadeOut(200, function() { $(this).remove(); });
         return false;
    });
    
    $(".related_markas a.delete").live('click', function() {
         $(this).closest("div.row").fadeOut(200, function() { $(this).remove(); });
         return false;
    });
    $(".related_models a.delete").live('click', function() {
         $(this).closest("div.row").fadeOut(200, function() { $(this).remove(); });
         return false;
    });

    // Добавление связанной статьи
    var new_related_article = $('#new_related_article').clone(true);
    $('#new_related_article').remove().removeAttr('id');

    $("input#related_articles").autocomplete({
        serviceUrl:'ajax/search_articles.php',
        minChars:0,
        noCache: false,
        onSelect:
            function(value, data){
                new_item = new_related_article.clone().appendTo('.related_articles');
                new_item.removeAttr('id');
                new_item.find('a.related_article_name').html(data.name);
                new_item.find('a.related_article_name').attr('href', 'index.php?module=ArticleAdmin&id='+data.id);
                new_item.find('input[name*="related_articles"]').val(data.id);
                $("#related_articles").val('');
                new_item.show();
            },
        fnFormatResult:
            function(value, data, currentValue){
                var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
                var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
                  return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
            }

    });
    
    var new_related_marka = $('#new_related_marka').clone(true);
    $('#new_related_marka').remove().removeAttr('id');

    $("input#related_markas").autocomplete({
        serviceUrl:'ajax/search_markas.php',
        minChars:0,
        noCache: false,
        onSelect:
            function(value, data){
                new_item = new_related_marka.clone().appendTo('.related_markas');
                new_item.removeAttr('id');
                new_item.find('a.related_marka_name').html(data.name);
                new_item.find('a.related_marka_name').attr('href', 'index.php?module=MarkaAdmin&id='+data.id);
                new_item.find('input[name*="related_markas"]').val(data.id);
                $("#related_markas").val('');
                new_item.show();
            },
        fnFormatResult:
            function(value, data, currentValue){
                var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
                var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
                  return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
            }

    });
    
    var new_related_model = $('#new_related_model').clone(true);
    $('#new_related_model').remove().removeAttr('id');

    $("input#related_models").autocomplete({
        serviceUrl:'ajax/search_models.php',
        minChars:0,
        noCache: false,
        onSelect:
            function(value, data){
                new_item = new_related_model.clone().appendTo('.related_models');
                new_item.removeAttr('id');
                new_item.find('a.related_model_name').html(data.name);
                new_item.find('a.related_model_name').attr('href', 'index.php?module=ModelAdmin&id='+data.id);
                new_item.find('input[name*="related_models"]').val(data.id);
                $("#related_models").val('');
                new_item.show();
            },
        fnFormatResult:
            function(value, data, currentValue){
                var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
                var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
                  return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
            }

    });
    
    $(".related_pages a.delete").live('click', function() {
         $(this).closest("div.row").fadeOut(200, function() { $(this).remove(); });
         return false;
    });
    
        // Добавление связанной статьи
    var new_related_page = $('#new_related_page').clone(true);
    $('#new_related_page').remove().removeAttr('id');

    $("input#related_pages").autocomplete({
        serviceUrl:'ajax/search_services.php',
        minChars:0,
        noCache: false,
        onSelect:
            function(value, data){
                new_item = new_related_page.clone().appendTo('.related_pages');
                new_item.removeAttr('id');
                new_item.find('a.related_page_name').html(data.name);
                new_item.find('a.related_page_name').attr('href', 'index.php?module=pageAdmin&id='+data.id);
                new_item.find('input[name*="related_pages"]').val(data.id);
                $("#related_pages").val('');
                new_item.show();
            },
        fnFormatResult:
            function(value, data, currentValue){
                var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
                var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
                  return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
            }

    });  
    

    // Удаление связанного товара
    $(".related_products a.delete").live('click', function() {
         $(this).closest("div.row").fadeOut(200, function() { $(this).remove(); });
         return false;
    });

    // Добавление связанного товара
    var new_related_product = $('#new_related_product').clone(true);
    $('#new_related_product').remove().removeAttr('id');

    $("input#related_products").autocomplete({
        serviceUrl:'ajax/search_products.php',
        minChars:0,
        noCache: false,
        onSelect:
            function(value, data){
                new_item = new_related_product.clone().appendTo('.related_products');
                new_item.removeAttr('id');
                new_item.find('a.related_product_name').html(data.name);
                new_item.find('a.related_product_name').attr('href', 'index.php?module=ProductAdmin&id='+data.id);
                new_item.find('input[name*="related_products"]').val(data.id);
                $("#related_products").val('');
                new_item.show();
            },
        fnFormatResult:
            function(value, data, currentValue){
                var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
                var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
                  return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
            }

    });



	$('input[name="date"]').datepicker({
		regional:'ru'
	});

	// Автозаполнение мета-тегов
	meta_title_touched = true;
	meta_keywords_touched = true;
	meta_description_touched = true;
	url_touched = false;
	update = {/literal}{if $article->url}true;{else}false;{/if}{literal}

	if($('input[name="meta_title"]').val() == generate_meta_title() || $('input[name="meta_title"]').val() == '')
		meta_title_touched = false;
	if($('input[name="meta_keywords"]').val() == generate_meta_keywords() || $('input[name="meta_keywords"]').val() == '')
		meta_keywords_touched = false;
	if($('textarea[name="meta_description"]').val() == generate_meta_description() || $('textarea[name="meta_description"]').val() == '')
		meta_description_touched = false;
	if($('input[name="url"]').val() == generate_url() || $('input[name="url"]').val() == '')
		url_touched = false;

	$('input[name="meta_title"]').change(function() { meta_title_touched = true; });
	$('input[name="meta_keywords"]').change(function() { meta_keywords_touched = true; });
	$('textarea[name="meta_description"]').change(function() { meta_description_touched = true; });
	$('input[name="url"]').change(function() { url_touched = true; });

	$('input[name="name"]').keyup(function() { set_meta(); });
	$('select[name="brand_id"]').change(function() { set_meta(); });
	$('select[name="categories[]"]').change(function() { set_meta(); });

});

function set_meta()
{
	if(!meta_title_touched)
	//	$('input[name="meta_title"]').val(generate_meta_title());
	if(!meta_keywords_touched)
	//	$('input[name="meta_keywords"]').val(generate_meta_keywords());
	if(!meta_description_touched)
	{
		descr = $('textarea[name="meta_description"]');
	//	descr.val(generate_meta_description());
		descr.scrollTop(descr.outerHeight());
	}
	if(!url_touched && !update)
		$('input[name="url"]').val(generate_url());
}

function generate_meta_title()
{
	name = $('input[name="name"]').val();
	return name;
}

function generate_meta_keywords()
{
	name = $('input[name="name"]').val();
	return name;
}

function generate_meta_description()
{
	if(typeof(tinyMCE.get("annotation")) =='object')
	{
		description = tinyMCE.get("annotation").getContent().replace(/(<([^>]+)>)/ig," ").replace(/(\&nbsp;)/ig," ").replace(/^\s+|\s+$/g, '').substr(0, 512);
		return description;
	}
	else
		return $('textarea[name=annotation]').val().replace(/(<([^>]+)>)/ig," ").replace(/(\&nbsp;)/ig," ").replace(/^\s+|\s+$/g, '').substr(0, 512);
}

function generate_url()
{
	url = $('input[name="name"]').val();
	url = url.replace(/[\s]+/gi, '-');
	url = translit(url);
	url = url.replace(/[^0-9a-z_\-]+/gi, '').toLowerCase();
	return url;
}

function translit(str)
{
	var ru=("А-а-Б-б-В-в-Ґ-ґ-Г-г-Д-д-Е-е-Ё-ё-Є-є-Ж-ж-З-з-И-и-І-і-Ї-ї-Й-й-К-к-Л-л-М-м-Н-н-О-о-П-п-Р-р-С-с-Т-т-У-у-Ф-ф-Х-х-Ц-ц-Ч-ч-Ш-ш-Щ-щ-Ъ-ъ-Ы-ы-Ь-ь-Э-э-Ю-ю-Я-я").split("-")
	var en=("A-a-B-b-V-v-G-g-G-g-D-d-E-e-E-e-E-e-ZH-zh-Z-z-I-i-I-i-I-i-J-j-K-k-L-l-M-m-N-n-O-o-P-p-R-r-S-s-T-t-U-u-F-f-H-h-TS-ts-CH-ch-SH-sh-SCH-sch-'-'-Y-y-'-'-E-e-YU-yu-YA-ya").split("-")
 	var res = '';
	for(var i=0, l=str.length; i<l; i++)
	{
		var s = str.charAt(i), n = ru.indexOf(s);
		if(n >= 0) { res += en[n]; }
		else { res += s; }
    }
    return res;
}

</script>
{/literal}

{if $message_success}
<!-- Системное сообщение -->
<div class="message message_success">
	<span>{if $message_success == 'added'}Запись добавлена{elseif $message_success == 'updated'}Запись обновлена{/if}</span>
	<a class="link" target="_blank" href="../article/{$article->url}">Открыть запись на сайте</a>
	{if $smarty.get.return}
	<a class="button" href="{$smarty.get.return}">Вернуться</a>
	{/if}
</div>
<!-- Системное сообщение (The End)-->
{/if}

{if $message_error}
<!-- Системное сообщение -->
<div class="message message_error">
	<span>{if $message_error == 'url_exists'}Запись с таким адресом уже существует{/if}</span>
	<a class="button" href="">Вернуться</a>
</div>
<!-- Системное сообщение (The End)-->
{/if}


<!-- Основная форма -->
<form method=post id=product enctype="multipart/form-data">
<input type=hidden name="session_id" value="{$smarty.session.id}">
	<div id="name">
		<input class="name" name=name type="text" value="{$article->name|escape}"/>
		<input name=id type="hidden" value="{$article->id|escape}"/>
		<div class="checkbox">
			<input name=visible value='1' type="checkbox" id="active_checkbox" {if $article->visible}checked{/if}/> <label for="active_checkbox">Активна</label>
		</div>
		
	</div>
	
	{if $article->id && $article->firstCategory && $article->firstCategory->id == 3}
		<div style="margin-top: -15px;margin-bottom: 10px;">
			с формой заказа <a target="_blank" href="/{$config->worksUrl}/{$article->url|escape}/?showform=1">https://atomicgarage.ru/{$config->worksUrl}/{$article->url|escape}/?showform=1</a>
		</div>
	{/if}

	<div id="product_categories" {if !$categories}style='display:none;'{/if}>
		<label>Категория</label>
		<div>
			<ul>
				{foreach name=categories from=$article_categories item=product_category}
				<li>
					<select name="categories[]">
						{function name=category_select level=0}
						{foreach from=$categories item=category}
								<option {if !$category->visible}style="color:#999"{/if} value='{$category->id}' {if $category->id == $selected_id}selected{/if} category_name='{$category->name|escape}'>{section name=sp loop=$level}&nbsp;&nbsp;&nbsp;&nbsp;{/section}{$category->name|escape}</option>
								{category_select categories=$category->subcategories selected_id=$selected_id  level=$level+1}
						{/foreach}
						{/function}
						{category_select categories=$categories selected_id=$product_category->id}
					</select>
					<span {if not $smarty.foreach.categories.first}style='display:none;'{/if} class="add"><i class="dash_link">Дополнительная категория</i></span>
					<span {if $smarty.foreach.categories.first}style='display:none;'{/if} class="delete"><i class="dash_link">Удалить</i></span>
				</li>
				{/foreach}
			</ul>
		</div>
	</div>

	<!-- Левая колонка свойств товара -->
	<div id="column_left">

		<!-- Параметры страницы -->
		<div class="block">
			<ul>
                <li><label class=property>Дата</label><input type=text name=date value='{$article->date|date}'></li>
                <!--<li><label class=property>Категория</label>
                    <select name="category_id">
                        <option value='0'>Корневая категория</option>
                        {function name=category_select level=0}
                        {foreach from=$cats item=cat}
                            {if $category->id != $cat->id}
                                <option value='{$cat->id}' {if $article->category_id == $cat->id}selected{/if}>{section name=sp loop=$level}&nbsp;&nbsp;&nbsp;&nbsp;{/section}{$cat->name}</option>
                                {category_select cats=$cat->subcategories level=$level+1}
                            {/if}
                        {/foreach}
                        {/function}
                        {category_select cats=$categories}
                    </select>
                </li>-->
			</ul>
		</div>
		<div class="block layer">
		<!-- Параметры страницы (The End)-->
			<h2>Параметры страницы</h2>
		<!-- Параметры страницы -->
			<ul>
				<li>
					<label class=property>Адрес</label><div class="page_url"> /article/</div><input name="url" class="page_url" type="text" value="{$article->url|escape}" />
					
				</li>
				<li><label class=property>Заголовок</label><input name="meta_title" type="text" value="{$article->meta_title|escape}" /></li>
				<li><label class=property>Ключевые слова</label><input name="meta_keywords"  type="text" value="{$article->meta_keywords|escape}" /></li>
				<li><label class=property>Описание</label><textarea name="meta_description" />{$article->meta_description|escape}</textarea></li>
			</ul>
			
			<div class="block layer">
	            <h2>Связанные марки</h2>
	            <div id=list class="related_markas">
	                {foreach from=$related_markas item=related_article}
	                <div class="row">
	                    <div class="name cell">
	                    <input type=hidden name=related_markas[] value='{$related_article->id}'>
	                    <a href="{url id=$related_article->id}">{$related_article->name}</a>
	                    </div>
	                    <div class="icons cell">
	                    <a href='#' class="delete"></a>
	                    </div>
	                    <div class="clear"></div>
	                </div>
	                {/foreach}
	                <div id="new_related_marka" class="row" style='display:none;'>
	                    <div class="name cell">
	                    <input type=hidden name=related_markas[] value=''>
	                    <a class="related_marka_name" href=""></a>
	                    </div>
	                    <div class="icons cell">
	                    <a href='#' class="delete"></a>
	                    </div>
	                    <div class="clear"></div>
	                </div>
	            </div>
	            <input type=text name="" id='related_markas' class="input_autocomplete" placeholder='Выберите марку чтобы добавить ее'>
	        </div>
	        <div class="block layer">
	            <h2>Связанные модели</h2>
	            <div id=list class="related_models">
	                {foreach from=$related_models item=related_article}
	                <div class="row">
	                    <div class="name cell">
	                    <input type=hidden name=related_models[] value='{$related_article->id}'>
	                    <a href="{url id=$related_article->id}">{$related_article->name}</a>
	                    </div>
	                    <div class="icons cell">
	                    <a href='#' class="delete"></a>
	                    </div>
	                    <div class="clear"></div>
	                </div>
	                {/foreach}
	                <div id="new_related_model" class="row" style='display:none;'>
	                    <div class="name cell">
	                    <input type=hidden name=related_models[] value=''>
	                    <a class="related_model_name" href=""></a>
	                    </div>
	                    <div class="icons cell">
	                    <a href='#' class="delete"></a>
	                    </div>
	                    <div class="clear"></div>
	                </div>
	            </div>
	            <input type=text name="" id='related_models' class="input_autocomplete" placeholder='Выберите модель чтобы добавить ее'>
	        </div>
	        <div class="block layer">
	            <h2>Связанные услуги</h2>
	            <div id=list class="related_pages">
	                {foreach from=$related_pages item=related_page}
	                <div class="row">
	                    <div class="name cell">
	                    <input type=hidden name=related_pages[] value='{$related_page->id}'>
	                    <a href="{url id=$related_page->id}">{$related_page->name}</a>
	                    </div>
	                    <div class="icons cell">
	                    <a href='#' class="delete"></a>
	                    </div>
	                    <div class="clear"></div>
	                </div>
	                {/foreach}
	                <div id="new_related_page" class="row" style='display:none;'>
	                    <div class="name cell">
	                    <input type=hidden name=related_pages[] value=''>
	                    <a class="related_page_name" href=""></a>
	                    </div>
	                    <div class="icons cell">
	                    <a href='#' class="delete"></a>
	                    </div>
	                    <div class="clear"></div>
	                </div>
	            </div>
	            <input type=text name=related id='related_pages' class="input_autocomplete" placeholder='Выберите статью чтобы добавить ее'>
	        </div>
			
			
		</div>
		<!-- Параметры страницы (The End)-->



	</div>
	<!-- Левая колонка свойств товара (The End)-->

	<!-- Правая колонка свойств товара -->
	<div id="column_right">

		<!-- Изображение категории -->
		<div class="block layer images">
			<h2>Изображение</h2>
			<input class='upload_image' name=image type=file>
			<input type=hidden name="delete_image" value="">
			{if $article->image}
			<ul>
				<li>
					<a href='#' class="delete"><img src='design/images/cross-circle-frame.png'></a>
					<img src="{$article->image|resizepost:100:100}" alt="" />
				</li>
			</ul>
			{/if}
		</div>



        <div class="block">
            <h2>Связанные товары</h2>
            <div id=list class="related_products">
                {foreach from=$related_products item=related_product}
                <div class="row">
                    <div class="name cell">
                    <input type=hidden name=related_products[] value='{$related_product->id}'>
                    <a href="/simpla/index.php?module=ProductAdmin&id={$related_product->id}">{$related_product->name}</a>
                    </div>
                    <div class="icons cell">
                    <a href='#' class="delete"></a>
                    </div>
                    <div class="clear"></div>
                </div>
                {/foreach}
                <div id="new_related_product" class="row" style='display:none;'>
                    <div class="name cell">
                    <input type=hidden name=related_products[] value=''>
                    <a class="related_product_name" href=""></a>
                    </div>
                    <div class="icons cell">
                    <a href='#' class="delete"></a>
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
            <input type=text name=related id='related_products' class="input_autocomplete" placeholder='Выберите товар чтобы добавить его'>
        </div>

        <div class="block layer">
            <h2>Связанные статьи</h2>
            <div id=list class="related_articles">
                {foreach from=$related_articles item=related_article}
                <div class="row">
                    <div class="name cell">
                    <input type=hidden name=related_articles[] value='{$related_article->id}'>
                    <a href="{url id=$related_article->id}">{$related_article->name}</a>
                    </div>
                    <div class="icons cell">
                    <a href='#' class="delete"></a>
                    </div>
                    <div class="clear"></div>
                </div>
                {/foreach}
                <div id="new_related_article" class="row" style='display:none;'>
                    <div class="name cell">
                    <input type=hidden name=related_articles[] value=''>
                    <a class="related_article_name" href=""></a>
                    </div>
                    <div class="icons cell">
                    <a href='#' class="delete"></a>
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
            <input type=text name=related id='related_articles' class="input_autocomplete" placeholder='Выберите статью чтобы добавить ее'>
        </div>



        <input class="button_green button_save" type="submit" name="" value="Сохранить" />

	</div>
	<!-- Правая колонка свойств товара (The End)-->

	<!-- Описагние товара -->
	<div class="block layer">
		<h2>Краткое описание</h2>
		<textarea name="annotation" class='editor_small'>{$article->annotation|escape}</textarea>
	</div>

	<div class="block">
		<h2>Полное  описание</h2>
		<textarea name="body"  class='editor_large'>{$article->text|escape}</textarea>
	</div>
	<!-- Описание товара (The End)-->
	<input class="button_green button_save" type="submit" name="" value="Сохранить" />
	
	{if $article->id}{include file="article_photo.tpl"}{/if}

</form>
<!-- Основная форма (The End) -->
