/*
 * Copyright (c) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */

componentAudio = function(name)
{
	//inherited
	this.componentElement = componentElement;
	this.componentElement(name);

	this.nextStepWaitToEnd = true;
	this.showPlayer = false;
	this.allowNextStep = true;
	this.preloaded = false;
	
	this.wmp64 = "{22D6F312-B0F6-11D0-94AB-0080C74C7E95}";
	this.wmp64Str = "MediaPlayer.MediaPlayer.1";
	this.wmp7up = "{6BF52A52-394A-11d3-B153-00C04F79FAA6}";
	this.wmp7upStr = "WMPlayer.OCX.7";
	this.wmpPluginStr = "Windows Media Player";

	this.pvWMPnone = true;
//	this.pvWMPembed = false;
	this.pvWMP64 = false;
	this.pvWMP7up = false;

	this.hasActiveX = false;
	this.hasGeckoActiveXObject = false;

	//this.player = null;
	this.timer = null;
	this.preloadTimer = null;
	this.playTimer = null;
	this.waitingTryCount = 0;

	this.orgX = 0;
	this.orgY = 0;

	this.mediaStatus = "online";
	this.mediaURL = "";

	this.waitForPlayerTimer = null;
	
	this.showControls = false;
	this.mediaDownload = 'Media download progress:';
};

componentAudio.prototype = new componentElement;
componentAudio.prototype.type = "componentAudio";

componentAudio.prototype.onCreate = function()
{
	this.orgX = this.x;
	this.orgY = this.y;
	this.mediaStatus = (lms) ? lms.mediaStatus() : 'online';
	this.mediaURL = unescape(lms.mediaURL());
	
	if (config.withoutMultimedia) this.mediaStatus = "textOnly";
	var t = "";
	
	if (this.mediaStatus == "off" || this.mediaStatus == "textOnly" //pg2-if textOnly too
			|| PageInterface.GetValue("state") == psDesigning
			|| PageInterface.GetValue("state") == psPreview) {
		if (this.mediaStatus == "off" && !this.showIconWhenMediaOff) {
			t += "";
		}else {
			t += this.editorPaint();
		}
		this.setHTML(t);
		this.allowNextStep = true;
		//this.showPlayer = true;
		this.mediaURL += this.src;
		this.setUrl(this.mediaURL);
		this.doMute(true);		
		this.preloaded = true;
	} else if (this.mediaStatus != "off") {
		this.mediaURL += this.src;
		var inner = this.findContentObj();
		if (inner) {
			inner.innerHTML = this.initPlayer();
		}else{
			return;
		}
		//this.setHTML(this.initPlayer());
		
		//if (!this.showPlayer) this.x = -1000;
		//this.player = document.getElementById(this.id + "MediaPlayer");
		this.setUrl(this.mediaURL);
		this.doMute(true);
		this.preload();
		this.noDisplay();
	}
};

componentAudio.prototype.repaint = function()
{
	if (this.mediaStatus == "off" || this.mediaStatus == "textOnly") return;

	if (is.moz) {
		var width = 0;
		var height = 0;
		if (this.showPlayer) {
			width = this.w;
			height = this.h;
		}	
	
		var s = "";
		s += '<OBJECT id="' + this.id + 'MediaPlayer" type="application/x-ms-wmp" width="' + width + '" height="' + height + '" data="'+this.mediaURL+'">';
		s += '<PARAM name="autostart" value="false" />';
		if (this.showControls == 1){
			s += '<param name="uiMode" value="full" />';
		}else{
			s += '<param name="uiMode" value="none" />';
		};
		//s += '<PARAM name="enableContextMenu" value="false" />'; //pg2-replaced with code below
		s += '<PARAM name="enableContextMenu" value="true" />';
		//s += '<PARAM name="enabled" value="false" />'; //pg2-replaced with code below
		s += '<PARAM name="enabled" value="true" />';
		s += ' </OBJECT>';
		var inner = this.findContentObj();
		if (inner) {
			inner.innerHTML = s;
		}
	}
}
componentAudio.prototype.editorPaint = function()
{
	return '<img src="'+ resourcesURL + this.iconSrc +'" />';
}

