/* Gestion des mini applications qui s'affichent en 1er plan */

var current_app = new Array(50);
var current_ind = 0;
var zIndMax = 0;
var apps_width = new Array(50);

function open_app(app, params, width) {
	if(!params) params = '';
	current_app[current_ind] = app.toUpperCase();
	if(width) apps_width[current_ind] = width;
	else apps_width[current_ind] = '50%';
	current_ind++;
	show_background();
	show();
	load(app, params);
	zIndMax += 2;
	
	//if(document.all) window.onscroll = update_scroll;
	//else 
	Event.observe(document, 'scroll', update_scroll);
	
	update_position();
}

function show_background() {
	if(current_ind == 1) {
		var bg = document.createElement('div');
		bg.id = 'APPS_BACKGROUND';
		bg.style.background = '#1E1E1E'; 
		bg.style.position = 'absolute';
		bg.style.top = '-200px';
		bg.style.left = '0px'; 
		bg.style.width = '100%'; 
		bg.style.zIndex = 0;
		bg.onmousedown = 'return false';
		document.body.appendChild(bg);
	}
	else { var bg = $('APPS_BACKGROUND'); }
	if(document.all) {
		bg.style.height = '100%';
		bg.style.width = '100%';
	}
	else {
		bg.style.height = (document.viewport.getHeight()+300)+'px';
//		bg.style.minHeight = '110%';
//		bg.style.minWidth = '200%';
	}
	Element.setOpacity(bg, 0.8);
	bg.style.zIndex = zIndMax+1;
	bg.style.top = document.viewport.getScrollOffsets().top+"px";
}

function hide_background() {
	zIndMax = 0;
	document.body.removeChild($('APPS_BACKGROUND'));
}

function hide(n) { 
	if(!n) n=current_ind-1;
	$('APPS_'+current_app[n]).style.display = 'none';
	$('APPS_'+current_app[n]).style.zIndex = 0;
}

function load(app, params) {
	n = current_ind-1;
	new Ajax.Updater($('APPS_'+current_app[n]),
					 'apps/'+app.toLowerCase()+'/application.php',
					 {
						 method: 'get',
						 parameters: params,
						 onComplete: function(transport) {
							update_position(); 
						 }
					 });
}

function show(n) {
	if(!n) n=current_ind-1;
	var app = document.createElement('div');
	app.id = 'APPS_'+current_app[n];
	app.className = 'application';
	app.style.width = apps_width[n];
	document.body.appendChild(app);
	$('APPS_'+current_app[n]).style.zIndex = zIndMax+2;
}

function close_app() {
	if(!current_app[current_ind-1] || current_app[current_ind-1] == '') return;
	hide();
	$('APPS_BACKGROUND').style.zIndex -= 2;
	zIndMax -=2 ;
	if(current_ind == 1)
		hide_background();
	current_ind--;
	if(document.all) window.onscroll = null;
	else document.stopObserving('scroll', update_scroll);
	document.body.removeChild($('APPS_'+current_app[current_ind]));
	current_app[current_ind] = '';
}

function update_position() {
	if(!current_app[current_ind-1] || current_app[current_ind-1] == '') return;
	with($('APPS_'+current_app[current_ind-1])) {
		var scrolls = document.viewport.getScrollOffsets();
		if(document.viewport.getHeight()/2-clientHeight/2<50) {
			style.top = '50px';
		} else {
			style.top = (document.viewport.getHeight()/2-clientHeight/2+scrolls.top-20)+"px";
		}
		style.left = (document.viewport.getWidth()/2-clientWidth/2+scrolls.left)+"px";	
	}
}

function update_scroll(event) {
	var app = $('APPS_'+current_app[current_ind-1]);
	var scrolls = document.viewport.getScrollOffsets();
	$('APPS_BACKGROUND').style.top = (scrolls.top-200)+"px";
	if(app.style.top == '50px' || app.style.left=='0px') return;
	if(!current_app[current_ind-1] || current_app[current_ind-1] == '') return;
	if(document.viewport.getHeight()/2-app.clientHeight/2<50) {
		app.style.top = '50px';
	} else {
		app.style.top = (document.viewport.getHeight()/2-app.clientHeight/2+scrolls.top)+"px";
	}
}