(function($){
	$.fn.lamps = function(options){
		var element = $(this);
		var selected = '';
		// настройки по умолчанию
		var options = $.extend({
			action: 'show',
			altaction: 'hide',
			check: '/template/ico/on.gif', // стоит галочка
			uncheck: '/template/ico/off.gif', // не стоит галочка
			handler: '/moduls/recipe/ajax.php' // стоит галочка
		},options);

		element.click(function(){
			var id = $(this).attr("rec_id");
			var action = $(this).attr("action");
			if(action==options.action){
				$(this).attr("src",options.check);
				$(this).attr("action",options.altaction);
			}
			else{
				$(this).attr("src",options.uncheck);
				$(this).attr("action",options.action);
			}
			$.post(options.handler, { action: action, id: id }, function(data){}, "json");
		});

	};
})(jQuery);

(function($){
	$.fn.imgdelete = function(options){
		var element = $(this);
		// настройки по умолчанию
		var options = $.extend({
			handler: '/moduls/recipe/ajax.php' // стоит галочка
		},options);

		element.click(function(){
			if (confirm('Вы действительно хотите удалить фото?')) {
				var div = $(this).parent('div');
				var id = $(this).attr('rec_id');
				div.hide('explode', {}, 1000, function(){div.remove();});
				$.post(options.handler, { action: 'deleteimg', id: id }, function(data){}, "json");
			}
		});

	};
})(jQuery);

(function($){
	$.fn.addIngredient = function(options){
		var element = $(this);

		// настройки по умолчанию
		var options = $.extend({
			add: $('#addIng'),
			edit: $('#editIng'),
			del: $('#deleteIng'),
			select: $('#select'),
			handler: '/moduls/recipe/ajax.php'
		},options);

		var editid = options.select.val();
		var action = 'addingredient';

		element.css('left', options.add.offset().left+'px');
		element.css('top', options.add.offset().top+options.add.height()+4+'px');

		options.add.click(function(){
			if(element.attr('show')!=1){
				action = 'addingredient';
				editid = options.select.val();
				element.attr('show',1);
				element.show();
			}else{
				element.attr('show',0);
				element.hide();
			}
		});

		options.edit.click(function(){
			if(element.attr('show')!=1){
				action = 'editingredient';
				editid = options.select.val();
				element.find("input[name='name']").val($('>option:selected',options.select).text());
				element.attr('show',1);
				element.show();
			}else{
				element.attr('show',0);
				element.hide();
			}
		});

		options.del.click(function(){
			action = 'deleteingredient';
			editid = options.select.val();
			if (confirm('Вы действительно хотите удалить инигредиент '+$('>option:selected',options.select).text()+'?')) { $.post(options.handler, { action: action, name: element.find("input[name='name']").val(), id: editid }, function(data){ options.select.html(data.content); }, "json"); }
		});

		element.find("input[name='save']").click(function(){
			$.post(options.handler, { action: action, name: element.find("input[name='name']").val(), id: editid }, function(data){ options.select.html(data.content); }, "json");
			element.find("input[name='name']").val('');
			element.attr('show',0);
			element.hide();
		});

	};
})(jQuery);

(function($){
	$.fn.orderlist = function(options){
		// настройки по умолчанию
		var element = $(this);
		var options = $.extend({
			handler: '', // дирректория ajax-обработчика
			loader: '/template/ico/loader.gif' //изображение загрузчика
		},options);

		element.click(function(){
			var modal = $('<div></div>').appendTo(document.body);
			modal.css({height: $(document).height(),width: '100%',background: 'url('+options.loader+') center center no-repeat #000',position: 'absolute',top: 0,left: 0,opacity: 0.5,filter: 'Alpha(Opacity=50)',zIndex: 2000});
			$.post( options.handler, { action: $(this).attr('act'), id: $(this).attr('rec_id'), parent_id: $(this).attr('parent_id'), page: $(this).attr('page') }, function(data){$('div.white_round div.body').html(data.content); modal.remove(); init(); }, "json");
		});

	};
})(jQuery);
