	if (window.addEditMode) {
		clearText = function(text) {
			return text.replace(/[:;\n]/g,'');
		}
		addPricesCommand  = function() {
			$driver = $('.prices-edit').attr('rel');
			window.addCommand('prices-edit', $driver, function(){
				$table = $('.prices-edit');
				var result = "";
				var count = $table.find('th').length + 1;
				for (var i=1; i < count; i++) {
					result += (i == 1)? "false:" : ";true:";
					result += clearText($table.find('tr:nth-child(2) > td:nth-child(' +i +')').text())+ ":";
					result += (new Number($table.find('tr:nth-child(3) > td:nth-child(' +i +')').text()))+ ":";
					result += $table.find('tr:nth-child(4) > td:nth-child(' +i +') select').get(0).value + ":";
					result += ($table.find('tr:nth-child(5) > td:nth-child(' +i +') input').get(0).checked)
						? "true" : "false";
				}
				return result;
			});
		};
		window.addEditMode({
					id : 'prices',
					name : 'Задать цены',
					activate : function() {
						$('.price-commentary')
							.disableSelection()
							.parents('td').click(function(){
								var $s = $(this).find('.price-commentary');
								var value = $s.html();
								$s.enableSelection();
								$s.parents('td').unbind('click');
								$s.empty();
								jin = $('<input type="text" />')
									.attr('value', value)
									.appendTo($s)
									.focus();
								
								addPricesCommand();
							});
						$('.price-value')
							.disableSelection()
							.parents('td').click(function(){
								var $s = $(this).find('.price-value');
								var value = $s.html();
								$s.enableSelection();
								$s.parents('td').unbind('click');
								$s.empty();
								jin = $('<input type="text" />')
									.attr('value', new Number(value) + 0)
									.attr('default', new Number(value) + 0)
									.appendTo($s)
									.change(function(){
										if(isNaN(this.value) || (this.value <= 0)) {
											this.value = $(this).attr('default');
										}
										else {
											$(this).attr('default') = this.value;
										}
									})
									.focus();
								
								addPricesCommand();
							});
						$('.price-currency')
							.attr('disabled','')
							.change(function() {
								addPricesCommand();
							});
						$('.price-special')
							.attr('disabled','')
							.change(function() {
								addPricesCommand();
							});
						
						var removeFunction = function(){
							var $index = $(this).parent().index();
							$('.catalog-prices tr').each(function(index,element) {
								$(element).children().eq($index).remove();
							});
							addPricesCommand();
						};	
								
						var pricesCount = $('.catalog-prices tr:first-child th').length;
						$td = $('<td style="padding:0 0.5em;vertical-align:top;border:none;" rowspan="4"></td>')
								.appendTo($('.catalog-prices tr:first-child'));
						var context = this;
						
						$input = $('<input type="button" id="price-add" value="Добавить"/>')
							.appendTo($td)
							.click(function(){
								$th = $('.catalog-prices tr:first-child th').last();
								$thclone = $th.clone();
								if ($thclone.html().indexOf('Розничная цена') != -1) {
									$thclone.html($thclone.html().replace('Розничная цена','Oптовая цена'));
								}
								$thclone.insertAfter($th);
								$('.catalog-prices tr').each(function() {
									if ($(this).find('th').length == 0) {
										$td = $(this).find('td').last();
										$clone = $td.clone().insertAfter($td);
										context.deactivate();
										context.activate();
									}
								});
								addPricesCommand();
							});
						
						
						var $tr = $('<tr/>').appendTo($('.catalog-prices'));
						$tr.append($('<td style="border:none;"/>'));
						for(var i=1; i<pricesCount; i++){
							$td = $('<td style="border:none;padding:0.5em 0.2em"></td>')
								.appendTo($tr);
							$input = $('<input type="button" id="price-remove" value="Удалить"/>')
								.appendTo($td)
								.click(removeFunction);
						}
					},
					deactivate : function() {
						$('.price-commentary, .price-value')
							.parents('td').unbind('click');
						$('.price-commentary, .price-value')
							.enableSelection()
							.find('input').each(function(){
								var jp = $(this).parent();
								var value = $(this).attr('value');
								$(this).remove();
								jp.html(value);
							});
						$('.price-special')
							.attr('disabled','disabled')
							.unbind('change');
						$('.price-currency')
							.attr('disabled','disabled')
							.unbind('change');
						$('.catalog-prices tr:first-child td:last-child').remove();
						$('.catalog-prices tr:last-child').remove();
						
					}
		}); // window.addEditMode({ id : 'prices' ..

		addAttachementCommand  = function() {
			$driver = $('.catalog-attachments').attr('rel');
			window.addCommand('prices-edit', $driver, function(){
				var result = '';
				$imgs = $('.catalog-attachments img').each(function(index,element){
					if (result != '') result += ":";
					src = new String($(element).attr('src'));
					result += src.substring(src.lastIndexOf('/') + 1);
				});
				return result;
			});
		};
		
		window.addEditMode({
					id : 'images',
					name : 'Изменить изображения',
					activate : function() {
						$ul = $('.catalog-attachments');
						$ul.disableSelection()
						.sortable({
							start: function(){
							},
							change: function() {
								addAttachementCommand();
							}
						});
						$ul.find('a').unbind('click');
						$ul.find('a').click(function(event){
							event.preventDefault();
						});
						
						$nd = $('<div style="margin:1em 0 1em 0; padding:0;"></div>')
								.insertAfter($ul);
						
						$uc = $('<div class="upload-container"/>').appendTo($nd);
						
						var reg=/\/(\d+)\/[^/]*/; 
						var $item = reg.exec(new String(window.location))[1]; 
							
						$form = $('<form id="upload-form" method="post" action="/modules/catalog/attachment.php" enctype="multipart/form-data">' +
									'<input type="hidden" name="item" value="'+ $item +'"/>' +
									'<input type="file" name="file" id="file" class="file" accept="image/gif,image/jpeg,image/png"/>' +
									'<input type="submit" class="submit" value="Загрузить"/></form>')
							.appendTo($uc);
						
						window.formCallback = function(return_message){
							eval("data="+return_message);
							if (data.message == "OK"){
								$('.catalog-attachments').append(
									'<li><a><img src="/catalog/preview-medium/' + data.filename + '" alt=""/></a></li>');
								//$('#upload-form-iframe').remove();
								addAttachementCommand();
							}
							else {
								window.alert(data.message);
							}
						}
						
						$form.jqupload({"callback" : "formCallback"});
   						$form.jqupload_form();
   						
						//$ni.click(function() {
							//window.dialog = window.open('/modules/catalog/attachment.html', 'dialog', 'width=500,height=400');
							
						//});
						
						$('<div id="attachment-trash"><p>Корзина</p></li>')
							.appendTo($nd)
							.droppable({
								accept: '.catalog-attachments li',
								drop : function(ev, ui) {
									ui.draggable.remove();
									if (document.all) {
										ui.draggable.addClass('to-kill');
										window.setTimeout(function(){
											$('.to-kill').remove();
										} ,1);
									}
									addAttachementCommand();
								},
								over : function(event, ui) {
									$('.ui-sortable-helper').css("background-color","red");
								},
								out : function(event, ui) {
									$('.ui-sortable-helper').css("background-color","transparent");
								},

								hoverClass: 'attachment-drop'
							});
					},
					deactivate : function() {
						$('.upload-container').parent().remove();
						$('#upload-form_iframe').remove();
						$ul = $('.catalog-attachments');
						$ul.enableSelection()
							.sortable('destroy');
					}
		}); // window.addEditMode({ id : 'prices' ..

	} //if
	
