/*
 * Copyright 2005-2007 ProfessorGio.com
 */

var ie = (navigator.appName.indexOf('Microsoft') != -1);
var playing = false;
var soundBubble;
var hideBubble = true;
var bubbleTimer = null;
var curClip = null;
var bubbleAppended = false;

function stopClip()
{
    if(playing)
    {
        if(ie)
        {
            if(document.getElementsByTagName('bgsound').length != 0)
                document.getElementsByTagName('bgsound')[0].setAttribute('src', null);
        }
        else
        {
            if(document.getElementsByName('curSoundClip').length != 0)
            {
                var sound = document.getElementsByName('curSoundClip')[0];
                sound.parentNode.removeChild(sound); 
            }
        }

        playing = false;
    }
}

function startClip(clip_name)
{
    if(ie)
    {
        if(document.getElementsByTagName('bgsound').length == 0)
        {
            var backSound = document.createElement('bgsound');
            document.body.appendChild(backSound);
            backSound.setAttribute('src', clip_name);
        }
        else
        {
            document.getElementsByTagName('bgsound')[0].setAttribute('src', clip_name);
        }
    }
    else
    {
        if(playing)
            stopClip();
        
        var embed = document.createElement("embed");
        embed.setAttribute('src', clip_name);
        embed.setAttribute('autostart', 'true');
        embed.setAttribute('loop', 'false');
        embed.setAttribute('name', 'curSoundClip');
        embed.setAttribute('hidden', true);

        document.getElementsByTagName('body')[0].appendChild(embed); 
    }

    playing = true;
}

function showSoundBubble(node)
{
    var x = 0;
    var y = 0;
    
    var element = node;
    while(element != null)
    {
        x += element.offsetLeft;
        y += element.offsetTop;
        element = element.offsetParent;
    }

    soundBubble.style.display = 'block';    
    var mc = document.getElementById('main_content');
    soundBubble.style.left = x + node.offsetWidth - mc.scrollLeft + 'px';
    soundBubble.style.top = y - soundBubble.offsetHeight - mc.scrollTop + 'px';
    curClip = 'sounds/' + node.name;
    hideBubble = false;
}

function hideSoundBubble()
{
    hideBubble = true;
    
    if(bubbleTimer != null)
        window.clearTimeout(bubbleTimer);
        
    bubbleTimer = window.setTimeout(hideSoundBubbleAux, 400);
}

function hideSoundBubbleAux()
{
    if(hideBubble)
    {
        soundBubble.style.display = 'none';
        bubbleTimer = null;
    }
}

function createSoundBubble(e)
{
    soundBubble = document.createElement('div');
    soundBubble.className = 'soundBubble';
    soundBubble.innerHTML = '<div style="margin-top: 15px"><img style="cursor: pointer;" src="images/play.gif" onClick="startClip(curClip);" /><img style="cursor: pointer; margin-left: 10px" src="images/stop.gif" onClick="stopClip();" /></div>';
    addListener(soundBubble, 'mouseout', function (e) {hideSoundBubble();}, false);
    addListener(soundBubble, 'mouseover', function (e) {hideBubble = false;}, false);
    document.getElementsByTagName('body')[0].appendChild(soundBubble);
    bubbleAppended = true;
}

addListener(window, 'load', createSoundBubble, false);