function add_message(message, before){
	var msg = document.getElement('.message');
	if (msg) {
		msg.set('html', message);
	}
	else {
		var b = document.getElement(before);
		if (b) {
			new Element('div', {
				'class': 'message',
				'html': message
			}).inject(b, 'before');
		}
	}
}

function remove_message(){
	var msg = document.getElement('.message');
	if (msg) {
		msg.dispose();
	}
}

function delete_item(item_id, script, message_confirm, message_process, message_not_exist){
	var item = $(item_id.toString());
	if (item) {
		var popup = new MessageBox(document.body, {
			'popup_class': 'popup',
			'loader_class': 'loader',
			'text_class': 'popup_text'
		});
		var ajax = new Ajax({
			'script': '/control/ajax/' + script + '.php'
		});
		ajax.addEvent('onRequest', function(response){
			popup.hide_loader();
			if (response.message && response.ok) {
				popup.set_text(response.message);
				
				if (response.id && $(response.id.toString())) {
					var tr = $(response.id.toString()).getParent('tr');
					if (tr) {
						tr.dispose();
					}
					var trs = item.getElement('tbody') && item.getElements('tr');
					if (!trs || trs.length == 0) {
						add_message(message_not_exist, item.get('class'));
						item.dispose();
					}
				}
				var t = setTimeout(function(){
					clearTimeout(t);
					popup.hide();
				}, 1000);
			}
			else 
				if (response.message) {
					popup.hide();
					add_message(response.message, item.get('class'));
				}
		});
		var dels = item.getElements('.tdelete');
		dels.addEvent('click', function(event){
			remove_message();
			var id = parseInt(event.target.get('id'));
			if (confirm(message_confirm)) {
				popup.show();
				popup.show_loader();
				popup.set_text(message_process);
				ajax.make_request({
					'id': id
				});
			}
		})
	}
}

var Ajax = new Class({
	Implements: [Options, Events],
	options: {
		script: '',
		container: null,
		onRequest: Class.empty,
		onFailure: Class.empty
	},
	
	initialize: function(options){
		if ($type(options.container) == 'element') {
            options.container = options.container;
        }
        else if ($type(options.container) == 'string') {
            options.container = $(options.container);
        }
        else {
            options.container = null;
        }
		
		this.setOptions(options);
	},
	
	
	make_request: function(send)
	{
		if(!this.options.script)
		{
			return;
		}
		
	
		new Request.JSON({
            url: this.options.script,
			onRequest: function(){
				//this.loader = new Loader(null, {'modal': true, 'loader_class': 'loader'});
			}.bind(this),
			
            onComplete: function(response){
				//this.loader.destroy();
				/*
				if (func) {
					func(response, this);
				}*/
            	this.fireEvent('onRequest', [response])
            }.bind(this),
            onFailure: function(){
				//this.loader.destroy();
				this.fireEvent('onFailure', [])
            }.bind(this)
        }).post(send);
	}
});

