Beautician = {
	
	Refresh: function() {
		
		$('.chart_link').each(function() {
			
			var link = $(this);
			
			if (link.attr('href') == '') link.attr('href', '/distributor/beautician/');
			
			$.ajax({
				type: 'POST',
				async: true,
				url: '/beautician.php',
				data: 'action=get_chart_size',
				success: function(xml) {
					chart_size = $(xml).text();
					link.html(chart_size > 0 ? ('Личная косметичка (' + $(xml).text() + ')') : '');
				}
			});
		});
		
		$('.add_to_chart').each(function() {
					
			var link = $(this);
		
			$.ajax({
				type: 'POST',
				async: true,
				url: '/beautician.php',
				data: 'action=is_in_chart&id=' + link.attr('href').substr(2),
				success: function(xml) {
					if ($(xml).text() == 'true') {
						
						switch (link.attr('rel')) {
					
							case 'catalog':
								link.html('Удалить из косметички');
								break;
								
							case 'product':
							default:
								break;
							
						}
												
					} else {
												
						switch (link.attr('rel')) {
					
							case 'catalog':
								link.html('<img alt="Добавить в косметичку" src="/images/in_basket.gif" />');
								break;
								
							case 'product':
							default:
								break;
							
						}
												
					}
				}
			});
		});
	},
	
	AddToChartClick: function(_link) {
		action = _link.html().indexOf('Удалить') != -1 ? 'remove' : 'add';
		_link.html('');
		switch (action) {
			case 'remove':
				Beautician.RemoveFromChart(_link.attr('href').substr(2));
				break;
			case 'add':
				Beautician.AddToChart(_link.attr('href').substr(2));
				break;
		}
		return false;
	},
		
	AddToChart: function(_catalog_position_id) {
		$.ajax({
			type: 'POST',
			async: true,
			url: '/beautician.php',
			data: 'action=add_to_chart&id=' + _catalog_position_id,
			success: function(xml) {
				Beautician.Refresh();
			}
		});
	},
	
	RemoveFromChart: function(_catalog_position_id) {
		$.ajax({
			type: 'POST',
			async: true,
			url: '/beautician.php',
			data: 'action=remove_from_chart&id=' + _catalog_position_id,
			success: function(xml) {
				Beautician.Refresh();
			}
		});
	},
	
	RefreshGroups: function() {
		$('.chart_groups ul').each(function() {
			var list = $(this);
			$.ajax({
				type: 'POST',
				async: true,
				url: '/beautician.php',
				data: 'action=get_groups',
				success: function(xml) {
					list.empty();
					if ($(xml).find('item').length > 0) {
						$(xml).find('item').each(function() {
							list.append(
								'<li id="item_' + $(this).attr('id') + '">' +
								'<div class="view">' +
								'<a href="javascript://" onclick="if (confirm(\'Вы уверены?\')) { Beautician.DeleteGroup(\'' + $(this).attr('id') + '\'); } return false;" title="Удалить"><img src="/images/cross-small.png" width="16" height="16" alt="Удалить" /></a>' +
								'<a href="javascript://" onclick="$(this).parents(\'ul\').find(\'.view\').show(); $(this).parents(\'ul\').find(\'.edit\').hide(); $(this).parent().hide(); $(this).parent().next().show(); return false;" title="Редактировать"><img src="/images/pencil-small.png" width="16" height="16" alt="Редактировать" /></a>' +
                $(this).find('title').text() +
								'</div><div class="edit">' +
								'<form onsubmit="Beautician.EditGroup($(this).find(\'.text\').val(), \'' + $(this).attr('id') + '\'); return false;">' +
								'<a href="javascript://" onclick="$(this).parent().parent().hide(); $(this).parent().parent().prev().show(); return false;" title="Отмена"><img src="/images/arrow-180-small.png" width="16" height="16" alt="Отмена" /></a>' +
								'<input type="image" src="/images/tick-small.png" alt="Сохранить" title="Сохранить" class="right" />' +
                '<input type="text" name="title" value="' + $(this).find('title').text() + '" class="text" />' +
								'</form>' +
								'</div>' +
								'</li>'
							);
						});
						list.sortable({
							update: function(event, ui) {
								var query = $(this).sortable('serialize');
								$.ajax({
									type: 'POST',
									async: true,
									url: '/beautician.php',
									data: 'action=sort_groups&' + query
								});
							}
						});
					} else {
						list.append('<li>Список пуст.</li>');
					}

          $('.chart_groups li a').bind('click', function() {
              $('.chart_active').removeClass('chart_active');
              $(this).parents('.chart_groups li').addClass('chart_active');
              })
					
				}
			});
		});
	},
	
	AddGroup: function() {
		var form = $('.chart_groups .add_group');
		var group_title = form.find('input[name = "title"]').val().replace(/(^\s+)|(\s+$)/g, '');
		if (group_title.length > 0) {
			form.find('input').attr('disabled', 'disabled');
			$.ajax({
				type: 'POST',
				async: true,
				url: '/beautician.php',
				data: 'action=add_group&title=' + group_title,
				success: function(xml) {
					Beautician.RefreshGroups();
					form.find('input[name = "title"]').val('');
					form.find('input').removeAttr('disabled');
				}
			});
		}
	},
	
	EditGroup: function(_group_title, _group_id) {
		var group_title = _group_title.replace(/(^\s+)|(\s+$)/g, '');
		if (group_title.length > 0) {
			$('.chart_groups ul').empty();
			$.ajax({
				type: 'POST',
				async: true,
				url: '/beautician.php',
				data: 'action=edit_group&group_id=' + _group_id + '&title=' + group_title,
				success: function(xml) {
					Beautician.RefreshGroups();
				}
			});
		}
	},
	
	DeleteGroup: function(_group_id) {
		$('.chart_groups ul').empty();
		$.ajax({
			type: 'POST',
			async: true,
			url: '/beautician.php',
			data: 'action=delete_group&group_id=' + _group_id,
			success: function(xml) {
				Beautician.RefreshGroups();
			}
		});
	}
	
}
