var curZap = 1;
var curCommande = 1;
var windowPopupWidth = 820;
var windowPopupHeight = 532;

function Is() {
agent = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion);
this.minor = parseFloat(navigator.appVersion);
this.ns = ((agent.indexOf('mozilla') != -1) &&
(agent.indexOf('spoofer') == -1) &&
(agent.indexOf('compatible') == -1) &&
(agent.indexOf('opera') == -1) &&
(agent.indexOf('webtv') == -1) &&
(agent.indexOf('hotjava') == -1));
this.ns2 = (this.ns && (this.major == 2));
this.ns3 = (this.ns && (this.major == 3));
this.ns4 = (this.ns && (this.major == 4));
this.ns6 = (this.ns && (this.major >= 5));
this.ie = ((agent.indexOf("msie") != -1) &&
(agent.indexOf("opera") == -1));
this.ie3 = (this.ie && (this.major < 4));
this.ie4 = (this.ie && (this.major == 4) &&
(agent.indexOf("msie 4") != -1));
this.ie5 = (this.ie && (this.major == 4) &&
(agent.indexOf("msie 5.") != -1) &&
(agent.indexOf("msie 5.5") == -1) &&
(agent.indexOf("mac") == -1));
this.iem5 = (this.ie && (this.major == 4) &&
(agent.indexOf("msie 5.") != -1) &&
(agent.indexOf("mac") != -1));
this.ie55 = (this.ie && (this.major == 4) &&
(agent.indexOf("msie 5.5") != -1));
this.ie6 = (this.ie && (this.major == 4) &&
(agent.indexOf("msie 6.") != -1));
this.ie7 = (this.ie && (this.major == 4) &&
(agent.indexOf("msie 7.0b") != -1));
this.nsdom = (this.ns4 || this.ns6);
this.ie5dom = (this.ie5 || this.iem5 || this.ie55);
this.iedom = (this.ie4 || this.ie5dom || this.ie6);
this.w3dom = (this.ns6 || this.ie6 ||this.ie7);
}

var is = new Is();
	
if (navigator.userAgent.indexOf("Windows NT 6.0") > -1) { var bVista = 1; } else { var bVista = 0; }
if (is.ie6) { var ie6 = 1; } else { var ie6 = 0; }

// Setup
function setupWindowsMedia(typeEcran) {
	document.getElementById('wmpPlayer').innerHTML = wmpCreate(typeEcran,'');
}


// Create wmp
function wmpCreate(typeEcran,url) {
// MODE16_9:int = 0;
// MODE4_3:int = 1;
	if(typeEcran===0){
		var videoPlayerWidth = 640;
		var videoPlayerHeight = 360;
	}else{
		var videoPlayerWidth = 480;
		var videoPlayerHeight = 360;
	}

	var str = "";
	str = '<object id="contentPlayer" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'+videoPlayerWidth+'" height="'+videoPlayerHeight+'">';
	//uiMode = invisible, none, mini, full.
	str += '<param name="URL" value="'+url+'" />';
	str += '<param name="uiMode" value="none">';
	//str += '<param name="windowlessVideo" value="true">';
	str += '<param name="enableContextMenu" value="true">';
	str += '<param name="ShowControls" value="true">';
	str += '<param name="ShowStatusBar" value="true">';
	str += '<param name="ShowDisplay" value="true">';
	 str += '<param name="autoStart" value="true">';
	str += '<param name="stretchToFit" value="true">';

//	str += '<param name="TransparentAtStart" value="true">';
//	str += '<param name="DisplaySize" value="0">';

	str += '</object>';

	return str;

}

// Notifier Media Change
function wmpMediaChange(item) {
	var wmp = document.getElementById('contentPlayer');
	try {
		document.getElementById('wmpSWF').mediaChange(wmp.currentMedia.name, wmp.currentMedia.sourceURL);
	} catch(e) {}
}

var playstateValues = new Array("Undefined","Stopped","Paused","Playing","Scan Forward","Scan Reverse","Buffering","Waiting","Media Ended","Transitioning","Ready","Reconnecting");


// Notifier State Change
function wmpPlayStateChange(newState)  {
	try {
		document.getElementById('wmpSWF').playStateChange(playstateValues[newState]);
	} catch(e) {}
}

function swapTypeEcran(typeEcran, typePlayer){
	
	if (curCommande==1) { if (bVista) { mHeight=560; } else { if(ie6){mHeight=534;}else{mHeight=554;} } } else { if (bVista) { mHeight=500; } else { if(ie6){mHeight=476;}else{mHeight=496;} } }

	if(typeEcran=="1"){
		document.getElementById('contentPlayer').width = 480;
		if(typePlayer=="0"){ // que mode popup				
			if (curZap==1) {mWidth=660;}  else {mWidth=510;}				
			resizeTo(mWidth,mHeight);
		}
	}else if(typeEcran=="0"){
		document.getElementById('contentPlayer').width = 640;
		if(typePlayer=="0"){ // que mode popup			
			if (curZap==1) {mWidth=820;}  else {mWidth=670;}			
			resizeTo(mWidth,mHeight);
		}
	}
}

// PlayVideo
function playVideo(url) {
	var wmp = document.getElementById('contentPlayer');
	if(wmp){
		wmp.URL = url;
		show();
	}
	//controlAction('open');  fait ds LauchVideo Flash
	//controlAction('play');


//controlAction('stop');
show();
controlAction('play');
//(function(){ controlAction('play'); }).delay(2000);
//show();

}



