

function getFormat() {
    var format = getCookie("ap7_format");
    if (format == null) format = getDefaultFormat();
    if (format == 'WMV' && !allowWMV()) {
        format = 'FLV';
    }
    return format;
}

function allowWMV() {
    return !isRP11Installed();
}

function isRP11Installed() {
    try {
        if (RealVersion && RealVersion.length > 3 && RealVersion.substring(0,2) == "11") return true;
    } catch (Exception) {
        return false;
    }
}

function getQuality() {
    var quality = getCookie("ap7_quality");
    if (quality == null) quality = getDefaultQuality();
    return quality;
}

function initSettings() {
    var quality = getQuality();
    var format = getFormat();

    // first, try to find the desired format (with best quality)
    for (i = 0; i < availableFormats.length; i++) {
        if (availableFormats[i]['format'] == format) {
            availableFormat = availableFormats[i]['format'];
            if (quality != availableQuality) {
                availableQuality = availableFormats[i]['quality'];
                availableStreamUrl = availableFormats[i]['url'];
                availableFileId = availableFormats[i]['fileId'];
                availableTrackingUri = availableFormats[i]['trackingUri'];
            }
        }
    }

    if (availableFormat == null) {
        // try to find the other format (with best quality)
        for (i = 0; i < availableFormats.length; i++) {
            availableFormat = availableFormats[i]['format'];
            if (quality != availableQuality) {
                availableQuality = availableFormats[i]['quality'];
                availableStreamUrl = availableFormats[i]['url'];
                availableFileId = availableFormats[i]['fileId'];
                availableTrackingUri = availableFormats[i]['trackingUri'];
            }
        }
    }

    //alert('initSettings: selected='+availableFormat+","+availableQuality+","+availableStreamUrl);
}

// use WMV as default on Windows; everywhere else, use FLV
function getDefaultFormat() {
    return isWindows() ? 'WMV' : 'FLV';
}

function getDefaultQuality() {
    return 'MQ';
}

function setFormat(format) {
    setCookieMaxExpire("ap7_format", format);
}

function setQuality(quality) {
    setCookieMaxExpire("ap7_quality", quality);
}

function checkParameters() {
    var elementNameToCheck = 'radio_' + getFormat() + '_' + getQuality();
    $(elementNameToCheck).checked = true;

    if (!allowWMV()) {
        try {
            $('pref_WMV_MQ').style.display = 'none';
            $('pref_WMV_HQ').style.display = 'none';
        } catch (Exception) {
            // ignore
        }
    }
}

function loadWMVPlayer(id_div,movieUrl, width, height) {

    if (isMac()) {
       create_silverlight_player(id_div, movieUrl, width, height);
       if ($('plein_ecran')) $('plein_ecran').hide();
       if ($('plein_ecran_haut')) $('plein_ecran_haut').hide();

    } else {

        loadMediaPlayer(id_div,movieUrl,width,height);

    }

}



function loadMediaPlayer(id_div,movieUrl,width,height) {

    if (Prototype.Browser.IE) {
        $(id_div).innerHTML='<object id="mediaplayer" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="' + width +
               '" height="' + height + '"><param name="uiMode" value="full" /><param name="autoStart" value="true" />'+
        '<param name="stretchToFit" value="true" /><param name="URL" value="' + movieUrl + '" /></object>';
    } else {
        $(id_div).innerHTML='<object id="mediaplayer" type="application/x-ms-wmp" width="' + width + '" height="' + height +
           '" data="'+ movieUrl + '"><param name="SRC" value="'+movieUrl+'" /><param name="URL" value="' + movieUrl +
           '" /><param name="uiMode" value="full" /><param name="autoStart" value="true" /><param name="stretchToFit" value="true" /></object>';
    }

}



function loadFlashPlayer(id_div, playerFlash, movieUrl, width, height,alternate) {

    var swfObject = getFlashPlayerSwfObject(playerFlash,movieUrl,width,height);

    var result = swfObject.write(id_div);
    if (!result) {
       $(id_div).innerHTML=alternate;
    }

}