componentAudio.prototype.onBeforeRemove = function()
{
	this.doStop();
	//var player = document.getElementById(this.id + "MediaPlayer");
	/*if (player) {
		delete this.player;
		this.player = null;
	}*/
	this.setVisible(false);
}

componentAudio.prototype.initPlayer = function()
{
	var player = null;
	try {
		if (window.ActiveXObject) {
			player = new ActiveXObject(this.wmp64Str);
		}
	} catch(e) {
	}
	if (!player) {
		try {
			player = navigator.mimeTypes["application/x-mplayer2"].enabledPlugin;
		} catch(e) {
		}
	}

	if (player) {
		player = null;
		this.pvWMPnone = false;
		try {
			if (window.ActiveXObject) {
				player = new ActiveXObject(this.wmp7upStr);
			}
			if (player) {
				var ver = player.versionInfo.split(".");
				if (Math.floor(ver[0]) >= 9) {
					this.pvWMP7up = true;
				} else {
					this.pvWMPnone = true;
				}
			}
		} catch(e) {
			this.pvWMPnone = true;
		}

		if (this.pvWMPnone) {
			this.pvWMPnone = false;
			try {
				if (window.ActiveXObject) {
					player = new ActiveXObject(this.wmp64Str);
				}
				if (player) {
					this.pvWMP64 = true;
					this.pvWMP7up = false;
				}
			} catch(e) {
				this.pvWMPnone = true;
			}
		}
	}
	
	player = null;

	// ten kod trzeba przeniesc do koponentu detekcja
	// komunikat jest uperdliwy (pokazuje siÄŹĹĽËť na kaÄŹĹĽËťdej stronie)
	// a sa przypadki ze w przegladarkach specjalnie wylaczony jest dzwiÄŹĹĽËťk (np. Windows Server)
	// lub ktos na FF nie chce instalowaÄŹĹĽËť tego dadatku.
	if (this.pvWMPnone == false) return this.getInnerHTML(); //usun ta linie jezeli odblokujesz stary kod
	//if (this.pvWMPnone && !PageInterface.GetValue("noActiveXErrorShowed") && !is.moz) {
	//	PageInterface.SetValue("noActiveXErrorShowed", true);
	//	showMessage("Twoja przeglądarka nie wspiera ActiveX lub GeckoActiveX dla poprawnego odtwarzania plików multimedialnych.\nJeżeli używasz przeglądarki Mozilla lub FireFox zainstaluj odpowiednią wtyczkę ze strony: http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx");
	//}else{
	//return this.getInnerHTML();
	//}
	
}

