
/****** GENERAL FUNCTIONS *******/

function scrollBox(boxId, scrollUp)
{
	var distance = scrollUp ? -220 : 220;
	var element = document.getElementById(boxId);
	
	var myAnim = new YAHOO.util.Scroll(element, {scroll: { by: [0, distance] }}, 0.3);	
	myAnim.animate();
	
	if ( !window.scrollPages ) window.scrollPages = new Array();
	if ( !window.scrollPages[boxId] ) window.scrollPages[boxId] = 1;
	
	var positionImage = document.getElementById(boxId + window.scrollPages[boxId]);
	positionImage.src = '/site_images/box.png';
	
	if ( scrollUp ) {
		window.scrollPages[boxId] = Math.max(1, window.scrollPages[boxId] - 1);
	}
	else {
		window.scrollPages[boxId] = Math.min(3, window.scrollPages[boxId] + 1);
	}
	
	var positionImage = document.getElementById(boxId + window.scrollPages[boxId]);
	positionImage.src = '/site_images/box_selected.png';
	
	return false;
}

function changeSort(selectElem)
{
	//alert(setQueryVariable('PrimarySort', selectElem.value));
	window.location = setQueryVariable('PrimarySort', selectElem.value);
}

/****** SONG PREVIEW FUNCTIONS *******/

var clickEventCount = 1;
var lastPlayedSong;
var mp3CurrentlyPlaying = false;

function loadSong(previewLinkElem)
{
	// Initialize the player, display the pause button.
	initializePlayer(previewLinkElem);
	
	// Fetch headers from preview URL to determine how to play the file.
	req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	req.open("HEAD", previewLinkElem.href, true);
	
	// Asynchronously handle playing the file.
	req.onreadystatechange = function() 
	{
		if (req.readyState==4) {
			
			// First check to see if the actual MP3 is being returned.
			if ( req.getResponseHeader("Content-Type") == 'audio/mpeg' ) {
				loadMp3Preview(previewLinkElem.href);
			}
			// If not, it means we're being redirected somewhere else.
			else if ( req.getResponseHeader("Content-Type") == 'text/html' ) {
				
				switch ( req.getResponseHeader("X-Preview-Type") ) {
					case 'MP3':
						loadMp3Preview(req.getResponseHeader("X-Preview-Location"));
						break;	
					case 'WMA':
						loadWmaPreview(req.getResponseHeader("X-Preview-Location"));
						break;			
				}
			}
		}
	};
	
	req.send(null);
	
	return false;
	
	///////////////////
	
	function initializePlayer(previewLinkElem)
	{
		// First reset the play button on the currently playing preview (if applicable).
		var previousLinkElem = document.getElementById('currentlyPlayingPreview');
		
		if ( previousLinkElem ) {
			previousLinkElem.id = '';
			previousLinkElem.innerHTML = '<img src="/site_images/buttons/play.gif" />';
		}
		
		// Next reset the hidden embedded WMA player.
		var container = document.getElementById('mediaPlayerContainer');
		container.innerHTML = '';
		
		// Set the button to pause if we started playing a new song.
		if ( previousLinkElem != previewLinkElem ) {
			previewLinkElem.id = 'currentlyPlayingPreview';
			previewLinkElem.innerHTML = '<img src="/site_images/buttons/pause.gif" />';
			
			pathToSong = previewLinkElem.href ? previewLinkElem.href : null;
		}
	}
	
	
	function loadMp3Preview(pathToPreview)
	{
		window.document.logo.SetVariable("pathToSong", pathToPreview);
    	window.document.logo.SetVariable("clickEvent", clickEventCount++);
    	
    	if ( lastPlayedSong != pathToPreview ) {
    		mp3CurrentlyPlaying = true;
    		lastPlayedSong = pathToPreview;
    	}
    	else {
    		mp3CurrentlyPlaying = false;
    	} 	
	}
	
	function loadWmaPreview(pathToPreview)
	{
		if ( mp3CurrentlyPlaying ) {
			window.document.logo.SetVariable("pathToSong", lastPlayedSong);
    		window.document.logo.SetVariable("clickEvent", clickEventCount++);
		}
		
		container = document.getElementById('mediaPlayerContainer');
		
		// Play the song
		if ( lastPlayedSong != pathToPreview ) {
			container.innerHTML = '<embed type="application/x-mplayer2" src="' + pathToPreview + '" name="MediaPlayer"></embed>';
			lastPlayedSong = pathToPreview;
		}
		// Stop playing the song.
		else {
			container.innerHTML = '';
			lastPlayedSong = '';
		}
	}
}

// Called by the flash player when the song finishes playing.
function swapImage() 
{
	prevElem = document.getElementById('currentlyPlayingPreview');
	
	if ( prevElem ) {
		prevElem.id = '';
		prevElem.innerHTML = '<img src="/site_images/buttons/play.gif" />';
	}
}


function stopPlaying()
{
	//alert('stopping');
	loadMp3Preview('/site_flash/stop.mp3', true);
	//window.document.logo.SetVariable('clickEvent', clickEventCount++);
	//alert(clickEventCount);
}
