
var GnxMediaControler = {

	SendMouseClickEvents	: false
,	SendKeyboardEvents		: false

,	Play : function () {	
		
		try {
			 
			if ( !this.CheckVersion() )
				throw -1;			
			
			if ( ( +this.codeProcStatus ).equals( 0 ) ) {
				
				if ( this.BufferStatus ) {
				
					this.codeProcStatus = 1;
					this.URL			= this.get_FilePath();
					
					this.controls.play();
					this.set_Volume( this.n4Volume );

					this.addEventListner( this.Macro );					
					this.n4IntervalId	= NxScript.Interval( "GnxMediaPlayer.IntervalOperator()", 100 );
				}
				else 
					throw -4;
			}
			
			else if ( ( +this.codeProcStatus ).equals( 2 ) ) {
				
				this.codeProcStatus = 1;
				this.controls.play();
			}
		}
		catch ( ex ) {
		 
			NxScript.Current.Exception( ex ); 
		}
	}

,	Pause : function() {
		
		try {
		
			if ( !this.CheckVersion() ) 
				throw -1;
			
			this.codeProcStatus = 2;
			this.controls.Pause();
		}
		catch ( ex ) {
		 
			NxScript.Current.Exception( ex ); 
		}
	}
	
,	Stop : function () {	
		
		try
		{
			if ( !this.CheckVersion() ) 
				throw -1;
				
			this.codeProcStatus		= 0;
			this.CurrentPosition	= 0;
			
			this.controls.Stop();
			
			NxScript.clearInterval( this.n4IntervalId );
		}
		catch ( ex ) { 
		
			NxScript.Current.Exception( ex ); 
		}
	}

,	Sound : function () {
		
		this.settings.mute		= ( this.codeSoundStatus ) ? true : false;
	}

,	get_Volume : function () {
		
		return this.settings.volume;
	}

,	set_Volume : function ( /* integer */ n4Volume ) {
		
		this.settings.volume = n4Volume;
	}

,	ChangeMode : function ( /* byte */ codeModeType ) {
		
		try {
				
			if ( ( +codeModeType ).equals( 1 ) )
				this.fullScreen = true;
		}
		catch ( ex ) { 
		
			NxScript.Current.Exception( -5 ); 
		}
	}

,	get_Position : function () {

		try {
		
			return this.controls.CurrentPosition;		
		}
		catch ( e ) {
		
			return 0;
		}
	}

,	set_Position : function ( /* integer */ n4Position ) {

		try {
		
			this.controls.CurrentPosition = n4Position;		
		}
		catch ( e ) { }	
	}
	
,	get_Duration : function () {

		try {
				
			return this.currentMedia.Duration;		
		}
		catch ( e ) {
		
			return 0;
		}
	}

,	get_FilePath : function () {
		
		if ( ( +this.codeFileType ).equals( 3 ) ) {
			
			return this.strFileDownloader + "?oidFile=" + NxScript.Request.QueryString( "oidFile" );
		}
	}

,	Macro : function ( /* object */ sender ) {
		
		var _oPlayer	= NxScript.findElement( sender.strClientId );
		
		if ( _oPlayer != null ) {
			
			if ( !( +_oPlayer.playState ).equals( 3 ) ) {

				_oPlayer.Play();
			}
			else {
				
				sender.n4MacroCount = 0;
				sender.arrMediaEvents.reset();
			}
		}
	}
	
,	Visible : function ( /* bool */ isVisible ) {

		this.style.display = ( isVisible ) ? "block" : "none";
	}
};

