var liveClientInitialized = false;
var liveClientJmsCallbackArray = Array();

function liveClientInit() {
    liveClientInitialized = true;
    registerLiveClientCallback();
    window.onunload = function () {
        liveClientClose();
    } ;
}

function liveClientClose() {
    getFlashMovieObject("liveclient").liveUpdateClose();
}

function getFlashMovieObject(movieName) {
    if (document.embeds[movieName])
        return document.embeds[movieName];
    if (window.document[movieName])
        return window.document[movieName];
    if (window[movieName])
        return window[movieName];
    if (document[movieName])
        return document[movieName];
    return null;
}

function registerLiveClientCallback() {
    if (liveClientInitialized === true) {
        for (var i = 0; i < liveClientJmsCallbackArray.length; i += 3) {
            getFlashMovieObject("liveclient").registerListener(liveClientJmsCallbackArray[i], liveClientJmsCallbackArray[i + 1], liveClientJmsCallbackArray[i + 2]);
        }
    }
}

function registerJmsCallback(channel, subTopic, functionName) {
    if (liveClientInitialized === true) {
        getFlashMovieObject("liveclient").registerListener(channel, subTopic, functionName);
    }
    else {
        liveClientJmsCallbackArray = liveClientJmsCallbackArray.concat(Array(channel, subTopic, functionName));
    }
}

function writeLiveClientEmbed(liveClientFlashUrl) {
    jQuery(function(){
        if (window.liveClientEmbedWritten) {
            return;
        }

        window.liveClientEmbedWritten = true;

        // Checking if there is a suitable version of flash player
        if (flashembed.isSupported([9,0])) {
            jQuery("body").append ("<div id='lccontainer' style='height:0px;width:0px'></div>");
            jQuery("#lccontainer").eq(0).flashembed ( {
                id: 'liveclient',
                src: liveClientFlashUrl + '.swf',
                onFail: function () {
                    // TODO provide error handling
                }
            });
        } else {
            // TODO inform the user about missing flash player
            // var alternateContent = "This content requires theAdobe Flash Player version 9 or above.";
            // document.write(alternateContent);  // insert non-flash content
        }
    });
}