function getFlashPlayerSwfObject(swf, movieUrl, width, height) {

    if (swf.indexOf("?") > -1) {
        swf = swf + "&";
    } else {
        swf = swf + "?";
    }
    skinUrl="http://artestras.rd.llnwd.net/nogeo/arte7/player/SkinUnderAllNoCaption.swf";
    
    swf = swf + "url_movie="+encodeURIComponent(movieUrl)+"&url_skin="+encodeURIComponent(skinUrl);

    //alert(swf);

    var so = new SWFObject(swf, "player", width, height, "9");
    so.useExpressInstall('http://artestras.rd.llnwd.net/nogeo/arte7/player/express_installation/playerProductInstall.swf');
    so.addParam("align", "middle");
    so.addParam("quality", "high");
    so.addParam("allowFullScreen", "true");
    so.addParam("allowScriptAccess", "sameDomain");
    so.addParam("type", "application/x-shockwave-flash");
    so.addParam("pluginspage", "http://www.adobe.com/go/getflashplayer");
    so.addParam("wmode", "transparent");
    return so;
}



function showFlashPlayer(fskWarning) {
    if (fskWarning) {
        window.setTimeout( function() {$("fskWarning").hide();$('playerFLV').show();},3000);
    } else {
        $('playerFLV').show();
    }
}



function showWMVPlayer(fskWarning) {
    var showId;
    if (showWMVInfobox()) {
        showId = 'infoboxWMV';
    } else {
        showId = 'playerWMV';
    }
    if (fskWarning) {
        window.setTimeout( function() {$("fskWarning").hide();$(showId).show();},3000);
    } else {
        $(showId).show();  
    }
}



function showWMVInfobox() {
    return !Prototype.Browser.IE && isWindows() && getFormat() == 'WMV' && isWMVPatchInstalled() != true && getCookie("ap7_wmvnotice") != 'false';

}

function isWindows() {
    return navigator.userAgent.indexOf('Win') != -1;
}

function isFirefox() {
    return navigator.userAgent.indexOf('Firefox') != -1;
}

function isMac() {
    return navigator.userAgent.indexOf('Mac') != -1; 
}

// check if the firefox WMP update has already been installed
// http://port25.technet.com/videos/downloads/wmpfirefoxplugin.exe
function isWMVPatchInstalled() {

    var isWMVPatchInstalled;

    for (var i in navigator.plugins) {
        var n = navigator.plugins[i].name;
        if (n != null && n.indexOf("Windows Media") >= 0 && n.indexOf("Microsoft") >= 0) {
            isWMVPatchInstalled = true;
        }
    }

    //alert('isWMVPatchInstalled='+isWMVPatchInstalled);

    return isWMVPatchInstalled;

}

function canDoFullScreen() {
    return Prototype.Browser.IE || isWMVPatchInstalled();
}

function fullScreen(id, alternateMessage) {
    var player = $(id);
    // on IE and with the updated WMV plugin, player is defined. We can set it to full screen.
    if (canDoFullScreen() && player) {
        if (player.playState == 3)
        {
            player.fullScreen = true;
        }
    } else {

        // everywhere else, we have to show the alternate message, telling the user to dblclick on the window. This works
        // in some player plugins
        alert(alternateMessage);

    }
}

function create_silverlight_player(id_div, file,width,height) {

   jQuery.ajaxSetup({ cache: true });
   jQuery.getScript("/de/CoreMedia/com.arte-tv.streaming/admin/styles/default/javascript/silverlight.js/2892864,templateId=renderJs,property=data,v=2,CmPart=com.arte-tv.streaming.js", function() {
      jQuery.getScript("/de/CoreMedia/com.arte-tv.streaming/admin/styles/default/javascript/wmvplayer.js/2892866,templateId=renderJs,property=data,v=2,CmPart=com.arte-tv.streaming.js", function() {

         var elm = document.getElementById(id_div);

         var src = '/parts/com.arte-tv.streaming/silverlight/wmvplayer.xaml';

         var cfg = {file:file,width:width,height:height,autostart:'true'};

         var silverlightPlayer = new jeroenwijering.Player(elm,src,cfg);

     });

   });

   jQuery.ajaxSetup({ cache: false });

}