var soundReady = false;

soundManager.url = 'swf/';
soundManager.debugMode = false;
soundManager.onload = function() {
    soundReady = true;
    loadSounds();
}

var data = new Object();
var dataPopulated = false;

function finishedPopulatingData()
{
    dataPopulated = true;
    loadSounds();
}

function loadSounds()
{
    if( dataPopulated && soundReady ) {
        for( var id in data ) {
            soundManager.createSound(
                { id: id, url: data[ id ].url, volume: 30 } );
        }
    }
}

function getTarget( e )
{
    var t;
    if( e.target ) {
        t = e.target;
    }
    else if( e.srcElement ) {
        t = e.srcElement;
    }
    if( t.nodeType == 3 ) {
        t = t.parentNode;
    }

    return t;
}

function getRelatedTarget( e )
{
    if( e == null) {
        var e = window.event;
    }

    if( e.relatedTarget != null) {
        return e.relatedTarget;
    }
    else if( e.type == 'mouseover' ) {
        if( e.fromElement != null) {
            return e.fromElement;
        }
    }
    else if( e.type == 'mouseout' ) {
        if( e.toElement != null) {
            return e.toElement;
        }
    }

    return null;
}

function getParentElemWithId( e, id )
{
    for( var t = getTarget( e ); t != null; t = t.parentNode ) {
        if( t.id && ( t.id.slice( t.id.lastIndexOf( ':' ) + 1 ) ==  id ) ) {
            return t;
        }
    }
    return null;
}

function isChildOfOrEqual( parent, child )
{
    while( child && child != parent ) {
        child = child.parentNode;
    }
    return child == parent;
}

var intervalId;
var popup;
var songElem;
var songId;
var popupVisible = false;
var popupFixed = false;
var prefix;
var poppedSongId = 0;

function showPopup( e, soundId, forcePlay )
{
    var progBar = document.getElementById( prefix + 'progressBar' );
    progBar.component.setValue( 1 );

    if( autoPlayOn || forcePlay ) {
        resumeSong( e );
    }
    else {
        pauseSong( e );
    }

    popup = document.getElementById( prefix + 'popup' );
    popup.component.show(e);

    var song = soundManager.getSoundById( soundId );
    progBar.component.setValue( 1 );

    intervalId = setInterval( function() {
            var dur;
            if( song.readyState == 3 ) {
                dur = song.duration;
            }
            else {
                dur = song.durationEstimate;
            }

            var prog = Math.floor( ( song.position / dur ) * 100 );
            if( prog < 1 ) {
                prog = 1;
            }
            progBar.component.setValue( prog ); }, 5000 );
    popupVisible = true;
}

function hidePopup( e )
{
    clearInterval( intervalId );
    popup.component.hide(e);
    popupVisible = false;
    popupFixed = false;
}

function handleBodyOnload( e )
{
    if( poppedSongId != 0 ) {
        prefix = poppedElemId.slice( 0, poppedElemId.lastIndexOf( ':' ) + 1 );
        if( document.createEventObject ) {
            e = document.createEventObject( 'MouseEvents' );
            e.srcElement = document.getElementById( prefix + 'song' );
            e.pageX = 200;
            e.pageY = 200;
        }
        songElem = poppedElemId;
        playSong( e, poppedSongId, false );
        popupFixed = true;
    }
}


function handleSongOnClick( e, id, songElemId )
{
    if( !e ) {
        var e = window.event;
    }

    prefix = songElemId.slice( 0, songElemId.lastIndexOf( ':' ) + 1 );

    var clickedSong = document.getElementById( prefix + 'song' );
    var clickedPopup = document.getElementById( prefix + 'popup' );

    if( popupFixed ) {
        if( clickedSong == songElem ) {
            if( clickedPopup == popup ) {
                return false;
            }
            else {
                stopPlaying( e );
            }
        }
        else {
            stopPlaying( e );
            songElem = clickedSong;
            playSong( e, id, true );
            popupFixed = true;
        }
    }
    else if( popupVisible ) { // not fixed, visible
        if( clickedSong == songElem ) { // clicked current song
            popupFixed = true;
            if( !autoPlayOn ) {
                resumeSong( e );
            }
        }
        else { // clicked different song than current
            stopPlaying( e );
        }
    }
    else { // not fixed, not visible
        stopPlaying( e );
    }

    return false;
}