componentAudio.prototype.getInnerHTML = function(url)
{
	var width = 0;
	var height = 0;
	if (this.showPlayer) {
		width = this.w;
		height = this.h;
	}
		
	var s = "";
	if (this.pvWMP7up) {
		// WMP 7up code
		s += '	<OBJECT id="' + this.id + 'MediaPlayer" ';
		s += '		width="' + width + '" height="' + height + '" ';
		s += '		CLASSID=CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6 ';
		s += '		standby="Loading Microsoft Windows Media Player components..."';
		s += '		TYPE="application/x-oleobject">';
		s += '		<param name="url" VALUE="' + this.mediaURL + '" />';		
		//s += '		<param name="uiMode" value="none" />';
		if (this.showControls == 1){
			s += '		<param name="uiMode" value="full" />';
		}else{
			s += '		<param name="uiMode" value="none" />';
		};		
		s += '		<param name="Volume" value="100" />';
		s += '		<param name="ShowControls" VALUE="false" />';
		s += '		<param name="ShowTracker" VALUE="false" />';
		s += '		<param name="ShowStatusBar" VALUE="false" />';
		s += '		<param name="VideoBorder3D" VALUE="false" />';
		s += '		<param name="VideoBorderWidth" VALUE="false" />';
		s += '		<param name="AutoStart" value="false" />';
		s += '		<param name="AnimationAtStart" value="false" />';
		s += '		<param name="TransparentAtStart" value="false" />';
		s += '	</OBJECT>';
	} else
	if (this.pvWMP64) {
		// WMP 6.4 code
		// IE code
		s += '	<OBJECT id="' + this.id + 'MediaPlayer" ';
		s += '		width="' + width + '" height="' + height + '" ';
		s += '		CLASSID=CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95 ';
		s += '		CODEBASE=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715 ';
		s += '		standby="Loading Microsoft Windows Media Player components..." ';
		s += '		TYPE="application/x-oleobject">';
		s += '		<param name="Volume" value="100" />';
		s += '		<param name="FileName" VALUE="" />';
		if (this.showControls == 1){
			s += '		<param name="ShowControls" VALUE="true" />';
		}else{
			s += '		<param name="ShowControls" VALUE="false" />';
		};			
		s += '		<param name="ShowTracker" VALUE="false" />';
		s += '		<param name="ShowStatusBar" VALUE="false" />';
		s += '		<param name="AutoStart" value="false" />';
		s += '	</OBJECT>';
	} else
	if (is.moz){
		s += '<OBJECT id="' + this.id + 'MediaPlayer" type="application/x-ms-wmp" width="' + width + '" height="' + height + '" data="'+this.mediaURL+'">';
		s += '<PARAM name="autostart" value="false" />';
		if (this.showControls == 1){
			s += '<param name="uiMode" value="full" />';
		}else{
			s += '<param name="uiMode" value="none" />';
		};
		//s += '<PARAM name="enableContextMenu" value="false" />'; //pg2-replaced with code below
		s += '<PARAM name="enableContextMenu" value="true" />';
		//s += '<PARAM name="enabled" value="false" />'; //pg2-replaced with code below
		s += '<PARAM name="enabled" value="true" />';
		s += ' </OBJECT>';
	} else {
		s = this.editorPaint();
		this.allowNextStep = true;
		this.showPlayer = true;
	}
	if (!this.showPlayer) { //pg2-to prevent displaying a dot of WMP control
		s = '<div style="visibility: hidden;">' + s + '</div>';
	}
	return s;
}

componentAudio.prototype.setUrl = function(url)
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.pvWMP7up) {
			player.URL = url;
		} else if (this.pvWMP64) {
			player.FileName = url;
		} else if (is.moz && player.controls){
			player.URL = url;
			player.controls.stop();
		}
	}
}

componentAudio.prototype.stopPreload = function()
{
	this.killPreloadTimer();
	this.waitingTryCount = 0;
	this.preloaded = true;	
}

componentAudio.prototype.preload = function()
{
	if (typeof(this.src) == 'undefined' || is.moz || this.src == ""){
		this.stopPreload();
		return true;
	};	
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.mediaStatus == "off") {
			this.stopPreload();
			return true;
		}
		this.checkDownloadProgress();
	} else {
		this.stopPreload();
		return true;
	};
};

