﻿// -------- 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);

    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) {

    // 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: player_jQuery_ajaxRequestFailed
    });
}

// remove tracks from player stream
function ajax_removeFromStream(idArray, successCallback) {

    var idInfo;
    var idInfoJson;

    idInfo = { ids: idArray };
    idInfoJson = $.toJSON(idInfo);

    // post ajax request to server
    $.ajax({
        url: g_removeFromStreamAsyncUrl, // note: global url
        type: g_method_post,
        dataType: g_dataType_json,
        data: idInfoJson,
        contentType: g_contentType_json,
        success: successCallback,
        error: player_jQuery_ajaxRequestFailed
    });
}

// reorder tracks in 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_reorderStream(positionQualifierEnum, targetEntryId, idArray, successCallback) {

    // create json data to pass back to server
    // fields must match the mappings in StreamInfo class
    var streamInfo = {
        type: g_extJsGrid_dataTypePlaylistEntry,
        positionqualifierenum: positionQualifierEnum,
        targetentryid: targetEntryId,
        ids: idArray
    };
        
    var streamInfoJson = $.toJSON(streamInfo);

    // post ajax request to server
    $.ajax({
        url: g_reorderPlayerAsyncUrl, // note: global url
        type: g_method_post,
        dataType: g_dataType_json,
        data: streamInfoJson,
        contentType: g_contentType_json,
        success: successCallback,
        error: player_jQuery_ajaxRequestFailed
    });
}