var GnxMediaProgressHandler = {

	Move : function () {
				
		if ( NxScript.findElement( this.strClientId ) != null ) {
		
			var _n4Position		= NxScript.findElement( this.strClientId ).get_Position();
			var _n4Duration		= NxScript.findElement( this.strClientId ).get_Duration();
			
			if ( !this.isDragStart )
				NxScript.findElement( this.strTrackControlerId ).style.pixelLeft = ( _n4Position * ( this.n4TrackBarWidth - this.n4TrackControlerWidth ) ) / _n4Duration;
		}
	}
	
,	onmousedown : function () {
		
		if ( NxScript.findElement( this.strClientId ) != null 
		&& NxScript.findElement( this.strTrackControlerId ) != null
		&& NxScript.findElement( this.strTrackBarId ) != null ) {
			
			if ( event != null && event.srcElement.id.equals( this.strTrackControlerId ) ) {
			
				NxScript.findElement( this.strClientId ).Pause();
				
				this.isDragStart	= true;
				this.oldPosition	= event.clientX;
				this.oldPixel 		= NxScript.findElement( this.strTrackControlerId ).style.pixelLeft;
				
				this.onmouseup		= this.getMouseUp;
				this.onmousemove	= this.getMouseMove;
			}
			else if ( event != null && event.srcElement.id.equals( this.strTrackBarId ) ) {
			
				NxScript.findElement( this.strClientId ).Pause();
				
				var _XPos			= event.offsetX;
				var _n4Duration		= NxScript.findElement( this.strClientId ).get_Duration();
				
				if ( _XPos > 0 && _XPos <= ( this.n4TrackBarWidth - this.n4TrackControlerWidth ) ) {
				
					NxScript.findElement( this.strTrackControlerId ).style.pixelLeft = ( _XPos - ( this.n4TrackControlerWidth / 2 ) );
					NxScript.findElement( this.strClientId ).set_Position( parseInt( ( NxScript.findElement( this.strTrackControlerId ).style.pixelLeft ) * _n4Duration / ( this.n4TrackBarWidth - this.n4TrackControlerWidth ) ) );
				}
				
				NxScript.findElement( this.strClientId ).Play();
			}
		}
	}

,	getMouseUp : function () {
		
		if ( NxScript.findElement( this.strClientId ) != null 
		&& NxScript.findElement( this.strTrackControlerId ) != null
		&& NxScript.findElement( this.strTrackBarId ) != null ) {
		
			var _n4Duration = NxScript.findElement( this.strClientId ).get_Duration();
			
			NxScript.findElement( this.strClientId ).set_Position( parseInt( ( NxScript.findElement( this.strTrackControlerId ).style.pixelLeft ) * _n4Duration / ( this.n4TrackBarWidth - this.n4TrackControlerWidth ) ) );
			NxScript.findElement( this.strClientId ).Play();
		}
		
		this.isDragStart	= false;
		this.oldPosition	= 0;
		this.oldPixel 		= 0;
		
		this.onmousemove	= null;
		this.onmouseup		= null;
	}

,	getMouseMove : function () {
		
		if ( this.isDragStart && NxScript.findElement( this.strTrackControlerId ) != null ) {
			
			var _XPos = this.oldPixel + ( event.clientX - this.oldPosition );
			
			if ( _XPos > 0 && ( this.n4TrackBarWidth - this.n4TrackControlerWidth ) >= _XPos )
				NxScript.findElement( this.strTrackControlerId ).style.pixelLeft = _XPos;
		}
	}

,	Reset : function () {
		
		NxScript.findElement( this.strTrackControlerId ).style.pixelLeft = 0;
	}
};


