﻿var tryAgainTimeout = 3;
var hostName = 'SilverlightMediaPlayer';
var retryCount = 0;
playerCheck = 1;

function GetSilverlightPopup() {
    var oldwindow = window.open('', 'SLPlayer', 'height=400,width=670,alwaysRaised=1,scrollbars=0,dependant=1');
    var newwindow;
    if (oldwindow == null) {
        alert(PlayerBlockedByPopup);
        //alert("The player seems to have been blocked by a popup blocker.");
    }
    else if (oldwindow.document != null) {
        var Host = oldwindow.document.getElementById("SilverlightMediaPlayer");

        if (Host)
            newwindow = oldwindow;
    }

    if (newwindow && (!newwindow.closed && newwindow.location)) {

    }
    else {
        newwindow = window.open('/Player/Popup/', 'SLPlayer', 'height=400,width=670,alwaysRaised=1,scrollbars=0,dependant=1');
        if (newwindow) {
            if (!newwindow.opener) newwindow.opener = self;
        }
    }
    return newwindow; 
}

function PutFocusOnPlayer() {
    var newwindow = GetSilverlightPopup();
    if (newwindow.focus) { newwindow.focus() }
    return;
}

function AddTrackToSilverlightPlayer(trackID, append) {
    if (!tryPopup())
        return;

    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        setTimeout(function () {
            AddTrackToSilverlightPlayer(trackID, append)
        }, 2000);
        //ResetRetryCount();
        return;
    }
    var Host = newwindow.document.getElementById(hostName);

    if (IsHostSafe(Host)) {
        Host.content.Player.AddTrackByTrackId(trackID, append);
        postData("/Ajax/LogAction/", "actionType=T&uniqueID=" + trackID);
        if (newwindow.focus) { newwindow.focus() }
    } else {
        if (CheckSilverlight(newwindow)) {
            retryCount++;
            setTimeout(function () {
                AddTrackToSilverlightPlayer(trackID, append)
            }, 2000);
        }
    }

    return;
}

function AddAlbumToSilverlightPlayer(albumID) {
    if (!tryPopup())
        return false;

    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        setTimeout(function () {
            AddAlbumToSilverlightPlayer(albumID)
        }, 2000);
        return false;
    }
    var Host = newwindow.document.getElementById(hostName);

    if (IsHostSafe(Host)) {
        Host.content.Player.AddAlbumByAlbumId(albumID);
        ResetRetryCount();
        if (newwindow.focus) { newwindow.focus() }
    } else {
        if (CheckSilverlight(newwindow)) {
            retryCount++;
            setTimeout(function () {
                AddAlbumToSilverlightPlayer(albumID)
            }, 2000);
        }
    }

    return false;
}

function AddPlaylistToSilverlightPlayer(playlistID) {
    if (!tryPopup())
        return false;

    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        setTimeout(function () {
            AddPlaylistToSilverlightPlayer(playlistID)
        }, 2000);
        return false;
    }
    var Host = newwindow.document.getElementById(hostName);

    if (IsHostSafe(Host)) {
        Host.content.Player.AddPlaylistByPlaylistId(playlistID);
        postData("/Ajax/LogAction/", "actionType=P&uniqueID=" + playlistID);
        ResetRetryCount();
        if (newwindow.focus) { newwindow.focus() }
    } else {
        if (CheckSilverlight(newwindow)) {
            retryCount++;
            setTimeout(function () {
                AddPlaylistToSilverlightPlayer(playlistID)
            }, 2000);
        }
    }

    //return false;
}

function AddTrackListToSilverlightPlayer(trackIDList, append) {
    if (!tryPopup())
        return false;

    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        //AddTrackListToSilverlightPlayer.delay(tryAgainTimeout, trackIDList, append);
        setTimeout(function () {
            AddTrackListToSilverlightPlayer(trackIDList, append)
        }, 2000);
        return false;
    }
    var Host = newwindow.document.getElementById(hostName);

    if (IsHostSafe(Host)) {
        Host.content.Player.AddTrackListByTrackIdList(trackIDList, append);
        ResetRetryCount();
        if (newwindow.focus) { newwindow.focus() }
    } else {
        if (CheckSilverlight(newwindow)) {
            retryCount++;
            //AddTrackListToSilverlightPlayer.delay(tryAgainTimeout, trackIDList, append);
            setTimeout(function () {
                AddTrackListToSilverlightPlayer(trackIDList,append)
            }, 2000);
        }
    }

    return false;
}

function tryPopup() {
    if (retryCount >= 5) {
        ResetRetryCount();
        return false;
    }
    return true;
}

function ResetRetryCount() {
    retryCount = 0;
}

function IsHostSafe(host) {
    var safe = false;
    if (host) {
        if (host.content) {
            if (host.content.Player) { 
                try
                {
                    if (host.content.Player.PlayerReady()) {
                        safe = true;
                    }
                }
                catch (err)
                {
                    safe = false;
                }
            }
        }
    }
    return safe;
}

function CheckSilverlight(hostwindow) {
    if (hostwindow) {
        if (hostwindow.Silverlight) {
            if (!hostwindow.Silverlight.isInstalled('3.0.0.0'))
                return false;
        }
    }
    return true;
}

function RunPlayerCheck(item) {
   
    var subscriber = $("body").hasClass('sub');
    var novauser = $("body").hasClass('nova');
    $(item + '.playable').each(function () {
        if ($(this).hasClass('album')) {

            if (!subscriber) return true; // Go to the next item
            var playable = $(this).attr('data-ls');
            if (playable == 'true') {
                $(this).unbind('click').click(function (ev) {
                    var alid = $(this).attr('data-alid');
                    if (alid.length > 0) {
                        AddAlbumToSilverlightPlayer(alid);
                    }
                    ev.preventDefault();
                });
                $(this).addClass('stset');
            }
        }
        else if ($(this).hasClass('track')) {
            if (!subscriber) return true; // Go to the next item
            var playable = $(this).attr('data-ls');
            if (playable == 'true') {
                $(this).addClass('stset');
                $(this).unbind('click').click(function (ev) {
                    var trackId = $(this).attr('data-trid');
                    AddTrackToSilverlightPlayer(trackId);
                    ev.preventDefault();
                });
            }
        }
        else if ($(this).hasClass('playlist')) {
            if (!subscriber && !novauser) return true; // Go to the next item
            var ptype = $(this).attr('data-ptype');
            if (!subscriber && novauser && ptype != 'nova') return true; // Go to the next item

            $(this).unbind('click').click(function (ev) {
                var plid = $(this).attr('data-plid');
                if (plid.length > 0) {
                    AddPlaylistToSilverlightPlayer(plid);
                }
                ev.preventDefault();
            });
            $(this).addClass('stset');
        }
    });

   
}

$(document).ready(function () {
    RunPlayerCheck('');
});





