/** article */
(function ($, LOGA) {

$(document).on('submit', '#formAdd, #formEdit', function() {
	if('' == $("input[name='article_category_id']").val() || 0 == $("input[name='article_category_id']").val()) {
		alert(LOGA.L('CHOOSE_CATEGORY'));
		return false;
	}
});

$(document).on('click', '#add_property', function() {
	var str,
		k;
	if(0 == $('#property_set li').length) {
		k = 0;
	}
	else {
		k = Number($('#property_set li:last').attr('k')) + 1;
	}
	str = '<li k="'+ k +'" class="property_item margin-top-sm margin-right">\r\n';
	str += '<span class="property_handle icon-bars"></span>\r\n';
	str += '<input type="text" name="a_property['+ k +'][name]" value="" size="10" class="control-input input-inline input-sm" required placeholder="'+ LOGA.L('PROPERTY_NAME') +'" /> ';
	str += '<input type="text" name="a_property['+ k +'][value]" value="" size="20" class="control-input input-inline input-sm" required placeholder="'+ LOGA.L('PROPERTY_VALUE') +'" /> ';
	str += '<span class="btn btn-sm delete">'+ LOGA.L('DELETE') +'</span>';
	str += '</li>';
	$('#property_set').append(str);
})
.on('click', '#property_set li span.delete', function() {
	$(this).parent().remove();
})
.on('click', '#add_image', function() {
	var str,
		k;
	if(0 == $('#image_set>div').length) {
		k = 0;
	}
	else {
		k = Number($('#image_set>div:last').attr('k')) + 1;
	}
	str = '<div k="'+ k +'" class="image_item width-sm-2-1 placeholder"><div class="row">\r\n';
	str += '	<div class="width-sm-2-1">\r\n';
	str += '		<span class="image_handle icon-bars"></span>\r\n';
	str += '		<input id="a_image_'+ k +'_url" type="hidden" name="a_image['+ k +'][url]" value="" />\r\n';
	str += '		<span id="a_image_'+ k +'_url_finder" to="#a_image_'+ k +'_url" preview="#a_image_'+ k +'_url_preview" class="btn btn-sm finder" thumb="yes">'+ LOGA.L('BROWSE_SERVER') +'</span>\r\n';
	str += '		<span class="btn btn-sm delete">'+ LOGA.L('DELETE') +'</span>\r\n';
	str += '		<input type="text" name="a_image['+ k +'][title]" value="" size="10" class="control-input input-sm margin-top-sm" placeholder="'+ LOGA.L('TITLE') +'" />\r\n';
	str += '		<textarea name="a_image['+ k +'][description]" class="control-input margin-top-sm" style="height:90px;" placeholder="'+ LOGA.L('DESCRIPTION') +'"></textarea>\r\n';
	str += '	</div>\r\n';
	str += '	<div class="width-sm-2-1">\r\n';
	str += '		<img id="a_image_'+ k +'_url_preview" style="max-height:150px" class="thumb" src="">\r\n';
	str += '	</div>\r\n';
	str += '</div></div>\r\n';
	$('#image_set').append(str);
})
.on('click', '#image_set div span.delete', function() {
	$(this).parent().parent().parent().remove();
})
.on('click', '#add_annex', function() {
	var str,
		k;
	if(0 == $('#annex_set>div').length) {
		k = 0;
	}
	else {
		k = Number($('#annex_set>div:last').attr('k')) + 1;
	}
	str = '<div k="'+ k +'" class="annex_item width-sm-3-1 placeholder">\r\n';
	str += '	<span class="annex_handle icon-bars"></span>\r\n';
	str += '	<span id="a_annex_'+ k +'_url_finder" to="#a_annex_'+ k +'_url" to_title="#a_annex_'+ k +'_title" class="btn btn-sm finder" finder_type="file">'+ LOGA.L('BROWSE_SERVER') +'</span>\r\n';
	str += '	<span class="btn btn-sm delete">'+ LOGA.L('DELETE') +'</span>\r\n';
	str += '	<input id="a_annex_'+ k +'_url" type="text" name="a_annex['+ k +'][url]" value="" size="20" class="control-input input-sm margin-top-sm" placeholder="'+ LOGA.L('URL') +'" />\r\n';
	str += '	<input id="a_annex_'+ k +'_title" type="text" name="a_annex['+ k +'][title]" value="" size="20" class="control-input input-sm margin-top-sm" placeholder="'+ LOGA.L('TITLE') +'" />\r\n';
	str += '	<textarea name="a_annex['+ k +'][description]" class="control-input margin-top-sm" style="height:60px;" placeholder="'+ LOGA.L('DESCRIPTION') +'"></textarea>\r\n';
	str += '</div>\r\n';
	$('#annex_set').append(str);
})
.on('click', '#annex_set div span.delete', function() {
	$(this).parent().remove();
});

$('.property_set_root').tree({
	maxDepth: 1,
	rootClass: 'property_set_root',
	listClass: 'property_set',
	itemClass: 'property_item',
	handleClass: 'property_handle',
	listNodeName: 'ul',
	itemNodeName: 'li',
	normalBtnHTML: ''
});
$('.image_set_root').tree({
	maxDepth: 1,
	rootClass: 'image_set_root',
	listClass: 'image_set',
	itemClass: 'image_item',
	handleClass: 'image_handle',
	listNodeName: 'div',
	itemNodeName: 'div',
	normalBtnHTML: ''
});
$('.annex_set_root').tree({
	maxDepth: 1,
	rootClass: 'annex_set_root',
	listClass: 'annex_set',
	itemClass: 'annex_item',
	handleClass: 'annex_handle',
	listNodeName: 'div',
	itemNodeName: 'div',
	normalBtnHTML: ''
});

})(jQuery, LOGA);