componentAudio.prototype.checkDownloadProgress = function()
{
	if(!is.ie){
		this.killPreloadTimer();
		this.preloaded = true;
		return;
	}
	
	var player = document.getElementById(this.id + "MediaPlayer");
	if (!player.network){
		this.preloaded = true;
		return;	
	}
	
	this.play();
	this.doMute(true);
	this.setVisible(false);
	var obj = null

	if (this.showMediaDowlnoadProgress){
		obj = document.getElementById("mediawaitlayer"+this.id);
		if (!obj){
			var waitlayer = document.getElementById('waitlayer');	
			var newdiv = document.createElement('div');
			newdiv.setAttribute("id", "mediawaitlayer"+this.id);		
			waitlayer.appendChild(newdiv);		
			var obj = document.getElementById("mediawaitlayer"+this.id);
		};
	};
	
	//if (this.isPlaying() && player.network.downloadProgress == 100){
	if (player.network.downloadProgress == 100){
		this.killPreloadTimer();
		this.preloaded = true;
		this.doStop();
		if(obj)
			obj.innerHTML = this.mediaDownload+' 100%';
	}else{
		if(obj && player.network.downloadProgress != 0 && player.network.downloadProgress != 100)
			obj.innerHTML = this.mediaDownload+' '+player.network.downloadProgress+'%';
		this.preloaded = false;
		this.preloadTimer = window.setTimeout('var o=page.findChildByName("' + this.id + '"); if(o) { o.checkDownloadProgress();}', 1000);	
	};
}

componentAudio.prototype.isReady = function()
{
	if (this.src == "" || typeof(this.src) == 'undefined') return true;
	
	var player = document.getElementById(this.id + "MediaPlayer");
	var pageState = PageInterface.GetValue("state");
	if (!player || pageState == psDesigning || pageState == psPreview || pageState == psView){ 
		return true;
	}
	return this.preloaded;
};

componentAudio.prototype.isActive = function()
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (!player ||PageInterface.GetValue("state") == psDesigning
			|| PageInterface.GetValue("state") == psPreview) {
		return true;
	}
	
	var res = false;
	
	if (player) {
		if (this.pvWMP7up) {
			res = (player.playState != 0 && player.playState != 1);
		} else if (this.pvWMP64) {
			res = ((player) ? player.ReadyState >= 4 : true);
		}
	}
	return res;
}

componentAudio.prototype.isPlaying = function()
{
	var res = false;
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.pvWMP7up) {
			res = (player.playState == 3);
		} else if (this.pvWMP64) {
			res = (player.PlayState == 2);
		}
	}
	
	return res;
}

componentAudio.prototype.isStopped = function()
{
	var res = false;
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.pvWMP7up) {
			res = (player.playState == 1);// || player.playState == 10);
			if (player.playState == 7) this.waitingTryCount++;
			if (this.waitingTryCount > 50) {
				this.preloaded = true;
				this.waitingTryCount = 0;
				return this.preloaded;
			}
		} else if (this.pvWMP64) {
			res = (player.playState == 0);
		} else if (is.moz){
			res = (player.playState == 1);
		}
	} else {
		res = true;
	}
	
	return res;
}

componentAudio.prototype.show = function()
{
	var result = true;
	if (typeof(this.visibleIf) != "undefined" && this.visibleIf != "") 
	{
		result = result && (eval(unescape(this.visibleIf)) == true);
	}
	if (result){
		this.firstMediaPlay();
	}
	//this.play();
};

componentAudio.prototype.hide = function()
{
	this.actionMediaStop();
	this.allowNextStep = true;
}

componentAudio.prototype.play = function()
{
	this.doPlay();
	this.invokeEvent("MediaPlay");	
	this.focus(); //pg2- otherwise in MF context menu and fullscreen (if video) doesn't work at startup
};

componentAudio.prototype.pause = function()
{
	this.doPause();
	this.invokeEvent("MediaPause");
	
};

componentAudio.prototype.stop = function()
{
	this.doStop();
};

componentAudio.prototype.killTimer = function()
{
	if (this.timer) {
		window.clearTimeout(this.timer);
		this.timer = null;
		this.invokeEvent("MediaPlay");	
	}
	if (this.playTimer) {
		window.clearTimeout(this.playTimer);
		this.playTimer = null;
	}
}

componentAudio.prototype.killPreloadTimer = function()
{
	if (this.preloadTimer) {
		window.clearTimeout(this.preloadTimer);
		this.preloadTimer = null;
	}
}

