// FG Forrest, a.s. (c) 1996-2007
// LARI02 - WMP Evropa2

// BrowserCheck Object -- prejmenovana funkce i promenna - kvuli existujicimu objektu BrowserCheck v dyn_layer.js
function BrowserCheckEnh() {
	var o = navigator.userAgent;
	if (o.indexOf("Opera")>=0) { this.b = "Opera"; this.v = o.substring(o.indexOf("Opera")+6, o.indexOf(" ",o.indexOf("Opera")+6)); }
	else if (o.indexOf("MSIE")>=0) { this.b = "IE"; this.v = o.substring(o.indexOf("MSIE")+5, o.indexOf(";",o.indexOf("MSIE")+5)); }
	else if (o.indexOf("Netscape6")>=0) { this.b = "Netscape"; this.v = o.substring(o.indexOf("Netscape")+10, o.length); }
	else if (o.indexOf("Netscape")>=0) { this.b = "Netscape"; this.v = o.substring(o.indexOf("Netscape")+9, o.indexOf(" ",o.indexOf("Netscape")+9)); }
	else if (o.indexOf("Firefox")>=0) { this.b = "Firefox"; this.v = o.substring(o.indexOf("Firefox")+8, o.length); }
	else if (o.indexOf("Gecko")>=0) { this.b = "Mozilla"; this.v = o.substring(o.indexOf("rv:")+3, o.indexOf(")",o.indexOf("rv:")+3)); }
	this.v = (this.v.indexOf(" (") != -1)? this.v.substring(0,this.v.indexOf(" (")) : this.v;
	this.op = (this.b=="Opera" && this.v>=5);
	this.ns = (this.b=="Netscape" && this.v>=4);
	this.ns4 = (this.b=="Netscape" && parseInt(this.v)==4);
	this.ns6 = (this.b=="Netscape" && parseInt(this.v)==6);
	this.ns7 = (this.b=="Netscape" && parseInt(this.v)==7);
	this.ff = (this.b=="Firefox");
	this.mo = (this.b=="Mozilla" && parseInt(this.v)>=1);
	this.ie = (this.b=="IE" && this.v>=4);
	this.ie4 = (this.b=="IE" && parseInt(this.v)==4);
	this.ie5 = (this.b=="IE" && parseInt(this.v)==5);
	this.ie6 = (this.b=="IE" && parseInt(this.v)==6);
	this.min = (this.op||this.ns||this.mo||this.ie);
}
var isbr = new BrowserCheckEnh();

function obj(id) {
	return document.getElementById(id);
}

function chngText(id,text) {
	document.getElementById(id).innerHTML = text;
}

/******* MPlayer variables *******/

var selectVol = 50;

var timer;
var loaded = false;
var currentPath = "";
var playerState = 0;
var rewFwdStep = 0;

/********************************/

// Detection WMP and ActiveX
var is_WMP = false;
var is_WMP64 = false;
var is_WMP7up = false;
var is_WMPskin = false;
var is_WMPskin64 = false;	// skin WMP64

if (isbr.ie) {
	if (window.ActiveXObject) {
		try { var mp = new ActiveXObject("MediaPlayer.MediaPlayer.1"); is_WMP = true; is_WMP64 = true; is_WMPskin64 = true; }
		catch(e) { }
		try { mp = new ActiveXObject("WMPlayer.OCX"); is_WMP = true; is_WMP7up = true; is_WMPskin = true; }
		catch(objError) { }
	}
} else {
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-mplayer2"])? navigator.mimeTypes["application/x-mplayer2"].enabledPlugin : false;
	if (plugin) { is_WMP = true; is_WMP64 = true; is_WMPskin64 = true; }
	plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-oleobject"])? navigator.mimeTypes["application/x-oleobject"].enabledPlugin : false;
	if (!isbr.op && plugin) { is_WMPskin = true; is_WMPskin64 = true; }
}

