
// For our popup div forms
var BNoverDiv = new Class({

	Implements: Options,

	options: { 
		printURL: null,
		submitURL: null,
		formName: null,
		formSubmitEvent: null,
		oDivCSS: null,
		iDivCSS: null,
		oDivProcCSS: null,
		iDivProcCSS: null,
		grayout: false
	},

	initialize: function(options){

		this.setOptions(options) ;

		this.printURL = this.options.printURL ;
		this.submitURL = this.options.submitURL ;
		this.formName = this.options.formName ;
		this.formSubmitEvent = this.options.formSubmitEvent ;
		this.oDivCSS = this.options.oDivCSS ;
		this.iDivCSS = this.options.iDivCSS ;
		this.oDivProcCSS = this.options.oDivProcCSS ;
		this.iDivProcCSS = this.options.iDivProcCSS ;
		this.grayout = this.options.grayout ;
		this.grayoutDiv = null ;
		this.retrieveHttpRequest = null ;
		this.postHttpRequest = null ;

		if (this.grayout) {
			this.grayoutDiv = new Element('div').inject(document.body, 'top').setStyles({
				display: 'none',
				position: 'absolute'
			}) ;
		}
		this.outerDiv = new Element('div').inject(document.body, 'top') ;
		this.innerDiv = new Element('div').inject(this.outerDiv, 'top') ;
		this.outerProcessingDiv = new Element('div').inject(document.body, 'top') ;
		this.innerProcessingDiv = new Element('div').inject(this.outerProcessingDiv, 'top') ;
	}, 

	show: function(){
		this.outerProcessingDiv.setStyle('display','none') ;
		this.innerProcessingDiv.setStyle('display','none') ;
		this.outerDiv.setStyles(this.oDivCSS) ;
		this.innerDiv.setStyles(this.iDivCSS) ;
	},

	activate: function(){
		this.show() ;
		if (this.grayout) {
			this.grayoutDiv.setStyles({
				height: window.getScrollSize().y,
				width: window.getScrollSize().x,
				backgroundColor: '#000000',
				opacity: .5,
				zIndex: 99,
				display: 'block'
			}) ;
		}
		this.retrieveHttpRequest = new Request({ 
			url: this.printURL,
			method: 'get',
			evalScripts: true, 
			onSuccess: this.processresponse.bind(this)
		}).send() ;
	},

	deactivate: function(){
		if (this.grayout) {
			this.grayoutDiv.setStyles({
				height: '0px',
				width: '0px',
				backgroundColor: 'transparent', 
				opacity: 0,
				display: 'none'
			}) ;
		}
		this.innerDiv.set('html', '') ;
		this.innerDiv.setStyles({ display: 'none' }) ;
		this.outerDiv.setStyles({ display: 'none' }) ;
	},

	deinitialize: function(){
		this.outerDiv.dispose() ;
	},

	postform: function(){
		if ($chk(this.formSubmitEvent)) {
			$(this.formName).addEvent('submit', this.formSubmitEvent) ;
		}
		this.innerDiv.setStyle('opacity', .5) ;
		this.outerProcessingDiv.setStyles(this.oDivProcCSS) ;
		this.innerProcessingDiv.setStyles(this.iDivProcCSS).set('html', '<span style="font-weight: bold; font-size: 12px">processing...</span>') ;
		this.postHttpRequest = new Request({
			url: this.submitURL,
			method: 'post',
			evalScripts: true,
			data: $(this.formName).toQueryString(),
			onSuccess: this.processresponse.bind(this)
		}).send() ;
	},

	processresponse: function(text, xml){
		this.innerProcessingDiv.setStyle('display', 'none') ;
		this.outerProcessingDiv.setStyle('display', 'none') ;
		this.innerDiv.setStyle('opacity', 1) ;
		this.innerDiv.set('html', text) ;
		if ($chk($('processresponseScripts'))) {
			eval($('processresponseScripts').get('html')) ;
		}
	}
}) ;