var GnxMediaVolumeHandler =  {

	ChangePosition		: function () {
		
		if ( NxScript.findElement( this.strVolumeControlerId ) != null )
			NxScript.findElement( this.strVolumeControlerId ).style.pixelLeft = ( Math.ceil( ( this.n4Volume / 100 ) * this.n4VolumeBarWidth ) ) - ( this.n4VolumeControlerWidth / 2 );
	}

,	onmousedown : function () {
		
		if ( NxScript.findElement( this.strClientId ) != null 
		&& NxScript.findElement( this.strVolumeBarId ) != null
		&& NxScript.findElement( this.strVolumeControlerId ) != null ) {
		
			if ( event != null && event.srcElement.id.equals( this.strVolumeControlerId ) ) {
			
				this.isDragStart	= true;
				this.oldPosition	= event.clientX;
				this.oldPixel 		= NxScript.findElement( this.strVolumeControlerId ).style.pixelLeft;
				
				this.onmousemove	= this.getMouseMove;
				this.onmouseup		= this.getMouseUp;
			}
			else if ( event != null && event.srcElement.id.equals( this.strVolumeBarId ) ) {
			
				if ( !this.isDragStart && NxScript.findElement( this.strClientId ) != null ) {
				
					var _XPos = event.offsetX;
					
					if ( _XPos > this.n4VolumeControlerWidth && _XPos <= ( this.n4VolumeBarWidth - this.n4VolumeControlerWidth ) ) {
					
						NxScript.findElement( this.strVolumeControlerId ).style.pixelLeft = ( _XPos - ( this.n4VolumeControlerWidth / 2 ) );
						NxScript.findElement( this.strClientId ).set_Volume( Math.ceil( ( _XPos * this.n4VolumeMax ) / ( this.n4VolumeBarWidth - this.n4VolumeControlerWidth ) ) );
					}
				}
			}					
		}
	}

,	getMouseUp : function () {
		
		this.isDragStart	= false;
		this.oldPosition	= 0;
		this.oldPixel 		= 0;
		
		this.onmousemove	= null;
		this.onmouseup		= null;
	}
	
,	getMouseMove : function () {
		
		if ( this.isDragStart && NxScript.findElement( this.strClientId ) != null  )
		{
			var _XPos = this.oldPixel + ( event.clientX - this.oldPosition );
				
			if ( _XPos > 0 && ( this.n4VolumeBarWidth - this.n4VolumeControlerWidth ) >= _XPos )
			{
				NxScript.findElement( this.strVolumeControlerId ).style.pixelLeft = _XPos;
				NxScript.findElement( this.strClientId ).set_Volume( Math.ceil( ( _XPos * this.n4VolumeMax ) / ( this.n4VolumeBarWidth - this.n4VolumeControlerWidth ) ) );
			}
		}
	}
};

