/*
Playdar.js JavaScript client library
Copyright (c) 2009 James Wheare
Distributed under the terms of the BSD licence
http://www.playdarjs.org/LICENSE

Modifications to original Playdar.js are under the Apache 2 license.
Copyright (c) 2011 Steven Robertson (steve@playnode.com)
http://www.apache.org/licenses/LICENSE-2.0.html
*/

/* NOTE: Call the following after all Playdar.js files are included.
Playdar.Util.addEvent(window, 'beforeunload', Playdar.beforeunload);
Playdar.Util.addEvent(window, 'unload', Playdar.unload);
*/

Playdar = {

    SERVER_ROOT: "localhost",
    SERVER_PORT: "60210",
    STAT_TIMEOUT: 2000,
    AUTH_COOKIE_NAME: "Playdar.Auth",
    AUTH_POPUP_NAME: "Playdar.AuthPopup",
    AUTH_POPUP_SIZE: { 'w': 500, 'h': 260 },
    MAX_POLLS: 4,
    MAX_CONCURRENT_RESOLUTIONS: 5,
    USE_STATUS_BAR: true,
    USE_SCROBBLER: true,
    USE_JSONP: true,

    // These are default Playdar authorisation details and should be overridden
    // by the script user. Additionally, you should provide "receiverurl" to
    // avoid end-users having to manually copy and paste auth code from Playdar.
    auth_details: {
        name: window.document.title,
        website: window.location.protocol + '//' + window.location.host + '/'
    },

    // The following sub-modules need to be initialised by users of this script.
    // This is structured to allow for other modules to be swapped-in in place
    // to provide different functionality.
    client: null,
    player: null,
    statusBar: null,

    // This is a function that does nothing. It may be used as a placeholder for
    // a function that could be, but not always provided, such as callbacks.
    nop: function () {},

    // Confirm when leaving page if audio is playing.
    beforeunload: function (e) {
        if (Playdar.player) {
            var sound = Playdar.player.getNowPlaying();
            if (sound && sound.playState == 1 && !sound.paused) {
                var confirmation = "The music will stop if you leave this page.";
                e.returnValue = confirmation;
                return confirmation;
            }
        }
    },

    // Called when leaving the page to stop playing and scrobbling music.
    unload: function (e) {
        if (Playdar.player) Playdar.player.stop_current(true);
        if (Playdar.scrobbler) Playdar.scrobbler.stop(true);
    }
};