// WMP Object
function WMP(id) {
	this.o = eval("document.mPlayer"+id);
	this.vol = selectVol;
	this.volStep = 10;
	this.muteState = true;
	this.active;
	
	this.play = play;
	this.pause = pause;
	this.stop = stop;
	this.previous = previous;
	this.next = next;
	this.ctrlAvailable = ctrlAvailable;
	this.volume = volume;
	this.volumeUpDwn = volumeUpDwn;
	this.mute = mute;
	this.url = url;
	this.playState = playState;
	this.currentPosition = currentPosition;
	this.currentPositionString = currentPositionString;
	this.duration = duration;
	this.durationString = durationString;
	this.verInfo = verInfo;
	
	function play() {
		eval("this.o"+((is_WMP7up)? ".controls" : "")+".play()");
	}
	function pause() {
		eval("this.o"+((is_WMP7up)? ".controls" : "")+".pause()");
	}
	function stop() {
		eval("this.o"+((is_WMP7up)? ".controls" : "")+".stop()");
	}
	function previous() {
		eval("this.o"+((is_WMP7up)? ".controls" : "")+".previous()");
	}
	function next() {
		eval("this.o"+((is_WMP7up)? ".controls" : "")+".next()");
	}
	function ctrlAvailable(val) {
		if (is_WMP7up) return this.o.controls.isAvailable(val);
	}
	function volume(v) {
		if (v == null) { return eval("this.o"+((is_WMP7up)? ".settings" : "")+".volume"); }
		eval("this.o"+((is_WMP7up)? ".settings" : "")+".volume = v");
		this.vol = v;
	}
	function volumeUpDwn(sign) {
		if (is_WMP7up) {
			if ((sign == "+" && this.vol < 100) || (sign == "-" && this.vol > 0)) {
				if (!this.muteState) { this.mute(); }
				var step = eval("this.vol "+sign+" this.volStep");
				if (step <= 0) obj("c3").style.backgroundPosition = "0px 0px"; // vol- - disabled
				else obj("c3").style.backgroundPosition = "0px -33px"; // vol- - enabled
				if (step >= 100) obj("c4").style.backgroundPosition = "0px 0px"; // vol- - disabled
				else obj("c4").style.backgroundPosition = "0px -33px"; // vol+ - enabled
				this.volume(step);
				obj("c5").style.backgroundPosition = "0px -25px"; // unmute
				obj("volumeBar").style.backgroundPosition = "0px " + -5 * this.vol/10 + "px";
				obj("volumeBar").title = "Hlasitost: " + this.vol + " %"; // nastaveni title
			}
		}
	}
	function mute() {
		eval("this.o"+((is_WMP7up)? ".settings" : "")+".mute = this.muteState");
		this.muteState = (this.muteState)? false : true;
	}
	function url(u) {
		if (u == null) {
			if (is_WMP7up) return this.o.URL;
			else if (is_WMP64) return this.o.FileName;
		}
		if (is_WMP7up) this.o.URL = u;
		else if (is_WMP64) this.o.FileName = u;
	}
	function playState() {
		if (is_WMP7up) return this.o.playState;
		else if (is_WMP64) return this.o.playState+1;
	}
	
	function currentPositionString() {
		if (is_WMP7up) {
			return this.o.controls.currentPositionString;
		}
		//	else if (is_WMP64) return parseInt(this.o.currentPosition);
	}
	function currentPosition(second) {
		if (is_WMP7up) {
			if (second) this.o.controls.currentPosition = second;
			else return this.o.controls.currentPosition;
		}
		//	else if (is_WMP64) return parseInt(this.o.currentPosition);
	}
	function duration() {
		if (is_WMP7up) {
			return this.o.currentMedia.duration;
		}
	}
	function durationString() {
		if (is_WMP7up) {
			return this.o.currentMedia.durationString;
		}
	}
	
	function verInfo() {
		return this.o.versionInfo;
	}
}

