/*
 * this code is copyright by MindContact :: Multimedia Creations | www.mindcontact.net
 */

/*------------------------------------------------------------------*/

var safeMode = false;

var fadeButtonAplha = 100;
var fadeButtonSpeed = 5;
var fadeInterfaceAplha = 100;
var fadeInterfaceSpeed = 25;

var scrollPos0 = 0;
var scrollPos1 = 580;
var scrollPos2 = 1220;
var scrollPos3 = 2000;

var flashObjects = Array();

var currentLevelId;
var currentInstrumentId = 1;
var currentTypeId = 0;

var ifData = Array();
var ifDataLoaded = false;

var imageStore = Array();

var loadStatus;

/*------------------------------------------------------------------*/

function bootstrap(safe)
{
	loadStatus = location.search == '?s=1' ? 1 : 0;
	safeMode = safe;
	fLoad('home', loadStatus);
	fLoad('character');
	loadImage('scd3', 'c/img/t-news.png');
	loadIframeContent('ifr1', 'news');
	loadImage('scd5', 'c/img/t-history.png');
	loadIframeContent('ifr2_0', 'history_0');
	loadIframeContent('ifr2_1', 'history_1');
	loadImage('scd7', 'c/img/t-contacts.png');
	loadIframeContent('ifr3', 'contacts');
}

function preloadImages()
{
	addImage('home', 'home.jpg', 955, 638);
}

function addImage(imageId, imageName, imageWidth, imageHeight)
{
	var i = new Image();
	i.id = imageId;
	i.name = imageId;
	i.src = 'c/img/' + imageName;
	i.width = imageWidth;
	i.height = imageHeight;
	
	imageStore[imageId] = i;
}

/*------------------------------------------------------------------*/

