﻿
var myListener = new Object();
myListener.onInit = function () {
    this.position = 0;
};


var TrackDuration;
myListener.onUpdate = function () {
    if (PlayingTrackID != null) {
        //document.getElementById("info_playing").innerHTML = this.isPlaying;
        //document.getElementById("info_url").innerHTML = this.url;
        //document.getElementById("info_volume").innerHTML = this.volume;
        //document.getElementById("info_position").innerHTML = Math.ceil(this.position / 1000);
        //document.getElementById("info_duration").innerHTML = this.duration;
        //document.getElementById("info_bytes").innerHTML = this.bytesLoaded + "/" + this.bytesTotal + " (" + this.bytesPercent + "%)";



        var isPlaying = (this.isPlaying == "true");

        if (isPlaying == false) {
            stop();
            return;
        }
        //document.getElementById("player_pause").style.display = (isPlaying) ? "inline" : "none";
        //document.getElementById("player_play").style.display = (isPlaying) ? "none" : "inline";

        var timelineWidth = 940;
        var sliderWidth = 0;
        var sliderPositionMin = 0;
        var sliderPositionMax = sliderPositionMin + timelineWidth - sliderWidth;
        var sliderPosition = sliderPositionMin + Math.round((timelineWidth - sliderWidth) * parseInt(this.position) / parseInt(this.duration));

        if (sliderPosition < sliderPositionMin) { sliderPosition = sliderPositionMin; }
        if (sliderPosition > sliderPositionMax) { sliderPosition = sliderPositionMax; }

        if (isNaN(sliderPosition) == false) {
            document.getElementById("playerslider" + PlayingTrackID).style.width = sliderPosition + "px";
        }

        var loaderwidth = (timelineWidth / 100) * parseInt(this.bytesPercent);
        if (isNaN(loaderwidth) == false) {
            document.getElementById("loader" + PlayingTrackID).style.width = loaderwidth + "px";
        }
        TrackDuration = this.duration;
    }
};

function getFlashObject() {
    return document.getElementById("myFlash");
}

var PlayingTrackID = null;

function play(id) {
    if (PlayingTrackID != null) {
        if (PlayingTrackID == id) {
            //stop();

            var slider = document.getElementById('playerslider' + PlayingTrackID);
            var sliderWidth = 940;
            var pixelpos = MouseX - findPos(slider)[0];

            var percentpos = (100 / sliderWidth) * pixelpos;
 

            setPosition((TrackDuration/100) * percentpos);

            //set position

            return;
        }
        else {
            stop();
        }
    }

    var mp3 = "mp3/" + id + ".mp3";
    //if (myListener.position == 0) {
        getFlashObject().SetVariable("method:setUrl", mp3);
    //}
    getFlashObject().SetVariable("method:play", "");
    getFlashObject().SetVariable("enabled", "true");


    document.getElementById("playbutton" + id).src = 'img/play_active.png';
    document.getElementById("trackinfo" + id).style.color = '#00ffc6';

    PlayingTrackID = id;

    return false;
}

function pause() {
    getFlashObject().SetVariable("method:pause", "");
    return false;
}

function stop() {
    getFlashObject().SetVariable("method:stop", "");
    document.getElementById("playerslider" + PlayingTrackID).style.width = "0px";
    document.getElementById("loader" + PlayingTrackID).style.width = "0px";

    document.getElementById("playbutton" + PlayingTrackID).src = 'img/play.png';
    document.getElementById("trackinfo" + PlayingTrackID).style.color = '#dddddd';
    PlayingTrackID = null;

    return false;
}

function setPosition(pos) {
    getFlashObject().SetVariable("method:setPosition", pos);
}

function setVolume(vol) {
    getFlashObject().SetVariable("method:setVolume", vol);
}




var findPos;
var MouseX;
var MouseY;
function findPosNonIE(obj) {
    if (obj.getBoundingClientRect) {
        var rect = obj.getBoundingClientRect();
        return [rect.left + document.documentElement.scrollLeft, rect.top + document.documentElement.scrollTop];
    }
    else {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
        }
        return [curleft, curtop];
    }
}
function findPosIE(obj) {
    var rect = obj.getBoundingClientRect();
    return [rect.left + document.documentElement.scrollLeft - 2, rect.top + document.documentElement.scrollTop - 2];
}

function MousePosIE(e) {
    SetMousePos(event.clientX + document.documentElement.scrollLeft, event.clientY + document.documentElement.scrollTop);
}
function MousePos(e) {
    SetMousePos(e.pageX, e.pageY);
}

function SetMousePos(x, y) {
    MouseX = x;
    MouseY = y;
}

if (document.all) {
    document.onmousemove = MousePosIE;
    findPos = findPosIE;
}
else {
    document.onmousemove = MousePos;
    findPos = findPosNonIE;
}

