﻿var player = null;
var tmp = false;
var pathtmp = null;
//kliknieta mp3
var objplay = null;

function playerReady(thePlayer) {
    //player = document.getElementById(thePlayer.id);
    player = document.getElementById('playerbg');
    addListeners();
}

function addListeners() {
    if (player) {
        player.addModelListener("STATE", "stateListener");
    } else {
        setTimeout("addListeners()", 1);
    }
}

function play(obj, path) {
    jQuery(".listen a img").attr("src", "/jedynka/_img/listen.gif");
    objplay = jQuery(obj);

    //alert(pathtmp+ "<br>" +path);

    if (pathtmp != path & pathtmp != null) {
        jQuery(".listen a img").attr("src", "/jedynka/_img/listen.gif");
        jQuery(".listen a span").text("");
        objplay.find("img").attr("src", "/jedynka/_img/stop.gif");
        tmp = false;
        pathtmp = path;
    }



    if (tmp == false) {
        player.sendEvent('LOAD', path);
        objplay.find("img").attr("src", "/jedynka/_img/stop.gif");
        tmp = true;
        pathtmp = path;
    }
    else {

        player.sendEvent('STOP');
        objplay.find("img").attr("src", "/jedynka/_img/listen.gif");
        objplay.find("span").text("");
        tmp = false;
    }
}

function stateListener(obj) {
    //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
    currentState = obj.newstate;
    previousState = obj.oldstate;

    if ((currentState == "COMPLETED") && (previousState == "PLAYING")) {
        objplay.find("span").text("");
        objplay.find("img").attr("src", "/jedynka/_img/listen.gif");
        tmp = false;
    }
    if (currentState == "PLAYING") {
        objplay.find("span").text("");
        tmp = true;
    }
    if (currentState == "BUFFERING") {
        objplay.find("span").text("");
    }
}