componentAudio.prototype.prepareNextStep = function()
{
	var player = document.getElementById(this.id + "MediaPlayer");
	
	if (!this.allowNextStep) {
		if (!this.nextStepWaitToEnd || !player){
			this.allowNextStep = true;
			this.timer = window.setTimeout('var o=pc.' + this.id + '; if(o) {o.prepareNextStep();}', 100);
		} else if (this.isPlaying() || this.isReady()) {
			if (player.playState == 7) this.waitingTryCount++;
			if (this.waitingTryCount > 50 || this.isStopped()) {
				this.allowNextStep = true;
				this.killTimer();
				this.waitingTryCount = 0;
				this.actionMediaStop();
				return;
			}
			this.timer = window.setTimeout('var o=pc.' + this.id + '; if(o) {o.prepareNextStep();}', 100);
		}
	}else{
		if (this.isStopped()) {
			this.killTimer();
			this.actionMediaStop();
			return;
		}
		this.timer = window.setTimeout('var o=pc.' + this.id + '; if(o) {o.prepareNextStep();}', 100);
	}
}

componentAudio.prototype.doPlay = function()
{
	if (this.mediaStatus == "off"){
		return false;
	}
	if (this.mediaStatus == "textOnly") {
		var popup = PageInterface.GetValue("getChild", "audioTextPopup");
		if (popup && this.showTextWhenMediaOff == "1") { //pg2-condition if the text should be visible
			var p = popup.findContentObj();
			p.innerHTML = this.text;
			popup.initText = function (){return this.findContentObj().innerHTML;};
			//if (!popup.visible) //pg2-removed, now is unconditional - to recalculate text's size
			popup.show();
		}
		return false;
	}
	
	this.x = this.orgX;
	this.setVisible(this.showPlayer);
	var res = false;
	var player = document.getElementById(this.id + "MediaPlayer");
	
	if (player) {
		if(PageInterface.GetValue("preloadState") != 1 && this.textVisible)
		{
			var popup = PageInterface.GetValue("getChild", "audioTextPopup");
			if (popup) {
				var p = popup.findContentObj();
				p.innerHTML = this.text;
				popup.initText = function (){return this.findContentObj().innerHTML;};
				//if (!popup.visible) //pg2-removed, to recalculate text's size
				popup.show();
			}else{
				this.showText();
			}
		}	
		
		if (!this.invokeEvent("MediaPlay")) {			
			if (!PageInterface.GetValue("pageIsShow")) return;
			this.doMute(false);		
			if (this.pvWMP7up) {
				if (player.controls) res = player.controls.play();
				if (this.nextStepWaitToEnd) this.allowNextStep = false;
			} else if (this.pvWMP64) {
				res = player.Play();
				if (this.nextStepWaitToEnd) this.allowNextStep = false;
			}else if (is.moz && player.controls){
				player.controls.play();
				if (this.nextStepWaitToEnd) this.allowNextStep = false;
			}
			this.prepareNextStep();
		} else return true;
	} else {
		this.allowNextStep = true;
		return false;
	}
	if(typeof(res)=="undefined") res = false;		
	return res;
}

componentAudio.prototype.doPause = function()
{
	var res = false;
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (!this.invokeEvent("MediaPause")) {
			if (this.pvWMP7up) {
				if (player.controls) res = player.controls.pause();
				this.waitingTryCount = 0;
			} else if (this.pvWMP64) {
				try{
				res = player.Pause();
				}catch(e){}
			}else if (is.moz && player.controls){
				player.controls.pause();
			}			
		} else return true;
	}
	
	return res;
}

componentAudio.prototype.doStop = function()
{
	var res = false;
	this.killTimer();
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.pvWMP7up) {
			if (player.controls) {
				res = player.controls.stop();
			}
			//this.allowNextStep = true; //pg2-moved below
			this.waitingTryCount = 0;
		} else
		if (this.pvWMP64) {
			res = player.Stop();
			this.setCurPos(0);
		}else 
		if (is.moz && player.controls){			
			player.controls.stop();
		}
		this.allowNextStep = true; //pg2-allow next step no mattr what browser it was launched in
		this.doMute(true);				
		this.invokeEvent("MediaStop");
	}
	return res;
}

