var cur = '';
var platform = '';
var imgPreload = new Image();
var prelo = true;
var x = 0, y = 0, myWidth = 0, myHeight = 0;
	
function switchApp(app) {
	if(cur == app) {
		return resetState();
	}
	
	/* document.location = '' + location.hostname + '#' + app; */
	document.location = '#' + app;
	document.title = 'CIOFS/FP Emilia Romagna - ' + $(app + '_name').innerHTML;
	var cont = $('main');
	var aps = $('apps');
	var cont_width = cont.getStyle('width').toInt();
	
	// make sure the app list is slid over
	if(cur == '') {
		var list = $('applist');
		list.tween('margin-top', '800');
	} else {
		var capp = $(cur + '_info');
		capp.tween('top', '900');
		$(cur + '_screen').fade(0);
	}
	
	// loop thru the apps
	for(i = 0; i < apps.length; i++) {
		name = apps[i];
		var el = $(name + '_icon');
		var effect = new Fx.Morph(name + '_icon');
		
		if(app == name) {
			effect.start('.' + name + platform + 'Active');
			el.fade(1.0);
			
			// bring the app info out
			var appd = $(name + '_info');
			var width = 100;
			if(!$(name + '_map')) { var width = 0; }
			var width = appd.getStyle('width').toInt() + width;
			if(width != cont_width) {
				cont.tween('width', width);
				aps.tween('width', width);
			}
			
			appd.tween('top', '10');
			
			cur = name;
		} else {
			effect.start('.' + name + platform + 'Icon');
			el.fade(0.5);
		}
	}

}

function resetState() {
	/* document.location = 'http://' + location.hostname + '#'; */
	document.location = '#';
	document.title = 'CIOFS/FP Emilia Romagna';
	var cont = $('main');
	var aps = $('apps');
	var cont_width = cont.getStyle('width').toInt();
	
	var list = $('applist');
	list.tween('margin-top', '900');

	// loop thru the apps and hide them/reset the icons
	for(i = 0; i < apps.length; i++) {
		name = apps[i];
		var el = $(name + '_icon');
		var effect = new Fx.Morph(name + '_icon');
	
		var app = $('applist');
		var width = 20;
		var width = app.getStyle('width').toInt() + width;
		cont.tween('width', width);
		aps.tween('width', width);
		
		effect.start('.' + name + platform + 'Icon');
		el.fade(1.0)
		
		// bring the app info out
		var app = $(name + '_info');
		app.tween('top', '900');
		
		if($(name + '_screen')) {
			$(name + '_screen').fade(0);
		}
		
		if($(name + '_movie')) {
			var container = $(name + '_movie');
			container.innerHTML = '';
			playing = false;
		}
	}
	cur = '';
}

