﻿// -------- streaming player helper functions -------- //

var g_playerPopup = null;

// launch the player
function player_launch() {
    var title       = "PsonarPlayer";
    var dimensions  = "width=" + g_playerWindowWidth + ",height=" + g_playerWindowHeight;
    var left        = (screen.width / 2) - (g_playerWindowWidth / 2);
    var top         = (screen.height / 2) - (g_playerWindowHeight / 2);

    // check flash is OK
    var bHasRequiredFlashVersion = checkAndShow_flashWarning();
    if (bHasRequiredFlashVersion) {
    
        // attempt to display popup
        g_playerPopup = window.open(g_playerUrl, "PsonarPlayer",
            dimensions + "," +
            "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,directories=0,resizable=1,titlebar=0,modal=1," +
            "left=" + left + ",top=" + top);

        if ((typeof (g_playerPopup) != "undefined") && (g_playerPopup !== null)) {
            g_playerPopup.blur();
            g_playerPopup.focus();
        } else {
            // popup blocker active - inform user
            show_popupBlockerWarning();
        }
    }
}

// reload the player tracklist
// note: this is done by calling the player_reload function on index.aspx of the player window
function player_reload() {
    if(player_isActive()) {
        g_playerPopup.player_reload();
    }
}

// reload the player tracklist and play the next track
// note: this is done by calling the player_reloadAndPlayNextTrack function on index.aspx of the player window
function player_reloadAndPlayTrack(response, textStatus) {
    if (player_isActive()) {
        if (response.status == 200) {
            // success
            var firstPlaylistEntryId = response.playlistEntryIds[0];
            g_playerPopup.player_reloadAndPlayTrack(firstPlaylistEntryId);
        }
    }
}

// check to see if the player is still active
function player_isActive() {
    return ((typeof (g_playerPopup) != "undefined") && (g_playerPopup !== null) && (!g_playerPopup.closed));
}

// actions button clicked
function player_handleActionsClicked() {
}

// track the advert clickthrough
function player_trackReferralClickthrough(controller, trackId, type) {

    // track clickthrough on google
    var trackUrl = '/Home/' + controller + '/ReferralSaleClickthrough' + '?trackid=' + trackId + "&type=" + type;
    pageTracker._trackPageview(trackUrl);
}

// --------------- stream helper functions -------------- //

// add tracks to player stream
function ajax_addToStream(type, idArray, positionQualifierEnum, targetEntryId, restrictByUser, successCallback, errorCallback) {

    // restrictByUser is important since if the user click on an artist / album / genre,
    // we need to know to only return tracks in the user's collection
    // if we don't do this we add tracks from the entire cloud into the player!
    // we don't care about this when passing through ids of specific tracks for obvious reasons.
    var streamInfo = {
        type: type,
        positionqualifierenum: positionQualifierEnum,
        targetentryid: targetEntryId,
        ids: idArray,
        restrictbyuser: restrictByUser
    };
       
    var streamInfoJson = $.toJSON(streamInfo);

    // post ajax request to server
    $.ajax({
        url: g_addToStreamAsyncUrl, // note: global url
        type: g_method_post,
        dataType: g_dataType_json,
        data: streamInfoJson,
        contentType: g_contentType_json,
        success: successCallback,
        error: errorCallback
    });
}

// update the player stream
// (this is a cut-down version of ajax_reorderPlaylist because we don't know the playlist id)
// call the success callback if the operation was successful
function ajax_updateStream(start, end, deleteIdArray, updateIdArray, successCallback) {

    var url = g_updateStreamAsyncUrl;

    // create json data to pass back to server
    // fields must match the mappings in StreamUpdateInfo class
    var streamUpdateInfo = {
        start: start,
        end: end,
        deleteids: deleteIdArray,
        updateids: updateIdArray
    };

    var streamUpdateInfoJson = $.toJSON(streamUpdateInfo);

    // post ajax request to server
    $.ajax({
        url: url,
        type: g_method_post,
        dataType: g_dataType_json,
        data: streamUpdateInfoJson,
        contentType: g_contentType_json,
        success: successCallback,
        error: jQuery_ajaxRequestFailed_redirectToLogin
    });
}