//ovladaci funkce
function Play() {
	
	if (!is_WMPskin || GetPlayerState(oWmp.playState()) == "Undefined") {
		externalPlay(currentPath);
		return;
	}
	if (!loaded) return;
	
	if (is_WMPskin) {
		if (oWmp.playState() != 3) {
			if (!oWmp.muteState) { oWmp.muteState = (oWmp.muteState)? false : true; oWmp.mute(); }
			obj("mPlayer1").style.visibility = "visible";
			obj("splashScreen").style.visibility = "hidden";
			obj("currPosition").style.visibility = "visible";
			obj("c1").style.backgroundPosition = "0px -90px"; // play / pause - unpause
			obj("c2").style.backgroundPosition = "0px -33px"; // stop - enabled
			posTimer();
			oWmp.play();
		} else {
			Pause();
		}
	}
}
function externalPlay(url) {
	wmp = window.open(url,"wmpWin","width=100,height=100");
	setTimeout("wmp.close()",1000);
	self.window.focus();
} 

function Pause() {
	if (!loaded || !is_WMPskin) return;
	if (oWmp.playState() != 3) return;
	obj("c1").style.backgroundPosition = "0px -45px"; // play / pause - pause style
	oWmp.pause();
}
function Stop() {
	if (!loaded || !is_WMPskin) return;
	oWmp.stop();
	
	clearInterval(timer);
	chngText('curPos', "");
	rewFwdStep = 0;
	
	oWmp.url(currentPath);
	obj("splashScreen").style.visibility = "visible";
	obj("currPosition").style.visibility = "hidden";
	obj('currPosition').style.width = "0px";
	obj("mPlayer1").style.visibility = "hidden";
	obj("c1").style.backgroundPosition = "0px -45px"; // play / pause - start
	obj("c2").style.backgroundPosition = "0px 0px"; // stop - disabled
	obj("c5").style.backgroundPosition = "0px -25px"; // unmute

	obj("volumeBar").style.backgroundPosition = "0px " + -5 * oWmp.vol/10 +"px";
	obj("volumeBar").title = "Hlasitost: " + oWmp.vol + " %"; // nastaveni title
	if (!oWmp.muteState) oWmp.muteState = true;
}

function Volume(way) {
	if (!loaded || !is_WMPskin) return;
	//if (oWmp.playState() != 3) return;
	oWmp.volumeUpDwn(way);
	selectVol = oWmp.volume();
}
function Mute() {
	if (!loaded || !is_WMPskin) return;
	//if (oWmp.playState() != 3) return;
	if (!is_WMP7up) return;
	oWmp.mute();
	
	if (!oWmp.muteState) {
		obj("c5").style.backgroundPosition = "0px -50px"; // mute
		obj("volumeBar").style.backgroundPosition = "0px 0px";
		obj("volumeBar").title = "Ztlumeno";
	} 
	else if (oWmp.muteState) {
		obj("c5").style.backgroundPosition = "0px -25px"; // unmute
		obj("volumeBar").style.backgroundPosition = "0px " + -5 * oWmp.vol/10 +"px";
		obj("volumeBar").title = "Hlasitost: " + oWmp.vol + " %"; // nastaveni title
	}
}
function posTimer() {
	if (is_WMPskin) {
		//timer = window.setInterval("chngText('curPos', oWmp.currentPositionString())",1000);
		timer = window.setInterval("updatePosition()",1000);
	}
}
function goPosition(value) {
	if (oWmp.playState() != 3) return;
	oWmp.currentPosition(value);
}

function updatePosition() {
	chngText('curPos', oWmp.currentPositionString());
	if (rewFwdStep == 0 && oWmp.duration() != 0) rewFwdStep = 300 / oWmp.duration();
	obj('currPosition').style.width = Math.round(oWmp.currentPosition()*rewFwdStep) + "px";
	//alert(oWmp.currentPosition()*rewFwdStep);
}

function getClick(e) {
	xpos = e.layerX ? e.layerX : e.offsetX ? e.offsetX : 0;
	goPosition(xpos/rewFwdStep);
}

function updateDowloaded() {
	obj('currDownloaded').style.width = 3*(obj("mPlayer1").Network.downloadProgress) + "px";
	//obj("curPos").innerHTML = "Buffering: " + obj("mPlayer1").Network.downloadProgress;
}

function makeClickable(id) {
	if (isbr.ie5) obj(id).style.cursor = "hand";
	else obj(id).style.cursor = "pointer";
}


