﻿

/** SPOTIFY **/

var userHasSpotify;

function playSpotify(song) {

    //Check for spotify
    if (confirmUserHasSpotify()) {



        if (!song.SpotifyId || song.SpotifyId == "") {
            $.ajax({
                url: "http://ws.spotify.com/search/1/track.xml?q=" + (song.MbId ? 'isrc:' + song.MbId : song.Artist + ',' + song.Title),
                dataType: 'xml',
                traditional: true,
                type: 'GET',
                success: function (result) {
                    //Parse spotify link id
                    if ($(result).find("track").length > 0) {
                        var parsedValue = $(result).find("track")[0].attributes[0].value; //"spotify:track:2b712q3E27nyW6LGsZxr0y"
                        song.SpotifyId = parsedValue.split(':')[2];
                    }

                    // window.open(parsedValue);
                    controlSpotify(song);
                }
            });
        } else {

            controlSpotify(song);

        }

    }

}

function controlSpotify(song) {

    if (song.Played) {
        var played = eval(song.Played.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
        var diff = new Date().getTime() - played.getTime();
        song.position = new Date(diff);
    } else {
        song.position = new Date().setTime(0); // start from 0 seconds if no position was set
    }


    //    var trackUri = "http://open.spotify.com/track/" + song.SpotifyId;

    var trackUri = "spotify:track:" + song.SpotifyId;

    if (song.position && song.position.getMinutes)
        trackUri += "#" + addLeadingZero(song.position.getMinutes()) + ':' + addLeadingZero(song.position.getSeconds());

    silentAlert("Starting Spotify: " + trackUri);
    document.location.href = trackUri; // more safe way to open spotify in all browsers than iframe solution.

    var placeholder = $('#iframe-placeholder');

    placeholder.empty();

    var iframe = document.createElement('iframe');
    iframe.id = 'iframe-preview';
    iframe.src = trackUri;
    iframe.scrolling = 'no';
    iframe.style.width = '0px';
    iframe.style.height = '0px';
    iframe.style.visibility = 'hidden';

    placeholder.append(iframe);

}

var cookieName = 'UserHasConfirmedSpotify';

function getHasSpotifyCookie() {
    var i, x, y;
    var documentCookies = document.cookie.split(';');
    for (i = 0; i < documentCookies.length; i = i + 1) {
        x = documentCookies[i].substr(0, documentCookies[i].indexOf('='));
        x = x.replace(/^\s+|\s+$/g, '');

        if (x === cookieName) {
            return true;
        }
    }
    return false;
};

function setHasSpotifyCookie() {
    var expDate = new Date();
    expDate = new Date(expDate.setDate(expDate.getDate() + 365));
    var value = escape('1') + ((expDate == null) ? '' : '; expires=' + expDate.toUTCString());
    document.cookie = cookieName + '=' + value;
}

function showConfirmSpotifyDialog() {
    var confirmResult =
                        confirm('You will need Spotify to preview a track on your computer.\r\n\r\n'
                        + 'Do you have Spotify installed?', '1', '2', '3', '4');
    return confirmResult;
}

function confirmUserHasSpotify() {
    if (userHasSpotify) {
        return true;
    }

    var cookieValue = getHasSpotifyCookie();
    if (cookieValue) {
        userHasSpotify = true;
        return true;
    }

    var confirmDialog = showConfirmSpotifyDialog();
    if (confirmDialog) {
        setHasSpotifyCookie();
        userHasSpotify = true;
        return true;
    }

    return false;
}


