
var loadInCenterbox = function(a) {
	
	new Ajax(a.href, {
		onSuccess: function(response) {
			showCenterBox(response);
		}
	}).request();
	
};

var redUrl = null;
var setCloseRedirect = function(url) {
	redUrl = url;
};
var activeCenterBoxes = new Array();

var closeCenterBox = function() {
	if(activeCenterBoxes.length == 0) return;
	activeCenterBoxes[activeCenterBoxes.length - 1].close();
	var tmp = new Array();
	for(var i = 0; i < activeCenterBoxes.length - 1; i++) {
		tmp[i] = activeCenterBoxes[i];
	}
	activeCenterBoxes = tmp;
}

var showCenterBox = function(content, width, height, uncloseable, usecaption) {
	
	var trans = new Element('div', {
		'styles': {
			'background-color': 'white',
			'opacity': 0,
			'position': 'absolute',
			'z-index': 150
		}
	} );
	activeCenterBoxes[activeCenterBoxes.length] = trans;
	
	var box = new Element('div', {
		'class': 'centerBox',
		'styles': {
			'opacity': 0
		}
	});
	
	box.inject($(document.body));
	var inner = new Element('div', {
		'styles': {
			'margin': !usecaption ? 0 : 0			
		}
	});
	
	if(content.innerHTML || content.innerHTML == '') content.inject(inner);
	else inner.innerHTML = content;
	inner.inject(box);
	
	if(width) {
		box.setStyles( { 'width': width } );
	}
	else {
		width = box.getSize().x - 2;
	}
	if(height) {
		box.setStyles( { 'height': height} );
	}
	else {
		height = box.getSize().y - 2;
	}
	
	if(!uncloseable) {
		var mailCloseIcon = new Element('div', {
			'class': 'mailCloseIcon',
			'styles': {
				'opacity': 0,
				'position': 'absolute',
				'background-image': 'url(/graphics/closebox.gif)',
				'background-repeat':'no-repeat',
				'width': 30,
				'height': 30,
				'cursor': 'pointer',
				'z-index':220
			}
		});
		mailCloseIcon.addEvent('click', function() {
			trans.fireEvent('click'); 
		});
		mailCloseIcon.inject($(document.body));
	}
			
	var pos = box.getPosition();
	
	var fx1 = new Fx.Morph(box, { duration: 400, wait: false, onComplete:function() {
		if(box.removed) {
			if(redUrl != null) window.location.href = redUrl;
			//box.dispose();					
		}		
	} }).start({ opacity: 1 });	
	
	trans.inject($(document.body));
	var fx2 = new Fx.Morph(trans, { duration: 400, wait: false, onComplete:function() {
		if(trans.removed) {
			trans.dispose();					
		}				
	} }).start({ opacity: 0.5 });
	
	if(mailCloseIcon) {
		var fx3 = new Fx.Morph(mailCloseIcon, { duration: 400, wait: false, onComplete:function() {
			if(mailCloseIcon.removed) {
				mailCloseIcon.dispose();					
			}				
		} }).start({ opacity: 1 });	
	}
	
	box.trans = trans;
	
	trans.close = function() {
		trans.removed = box.removed = mailCloseIcon = 1;
		fx1.start({ opacity: 0 });
		fx2.start({ opacity: 0 });
		if(fx3) fx3.start({ opacity: 0 });
	}
	
	if(!uncloseable) {
		trans.addEvent('click', trans.close);
	}
	
	var fxBox = new Fx.Morph(box, { duration: 800, wait: false });	
	if(mailCloseIcon) var fxClose = new Fx.Morph(mailCloseIcon, { duration: 800, wait: false });	
	var smooth = false;
	window.addEvent('scroll', function() {
		trans.setStyles( {
			'top': window.getScrollTop(),
			'left': window.getScrollLeft(),
			'width': '100%',
			'height': '100%'
		});
		
		var h = (window.innerHeight ? window.innerHeight : document.body.clientHeight);
		if(height && h > height) h = height;
		var w = (window.innerWidth ? window.innerWidth : document.body.clientWidth);
		if(width && w > width) w = width;
		
		//var top = (window.innerHeight ? window.innerHeight : document.body.clientHeight) / 2 + window.getScrollTop() - height / 2;
		//var left = (window.innerWidth ? window.innerWidth : document.body.clientWidth) / 2 + window.getScrollLeft() - width / 2;
		
		var top = window.getScrollTop() + (window.innerHeight ? window.innerHeight : document.body.clientHeight) / 2 - h / 2;
		var left = window.getScrollLeft() + (window.innerWidth ? window.innerWidth : document.body.clientWidth) / 2 - w / 2;
		
		
		if(top < 15) top = 15;
		if(left < 15) left = 15;
		
		/*if(!height && h > 400) h = 400;
		else if(!height && h < 200) h = 200;
		if(!width && w > 700) w = 700;
		else if(!width && w < 350) w = 350;
		*/
		
		
		if(!smooth) {
			smooth = true;
			fxBox.set( {
				'top': top,
				'left': left,
				'width': w,
				'height': h
			});
			if(fxClose) {
				fxClose.set( {
					'top': top - 15,
					'left': left - 15
				});
			}
		}
		else {
			fxBox.start( {
				'top': top,
				'left': left,
				'width': w,
				'height': h
			});
			if(fxClose) {
				fxClose.start( {
					'top': top - 15,
					'left': left - 15
				});
			}
		}
	}); 
	
	window.addEvent('resize', function() {
		//window.fireEvent('scroll');
	}); 
	
	window.fireEvent('scroll');
	
}
