
	majAnchorParam = function(param_name, default_param)
	{
		var search = new RegExp(param_name+'=(.*)&|'+param_name+'=(.*)').exec(document.location.hash);
		if (!search) return default_param;
		
		var result = search[2] ? search[2] : search[1];
		return typeof(result) != 'undefined' ? result : default_param;
	}

	/**
	 * Обновление содержимого
	 */
	majContentUpdate = new Class({
		Implements: [Options],
		options: {
			url: '#',
			container: false,
			get: {}
		},
		initialize: function(options)
		{
			this.setOptions(options);
			
			this.options.container.empty();
			new Element('div').addClass('loading-panel').inject(this.options.container);
			
			new Request.HTML({
				url: this.options.url,
				update: this.options.container
			}).get(this.options.get);
		}
	});
	
	/**
	 * Показ уведомлений в общепринятом формате
	 */
	majNotifications = new Class({
		Implements: [Options],
		options: {},
		is_complete: true,
		initialize: function(messages, options)
		{
			this.setOptions(options);
			var object  = this;
			var notimoo = new Notimoo({visibleTime: 3000});
			$each(messages, function(box){
				if (box.repeat) object.is_complete = false;
				notimoo.show({
        			title: box.title,
            		message: box.message
    			});
			});
		}
	});
	
	/**
	 * Форма аякс
	 */
	var majAJAXForm = new Class({
		Implements: [Options],
		options: {
			element: false,
			form: false,
			callback: false,
			wait: true
		},
		initialize: function(options)
		{
			this.setOptions(options);
			if (!this.options.element) throw 'You need to define submit element of the form.';
			if (!this.options.form)    throw 'You need to define form element.';
			
			var element  = this.options.element;
			var form     = this.options.form;
			var callback = this.options.callback;
			
			element.addEvent('click', function(){
				element.set('disabled', true);
				
				form.set('send', {onComplete: function(responce){
					element.set('disabled', false);
					
					if (callback){
						callback(JSON.decode(responce));
					}
				}});
				form.send();
				
				return false;
			});
		}
	});
	
	/**
	 * Окно для приватных сообщений
	 */
	var majWidgetPmBox = new Class({
		Implements: [Options],
		options: {
			className:    'pm-box-window',
			modal: 		  false,
			draggable:    true,
			resizable:    true,
			close:        true,
			maximize:     true,
			min_width:    331,
			min_height:   262,
			sendUrl:      '',
			retrieveUrl:  '',
			title: ' ',
			profile: 'Я',
			submit: 'Отправить',
			online: 'онлайн',
			offline: 'оффлайн'
		},
		window: false,
		initialize: function(element, options)
		{
			this.setOptions(options);
			
			var object    = this;
			var is_opened = false;
			var updateMessages = function(){};
			
			object.options.callClose = function()
			{
				is_opened = false;
				updateMessages = function(){};
			}
			
			element.addEvent('click', function(){
				if (is_opened) return false;
				is_opened = true;
				
				var notification = element.getElement('.new');
				if (notification) notification.dispose();
				
				object.window = new majWidget(object.options);
				var contentContainer = new Element('div').addClass('pm-box-content').inject(object.window.elements.content);
				var content          = new Element('ul').inject(contentContainer);
				
				var inputContainer = new Element('div').addClass('pm-box-text').inject(object.window.elements.content);
				var input          = new Element('textarea').inject(inputContainer);
				
				var submitContainer = new Element('div').addClass('pm-box-submit').inject(object.window.elements.content);
				var submit          = new Element('input').set('type', 'button').set('value', object.options.submit).addClass('button').inject(submitContainer);
				
				submit.addEvent('click', function(){
					var value = input.get('value');
					input.set('value', '');
					input.focus();
					if (!value.length) return false;
					
					new Request.JSON({url: object.options.sendUrl}).post({content: value});
				});
				
				object.window.show();
				
				var last = 0;
				updateMessages = function()
				{
					new Request.JSON({url: object.options.retrieveUrl, onSuccess: function(responce){
						if (!content) return false;
						
						if (responce.error)
						{
							var line = new Element('li').inject(content)
							new Element('pre').addClass('pm-box-message').set('text', responce.error).inject(line);
							return false;
						}
						
						var title = object.options.profile;
						if (!responce.online) title = title+' - '+object.options.offline;
						
						object.window.elements.headerText.set('text', title);
						
						$each(responce.content, function(message){
							last = message.pk;
							var line = new Element('li').inject(content);
							new Element('span').addClass('pm-box-author').set('text', message.profile+':').inject(line);
							new Element('span').addClass('pm-box-message').set('text', message.message).inject(line);
						});
						content.scrollTop = content.scrollHeight;
						
						setTimeout(updateMessages, 1500);
					}}).get({last: last});
				}
				updateMessages();
				return false;
			});
		}
	});
	
	/**
	 * Выполнение действия
	 */
	var majWidgetAction = new Class({
		Implements: [Options],
		options: {
			modal:        true,
			modalOpacity: '0.5',
			title:        ' ',
			draggable:    true,
			resizable:    false,
			close:        true,
			maximize:     false,
			min_width:    120,
			min_height:   80,
			text: '',
			form: false,
			proceed: 'Продолжить',
			cancel: 'Отмена',
			proceedUrl: '',
			cancelUrl: '',
			callback: function(element)
			{
				element.dispose();
			}
		},
		window: false,
		initialize: function(element, options)
		{
			this.setOptions(options);
			
			var object = this;
			
			element.addEvent('click', function(){
				object.window = new majWidget(object.options);
				
				var data = new Element('div').addClass('majWidget-content-data').inject(object.window.elements.content);
				var form = object.options.form;
				
				if (form)
				{
					form = object.options.form.setStyle('display', 'block').inject(data);
				}
				else
				{
					data.set('text', object.options.text);
				}
				
				var box = new Element('div').addClass('majWidget-buttons').inject(data, 'bottom');
				var confirm = new Element('input').set('type', 'button').addClass('button').set('value', object.options.proceed).inject(box, 'bottom');
		    	var cancel = new Element('input').set('type', 'button').addClass('button').set('value', object.options.cancel).inject(box, 'bottom');
				
				if (form)
				{
					object._initForm(object, element, data, confirm, cancel, form);
				}
				else
				{
					object._initPrompt(object, element, data, confirm, cancel);
				}
				
				object.window.show();
				return false;
			});
		},
		_initForm: function(object, element, data, confirm, cancel, form)
		{
			cancel.addEvent('click', function(){
	    		object.window.close();
	    	});
	    	
	    	var notimooManager = new Notimoo({visibleTime: 3000});
	    	
	    	confirm.addEvent('click', function(){
	    		confirm.set('disabled', true);
				
				form.set('send', {
					onComplete: function(responce){
						confirm.set('disabled', false);
	
						var to_close = new majNotifications(JSON.decode(responce)).is_complete;
						
						if (to_close) 
						{
							object.options.callback(element);
							object.window.close();
						}
					}
				});
				form.send();
	    	});
		},
		_initPrompt: function(object, element, data, confirm, cancel)
		{
			var notimooManager = new Notimoo({visibleTime: 3000});
	    	
	    	function eventProcess(button, url)
	    	{
	    		var proceedUrl = url;
	    		button.addEvent('click', function(){
		    		if (!proceedUrl.length) { object.window.close(); return false; }
		    		
		    		button.set('disabled', true);
		    		new Request.JSON({url: proceedUrl, onSuccess: function(responce){
		    			button.set('disabled', false);
		    			
		    			new majNotifications(responce);
		    			
		    			object.options.callback(element);
		    		}}).get();
		    		
		    		object.window.close();
		    	});
	    	}
	    	
	    	eventProcess(confirm, object.options.proceedUrl);
	    	eventProcess(cancel, object.options.cancelUrl);
		}
	});
	
	var majComment = new Class({
		Implements: [Options],
		options: {
			submit:   false,
			textarea: false,
			callback: false
		},
		initialize: function(form, options)
		{
			this.setOptions(options);
			
			var callback = this.options.callback;
			var submit   = this.options.submit   ? this.options.submit   : form.getElement('input[type=submit]');
			var textarea = this.options.textarea ? this.options.textarea : form.getElement('textarea');
			
			form.addEvent('submit', function(){
				if (!textarea.get('value').length) return false;
				
				submit.set('disabled', true);
				form.set('send', {onComplete: function(responce){
					submit.set('disabled', false);
					
					textarea.set('value', '');
					
					if (callback){
						callback(JSON.decode(responce));
					}
				}});
				form.send();
				
				return false;
			});
		}
	});
	
	/**
	 * Добавление функциональности к элементам
	 */
	Element.implement({
		majAJAXForm: function(options){
			options = options || {};
			$extend(options, {element:this});
			
			return new majAJAXForm(options);
		},
		mwPmBox: function(options) {return new majWidgetPmBox(this, options);},
		mwAction: function(options) {return new majWidgetAction(this, options);},
		majComment: function(options) {return new majComment(this, options);}
	});
