/*
WAV Player
Interface helper logic

Karl Glasgow
code@keg4.com

Copyright 2009, All Rights Reserved.
*/


var objActiveSound = null;
function PlaySound(strWaveUrl) 
{
    var objBody = document.getElementsByTagName('body')[0];
    
    if (document.all) 
    {
        var objBgSound = document.getElementsByTagName('bgsound')[0];
        if (!objBgSound) 
        {
            objBgSound = document.createElement('bgsound');
            objBody.appendChild(objBgSound);
        }
        objBgSound.src = strWaveUrl;
        
        objActiveSound = objBgSound;
        return true;
    } 

    if (objActiveSound != null) 
    {
        objBody.removeChild(objActiveSound);
    }
    
    var objObject = document.createElement('object');
    
    var objParam = document.createElement('param');
    objParam.name = 'src';
    objParam.value = strWaveUrl;
    objObject.appendChild(objParam);
    
    var objParam = document.createElement('param');
    objParam.name = 'autostart';
    objParam.value = 'true';
    objObject.appendChild(objParam);
    
    var objParam = document.createElement('param');
    objParam.name = 'autoplay';
    objParam.value = 'true';
    objObject.appendChild(objParam);
    
    var objParam = document.createElement('param');
    objParam.name = 'controller';
    objParam.value = 'true';
    objObject.appendChild(objParam);
    
    var objEmbed = document.createElement('embed');
    objEmbed.src = strWaveUrl;
    objEmbed.autostart = 'true';
    objEmbed.autoplay = 'true';
    objEmbed.controller = 'true';
    objEmbed.loop = 'false';
    
    HideSoundObject(objObject);
    HideSoundObject(objEmbed);
    
    objObject.appendChild(objEmbed);
    objBody.appendChild(objObject);
    
    objActiveSound = objObject;
    return true;
}

function HideSoundObject(objTarget) 
{
    objTarget.style.position = 'absolute';
    objTarget.style.top = '0px';
    objTarget.style.left = '0px';
    objTarget.style.width = '0px';
    objTarget.style.height = '0px';
}

function StopSound() 
{
    if (document.all) 
    {
        if (document.getElementsByTagName('bgsound')[0]) 
        {
            document.getElementsByTagName('bgsound')[0].src = '';
            return true;
        }
        return false;
    } 

    if (objActiveSound != null) 
    {
        var objBody = document.getElementsByTagName('body')[0];
        objBody.removeChild(objActiveSound);

        objActiveSound = null;
        return true;
    }
    
    return false;
}

function PreLoadSounds(arySoundUrls) 
{
    if (!arySoundUrls || !arySoundUrls.length) 
    {
        return false;
    }

    for (s = 0; s < arySoundUrls.length; s++) 
    {
        var objBody = document.getElementsByTagName('body')[0];
        
        var objEmbed = document.createElement('embed');
        objEmbed.src = arySoundUrls[s];
        objEmbed.autostart = 'false';
        objEmbed.autoplay = 'false';
        objEmbed.hidden = 'true';
        
        HideSoundObject(objEmbed);
        
        objBody.appendChild(objEmbed);
    }
}