function manageButton(src)
{
	fadeOut(src.id, fadeButtonAplha, fadeButtonSpeed);

	switch (src.id)
	{
		case 'news':
		{
			loadImage('scd3', 'c/img/t-news.png');
			loadShowcase(1, src.id);
		}
			break;

		case 'bio':
		{
			loadImage('scd7', 'c/img/t-bio.png');
			loadShowcase(3, src.id);
		}
			break;

		case 'where':
			loadShowcase(0, src.id);
			break;

		case 'philosophy':
		{
			loadImage('scd7', 'c/img/t-philosophy.png');
			loadShowcase(3, src.id);
		}
			break;

		case 'aphorisms':
		{
			loadImage('scd7', 'c/img/t-aphorisms.png');
			loadShowcase(3, src.id);
		}
			break;

		case 'modules':
		{
			loadImage('scd5', 'c/img/t-modules.png');
			loadShowcase(2, src.id);
		}
			break;

		case 'cv':
		{
			loadImage('scd7', 'c/img/t-cv.png');
			loadShowcase(3, src.id);
		}
			break;

		case 'school':
		{
			loadImage('scd7', 'c/img/t-school.png');
			loadShowcase(3, src.id);
		}
			break;

		case 'history':
		{
			loadImage('scd5', 'c/img/t-history.png');
			loadShowcase(2, src.id);
		}
			break;

		case 'contacts':
		{
			loadImage('scd7', 'c/img/t-contacts.png');
			loadShowcase(3, src.id);
		}
			break;

		case 'credits':
		{
			loadImage('scd7', 'c/img/t-credits.png');
			loadShowcase(3, src.id);
		}
			break;
			
		case 'help':
		{
			loadImage('scd5', 'c/img/t-help.png');
			loadShowcase(2, src.id);
		}
			break;

		case 'scd0':
			document.location.href = 'mailto:info@suzukicenter.it';
			break;

		case 'scd1':
		{
			gMapUrl = 'http://maps.google.it/maps?f=q&hl=it&geocode=&q=via+cagliari+43+torino&sll=41.442726,12.392578&sspn=9.748257,22.675781&ie=UTF8&t=h&z=16&iwloc=addr';
			gMap = window.open(gMapUrl, 'gMap', 'width=800,height=600,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
		}
			break;

		case 'scd2':
		case 'scd4':
		case 'scd6':
		case 'scd8':
			unloadShowcase();
			break;

		case 'ic_close':
		case 'ic_closeLeft':
			unloadLevel();
			break;

		case 'ic_goBack':
			xoopsGetElementById('courses').scrollLeft = 0;
			break;

		case 'ic_lh0':
		case 'ic_lh1':
		case 'ic_lh2':
		case 'ic_lh3':
		case 'ic_oh0':
		case 'ic_oh1':
		case 'ic_oh2':
		case 'ic_oh3':
		{
			var t = src.id.substring(3, 4);
			var l = src.id.substring(5, 6);
			currentLevelId = l;
			currentTypeId = t == 'l' ? 0 : 1;
			updateData();
		}
			break;
	}
}

function mOver(src)
{
	fadeIn(src.id, fadeButtonAplha, fadeButtonSpeed, 0);
}

function mOut(src)
{
	fadeOut(src.id, fadeButtonAplha, fadeButtonSpeed);
}

/*------------------------------------------------------------------
 * Flash management
 */

function animateCharacter(data)
{
	flashMe('f_character', 'animCharacter', data);
}

function flashMe(targetId, name, value)
{
	eval("xoopsGetElementById('" + targetId + "').SetVariable('" + name + "', '" + value + "');");
}

function goLevel(levelId)
{
	currentInstrumentId = 1;

	if (!ifDataLoaded)
		loadIfData();

	loadLevel(levelId);
}

function declareFO(afId, fId, fUrl, fWidth, fHeight, fParams)
{
	sfo = "flashObjects['" + afId + "'] = { movie:\"" + fUrl +  "\", width:\"";
	sfo+= fWidth +  "\", height:\"" + fHeight +  "\", majorversion:\"8\", ";
	sfo+= "build:\"0\", id:\"" + fId +  "\", name:\"" + fId +  "\", ";
	sfo+= "swLiveConnect:\"true\", allowScriptAccess:\"sameDomain\"" + fParams + " };";

	eval(sfo);
}

function fLoad(foId) { fLoad(foId, null); }
function fLoad(foId, fParams)
{
	/*
	 * flash obj:
	 * lavagna
	 * personaggio
	 * 4 menu bottoni
	 * n video player
	 */
	
	fId = 'f_' + foId;
	containerId = 'c_' + fId;

	if (fParams != null)
		afId = fId + '_' + fParams;
	else
		afId = fId;

	if (flashObjects[afId] == null)
		switch (foId)
		{
			case 'home':
				declareFO(afId, fId, 'c/flash/home.swf?loadStatus=' + fParams, '337', '390', '');
				break;
				
			case 'character':
				declareFO(afId, fId, 'c/flash/character.swf', '250', '616', ', wmode:\"transparent\"');
				break;

			case 'menu':
				declareFO(afId, fId, 'c/flash/menu.swf?level='+ fParams, '200', '600', ', wmode:\"transparent\"');
				break;

			case 'vp':
				flashObjects[afId] = { 	movie:"c/flash/vp.swf", 
										width:"320", 
										height:"256", 
										majorversion:"8", 
										build:"0", 
										id:fId, 
										name:fId,
										swLiveConnect:"true",
										scale:"noscale",
										salign:"lt",
										wmode:"transparent",
										flashvars:"&MM_ComponentVersion=1&skinName=c/flash/Corona_Skin_3&streamName=../video/" + fParams + "&autoPlay=false&autoRewind=true" 
									};
				break;
		}

		UFO.create(flashObjects[afId], containerId);
}

function fUnload(foId)
{
	fId = 'f_' + foId;
	containerId = 'c_' + fId;
	
	setHidden(containerId);
}

/*------------------------------------------------------------------*/

function loadLevel(levelId)
{
	currentLevelId = levelId;

	fUnload('home');
	fUnload('character');
	setLayerVisibility('c_interface', true, fadeInterfaceAplha, fadeInterfaceSpeed);
	setVisible('courses');
	setVisible('ic_levelContainer');

	updateData();
}

function unloadLevel()
{
	if (!safeMode)
	{
		fUnload('menu');
		setHidden('ic_levelContainer');
		setHidden('courses');
		xoopsGetElementById('courses').scrollLeft = 0;
		setLayerVisibility('c_interface', false, fadeInterfaceAplha, fadeInterfaceSpeed);
		fLoad('character');
		fLoad('home', 1);
	}
	else
	{
		if (loadStatus)
			window.location.reload();
		else
			window.location.href += '?s=1';
	}
}

function updateData()
{
	// cambio sfondo ed elementi interfaccia
	loadImage('ic_levelContainer', 'c/img/level' + currentLevelId + '.jpg');
	loadImage('ic_close', 'c/img/hi-l' + currentLevelId + '-close.png');
	loadImage('ic_closeLeft', 'c/img/hi-l' + currentLevelId + '-close.png');
	loadImage('ic_goBack', 'c/img/hi-l' + currentLevelId + '-back.png');

	// carico menu
	fLoad('menu', currentLevelId);

	// carico video
	loadVideo(currentLevelId, currentInstrumentId, currentTypeId);

	// bottoni livelli e orchestre
	var hasOrchestra = false;

	for (i = 0; i < 4; i++)
	{
		if (ifData[currentInstrumentId]['level'][i] != null)
		{
			setVisible('ic_l' + i);
			setVisible('ic_lh' + i);
		}
		else
		{
			setHidden('ic_l' + i);
			setHidden('ic_lh' + i);
		}

		if (ifData[currentInstrumentId]['orchestra'][i] != null)
		{
			setVisible('ic_o' + i);
			setVisible('ic_oh' + i);
			hasOrchestra = true;
		}
		else
		{
			setHidden('ic_o' + i);
			setHidden('ic_oh' + i);
		}
	}

	if (hasOrchestra)
		setVisible('ic_orchestras');
	else
		setHidden('ic_orchestras');

	// carico metadati del corso
	var noVideo = 'video momentaneamente non disponibile';
	var instrumentName = ifData[currentInstrumentId]['name'];
	var levelName = '';
	var levelAge = ifData[currentInstrumentId]['level'][currentLevelId]['age'];
	var videoDesc = '';
	var boxContent = '';
	var teachers = 'insegnanti:<br />' + ifData[currentInstrumentId]['level'][currentLevelId]['teachers'];

	if (currentTypeId == 0)
	{
		levelName = ifData[currentInstrumentId]['level'][currentLevelId]['name']; 
		
		videoDesc = (ifData[currentInstrumentId]['level'][currentLevelId]['desc'] != null) ?
					ifData[currentInstrumentId]['level'][currentLevelId]['desc'] : noVideo;
	}
	else
	{
		levelName = ifData[currentInstrumentId]['orchestra'][currentLevelId]['name']; 
		videoDesc = (ifData[currentInstrumentId]['orchestra'][currentLevelId]['desc'] != null) ?
					ifData[currentInstrumentId]['orchestra'][currentLevelId]['desc'] : noVideo;
	}


	// nome corso
	setText('ic_titleInstrument', instrumentName);

	// nome livello
	setText('ic_titleLevel', levelName + '<br />' + levelAge);

	// insegnanti
	setText('ic_teachers', teachers);

	// descrizione video
	boxContent = '<div class=\"ifdinstr\">' + instrumentName + '</div>';
	boxContent += '<div class=\"ifdteachers\">' + teachers + '</div><br /><br />';
	boxContent += '<div class=\"ifdlevel\">' + levelName + '</div>';
	if (currentTypeId == 0)
		boxContent += '<div class=\"ifdage\">' + levelAge + '</div>';
	boxContent += '<br /><br /><div class=\"ifddesc\">' + videoDesc + '</div>';

	setText('ic_desc',	boxContent);
}

function loadIfData()
{
// Ritmica
	ifData[0] = createInstrumentData('Ritmica');
	ifData[0]['level'][0] = createLevelData('Children\'s Music Laboratory ', '', 'bambini dai 3 ai 10 anni', 'Stefania Maio, Silvia Rao');
	ifData[0]['level'][1] = createLevelData('Corsi Superiori di Teoria e Solfeggio e Pianoforte Complementare', null, 'ragazzi dagli 11 ai 18 anni', 'Natascia Kotsioubinskaia');
	ifData[0]['level'][2] = null;
	ifData[0]['level'][3] = null;
	ifData[0]['orchestra'][0] = null;
	ifData[0]['orchestra'][1] = null;
	ifData[0]['orchestra'][2] = null;
	ifData[0]['orchestra'][3] = null;

// Violino
	ifData[1] = createInstrumentData('Violino');
	ifData[1]['level'][0] = createLevelData('livello base', 'federica, 5 anni', 'bambini dai 3 ai 6 anni', 'Lee Robert, Federica Biribicchi');
	ifData[1]['level'][1] = createLevelData('livello intermedio', 'tommaso, 7 anni', 'bambini dai 6 ai 9 anni', 'Lee Robert, Federica Biribicchi');
	ifData[1]['level'][2] = createLevelData('livello avanzato', 'marco, 10 anni', 'bambini dai 10 ai 14 anni', 'Lee Robert, Federica Biribicchi');
	ifData[1]['level'][3] = createLevelData('post-diploma Suzuki', 'anais, 14 anni', 'ragazzi dai 14 ai 18 anni', 'Liana Mosca');
	ifData[1]['orchestra'][0] = createOrchestraData('Orchestra Twinkle twinkle', '');
	ifData[1]['orchestra'][1] = createOrchestraData('Orchestra Humoresque', '');
	ifData[1]['orchestra'][2] = createOrchestraData('Orchestra Suzuki', '');
	ifData[1]['orchestra'][3] = null;

// Violoncello
	ifData[2] = createInstrumentData('Violoncello');
	ifData[2]['level'][0] = createLevelData('livello base', 'maria, 5 anni', 'bambini dai 3 ai 6 anni', 'Antonio Mosca, Liviana Pittau');
	ifData[2]['level'][1] = createLevelData('livello intermedio', 'filippo, 7 anni', 'bambini dai 6 ai 9 anni', 'Antonio Mosca, Liviana Pittau');
	ifData[2]['level'][2] = createLevelData('livello avanzato', 'alessandro, 11 anni', 'bambini dai 10 ai 14 anni', 'Antonio Mosca, Liviana Pittau');
	ifData[2]['level'][3] = createLevelData('post-diploma Suzuki', 'lorenzo, 14 anni', 'ragazzi dai 14 ai 18 anni', 'Marco Mosca');
	ifData[2]['orchestra'][0] = createOrchestraData('Orchestra Twinkle twinkle', '');
	ifData[2]['orchestra'][1] = createOrchestraData('Orchestra Humoresque', '');
	ifData[2]['orchestra'][2] = createOrchestraData('Orchestra Suzuki', '');
	ifData[2]['orchestra'][3] = null;

// Contrabbasso
	ifData[3] = createInstrumentData('Contrabbasso');
	ifData[3]['level'][0] = createLevelData('livello base', null, 'bambini dai 3 ai 6 anni', 'Antonello Peretto');
	ifData[3]['level'][1] = null;
	ifData[3]['level'][2] = null;
	ifData[3]['level'][3] = null;
	ifData[3]['orchestra'][0] = null;
	ifData[3]['orchestra'][1] = null;
	ifData[3]['orchestra'][2] = null;
	ifData[3]['orchestra'][3] = null;

// Arpa
	ifData[4] = createInstrumentData('Arpa');
	ifData[4]['level'][0] = createLevelData('livello base', 'cecilia, 6 anni', 'bambini dai 3 ai 6 anni', 'Gabriella Bosio');
	ifData[4]['level'][1] = createLevelData('livello intermedio', 'giorgia, 10 anni', 'bambini dai 6 ai 9 anni', 'Gabriella Bosio');
	ifData[4]['level'][2] = createLevelData('livello avanzato', 'stefania, 13 anni', 'bambini dai 10 ai 14 anni', 'Gabriella Bosio');
	ifData[4]['level'][3] = null;
	ifData[4]['orchestra'][0] = createOrchestraData('Orchestra Trilli e glissče', '');
	ifData[4]['orchestra'][1] = createOrchestraData('Orchestra Trilli e glissče', '');
	ifData[4]['orchestra'][2] = createOrchestraData('Orchestra Trilli e glissče', null);
	ifData[4]['orchestra'][3] = null;

// Mandolino
	ifData[5] = createInstrumentData('Mandolino');
	ifData[5]['level'][0] = createLevelData('livello base', 'danfo sin&egrave;, 6 anni', 'bambini dai 3 ai 6 anni', 'Elena Parasacco');
	ifData[5]['level'][1] = createLevelData('livello intermedio', 'margherita, 8 anni', 'bambini dai 6 ai 9 anni', 'Elena Parasacco');
	ifData[5]['level'][2] = createLevelData('livello avanzato', 'giulia, 16 anni', 'bambini dai 10 ai 14 anni', 'Amelia Saracco');
	ifData[5]['level'][3] = null;
	ifData[5]['orchestra'][0] = createOrchestraData('Orchestra Ti Pizzico', '');
	ifData[5]['orchestra'][1] = null;
	ifData[5]['orchestra'][2] = null;
	ifData[5]['orchestra'][3] = null;

// Chitarra
	ifData[6] = createInstrumentData('Chitarra');
	ifData[6]['level'][0] = createLevelData('livello base', null, 'bambini dai 3 ai 6 anni', 'Francesca Galvagno');
	ifData[6]['level'][1] = createLevelData('livello intermedio', null, 'bambini dai 6 ai 9 anni', 'Francesca Galvagno');
	ifData[6]['level'][2] = createLevelData('livello avanzato', null, 'bambini dai 10 ai 14 anni', 'Francesca Galvagno');
	ifData[6]['level'][3] = null;
	ifData[6]['orchestra'][0] = createOrchestraData('Green Sleeves Guitar Orchestra', '');
	ifData[6]['orchestra'][1] = null
	ifData[6]['orchestra'][2] = null
	ifData[6]['orchestra'][3] = null;

// Pianoforte
	ifData[7] = createInstrumentData('Pianoforte');
	ifData[7]['level'][0] = createLevelData('livello base', null, 'bambini dai 3 ai 6 anni', 'Stefania Maio<br />Laura Combatti');
	ifData[7]['level'][1] = createLevelData('livello intermedio', null, 'bambini dai 6 ai 9 anni', 'Stefania Maio<br />Laura Combatti');
	ifData[7]['level'][2] = createLevelData('livello avanzato', null, 'bambini dai 10 ai 14 anni', 'Stefania Maio<br />Laura Combatti');
	ifData[7]['level'][3] = null;
	ifData[7]['orchestra'][0] = createOrchestraData('Orchestra di pianoforti', null);
	ifData[7]['orchestra'][1] = createOrchestraData('Orchestra di pianoforti', null);
	ifData[7]['orchestra'][2] = createOrchestraData('Orchestra di pianoforti', null);
	ifData[7]['orchestra'][3] = null;

	ifDataLoaded = true;
}

function createInstrumentData(instrumentName)
{
	var instrumentData = Array();

	instrumentData['name'] = instrumentName;
	instrumentData['level'] = Array();
	instrumentData['orchestra'] = Array();

	return instrumentData;
}

function createLevelData(levelName, levelDesc, levelAge, levelTeachers)
{
	var levelData = Array();

	levelData['name'] = levelName;
	levelData['age'] = levelAge;
	levelData['teachers'] = levelTeachers;
	levelData['desc'] = levelDesc;

	return levelData;
}

function createOrchestraData(orchestraName, orchestraDesc)
{
	var orchestraData = Array();

	orchestraData['name'] = orchestraName;
	orchestraData['desc'] = orchestraDesc;

	return orchestraData;
}

/*------------------------------------------------------------------*/
function loadInstrument(instrumentId)
{
	currentInstrumentId = instrumentId;

	if (!ifDataLoaded)
		loadIfData();

	if (ifData[currentInstrumentId]['level'][currentLevelId] == null)
		currentLevelId = 0;

	updateData();
	xoopsGetElementById('courses').scrollLeft = 850;
}

function loadVideo(currentLevelId, currentInstrumentId, currentTypeId)
{
	var videoName = '';
	var currentType = currentTypeId == 0 ? 'level' : 'orchestra';

	if (!ifDataLoaded)
		loadIfData();

	if (ifData[currentInstrumentId][currentType][currentLevelId]['desc'] != null)
		videoName = currentInstrumentId + '.' + currentLevelId + '.' + currentTypeId;
	else
		videoName = 'no-video';

	fLoad('vp', videoName);
}

/*------------------------------------------------------------------*/

function loadShowcase(position, content)
{
	o = xoopsGetElementById('showcase');

	fUnload('home');
	setLayerVisibility('c_interface', true, fadeInterfaceAplha, fadeInterfaceSpeed);
	setVisible('showcase');
	eval('o.scrollLeft = scrollPos' + position + ';');
	
	setVisible('ifr1');
	setVisible('ifr2_0');
	setVisible('ifr2_1');
	setVisible('ifr3');

	if (content != null)
		if (position != 2)
			loadIframeContent('ifr' + position, content);
		else
		{
			loadIframeContent('ifr2_0', content + '_0');
			loadIframeContent('ifr2_1', content + '_1');
		}
}

function unloadShowcase()
{
	if (!safeMode)
	{
		setHidden('ifr1');
		setHidden('ifr2_0');
		setHidden('ifr2_1');
		setHidden('ifr3');
	
		setHidden('showcase');
		setLayerVisibility('c_interface', false, fadeInterfaceAplha, fadeInterfaceSpeed);
		fLoad('home', 1);
	}
	else
	{
		if (loadStatus)
			window.location.reload();
		else
			window.location.href += '?s=1';
	}
}

function loadIframeContent(targetId, contentName)
{
	eval(targetId + ".location.href = 'c/txt/" + contentName + ".html'");
}

/*------------------------------------------------------------------*/

function setLayerVisibility(targetId, value, alpha, speed)
{
	if (value)
	{
		setVisible(targetId);
		fadeIn(targetId, alpha, speed, 0);
	}
	else
	{
		fadeOut(targetId, alpha, speed);
		setHidden(targetId);
	}
}

function setText(targetId, text)
{
	xoopsGetElementById(targetId).innerHTML = text;
}

function message(text)
{
	alert(text);
}

/*
 * fade functions
 */

function fadeIn(targetId, alphaMax, speed, alphaMin)
{
	t = xoopsGetElementById(targetId);
	v = -1;
	try { v = t.filters.alpha.opacity; } catch(e) {}
	try { v = t.style.opacity; } catch(e) {}

	if (v != alphaMax)
	{
	    if(alphaMin < alphaMax)
		{
	        alphaMin += speed;
	        setFade(targetId, alphaMin);
	        setTimeout("fadeIn('" + targetId + "', " + alphaMax + ", " + speed + ", " + alphaMin + ")", 1);
	    }
	}
}

function fadeOut(targetId, alpha, speed)
{
	t = xoopsGetElementById(targetId);
	v = -1;
	try { v = t.filters.alpha.opacity; } catch(e) {}
	try { v = t.style.opacity; } catch(e) {}

	if (v != 0)
	{
	    if(alpha > 0)
		{
	        alpha -= speed;
			setFade(targetId, alpha);
	        setTimeout("fadeOut('" + targetId + "', " + alpha + ", " + speed + ")", 1);
	    }
	}
}

function setFade(targetId, alpha)
{
	t = xoopsGetElementById(targetId);
	try { t.filters.alpha.opacity = alpha; } catch(e) {}
	try { t.style.opacity = alpha/100; } catch(e) {}
}

function setVisible(id){
	xoopsGetElementById(id).style.visibility = "visible";
}

function setHidden(id){
	xoopsGetElementById(id).style.visibility = "hidden";
}

function loadImage(targetId, imageUrl)
{
	xoopsGetElementById(targetId).style.backgroundImage = 'url(' + imageUrl + ')';
}
