var audioWindow = null;
var URL = null;

function closeAudioWindow()
{
    audioWindow.close();
}

function playAudio(audioId, themeName, flav, bg, loop)
{
    URL = '/web/do/site/audio/play?ID=' + audioId + '&THEME=' + themeName + '&flavor=' + flav + '&BG=' + bg + '&LOOP=' + loop;
    audioWindow = window.open(URL, 'audioWindow', 'width=200,height=70,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,screenx=2,screeny=2,top=2,left=2');
    //audioWindow.focus();
}

function playMusic(css, path, loop)
{
    URL = '/sites/templates/audio/playMusic.html?CSS=' + css + '&PATH=' + path + '&LOOP=' + loop;
    audioWindow = window.open(URL, 'audioWindow', 'width=200,height=50,titlebar=no,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,screenx=2,screeny=2,top=2,left=2');
    audioWindow.focus();
}

function audioPlay(audioId, css, path, loop, popup)
{
    if (audioId > 0)
    {
        if (popup == 'true')
        {
            URL = '/sites/templates/audio/playMusic.html?CSS=' + css + '&PATH=' + path + '&LOOP=' + loop;
            audioWindow = window.open(URL, 'audioWindow', 'width=200,height=50,titlebar=no,directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,screenx=2,screeny=2,top=2,left=2');
            audioWindow.focus();
        }
        else
        {
            //detect audio popup
            if(audioWindow)
            {
                closeAudioWindow();
            }

            var sAgent = navigator.userAgent.toLowerCase() ;
            if (sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1)
            {
                if (loop == 'true')
                {
                    loop = 'infinite';
                }
                else
                {
                    loop = 1;
                }
                soundPath = "<bgsound src='" + path + "' autostart='true' loop='" + loop + "' hidden='true'>";
                alert(soundPath);
            }
            else
            {
                soundPath = "<embed src='" + path + "' autostart='true' loop='" + loop + "' hidden='true'>";
                alert(soundPath);
            }
            document.write(soundPath);
        }
    }
}

// falling images //
var fallingspeed;
var fallingcount;
var dx, xp, yp;
// coordinate and position variables
var am, stx, sty;
// amplitude and step variables
var obj, i, doc_width = 800, doc_height = 600;

function winSize()
{
    try
    {
        doc_width = window.innerWidth?window.innerWidth:document.body.clientWidth;
        doc_height = window.innerHeight?window.innerHeight:document.body.clientHeight;
    }
    catch (e)
    {
    }
}


function writeFalling(img)
{
    dx = new Array();
    xp = new Array();
    yp = new Array();
    am = new Array();
    stx = new Array();
    sty = new Array();

    document.write("<STYLE type=\"text/css\">.flake {position:absolute;top:-200;}</STYLE>");
    for (i = 0; i < fallingcount; i++)
    {
        dx[i] = 0;
        // set coordinate variables
        xp[i] = Math.random() * (doc_width - 30) + 10;
        // set position variables
        yp[i] = Math.random() * doc_height;
        am[i] = Math.random() * 20;
        // set amplitude variables
        stx[i] = 0.02 + Math.random() / 10;
        // set step variables
        sty[i] = 0.7 + Math.random();
        // set step variables
        document.write("<div id=\"dot" + i + "\" class=\"flake\"><img src=\"");
        document.write(img + "\" border=\"0\"></div>");
    }
}

function snowMove(id, left, top)
{
    obj = document.getElementById?document.getElementById(id).style:
          document.all?document.all[id].style:
          document.layers?document.layers[id]:null;
    if (obj)
    {
        obj.left = left;
        obj.top = top;
    }
}

function snow()
{
    winSize();
    doc_scroll = (window.pageYOffset != null)?window.pageYOffset:document.body.scrollTop;
    for (i = 0; i < fallingcount; ++ i)
    {
        // iterate for every dot
        yp[i] += sty[i];
        if (yp[i] > doc_height + doc_scroll - 50)
        {
            xp[i] = Math.random() * (doc_width - am[i] - 30);
            yp[i] = doc_scroll;
            stx[i] = 0.02 + Math.random() / 10;
            sty[i] = 0.7 + Math.random();
        }
        dx[i] += stx[i];
        snowMove("dot" + i, xp[i] + am[i] * Math.sin(dx[i]), yp[i]);
    }
    setTimeout("snow()", fallingspeed);
}

function playFalling(img, spd, cnt)
{
    fallingspeed = spd;
    fallingcount = cnt;
    writeFalling(img, cnt);
    //window.onload=snow;
    snow();
}

function floatiesPlay(id, img, spd, cnt)
{
    if (id > 0)
    {
        fallingspeed = spd;
        fallingcount = cnt;
        writeFalling(img, cnt);
        snow();
    }
}

