/*
    general purposes javascript functions for use throughout the phobic website 
*/

/*
    this function will create an array of images consisting of those with names beginning with namePrefix
*/

function loadImageList(namePrefix,numImages) {
    var imageList = new Array();
    var fullName = namePrefix + '.';
    
    for(i = 0; i < numImages; i++) {
        imageList[i] = new Image();
        imageList[i].src = fullName + i + '.png';
    }
    
    return imageList;
}

/*
 target = name of the span element containing the target
 imgList = array of images
 index of the new image
*/
function switchImage(target,imgList,ID) {
    // set the target span to the new image
    document[target].style.backgroundimage.src = imgList[ID];
    
    // reset all borders
    for(i = 1; i <= imgList.length; i++) {
        document[imgList[i]].style.border = 'none';
    }
    
    // now set the border on the newly-selected image
    document[imgList[ID]].style.border = '#999999 2px solid';
    
    // document[imgList[ID]].style.border = '#999999 2px solid';
}

/*
    preload all images
*/

var wallpaperList = new Array();
wallpaperList = loadImageList('wallpaper',1);

////////////////////////////////////////////////////////
// Audio Player ----------------------------------------
////////////////////////////////////////////////////////

var audioPlayer = null;

function pauseSong()
{
	if(audioPlayer)
	{
		audioPlayer.pause();
		var pauseButton = document.getElementById("pauseButton");
		if(pauseButton)pauseButton.innerHTML = '<a href="javascript:unpauseSong()">unpause</a>';
	}
}

function unpauseSong()
{
	if(audioPlayer)
	{
		audioPlayer.play();
		var pauseButton = document.getElementById("pauseButton");
		if(pauseButton)pauseButton.innerHTML = '<a href="javascript:pauseSong()">pause</a>';
	}
}

function stopSong()
{
	deleteAudioPlayer();
	
	var audioBox = document.getElementById("audiobox");
	if(audioBox)audioBox.parentNode.removeChild(audioBox);
}

function deleteAudioPlayer()
{
	if(!audioPlayer)return;

	//be really safe to ensure the song actually stops playing
	audioPlayer.pause();
	audioPlayer.setAttribute("src", "");
	delete audioPlayer;
	audioPlayer = null;
}

function showAudioError(desc)
{
	alert('\nThe audio cannot be played because ' + desc + "\n\nWe reccommend that you use a recent version of Firefox or Safari.\n\n");
}

function playSong(title, url_without_extension)
{
	//clear current audio player if needed
	deleteAudioPlayer();

	//check for support of audio tag
	if(typeof Audio == 'unknown')
	{
		showAudioError('your browser does not seem to support the HTML 5 <audio> tag.');
		return;
	}

	//create temp audio player for querying
	var tempAudioPlayer = new Audio('none');
	
	//check if creation worked
	if(!tempAudioPlayer)
	{
		showAudioError('your browser does not seem to support the HTML 5 <audio> tag.');
		return;
	}
	
	//check if type query is supported
	if(typeof tempAudioPlayer.canPlayType != 'function')
	{
		showAudioError('your browser does not seem to have full support the HTML 5 <audio> tag.');
		return;
	}
	
	//check codec support
	var extension = '';
	if(tempAudioPlayer.canPlayType('audio/ogg') != "")extension = '.oga';
	else if(tempAudioPlayer.canPlayType('audio/mpeg') != "")extension = '.mp3';
	if(extension == '')
	{
		showAudioError('your browser does not seem to support OGG or MP3 media types nativly.');
		return;
	}
	
	//create actual audio player
	audioPlayer = new Audio(url_without_extension + extension);
	if(!audioPlayer)
	{
		showAudioError('an error occurred creating the audio player.');
		return;
	}
	
	//start playing
	audioPlayer.play();

	//setup audio box
	
	var audioBox = document.getElementById("audiobox");
	
	var html = "Now playing: " + title + '<br/><br/>' +
		'<span id="pauseButton"><a href="javascript:pauseSong()">pause</a></span> | ' +
		'<a href="javascript:stopSong()">stop & close</a><br/><br/>';

	if(!audioBox)
	{
		audioBox = document.createElement('div');
		audioBox.setAttribute("id", "audiobox");
		audioBox.innerHTML = html;
		document.body.appendChild(audioBox);
	} else
	{
		audioBox.innerHTML = html;
	}
}