/*******
* Player
*******/
/* Ready-Function of player (initializing) */
var playerIsReady = false;
var player;

function playerReady(thePlayer) {
    playerIsReady = true;
    player = window.document[thePlayer.id];
    player.addModelListener("STATE", "stateListener");
}

var currentlyPlaying = null;

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
    currentState = obj.newstate;
    previousState = obj.oldstate;
    
    if ((currentState == "COMPLETED") && (previousState == "PLAYING")) {
        $("a.ClipActive").each(function() {
            $(this).removeClass("ClipActive");
        });
        currentlyPlaying = null;
    }
}

//loads and plays choosen Sound-File (Speaker-Tools)
function play(PathToSoundFile, btnID) {
    
    if (!playerIsReady) {
        player = window.document[thePlayer.id];
        player.addModelListener("STATE", "stateListener");
        playerIsReady = true;
    }
    
    if (typeof player == 'undefined') {
        alert(noFlashText); //noFlashText is definied within containing page (and filled from Umbraco-Dictionary)
        return;
    }

    if (currentlyPlaying == btnID) {
        player.sendEvent('STOP');
        $('#' + btnID).removeClass("PlayButtonPlaying").addClass('PlayButton');
        currentlyPlaying = null;
        return;
    }

    $(".PlayButtonPlaying").each(function() {
        $(this).removeClass("PlayButtonPlaying");
        $(this).addClass('PlayButton');
    });

    $('#' + btnID).addClass("PlayButtonPlaying");
    player.sendEvent('LOAD', PathToSoundFile); //load Sound-File
    player.sendEvent('PLAY'); //play Sound-File
    currentlyPlaying = btnID;
}


//loads and plays choosen Video or Sound-File (Demo-Clips)
function playDemo(PathToSoundFile, btnID) {
    if (!playerIsReady) {
        player = window.document[thePlayer.id];
        player.addModelListener("STATE", "stateListener");
        playerIsReady = true;
    }

    if (currentlyPlaying == btnID) {
        player.sendEvent('STOP');
        $('#' + btnID).removeClass("ClipActive");
        currentlyPlaying = null;
        return;
    }

    $("a.ClipActive").each(function() {
        $(this).removeClass("ClipActive");
    });

    $('#' + btnID).addClass("ClipActive");
    player.sendEvent('LOAD', PathToSoundFile); //load Sound-File
    player.sendEvent('PLAY'); //play Sound-File
    currentlyPlaying = btnID;
}

//run this code after Page has completely loaded
$(document).ready(function() {

    $("a.demoFancybox").fancybox({
        'zoomSpeedIn': 500,
        'zoomSpeedOut': 500,
        'overlayShow': true
    });

    $('a.demoFancybox').each(function() {
        var fWidth = parseInt($(this).attr('href').match(/width=[0-9]+/i)[0].replace('width=', ''));
        var fHeight = parseInt($(this).attr('href').match(/height=[0-9]+/i)[0].replace('height=', ''));
        $(this).fancybox({ 'frameWidth': fWidth, 'frameHeight': fHeight });
    });
});


var demosPrepared = false;
function prepareDemos() {

    if (demosPrepared)
        return;
    demosPrepared = true;

    $("body").append('<div id="SoundPlayer"><div id="getPlugin">To view this site, you need JavaScript enabled and the latest Adobe Flash Player installed.<p><a href="http://www.adobe.com/go/getflashplayer"><img src="/_img/get_adobe_flash_player.png" alt="Get Adobe Flash player" /></a></p></div></div>');

    /* FLV-Player */
    /* See http://www.longtailvideo.com/players/jw-flv-player/ for more infos */
    var flashvarsMP3 = {};
    var paramsMP3 = {};
    var attributesMP3 = {};

    paramsMP3.allowfullscreen = "true";
    paramsMP3.allowscriptaccess = "always";

    attributesMP3.id = 'player';
    attributesMP3.name = 'player';

    swfobject.embedSWF("/_swf/player-licensed.swf", "SoundPlayer", "0px", "0px", "9.0.124", "/_swf/expressInstall.swf", flashvarsMP3, paramsMP3, attributesMP3);
}