var GnxMediaEventHandler = {

	OpenState : function ( oldState, NewState ) {
	
		var _StatusViewer	= NxScript.findElement( "OpenState" );
		var	_MediaPlayer	= NxScript.findElement( GnxMediaPlayer.strClientId );

		if ( _StatusViewer != null )
			_StatusViewer.innerHTML += _MediaPlayer.openState + " : " + this.get_OpenDescription( _MediaPlayer.openState ) + "<br>";
	}
	
,	PlayState : function ( oldState, NewState ) {
		
		var _StatusViewer	= NxScript.findElement( "PlayState" );
		var	_MediaPlayer	= NxScript.findElement( GnxMediaPlayer.strClientId );
		var	_DurationViewer	= NxScript.findElement( GnxMediaPlayer.strDurationViewerId );
		var	_ProgressViewer	= NxScript.findElement( GnxMediaPlayer.strProgressViewerId );
		var	_ProgressBar	= NxScript.findElement( GnxMediaPlayer.strTrackBarId );
		
		if ( _StatusViewer != null )
			_StatusViewer.innerHTML += _MediaPlayer.playState + " : " + this.get_PlayDescription( _MediaPlayer.playState ) + "<br>";

		switch ( _MediaPlayer.playState ) {
		
			case 1 :
			
				if ( _DurationViewer != null ) _DurationViewer.Reset();
				if ( _ProgressViewer != null ) _ProgressViewer.Reset();
				if ( _ProgressBar != null ) _ProgressBar.Reset();									
				
				_MediaPlayer.codeProcStatus = 0;
				_MediaPlayer.arrMediaEvents.reset();
			
			break;
			
			case 2	: this.BufferState( true );			break;
			case 3	: this.BufferState( true );			break;
			case 6	: this.BufferState( false );		break;
			case 10 : 
				
				_MediaPlayer.codeProcStatus = 0;	
				_MediaPlayer.arrMediaEvents.reset();
				
			break;
		}
	}

,	BufferState : function ( bStatus ) {
		
		var	_MediaPlayer	= NxScript.findElement( GnxMediaPlayer.strClientId );
		var	_bufProgress	= NxScript.findElement( GnxMediaPlayer.strBufferProgressId );
		
		if ( _bufProgress != null ) {
			
			if ( bStatus )
				_bufProgress.style.display = "none";
			else
				_bufProgress.style.display = "block";
		
			_MediaPlayer.BufferStatus = bStatus;
		}
	}

,	get_OpenDescription : function ( codeOpenState ) {

		var strValue = null;

		switch ( codeOpenState )
		{
			case 0	: strValue = "Undefined";				break;
			case 1	: strValue = "PlaylistChanging";		break;
			case 2	: strValue = "PlaylistLocating";		break;
			case 3	: strValue = "PlaylistConnecting";		break;
			case 4	: strValue = "PlaylistLoading";			break;
			case 5	: strValue = "PlaylistOpening";			break;
			case 6	: strValue = "PlaylistOpenNoMedia";		break;
			case 7	: strValue = "PlaylistChanged";			break;
			case 8	: strValue = "MediaChanging";			break;
			case 9	: strValue = "MediaLocating";			break;
			case 10 : strValue = "MediaConnecting";			break;
			case 11 : strValue = "MediaLoading";			break;
			case 12 : strValue = "MediaOpening";			break;
			case 13 : strValue = "MediaOpen";				break;
			case 14 : strValue = "BeginCodecAcquisition";	break;
			case 15 : strValue = "EndCodecAcquisition";		break;
			case 16 : strValue = "BeginLicenseAcquisition"; break;
			case 17 : strValue = "EndLicenseAcquisition";	break;
			case 18 : strValue = "BeginIndividualization";	break;
			case 19 : strValue = "EndIndividualization";	break;
			case 20 : strValue = "MediaWaiting";			break;
			case 21 : strValue = "OpeningUnknownURL";		break;
		}

		return strValue;
	}

,	get_PlayDescription : function ( codePlayState ) {

		var strValue = null;

		switch ( codePlayState )
		{
			case 0	: strValue = "Undefined";				break;
			case 1	: strValue = "Stopped";					break;
			case 2	: strValue = "Paused";					break;
			case 3	: strValue = "Playing";					break;
			case 4	: strValue = "ScanForward";				break;
			case 5	: strValue = "ScanReverse";				break;
			case 6	: strValue = "Buffering";				break;
			case 7	: strValue = "Waiting";					break;
			case 8	: strValue = "MediaEnded";				break;
			case 9	: strValue = "Transitioning";			break;
			case 10 : strValue = "Ready";					break;
			case 11 : strValue = "Reconnecting";			break;
		}

		return strValue;
	}
};

var GnxMediaDurationViewHandler = {

	Write : function () {		
		
		this.innerHTML = this.TimeFormat( NxScript.findElement( this.strClientId ).get_Duration() );
	}
		
,	Reset : function () {

		this.innerHTML = this.TimeFormat( 0 );
	}
}

var GnxMediaProgressViewHandler = {
	
	Write : function () {		
		
		this.innerHTML = this.TimeFormat( NxScript.findElement( this.strClientId ).get_Position() );
	}
		
,	Reset : function () {

		this.innerHTML = this.TimeFormat( 0 );
	}
}

var GnxMediaActivate = NxScript.Classes.create();

