var osName = new String(navigator.userAgent).toLowerCase();

var isLinux = osName.indexOf('linux') != -1 ? true : false;
var isWindows = osName.indexOf('win') != -1 ? true : false;
var isMac = osName.indexOf('mac') != -1 ? true : false;

var libloader = new ExternalLibLoader();
var playerInitialized = false;

if (isLinux) {
    this.libloader.addLib("js/VLCobject.js", linuxLibrariesReady);
    this.libloader.addLib("js/VLCplayer.js", linuxLibrariesReady);
} else if (isWindows || isMac) {
    this.libloader.addLib("js/WindowsMediaPlayer.js", windowsReady);
}

this.libloader.LoadLibs();

//function inkoved when one of windows libraries are ready
function windowsReady() {
    playerInitialized = true;
}

//function inkoved when one of linux libraries is ready
function linuxLibrariesReady() {

    var sc = document.getElementsByTagName("script");
    var numberOfLoadedLibraries = 0;

    for (var i = 0; i < sc.length; i++) {
        if (sc[i].src.indexOf("js/VLCobject.js") > -1) {
            numberOfLoadedLibraries++;
        }
        if (sc[i].src.indexOf("js/VLCplayer.js") > -1) {
            numberOfLoadedLibraries++;
        }
    }
    //all libraries has been downloaded player is read to use
    if (numberOfLoadedLibraries == 2 && !playerInitialized) {
        playerInitialized = true;
    }
}

function createPlayer() {    
    waitForInit();

    var width = '360';
    var height = '333';

    var floorPlayerParentId = 'floor-player';
    var floorPlayerId = 'FloorPlayer';

    var englishPlayerParentId = 'english-player';
    var englishPlayerId = 'EnglishPlayer';

    if (isWindows) {        
        createWMP(floorPlayerParentId, floorPlayerId, width, height, '');        
        createWMP(englishPlayerParentId, englishPlayerId, width, height, '');       
    }

    var pWMP;

    if (GetActiveTab() == 'Floor') {
        pWMP = document.getElementById('FloorPlayer');
    }
    else {
        pWMP = document.getElementById('EnglishPlayer');
    }

    if (pWMP != null && pWMP.controls != null) {
    }    
}

function createPlayerAndSetUrl(url) {
    waitForInit();

    var width = '360';
    var height = '333';

    var floorPlayerParentId = 'floor-player';
    var floorPlayerId = 'FloorPlayer';

    var englishPlayerParentId = 'english-player';
    var englishPlayerId = 'EnglishPlayer';            

    if (isLinux) {
        if (GetActiveTab() == 'Floor'){
            createVlcPlayer(floorPlayerParentId, floorPlayerId, width, height, url);
        }
        else{
            createVlcPlayer(englishPlayerParentId, englishPlayerId, width, height, url);
        }
    }
    else if (isMac) {
        if (GetActiveTab() == 'Floor'){
            createWMP(floorPlayerParentId, floorPlayerId, width, height, url);
        }
        else{        
            createWMP(englishPlayerParentId, englishPlayerId, width, height, url);
        }
    }   
}

function waitForInit() {
    if (!playerInitialized) {
        setTimeout("waitForInit()", 100);
        return;
    }
}
//attach createPlayer to window.onload event
window.onload = createPlayer;
