var ActionBox = new Class({ 

	initialize: function(options) {
		this.wrapDivName = 'actionbox';
		this.mainDivName = 'actionbox';
		this.closeLinkName = 'actionclose';
		this.authErrorMsg = 'auth_error';
		this.loginUrl = 'login.htm';
		this.lastXPos = 0;
		this.lastYPos = 0;
		this.standardLeftPos = 220;
		this.standardTopPos = 60;
		this.boxWidth = 400;
		this.pageWidth = 950;
		this.topPosOffset = -60;
		this.listeners = [];
		this.onSubmitEvent = null;
		
		window.addEvent('load',function() {
			this.setCloseEvent();
		}.bind(this));
		
		window.addEvent('keydown', function(event) {  
			var event = new Event(event);
		    if (event.key == 'esc') {
		        this.close();      
		    }			
		}.bind(this));		
		
		document.addEvent('mouseup', function(evt) {
			this.lastXPos = evt.clientX;
			this.lastYPos = evt.clientY;
		}.bind(this));
	},
	
	addListener: function(listener) {
		if (listener) this.listeners.push(listener);
	},
	
	open: function(url, formId, formSubmitBtnId, skipCheckPosition, standardPos, loadCallBackFunc) {
		for(var i = 0; i < this.listeners.length; i++) {
			if (this.listeners[i].onActionBoxOpen)
				this.listeners[i].onActionBoxOpen();
		}
		
		this.mainDivName = 'actionbox';
		if (!skipCheckPosition) 
			$(this.mainDivName).empty();
		var postdata = '';
		var ps = url.indexOf('?');
		if (ps >= 0) {
			postdata = url.substring(ps + 1);
			url = url.substring(0, ps);
		}
		this.formId = formId;
		this.formSubmitBtnId = formSubmitBtnId;
		this.showLoader();

		if (!skipCheckPosition) {
			$(this.mainDivName).style.top = (this.standardTopPos + this.getWndScrollTop()) + 'px';
			$(this.mainDivName).style.left = this.standardLeftPos + 'px';
		}

		
		var myAjax = new Ajax(url, {
			method: postdata==''?'get':'post',
			data: postdata, 
			onComplete: function(data) {
					
				this.onContentLoad(data);
				if (loadCallBackFunc)
					loadCallBackFunc();

				if($('captcha')){
					$('captcha').style.display = 'none';
				}
			}.bind(this)
		}).request();	
	},

	close: function(){
		$(this.wrapDivName).empty();
		
		for(var i = 0; i < this.listeners.length; i++) {
			if (this.listeners[i].onActionBoxClose)
				this.listeners[i].onActionBoxClose();
		}
	},
	
	onContentLoad: function(data){
		if (data.trim() == this.authErrorMsg) {
			location.href = this.loginUrl;
		} else {
			$(this.mainDivName).setHTML(data);
			$(this.mainDivName).getElements('script').each(function(item){eval(item.innerHTML);item.remove();});
			this.prepareForm();
		}
	},
	
	prepareForm: function() {
		this.hideLoader();
		if (emulateCheckbox)
			emulateCheckbox();
		this.setCloseEvent();
		if($defined(this.formId) && this.formId.length>0) {
			this.prepareFormEvents();
		}
	},
	
	prepareFormEvents: function() {
		$(this.formSubmitBtnId).addEvent('click', function() {
				$(this.formSubmitBtnId).disabled=true; 
				if (this.onSubmitEvent)
					this.onSubmitEvent();
				this.submitForm();
			}.bind(this));
	},
	
	setOnSubmitEvent: function(func){	
		this.onSubmitEvent = func;
	},
	
	submitForm: function() {
		var frm = $(this.formId);
		var postData =  frm.toQueryString();
		var ajax = new Ajax(frm.action, 
		{
			method: 'post', 
			data: postData,
			onComplete: function(data) {	
				if (data.trim().length < 1) {
					$(this.mainDivName).empty();
					this.refreshPage();
				} else if (data.trim() == this.authErrorMsg) {	
					location.href = this.loginUrl;
				} else {	
					$(this.mainDivName).setHTML(data);
					this.prepareForm();
				}
			}.bind(this)
		}
		).request();
	},
	
	refreshPage: function() {
		var url = window.location.href;
		var ps = url.lastIndexOf('#');
		if (ps >= 0) 
			url = url.substring(0, ps);
		window.location.href = url;
	},
	
	setCloseEvent: function(){
		if ($(this.closeLinkName)){
			$(this.closeLinkName).addEvent('click', function() {
				this.close();
			}.bind(this));	
		}		
	},
	
	showLoader: function(){
	},
	
	hideLoader: function(){
	}, 
	
	checkBoxPosition: function() {
		var box = $(this.mainDivName);
		var scrollTop = this.getWndScrollTop();
		box.style.top = (this.lastYPos + scrollTop + this.topPosOffset) + 'px';
		var x = this.lastXPos - Math.abs((this.getWndWidth() - this.pageWidth) / 2) - (this.boxWidth/2);
		box.style.left = x + 'px';
	}, 
	
	getWndScrollTop: function() {
		return document.body.scrollTop;
	}, 
	getWndHeight: function() {
		return document.body.clientHeight;
	},
	getWndWidth: function() {
		return document.body.clientWidth;
	}
	
});