/*	
	window.addEditMode({
					id : 'items',
					name : 'Редактирование позиций',
					isTable : $('table.category-items').length > 0,
					activate : function() {
						
						if (this.isTable) {
							var $table = $('table.category-items');
							
							var $ul;
							if ($table.length > 0) {
								$ul = $('<ul class="content-items"></ul>').insertBefore($table);
								$table.find('a').each(function(){
									var $li = $('<li></li>')
										.appendTo($ul)
									$li.append($(this).parent().clone());
								});
								$table.remove();
							}
							else {
								$ul = $('ul.content-items');
							}	

							$ul.disableSelection()
							.sortable({
								start: function(){
								}
							});
							$ul.find('li').mousedown(function(){
								$(this).addClass('ui-sortable-helper');
							});
							$ul.mouseup(function(){
								$('.ui-sortable-helper').removeClass('ui-sortable-helper');
							});
							
						}
						else {
							var $ul = $('ul.category-items');
							$ul.disableSelection()
							.sortable({
								start: function(){
								}
							});
							//$ul.children('li').css('border','solid 1px silver');
						}
						$('<li id="content-trash"><p>Корзина</p></li>').
							appendTo($("ul.catalog-tree"));

						$("ul.catalog-tree")
							.find("li")
								.not('.catalog-leaf')
							.droppable({
								accept: '.content-items li, .category-items li',
								drop: function(ev, ui) {
									alert(1);
								},
								hoverClass: 'category-drop'
							});
						
						$("ul.catalog-tree")
							.droppable({
								accept: '.content-items li, .category-items li',
								over : function(e) {
									$('.ui-sortable-helper').fadeTo(250, 0.4);
								},
								out : function(e) {
									$('.ui-sortable-helper').fadeTo(250, 1.0);
								}
							});
							
						
						var $ul = this.isTable? $('ul.content-items') : $('ul.category-items');
						$su = $('<ul style="margin:-2em 0 2em 0; padding:0; list-style:none;"></ul>')
								.insertBefore($ul);
						$ni = $('<li id="new-item"><p><a href="/item/new/"><span>Новая позиция</span></a></p></li>')
							.appendTo($su)
						$su.sortable({
								revert: true,
								dropOnEmpty :true,
								connectWith : $ul,
								start : function() {
								},
								stop: function() {
									//<li xmlns:php="http://php.net/xsl"><p><a href="/item/2184/"><span>Кассета К-01   </span></a></p><p class="preview"><a href="/item/2184/"><span><img alt="" src="/catalog/preview-medium/2184-0.jpg"/></span></a></p><p><strong>Цена розн. –</strong> 204 руб.</p><p><strong>Цена опт. –</strong> от 191 руб.</p></li>
								}
							});
					},
					deactivate : function() {
						if (this.isTable) {
							 $('ul.content-items').sortable('destroy');
						} else {
							 $('ul.category-items').sortable('destroy');
						}
						$('#content-trash').remove();
						$('#new-item').parent().remove();
						$("ul.catalog-tree")
							.find("li")
								.not('.catalog-leaf').droppable('destroy');
						$("ul.catalog-tree").droppable('destroy');
					}
				});
	*/