// Controller Action
function controlAction(action) {

	var wmp = document.getElementById('contentPlayer');
	var F = document.getElementById('wmpSWF');


	switch (action) {
		case "play":
			show();
			wmp.controls.play();
			break;


		case "stop":
			hide();
			wmp.controls.stop();
			break;

		case "open":
			//
			break;

		case "close":
			break;

		case "ecran":
			break;
	}

}


// Net Info
function checkNetStats() {
	try{
		var wmp = document.getElementById('contentPlayer');
		var dataObject = new Object();
		//dataObject.bandWidth = wmp.network.bandWidth;
		//dataObject.bitRate = wmp.currentMedia.duration ;
		dataObject.bitRate = wmp.network.bitRate;
		dataObject.bufferingProgress = wmp.network.bufferingProgress;
		//dataObject.downloadProgress = wmp.network.downloadProgress;
		dataObject.duration = wmp.controls.currentItem.duration;
		//alert(dataObject.duration);
		dataObject.status = wmp.status;
		if(wmp.PlayState==1) {
			dataObject.status="";
		}

		dataObject.height = wmp.controls.currentItem.imageSourceHeight;
		dataObject.width = wmp.controls.currentItem.imageSourceWidth;

		dataObject.playheadPercent = (wmp.controls.currentPosition / wmp.controls.currentItem.duration);
		//dataObject.currentPosition = wmp.controls.currentPosition;
		//dataObject.currentPositionTimecode = wmp.controls.currentPositionTimecode;
		dataObject.currentPositionString = wmp.controls.currentPositionString;
		if(dataObject.currentPositionString=="") dataObject.currentPositionString=".";
		return dataObject;
	}
	catch(e){
		//
	}

}




// Mute button
function toggleMute(son) {
	var wmp = document.getElementById('contentPlayer');
	if(son==1) {
		if (wmp.settings.mute) {
			wmp.settings.mute = false;
			return wmp.settings.volume + "%";
		}
	}else {
		wmp.settings.mute = true;
		return "Mute";
	}
}


// Volume with scroll
function setVolumeScroll(valeur) {
	var wmp = document.getElementById('contentPlayer');
	if(wmp){
		var m = wmp.settings.mute;
		if((valeur>=0) && (valeur<=100)){
			wmp.settings.volume = valeur;
		}
		wmp.settings.mute=m;
		return wmp.settings.volume;
	}
}


function hide(){
	document.getElementById('wmpPlayer').style.visibility = "hidden";
}

function show(){
	document.getElementById('wmpPlayer').style.visibility = "visible";
}


// Patch
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;
}

// FullScreen ?
function canDoFullScreen() {
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("msie") != -1 || isWMVPatchInstalled()) {
		return true;
	}
	return false;
}

// FullScreen
function fullScreen() {
    var wmp = document.getElementById('contentPlayer');
    if(wmp){
    	controlAction('ecran');
	    // on IE and with the updated WMV plugin, player is defined. We can set it to full screen.
	    if (canDoFullScreen() && wmp) {
		//if (player.playState == 3)
		//{
		    wmp.fullScreen = true;

		//}
	    } else {
		//
	    }
    }
}


function veille(){
	var wmp = document.getElementById('contentPlayer');
	wmp.controls.stop();
	hide();
}


function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}



function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function initPlayer(t_dateHeure, n_typePlayer, n_idThematique, n_idChaine, n_typeEcran, n_son, n_volume, n_resolution, t_decouverte, t_pack){

		var chaineVar = new Array();
		if(t_dateHeure!="")  chaineVar["dateHeure"] = t_dateHeure;
		if(t_decouverte!="") chaineVar["decouverte"] = t_decouverte;
		if(n_typeEcran!="") chaineVar["typeEcran"] = n_typeEcran;
		if(n_typePlayer!="") chaineVar["typePlayer"] = n_typePlayer;
		if(n_volume!="") chaineVar["volume"] = n_volume;
		if(n_idThematique!="") chaineVar["idThematique"] = n_idThematique;
		if(n_idChaine!="") chaineVar["idChaine"] = n_idChaine;
		if(n_resolution!="") chaineVar["resolution"] = n_resolution;
		if(n_son!="") chaineVar["son"] = n_son;
		if(t_pack!="") chaineVar["packs"] = t_pack;

		var flashvars = chaineVar;

		if(n_typePlayer == "2") {

			var params={
				wmode: "transparent",
				allowScriptAccess: "always",
				bgcolor:"#000"
				};
			var attributes={
				id: "wmpSWF",
				name: "wmpSWF"
				};
			swfobject.embedSWF(sJSDocumentHTTP+"/webtv/player.swf","wmpViewer", "813", "480", "9.0.0", "",  flashvars, params, attributes);
			//setupWindowsMedia(0);  //typeEcran
		}else{
			var params={
				wmode: "transparent",
				allowScriptAccess: "always",
				menu: "false",
				bgcolor:"#000"
				};
			var attributes={
				id: "wmpSWF",
				name: "wmpSWF"
				};
			swfobject.embedSWF(sJSDocumentHTTP+"/webtv/player.swf","wmpViewer", "813", "480", "9.0.0", "",  flashvars, params, attributes);
			setupWindowsMedia(n_typeEcran);  //typeEcran
		}
		return;
}
