
/*
 *	Scripts include
 *		- interactive elements
 *		- expanding paragraphs
 *		- sounds
 */

/* MAIN FUNCTIONS */

document.getElementsByClassName = function( cl ) {
	// By Stephen Chapman
	var retnode = [];
	var myclass = new RegExp( '\\b'+cl+'\\b' );
	var elem = this.getElementsByTagName( '*' );
	for ( var i = 0; i < elem.length; i++ ) {
		var classes = elem[ i ].className;
		if ( myclass.test( classes ) ) retnode.push( elem[ i ] );
	}
	return retnode;
}

function addEvent( elm, evType, fn, useCapture ) {
	// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
	// By Scott Andrew
	if ( elm.addEventListener ) {
		elm.addEventListener( evType, fn, useCapture );
		return true;
	} else if ( elm.attachEvent ) {
		var r = elm.attachEvent( 'on' + evType, fn );
		return r;
	} else {
		elm[ 'on' + evType ] = fn;
	}
}

function init( e ) {

	soundManager.createSound( { id: 'mouseoverSound', url: 'sounds/agogo1.mp3' } );
	soundManager.createSound( { id: 'clickSound', url: 'sounds/metalbu.mp3' } );

	/* ACTIVE ELEMENT :: EVENTS */

	addInteractiveElement( 'trumpet', true, true );
	addInteractiveElement( 'saxophone', true, true );
	addInteractiveElement( 'filer', true, true );
	addInteractiveElement( 'fakta', true, true );
	addInteractiveElement( 'spela', true, true );
	addInteractiveElement( 'boka', true, true );
	addInteractiveElement( 'radio', false, true ); /* is also an expandable -> click */
	addInteractiveElement( 'lock', false, true ); /* is also an expandable -> click */
	addInteractiveElement( 'lockout', true, true );
	addInteractiveElement( 'hem', true, false );
	addInteractiveElement( 'key', true, false );

	/* EXPAND PARAGRAPH ON HEADLINE-CLICK :: EVENTS */

	var length;
	if ( ( length = document.getElementsByClassName( 'expandable' ).length ) > 0 ) {

		var expandables = document.getElementsByClassName( 'expandable' );
		var buttons = document.getElementsByClassName( 'expandButton' );
		var spans = document.getElementsByClassName( 'expandSpan' );

		for ( var i = 0; i < length; i++ ) {

			buttons[ i ].id = 'expandable' + i;
			var buttonInteraction = new InteractiveElement( buttons[ i ].id );
			buttonInteraction.addEvent( true, false );

			var expandable = new Expandable( spans[ i ] );
			addEvent( buttons[ i ], 'click', expandable.toggleExpand, false );
		}
	}

	/* SOUND :: EVENTS */

	var link = document.getElementById( 'radio' );
	addEvent( link, 'click', radioClick, false );

	var link = document.getElementById( 'listen' );
	var ul = link.getElementsByTagName( 'ul' )[ 0 ];

	var li = ul.getElementsByTagName( 'li' );

	for ( var i = 0; i < li.length; i++ ) {

		soundManager.createSound( { id: li[ i ].id, url: 'sounds/' + li[ i ].id + '.mp3' } );
		playList.addSong( li[ i ].id );

		var song = new Song( li[ i ].id );
		addEvent( li[ i ], 'click', song.play, false );
	}

	playList.setCurrentSong( playList.songIdList[ 0 ] );

}

/* SOUND FUNCTIONS */

