﻿eXactMusic.Web.Player = function() {
}

eXactMusic.Web.Player.prototype = {
    _frequency: 100,
    _tickHandler: null,
    _timer: null,
    _window: null,

    _path: '',
    _artist: null,
    _title: '',
    _duration: 0,
    _id: 0,

    InitTimer: function() {
        this._tickHandler = Function.createDelegate(this, this.OnTimerTick);
        this._timer = new Sys.Preview.Timer();
        this._timer.set_interval(this._frequency);
        this._timer.add_tick(this._tickHandler);
        this._timer.set_enabled(true)
    },

    DisposeTimer: function() {
        if (this._timer != null) {
            this._timer.set_enabled(false);
            this._timer.remove_tick(this._tickHandler);
            this._timer.dispose();
            this._timer = null;
        }

        this._tickHandler = null;
    },

    OnTimerTick: function() {
        if (this._window.sendToActionScript) {

            this._window.sendToActionScript(this._path, this._artist, this._title, this._duration, this._id);

            this.DisposeTimer();
        }
    },

    dispose: function() {
        this.DisposeTimer();
    },

    AddTrack: function(path, artist, title, duration, id) {
        var playerPath = 'Controls/Player.aspx';
        var ActualPath = path.toString();

        ActualPath = ActualPath.substring(0, ActualPath.indexOf("Tracks/Preview", 0))

        if (this._window == null || this._window.closed) {
            this._window = (new eXactMusic.Core.JSAdapter()).OpenWindow("", 'Player', 364, 390, 'no');

            if (this._window.location.toString() == 'about:blank' ||
                this._window.location.toString().substr(this._window.location.toString().length - 16, 16) != 'Controls/Player.aspx') {
                this._window.location = ActualPath + playerPath;
            }

            if (!this._window.opener)
                this._window.opener = self;
        }
        if (this._window != null) {
            this._window.focus();
        }
        this._path = path;
        this._artist = artist;
        this._title = title;
        this._duration = duration;
        this._id = id;
        this.InitTimer();
    }
}

Player = new eXactMusic.Web.Player();

eXactMusic.Web.Player.registerClass("eXactMusic.Web.Player", null, Sys.IDisposable);
if (typeof (Sys) !== "undefined") { Sys.Application.notifyScriptLoaded() };