componentAudio.prototype.doRewind = function()
{
	var res = false;
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.pvWMP7up) {
			if (player.controls) res = player.controls.fastReverse();
		} else if (this.pvWMP64) {
			res = player.FastReverse();
		}
	}
	
	return res;
}

componentAudio.prototype.doFastForward = function()
{
	var res = false;
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.pvWMP7up) {
			if (player.controls) res = player.controls.fastForward();
		} else if (this.pvWMP64) {
			res = player.FastForward();
		}
	}
	return res;
}

componentAudio.prototype.doMute = function(b)
{
	b = b == true;
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		if (this.pvWMP7up) {
			if (player.settings) player.settings.mute = b;
		} else if (this.pvWMP64) {
			player.Mute = b;
		} else if (is.moz && player.settings){
			player.settings.volume = (b)?0:100;
		}		
	}
}

componentAudio.prototype.actionVolumeUp = function(value)
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		var oldValue = player.settings.volume; 
		player.settings.volume = oldValue + value;
	}
}

componentAudio.prototype.actionVolumeDown = function(value)
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		var oldValue = player.settings.volume; 
		player.settings.volume = oldValue - value;
	}
}

componentAudio.prototype.setCurPos = function(strTime)
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		player.currentPosition = strTime;
	}
}

componentAudio.prototype.mediaLength = function()
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		return(parseInt(parseFloat(player.Duration) * 1000));
	} else {
		return 0;
	}
}

componentAudio.prototype.getClipLength = function()
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		return player.SelectionEnd;
	} else {
		return 0;
	}
}

componentAudio.prototype.getCurPos = function(getString)
{
	var curPos = 0;
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		curPos = player.currentPosition;
		if (getString) curPos = convertTimeToString(curPos);
	}
	
	return curPos;
}

componentAudio.prototype.convertTimeToString = function(dTime)
{
	var player = document.getElementById(this.id + "MediaPlayer");
	if (player) {
		dTime = new Date(1, 1, 1, 0, 0, dTime);
		hh = dTime.getHours();
		mm = dTime.getMinutes();
		ss = dTime.getSeconds();
		if (hh < 10) hh = "0" + hh;
		if (mm < 10) mm = "0" + mm;
		if (ss < 10) ss = "0" + ss;
		dTime = hh + ":" + mm + ":" + ss;
	}
	return dTime;
}

componentAudio.prototype.showText = function()
{
	if (this.text != null && this.text != "" && this.textVisible
			&& PageInterface.GetValue("state") != psDesigning
			&& PageInterface.GetValue("state") != psPreview) {
		if (this.allowGF) {
			text = '<font size="'+this.fontSize+'" face="'+this.fontFamily+'" color="'+this.fontColor+'">'+this.text+'</font>';
		}else {
			text = this.text;
		};
		PageInterface.Execute("addScrollText", text);		
	};
};

componentAudio.prototype.firstMediaPlay = function()
{
	this.doStop();
	this.playTimer = window.setTimeout('var o=pc.' + this.id + '; if(o) {o.doPlay();}', 500);
};

componentAudio.prototype.actionMediaPlay = function()
{
	this.doPlay();
};

componentAudio.prototype.actionMediaStop = function()
{
	this.setVisible(this.showPlayer);
	this.doStop();
};

componentAudio.prototype.actionMediaPause = function()
{
	this.doPause();
};

componentAudio.prototype.onFree = function()
{
	this.stop();
	this.killTimer();
	this.killPreloadTimer();
};

componentAudio.prototype.changeplayState = function()
{
	
}

componentAudio.prototype.stepStop = function()
{
	this.hide();
	this.stop();
};