var MessageBox = new Class({
	Implements: [Options, Events],
	options: {
		modal: true,
		overlay_color: '#000',
		overlay_opacity: 0.7,
		popup_class: '',
		loader_class: '',
		buttons: [],
		text_class: ''
	},
	
	initialize: function(container, options){
		if (container) {
			if ($type(container) == 'element') {
				this.container = container;
			}
			else 
				if ($type(container) == 'string') {
					this.container = $(container);
				}
				else {
					this.container = document.body;
				}
		}
		else {
			this.container = document.body;
		}
		
		this.setOptions(options);
		
		this.create_overlay();
		this.create_popup();
		
		if (this.options.modal) {
			this.fx = {
				overlay_animation: new Fx.Tween(this.overlay, {
					property: "opacity"
				}),
				loader_animation: new Fx.Tween(this.popup, {
					property: "opacity"
				})
			}
		}
		else {
			this.fx = {
				loader_animation: new Fx.Tween(this.popup, {
					property: "opacity"
				})
			}
		}
		
		window.addEvent('resize', function(){
			this.set_position();
		}.bind(this));
		
		//this.show();
	},
	
	show: function(){
		this.set_position();
		if (this.options.modal) {
			this.fx.overlay_animation.start(0, this.options.overlay_opacity);
			this.fx.loader_animation.start(0, 1);
		}
		else {
			this.fx.loader_animation.start(0, 1);
		}
	},
	
	hide: function(){
		if (this.options.modal) {
			this.fx.overlay_animation.start(this.options.overlay_opacity, 0);
			this.fx.loader_animation.start(1, 0);
		}
		else {
			this.fx.loader_animation.start(1, 0);
		}
	},
	
	set_text: function(text)
	{
		if(!this.text)
		{
			var panel_h = 0;
			if (this.panel) {
				panel_h = this.panel.getCoordinates().height;
			}
			this.text = new Element('div', {
				'class': this.options.text_class,
				'style': 'overflow:auto;height:' + (this.popup.getCoordinates().height - panel_h - 10) + 'px'
			}).inject(this.popup);
		}
		
		if(!text)
		{
			text = '';
		}
		this.text.set('html', text);
	},
	
	destroy: function(){
		this.overlay.dispose();
		this.popup.dispose();
	},
	
	create_overlay: function(){
		this.overlay = new Element('div').inject(document.body, 'top');
		this.overlay.setStyles({
			'position': 		'fixed',
			'visibility':		'hidden',
			'width':			'100%',
			'height':			'100%',
			'z-index':  		'10000',
			'top':      		'0px',
			'left': 			'0px',
			'background-color': this.options.overlay_color
		});
		
		if(Browser.Engine.trident && Browser.Engine.version < 5){
			this.overlay.setStyles({
				'width': document.getCoordinates().width + 'px',
				'height': document.getCoordinates().height + 'px',
				'position':'absolute'
			});
		}
	},
	
	create_popup: function(){
		this.popup = new Element('div', {'class': this.options.popup_class}).inject(document.body, 'top');
		
		//var width = document.getCoordinates().width - this.popup.getCoordinates().width;
		//var height = document.getCoordinates().height - this.popup.getCoordinates().height;//+ document.body.getScroll().y;
		this.set_position();
		this.popup.setStyles({
			'visibility': 		'hidden',
			'position': 		'absolute',
			'z-index':  		'10001'
		});
		
		if(this.options.buttons.length)
		{
			this.panel = new Element('div', {}).inject(this.popup);
			this.options.buttons.each(function(button){
				var btn = new Element('div', {
					'class': 'mb_icon ' + button
				}).inject(this.panel);
				
				if(button == 'close')
				{
					btn.addEvent('click', function(event){
						this.hide();
					}.bind(this));
				}
			}.bind(this));
		}
	},
	
	set_position: function(){
		var width = document.getCoordinates().width  - this.popup.getCoordinates().width;// + document.getScroll().x;
		var height = document.getCoordinates().height - this.popup.getCoordinates().height;// + document.getScroll().y;
		
		this.popup.setStyles({
			//'margin-left': 		(Math.ceil(width / 2)) + 'px',
			'margin-top': 		(height / 2 + document.getScroll().y) + 'px'
		});
		this.popup.setStyle('left', (Math.ceil(width / 2)) + 'px');
		if(Browser.Engine.trident && Browser.Engine.version < 5){
			this.overlay.setStyles({
				'width': document.getCoordinates().width + 'px',
				'height': document.getCoordinates().height + 'px',
				'position':'absolute'
			});
		}
	},
	
	resize: function(w, h){
		this.popup.setStyles({'width': w + 'px', 'height': h + 'px'});
		this.set_position();
	},
	
	show_loader: function()
	{
		this.loader_animation = new Element('div', {'class': this.options.loader_class}).inject(this.popup);
	},
	
	hide_loader: function()
	{
		this.loader_animation.dispose();
	}
});


function show_picture(){
	var popup = new MessageBox(document.body, {
		'popup_class': 'popup',
		'loader_class': 'loader'
	});
	var ims = document.getElements('.img_pre');
	ims.addEvent('click', function(event){
		var img = event.target.getAttribute('src');
		img = img.replace("_tmb", "");
		//popup.set_text("<img src='" + img + "'>");
		var bimg = new Element('img', {
			'src': img,
			'title': 'Кликните, чтобы закрыть',
			'style': 'cursor:pointer'
		}).inject(popup.popup);
		
		//var bimg = popup.popup.getElement('img');
		bimg.addEvent('load', function(event){
			//popup.set_position();
			popup.show();
			//popup.resize(bimg.getCoordinates().width, bimg.getCoordinates().height);
			//alert(document.getCoordinates().width);
			//alert(popup.popup.getCoordinates().width);
			//alert(popup.popup.getCoordinates().width);
			//alert(popup.popup.getCoordinates().left);
			bimg.addEvent('click', function(event){
				popup.hide();
				bimg.dispose();
			});
		});
		bimg.set('src', img);
	});
}