// <![CDATA[

var bubblesAngle = 0;

function selectLanguage( ) {
	var url = window.location.href;
	var selectedLanguage = document.getElementById("selector").value;
	
	if ( url.lastIndexOf("index.htm") != -1 ) {
		// found that we are in the index.html page...
		window.location.href = url.replace("index.htm", "index_" + selectedLanguage + ".htm");
		return true;
	}

	if ( url.lastIndexOf("_") != -1 ) {
		// normal case : we should have the language in the URL (ie, the suffix "_en.html", "_fr.html" or another language)
		var currentLanguage = url.substr( url.lastIndexOf("_") + 1, 2 );
		window.location.href = url.replace("_" + currentLanguage + ".html", "_" + selectedLanguage + ".html");
		return true;
	}

  return false; // no language suffix found in the URL
}



function init( ) {
	// disables the language selector
	
	
	// hide all messages except for the first one
	var messages = getNewsMessages( );
	if ( messages ) {
		for ( i=0 ; i < messages.length ; ++i ) {
			messages[i].id = String(i+1);
			
//			messages[i].style.visibility = "visible";
			if ( i == 0 )
				messages[i].style.visibility = "visible";
				//messages[i].show( );
			else
			messages[i].style.visibility = "hidden";
				//messages[i].hide( );
		}
	}
	delete messages; 
	
	bubblesAngle = 0;
	
	// start the periodical executor that will call
	// the showNextNews() periodically
	var periode = new PeriodicalExecuter( showNextNews, 7 );
	
	var bubblesPeriode = new PeriodicalExecuter( rollBubbles, 1 );
}

// Note
// You must delete the messages Array object
// returned when you no longer need it
function getNewsMessages( ) {
	var newsContainer = document.getElementById( "news" );
	var messages = new Array( );
	var messageIndex = 0;
	
	// parses all the news messages
	if ( newsContainer ) {
		for (i=0 ; i < newsContainer.childNodes.length ; ++i ) {
			if ( newsContainer.childNodes[i].className == "message" )
				messages[messageIndex++] = newsContainer.childNodes[i];
		}
	}
	
	return messages;
}

function showNextNews( ) {  
	var messages = getNewsMessages( );
	
	// if there are several messages...
	if ( messages && messages.length > 1 ) {
		// get the first visible message, then hide it and shox the next one (if any, else the first one)
		var currentMessage;
		var nextMessage; 
		for ( i=0 ; i < messages.length ; ++i ) {
			if ( messages[i].style.visibility == "visible" ) {
				currentMessage = messages[i];
				nextMessage = messages[i+1 >= messages.length ? 0 : i+1];
				break;
			}
		}

		if ( currentMessage && nextMessage ) {
			currentMessage.style.visibility = "hidden";
			nextMessage.style.visibility = "visible";
			//Effect.Fade( currentMessage.id, { duration: 0.5, delay: 0.0, from: 0.99, to: 0.0 } );
			//Effect.Appear( nextMessage.id, { duration: 1.0, delay: 0.0, from: 0.0, to: 0.99 } );
		}
	}
	
	delete messages;
}

// Note
// You must delete the bubbles Array object
// returned when you no longer need it
function getBubbles( ) {
	var bubblesContainer = document.getElementById( "bubbles" );
	var bubbles = new Array( );
	var bubbleIndex = 0;
	
	// parses all the bubbles
	if ( bubblesContainer ) {
		for (i=0 ; i < bubblesContainer.childNodes.length ; ++i ) {
			if ( bubblesContainer.childNodes[i].className == "bubble" )
				bubbles[bubbleIndex++] = bubblesContainer.childNodes[i];
		}
	}
	
	return bubbles;
}


function rollBubbles( ) {
	var bubbles = getBubbles( );
	var angle;
	var dx, dy;
	
	// if there is at least one bubble...
	if ( bubbles && bubbles.length ) {
		for ( i=0 ; i < bubbles.length ; ++i ) {
			angle = bubblesAngle;
			dx = Math.floor( Math.cos(angle)*40 );
			dy = Math.floor( Math.sin(angle)*20 );
			bubbles[i].style.left = String(dx) + "px";
			bubbles[i].style.top = String(dy) + "px";
		}
		bubblesAngle = bubblesAngle + 0.1;
	}
	
	delete bubbles;
}

// ]]>
