/*
* Включаем кнопку закрыть
* userRegistration.buttons.btnClose.setStyle('top', 4);
*
*
*
*
*
*
*
*
*
*
*/

var MessageBox = new Class({
	Implements: Options,

	options: {
		title: null,
		width: 300,
		height: null,
		buttons: {
			ok: {type: 'ok', text: 'OK'},
			cancel: {type: 'cancel', text: 'Cancel'}
		},
		fullPageLink: null,
		className: null,
		fileupload: null
	},

	initialize: function(options) {
		this.setOptions(options);
		this.create();
		this.attachEffects();
		this.attachEvents();
	},

	create: function(){
		this.ajax = null;
		this.request = null;
		this.buttons = {};
		this.overlay = $('overlay').setStyles({'opacity': 0.1});

		this.box = new Element('div', {
			'class': 'wm',
			'styles': {
				'width': this.options.width,
				'marginLeft': -this.options.width/2,
				'display': 'none'
			}
		}).inject($(document.body));
		if(this.options.className) {this.box.addClass(this.options.className);}

		this.boxOuter	= new Element('div', {'class': 'wm-outer'}).inject(this.box);
		this.titleHolder = new Element('div', {'class': 'wm-title'}).inject(this.boxOuter);
		this.title		= new Element('h3', {'html': this.options.title}).inject(this.titleHolder);
		this.inner		= new Element('div', {'class': 'wm-inner' }).inject(this.boxOuter);
		this.buttonsWrapBox = new Element('div', {'class': 'wm-btns' }).inject(this.boxOuter);
		this.buttonsBox = new Element('div', {'class': 'wm-btns-i' }).inject(this.buttonsWrapBox);
		this.ajaxLoader = new Element('div', {'class': 'wm-ajax-loader'}).inject(this.inner);
		this.message	= new Element('div', {'class': 'wm-message'}).inject(this.inner);
		this.html		= new Element('div', {'class': 'wm-html'}).inject(this.inner);
		this.ajaxLoader1= new Element('div', {'class': 'wm-ajax-loader1'}).inject(this.buttonsBox);

		this.addButtons();
	},

	hide: function(){
		this.ajax = false;
		this.html.set('html', '').setStyle('display', 'none');
		this.ajaxLoader.setStyle('display', 'none');
		this.ajaxLoader1.setStyle('display', 'none');
		this.box.setStyle('display', 'none');
		this.overlay.setStyle('display', 'none');
		this.buttons.btnOk.setStyle('display', 'none');
		this.buttons.btnCancel.set('html', $lang.close);
		if(this.request) {
			this.request.cancel();
		}

		if(typeof this.onHide == 'function') this.onHide();
	},

	show: function(){
		this.message.setStyle('display', 'none');
		this.showOverlay();
		this.box.setStyle('display', 'block');
		this.setPosition();
	},

	attachEvents: function(){
		var boxTimeoutID = null;
		var repos = function () {
				this.boxFx.cancel();
				this.boxFx.start('top', $(window).getScroll().y + $(window).getSize().y/2);
			}.bind(this);

		this.hideEventId = this.hide.bind(this);
		this.buttons.btnCancel.addEvent('click', this.hideEventId);

		this.buttons.btnClose.addEvents({
			click: this.hide.bind(this),
			mouseenter: function(){
				this.buttons.btnCloseFx.cancel();
				this.buttons.btnCloseFx.start('background-color', '#fff');
			}.bind(this),
			mouseleave: function(){
				this.buttons.btnCloseFx.cancel();
				this.buttons.btnCloseFx.start('background-color', '#e5b3ce');
			}.bind(this)
		});

		$(window).addEvents({
			'scroll': function(e) {
				boxTimeoutID = $clear(boxTimeoutID);	//Cancels myFunction (repos).
				boxTimeoutID = repos.delay(700); 		//Waits 1 seconds then executes myFunction.
			},
			'resize': function(e) {
				boxTimeoutID = $clear(boxTimeoutID);	//Cancels myFunction (repos).
				boxTimeoutID = repos.delay(700); 		//Waits 1 seconds then executes myFunction.
			}
		});
	},

	attachEffects: function() {
		this.buttons.btnCloseFx = new Fx.Tween(this.buttons.btnClose, {duration: 250});
		this.boxFx = new Fx.Tween(this.box);
	},

	setPosition: function() {
		this.box.setStyles({
			'top': $(window).getScroll().y + $(window).getSize().y/2,
			'marginTop':-this.box.getStyle('height').toInt()/2
		});
	},

	addButtons: function(type) {
		this.buttons.btnCancelM = new Element('div', {
			'class': 'btn-cancel'
		}).inject(this.buttonsBox);
		this.buttons.btnCancel = new Element('span', {
			'html': $lang.close
		}).inject(this.buttons.btnCancelM);

		this.buttons.btnOk = new Element('div', {
			'class': 'btn-ok',
			'html': $lang.save,
			'styles': {'display': 'none'}
		}).inject(this.buttonsBox);

		this.buttons.btnClose = new Element('div').inject(this.titleHolder);
	},

	getFileUpload: function() {
		var form = this.html.getElement('form');
		var ifile = form.getElement('input.fileupload');
		if(ifile) {
			setLink = function() {
				var filePath = this.getProperty('value');
				this.getNext('input').setProperty('value', filePath);
			};
			ifile.setStyle('opacity', '0').addEvent('change', setLink);
		}

		form.setProperty('target', 'ifileupload');
	},

	showOverlay: function() {
		var scroll = $(window).getScrollSize();
		this.overlay.setStyles({'display': 'block', 'height': scroll.y + 'px'});
	}
});
