﻿var tryAgainTimeout = 3;
var hostName = 'SilverlightMediaPlayer';
var retryCount = 0;

function GetSilverlightPopup() {
    var oldwindow = window.open('', 'SLPlayer', 'height=400,width=670,alwaysRaised=1,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('/Popup.aspx', 'SLPlayer', 'height=400,width=670,alwaysRaised=1,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 false;

    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        AddTrackToSilverlightPlayer.delay(tryAgainTimeout, trackID);
        ResetRetryCount();
        return false;
    }
    var Host = newwindow.document.getElementById(hostName);

    if (IsHostSafe(Host)) {
        Host.content.Player.AddTrackByTrackID(trackID, append);
        postData("/Ajax/LogAction.ashx", "uniqueID=" + trackID);
        if (newwindow.focus) { newwindow.focus() }
    } else {
        if (CheckSilverlight(newwindow)) {
            retryCount++;
            AddTrackToSilverlightPlayer.delay(tryAgainTimeout, trackID);
        }
    }

    return false;
}

function AddAlbumToSilverlightPlayer(albumID) {
    if (!tryPopup())
        return false;
        
    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        AddAlbumToSilverlightPlayer.delay(tryAgainTimeout, albumID);
        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++;
            AddAlbumToSilverlightPlayer.delay(tryAgainTimeout, albumID);
        }
    }

    return false;
}

function AddPlaylistToSilverlightPlayer(playlistID) {
    if (!tryPopup())
        return false;
        
    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        AddPlaylistToSilverlightPlayer.delay(tryAgainTimeout, playlistID);
        return false;
    }
    var Host = newwindow.document.getElementById(hostName);

    if (IsHostSafe(Host)) {
        Host.content.Player.AddPlaylistByPlaylistID(playlistID);
        postData("/Ajax/LogPlaylistAction.ashx", "uniqueID=" + playlistID);
        ResetRetryCount();
        if (newwindow.focus) { newwindow.focus() }
    } else {
        if (CheckSilverlight(newwindow)) {
            retryCount++;
            AddPlaylistToSilverlightPlayer.delay(tryAgainTimeout, playlistID);
        }
    }

    //return false;
}

function AddTrackListToSilverlightPlayer(trackIDList, append) {
    if (!tryPopup())
        return false;
        
    var newwindow = GetSilverlightPopup();
    if (!newwindow) {
        retryCount++;
        AddTrackListToSilverlightPlayer.delay(tryAgainTimeout, trackIDList, append);
        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);
        }
    }

    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)
                safe = true;
        }
    }
    return safe;
}

function CheckSilverlight(hostwindow) {
    if (hostwindow) {
        if (hostwindow.Silverlight) {
            if (!hostwindow.Silverlight.isInstalled('3.0.0.0'))
                return false;
        }
    }
    return true;
}