function PlayList() {

	this.visible = false;
	this.currentSongId;
	this.songIdList = new Array();
	this.playState = 0;

	this.length = function () {
		return this.songIdList.length;
	}
	this.getNextSong = function() {
		for ( i = 0; i < this.songIdList.length; i++ ) {
			if ( this.currentSongId == this.songIdList[ i ] )
				if ( i < this.songIdList.length-1 )
					return this.songIdList[ i+1 ];
				else
					return this.songIdList[ 0 ];
		}
	}
	this.playNextSong = function() {
		alert( "Kaboom!" );
		var next = this.getNextSong();
		this.setCurrentSong( next );
		this.playCurrentSong();
	}
	this.setCurrentSong = function ( songId ) {

		if ( this.currentSongId != null ) {
			soundManager.stop( this.currentSongId );
			var element = document.getElementById( this.currentSongId );
			element.style.color = 'white';
			element.style.cursor = 'pointer';
		}

		this.currentSongId = songId;

		if ( this.currentSongId != null ) {
			var element = document.getElementById( this.currentSongId );
			element.style.color = 'green';
			element.style.cursor = 'default';
		}

	}
	this.playCurrentSong = function() {
		this.playState = 1;
		soundManager.play( this.currentSongId, {onfinish: this.playNextSong} );
	}
	this.stopCurrentSong = function() {
		this.playState = 0;
		if ( this.currentSongId != null ) {
			soundManager.stop( this.currentSongId );
		}
	}
	this.addSong = function( songId ) {
		this.songIdList.push( songId );
	}
}

function Song( elementId ) {

	var songId = elementId;

	this.play = function() {

		if ( playList.currentSongId != songId ) {

			playList.setCurrentSong( songId );
			playList.playCurrentSong();
		}
	}
}

/* Clicking the radio img */
function radioClick( e ) {

	if ( playList.visible ) {
		playList.stopCurrentSong();
		playList.visible = false;
	}
	else {
		playList.playCurrentSong();
		playList.visible = true;
	}
}

/* EXPAND PARAGRAPH ON HEADLINE-CLICK */

function Expandable( span ) {

	span.style.display = 'none';

	this.toggleExpand = function () {

		if ( span.style.display == 'none' )
			span.style.display = 'inline';
		else
			span.style.display = 'none';

		//document.forms.login.username.focus();
		//document.forms.login.username.select();
		// expandables does not have ids: focus any form?

	}
}

/* INTERACTIVE ELEMENTS */

/*
 *	Interactive element is clickable, sounds and/or creates a hovertext
 */
function addInteractiveElement( elementId, isSound, isHover ) {

	var element;
	if ( document.getElementById( elementId ) ) {
		element = new InteractiveElement( elementId );
		element.addEvent( isSound, isHover );
	}
}
function InteractiveElement( elementId ) {

	this.interactiveElement = document.getElementById( elementId );
	var imageElement = this.interactiveElement.getElementsByTagName( 'span' )[ 0 ];

	this.hoverOver = function( e ) {
		imageElement.style.display = 'inline';
	}
	this.playClick = function( e ) {
		soundManager.play( 'clickSound', { volume: 6 } );
	}
	this.playOver = function( e ) {
		soundManager.play( 'mouseoverSound', { volume: 3 } );
	}
	this.playHoverOver = function( e ) {
		soundManager.play( 'mouseoverSound', { volume: 3 } );
		imageElement.style.display = 'inline';
	}
	this.hoverOut = function( e ) {
		imageElement.style.display = 'none';
	}
	this.addEvent = function ( isSound, isHover ) {
		if ( isSound && isHover ) {
			addEvent( this.interactiveElement, 'mouseover', this.playHoverOver, false );
			addEvent( this.interactiveElement, 'mouseout', this.hoverOut, false );
		}
		else {
			if ( isSound )
				addEvent( this.interactiveElement, 'mouseover', this.playOver, false );
			if ( isHover ) {
				addEvent( this.interactiveElement, 'mouseover', this.hoverOver, false );
				addEvent( this.interactiveElement, 'mouseout', this.hoverOut, false );
			}
		}
		addEvent( this.interactiveElement, 'click', this.playClick, false );
	}
}

/* MAIN */

//addEvent( window, 'load', init, false ); // Moved to soundManager.onload...

var playList = new PlayList();

/* SOUND INIT */

soundManager.url = 'script/soundmanager2.swf'; // override default SWF url
soundManager.debugMode = false;

soundManager.onload = function() {
	// Placing init() here is a workaround to make soundManager initialize before my stuff...
	init();
}