function handleSongMouseOver( e, soundId, songElemId )
{
    if( popupFixed ) {
        return;
    }

    prefix = songElemId.slice( 0, songElemId.lastIndexOf( ':' ) + 1 );


    var to = getRelatedTarget( e );

    if( !e ) {
        var e = window.event;
    }

    var parent = getParentElemWithId( e, 'song' );
    if( popupVisible ) {
        if( parent == songElem ) {
            return;
        }
        else {
            stopPlaying( e );
        }
    }

    songElem = parent;
    playSong( e, soundId, false );
}

function handleSongMouseOut(e, songElemId)
{
    if( popupFixed ) {
        return;
    }
    prefix = songElemId.slice( 0, songElemId.lastIndexOf( ':' ) + 1 );

    var to = getRelatedTarget( e );
    if( popupVisible ) {
        if(  isChildOfOrEqual( popup, to ) || isChildOfOrEqual( songElem, to ) ) {
            return;
        }
        stopPlaying( e );
    }
}

function handlePopupMouseOut(e, songElemId, popupElemId )
{
    if( popupFixed ) {
        return;
    }
    prefix = songElemId.slice( 0, songElemId.lastIndexOf( ':' ) + 1 );
    if( !e ) {
        var e = window.event;
    }
    var to = getRelatedTarget( e );
    if( popupVisible ) {
        if( isChildOfOrEqual( document.getElementById( popupElemId ), to ) || isChildOfOrEqual( document.getElementById( songElemId ), to )) {
            return;
        }
        stopPlaying( e );
    }
}


var isPaused = false;
function pauseSong( e )
{
    if( soundReady ) {
        soundManager.pause( songId );
    }
    document.getElementById( prefix + 'playToggle' ).src = 'img/play.gif';
    isPaused = true;
    clearPlayTimeout();
}

function resumeSong( e )
{
    if( soundReady ) {
        if( soundManager.sounds[ songId ].playState == 0 ) {
            soundManager.play( songId );
        }
        else {
            soundManager.resume( songId );
        }
        document.getElementById( prefix + 'playToggle' ).src = 'img/pause.gif';
        isPaused = false;
        startPlayTimeout( songId );
    }
    else {
        document.getElementById( prefix + 'playToggle' ).src = 'img/play.gif';
        isPaused = true;
    }
}

function handlePlayToggleClick( e, buttonId )
{
    prefix = buttonId.slice( 0, buttonId.lastIndexOf( ':' ) + 1 );
    if( isPaused ) {
        resumeSong( e );
    }
    else {
        pauseSong( e );
    }
    return false;
}


function playSong( e, id, forcePlay )
{
    if( soundReady ) {
        songId = id;
        if( autoPlayOn || forcePlay ) {
            soundManager.play( id );
        }
        showPopup( e, id, forcePlay );
    }
    else {
        setTimeout( function() { playSong( e, id, forcePlay ); }, 1000 );
    }
}

function stopPlaying( e )
{
    if( soundReady ) {
        if( songId ) {
            soundManager.pause( songId );
            soundManager.setPosition( songId, 0 );
            soundManager.stop( songId );
        }
        soundManager.stopAll();
        hidePopup( e );
        clearPlayTimeout();
    }
}

var playTimeoutId;
function startPlayTimeout( id )
{
    playTimeoutId = window.setTimeout( function() {
        songIdWasPlayed = id;
        Seam.Component.getInstance( "songListAction" ).songWasPlayed( id, doNothingCallback );
    }, 10000 );
}

// Don't display loading indicator for seam remoting calls (times played)
Seam.Remoting.displayLoadingMessage = function() {};
Seam.Remoting.hideLoadingMessage = function() {};

function clearPlayTimeout()
{
    window.clearTimeout( playTimeoutId );
}

var autoPlayOn = true;
function setAutoPlay( autoPlay ) {
    autoPlayOn = autoPlay;
}

function doNothingCallback()
{
}

function showBusy()
{
    document.getElementById( "busy" ).style.visibility = "visible";
    document.getElementById( "coverControls" ).style.visibility = "visible";
    return true;
}

function showHelp()
{
    document.getElementById( "help" ).style.visibility = "visible";
    return false;
}

function hideHelp()
{
    document.getElementById( "help" ).style.visibility = "hidden";
    return false;
}