//var is = new BrowserCheck();
// HTML
function writeWMPobject() {
	if (is_WMPskin) {
		document.writeln('<object id="mPlayer1" width="300" height="225" classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=7,0,0,1954" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">');
		document.writeln('	<param name="autoStart" value="False">');
		document.writeln('	<param name="enableContextMenu" value="false">');
		document.writeln('	<param name="uiMode" value="none">');
		document.writeln('</object>');
	} else if (is_WMP64) {
		document.writeln('<object id="mPlayer1" width="300" height="225" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">');
		document.writeln('	<param name="autoStart" value="False">');
		document.writeln('	<param name="ShowTracker" value="False">');
		document.writeln('	<param name="EnableTracker" value="False">');
		document.writeln('	<param name="ShowPositionControls" value="False">');
		document.writeln('	<param name="EnablePositionControls" value="False">');
		document.writeln('</object>');
	}

	oWmp = new WMP(1);
	var WMPmajor = (oWmp.verInfo())? parseInt(oWmp.verInfo()) : 6;
	if (!isbr.op && WMPmajor >= 7) { is_WMP7up = true; }
	else { is_WMPskin = false; }
	
}


function setClip(mediaUrl, title) {
	
	if ((!mediaUrl) || (mediaUrl == "")) return;
	currentPath = mediaUrl;
	if (title) obj("playerTitle").innerHTML = title;
	
	if (!is_WMPskin) {
		obj("c1").style.backgroundPosition = "0px -45px"; // play / pause - start
		return;
	}

	if (oWmp.playState()==3 || oWmp.playState()==2 || oWmp.playState()==1) // prozatim je checkovana i 1 (stopped) - IE problem
	{
		Stop();
	}
	
	obj("c1").style.backgroundPosition = "0px -45px"; // play / pause - start
	
	if (oWmp.vol > 0) obj("c3").style.backgroundPosition = "0px -33px"; // vol- - enabled
	if (oWmp.vol < 100) obj("c4").style.backgroundPosition = "0px -33px"; // vol+ - enabled
	obj("c5").style.backgroundPosition = "0px -25px"; // vol+ - enabled
	obj("volumeBar").style.display = "block";

	
	oWmp.url(currentPath);
	if (oWmp.url()) loaded = true;
}



// Get Player State
var psWMP7up = ["Undefined","Stopped","Paused","Playing","ScanForward","ScanReverse","Buffering","Waiting","MediaEnded","Transitioning","Ready","Reconnecting"];
function GetPlayerState(NewState) {
	if (NewState >= 0 && NewState <= 11) { return psWMP7up[NewState]; }
	else { return "Undefined"; }
}

// Mozilla ActiveX plugin
var ffPlugin = "10,101,102,103,104,105,106,107,15,20";
var moPlugin = "14,15,16,17";
function installMozAx(ver) {
	ver = new String(ver);
	if (isbr.mo || (isbr.ff && ver.substring(0,2) == 15) || (isbr.ff && ver.substring(0,2) == 20) ) { ver = ver.substring(0,2); }
	if ((isbr.ff && ffPlugin.indexOf(ver) == -1) || (isbr.mo && moPlugin.indexOf(ver) == -1)) { alert("Pro tuto verzi není plug-in k dispozici."); return; }
	var xpi = {'Mozilla ActiveX Plug-in':'http://'+window.location.host+'/file/u/plugins/'+isbr.b.toLowerCase()+'/mozactivex-'+ver+'.xpi'};
	//alert('Mozilla ActiveX Plug-in:http://'+window.location.host+'/file/u/plugins/'+isbr.b.toLowerCase()+'/mozactivex-'+ver+'.xpi');
	InstallTrigger.install(xpi);
}

function initLoading() {
	obj("volumeBar").style.backgroundPosition = "0px " + -5 * selectVol/10 + "px"; // nastaveni ukazatele volume na defaultni hodnotu
	obj("volumeBar").title = "Hlasitost: " + selectVol + " %"; // nastaveni title
	
	if ((isbr.ff || isbr.mo) && !is_WMPskin) {
		obj("state").innerHTML = "<a href=\"#\" onclick=\"installMozAx("+isbr.v.replace(/\./g,"")+"); return false\">Nainstalovat Mozilla ActiveX plugin</a>";
	}
}