GnxMediaActivate.prototype = {

	initialize : function () {
		
		/*
		 * codeFileType Description
		 *
		 * @codeFileType 1 : ÆÄÀÏ °æ·Î¸¦ Á÷Á¢ ¹Þ´Â´Ù.  
		 * @codeFileType 2 : oidFile ¸¸ ¹Þ´Â´Ù  
		 * @codeFileType 3 : QueryString °ª¿¡ ÀÇÁ¸ÇÏ¿© ÆÄÀÏ °æ·Î¸¦ Ã£¾Æ¿Â´Ù.  
		 */
		
		this.n4IntervalId			= 0;
		this.codeFileType			= 1;
		this.arrMediaFiles			= new NxScriptArray();
		this.arrMediaEvents			= new NxScriptArray();		

		this.strClientId			= null;
		this.n4Width				= 0;
		this.n4Height				= 0;
		this.strUIMode				= null;
		this.n4Volume				= 0;
		this.isAutoStart			= false;
		
		this.strPlayStateEventFunc	= null;
		this.strOpenStateEventFunc	= null;
		
		this.strTrackBarId			= null;
		this.n4TrackBarWidth		= 0;
		this.strTrackControlerId	= null;
		this.n4TrackControlerWidth	= null;
		
		this.strDurationViewerId	= null;		
		this.strProgressViewerID	= null;
		
		this.strVolumeBarId			= null;	
		this.n4VolumeBarWidth		= 0;	
		this.strVolumeControlerId	= null;	
		this.n4VolumeControlerWidth	= 0;
		
		this.strBufferProgressId	= null;
		
		this.codeProcStatus			= 0;
		this.codeSoundStatus		= 0;
		this.BufferStatus			= true;
		
		//this.strFileDownloader		= "http://work.nexon.com/emong/Long/nxcom/Presentationlayer/NxFile/download/MovieDownloader.aspx";	
		this.strFileDownloader		= "http://file.nexon.com/NxFile/Download/MovieDownloader.aspx";
		this.isDragStart			= false;
		this.oldPosition			= 0;
		this.oldPixel				= 0;
		this.n4VolumeMax			= 100;
		this.n4VolumeMin			= 0;

		this.n4MacroCount			= 0;
		
		NxScript.addScript( { Interval		: window.setInterval } );
		NxScript.addScript( { clearInterval : window.clearTimeout } );
		
		NxScript.addEventListner( document, { oncontextmenu : function() { return false; } } );
		NxScript.addEventListner( document, { onselectstart : function() { return false; } } );
		NxScript.addEventListner( document, { ondragstart	: function() { return false; } } );
		
		NxScript.Current.addExceptionMessage( -1, "¹Ìµð¾î ÇÃ·¹ÀÌ¾î ¹öÀüÀÌ ³·½À´Ï´Ù. ¹Ìµð¾î ÇÃ·¹ÀÌ¾î¸¦ 9.0ÀÌ»óÀ¸·Î ¾÷µ¥ÀÌÆ®ÇØ ÁÖ¼¼¿ä." );
		NxScript.Current.addExceptionMessage( -2, "Àç»ý °¡´ÉÇÑ ¹Ìµð¾î ÆÄÀÏÀÌ ¾ø½À´Ï´Ù." );
		NxScript.Current.addExceptionMessage( -3, "¹Ìµð¾î °´Ã¼°¡ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù." );
		NxScript.Current.addExceptionMessage( -4, "ÆÄÀÏ ºÒ·¯¿À´Â Áß ÀÔ´Ï´Ù. Àá½Ã¸¸ ±â´Ù¸®¼¼¿ä." );
		NxScript.Current.addExceptionMessage( -5, "Àç»ý ÈÄ Ç®½ºÅ©¸° ¸ðµå·Î º¯°æ °¡´ÉÇÕ´Ï´Ù." );
	}

,	Write : function () {

		NxScript.Current.Activate.Renderer( {  
		
				codeObjectType			: NxScript.Constants.codeObjectType_Media
			,	strClientId				: this.strClientId
			,	n4Width					: this.n4Width
			,	n4Height				: this.n4Height
			,	strUIMode				: this.strUIMode
			,	n4Volume				: this.n4Volume
			,	isAutoStart				: this.isAutoStart
			,	strPlayStateEventFunc	: this.strPlayStateEventFunc
			,	strOpenStateEventFunc	: this.strOpenStateEventFunc				
		} );
	} 

,	Execute : function () {

		if ( NxScript.findElement( this.strClientId ) != null ) {
			
			NxScript.Classes.extend( NxScript.findElement( this.strClientId ), this );
			NxScript.Classes.extend( NxScript.findElement( this.strClientId ), GnxMediaControler );
		}
		
		if ( NxScript.findElement( this.strDurationViewerId ) != null ) {
		
			NxScript.Classes.extend( NxScript.findElement( this.strDurationViewerId ), this );
			NxScript.Classes.extend( NxScript.findElement( this.strDurationViewerId ), GnxMediaDurationViewHandler );
			
			NxScript.findElement( this.strDurationViewerId ).Reset();
		}
			
		if ( NxScript.findElement( this.strProgressViewerId ) != null ) {
		
			NxScript.Classes.extend( NxScript.findElement( this.strProgressViewerId ), this );
			NxScript.Classes.extend( NxScript.findElement( this.strProgressViewerId ), GnxMediaProgressViewHandler );
			
			NxScript.findElement( this.strProgressViewerId ).Reset();	
		}
		
		if ( NxScript.findElement( this.strTrackBarId ) != null 
		&& NxScript.findElement( this.strTrackControlerId ) != null ) {
		
			NxScript.Classes.extend( NxScript.findElement( this.strTrackBarId ), this );
			NxScript.Classes.extend( NxScript.findElement( this.strTrackBarId ), GnxMediaProgressHandler );
			
			NxScript.findElement( this.strTrackBarId ).Reset();
		}
		
		if ( NxScript.findElement( this.strVolumeBarId ) != null 
		&& NxScript.findElement( this.strVolumeControlerId ) != null ) {
		
			NxScript.Classes.extend( NxScript.findElement( this.strVolumeBarId ), this );
			NxScript.Classes.extend( NxScript.findElement( this.strVolumeBarId ), GnxMediaVolumeHandler );
			
			NxScript.findElement( this.strVolumeBarId ).ChangePosition( this.n4Volume );
		}
		
		
		if ( this.isAutoStart )
			NxScript.findElement( this.strClientId ).Play();
	}

,	CheckVersion : function () {
		
		var _oPlayer = NxScript.findElement( this.strClientId );
		
		if ( _oPlayer != null && !( typeof _oPlayer.versionInfo ).equals( "undefined" ) )
		{
			var _strVersion	= _oPlayer.versionInfo;
			var _strSplit	= _strVersion.split( "." );
			
			if ( _strSplit.length > 0 && ( +_strSplit[ 0 ] ) >= 8 )
				return true;
			else
				return false;
		}
	}
	
,	TimeFormat	: function ( totalSec ) {
		
		var _second	= parseInt( totalSec ) % 60;
		var _minute	= parseInt( totalSec / 60 );
		
		return ( ( _minute < 10 ) ? "0" : "" ) + _minute + ":" + ( ( _second < 10 ) ? "0" : "" ) + _second; 
	}

,	IntervalOperator : function () {
		
		if ( NxScript.findElement( this.strTrackBarId ) != null )
			NxScript.findElement( this.strTrackBarId ).Move();
		
		if ( NxScript.findElement( this.strDurationViewerId ) != null )
			NxScript.findElement( this.strDurationViewerId ).Write(); 
		
		if ( NxScript.findElement( this.strProgressViewerId ) != null )
			NxScript.findElement( this.strProgressViewerId ).Write();

		for ( var i = 0; i < this.arrMediaEvents.count(); i++ )
			this.arrMediaEvents.row( i ).func( this );
	}

,	addEventListner : function ( ofunc ) {
	
		this.arrMediaEvents.add( { func : ofunc } );
	}	
} 
