/**
 * Creates a new video player connected to a specific plugin object in the HTML code without scripting possibilities
 * @class Represents a video player with core functionality
 * @constructor
 * @param {string} elementId the HTML ID of the WMP object
 * @author Tor Erik Alræk
 * @version 1.1
 */


function WmvSimpleCorePlayer(elementId,log,subTitlesElementId,options) {
	log.debug('WmvSimpleCorePlayer created');
	
	// options:
	// {
	//   showControls: true|false
	// }
	
	// PRIVATE VARIABLES 
	var p; // player element
	var parent; // player element container
	var className = '';
	this.p = p;
	var metaUrl = '';
	var progId;
	var hostProgId;
	var live = false;
	var timeBegin;
	var startPos;
	var startAsPaused = false;
	var startMuted = false;

	var me = this;



	// CONSTRUCTOR OPERATIONS
	try {
		p = $(elementId);
		parent = p.parentNode;
		className = p.className;
		

	} catch (e) {
		log.error('SimplePlayer',e);
		log.error('SimplePlayer','Could not access HTML element for WMP');
	}

/**
 * Indicates a "status message" type
 * @type int
 */
	var MSG_STATUS = 0;
	
/**
 * Indicates a buffering progress message
 * @type int
 */
	var MSG_BUFFERING = 1;
/**
 * Indicates a error message from Windows Media Player
 * @type int
 */
	var MSG_ERROR = 2;
/**
 * Status message: The media/stream is being opened
 * @type int
 */
	var MSG_OPENING = 101;
/**
 * Status message: Got no connection when trying to open media
 * @type int
 */
	var MSG_NO_CONNECTION = 102;
/**
 * Status message: Opened wrong stream (based on progId)
 * @type int
 */
	var MSG_WRONG_PROGRAM = 103;
/**
 * Status message: Checks if live video stream has started
 * @type int
 */
	var MSG_CHECKS_IF_STARTED = 104;
/**
 * Status message: Trying to open the live stream again
 * @type int
 */
	var MSG_TRIES_AGAIN = 105;

	this.MSG_STATUS = MSG_STATUS;
	this.MSG_BUFFERING = MSG_BUFFERING;
	this.MSG_ERROR = MSG_ERROR;
	this.MSG_OPENING = MSG_OPENING;
	this.MSG_NO_CONNECTION = MSG_NO_CONNECTION;
	this.MSG_WRONG_PROGRAM = MSG_WRONG_PROGRAM;
	this.MSG_CHECKS_IF_STARTED = MSG_CHECKS_IF_STARTED;
	this.MSG_TRIES_AGAIN = MSG_TRIES_AGAIN;


	// PUBLIC functions

	this.getProgId = getProgId;
	this.getStarted = getStarted;
	this.getFinished = getFinished;
	this.start = start;
	this.play = play;
	this.pause = pause;
	this.stop = stop; 
	this.togglePlay = togglePlay;
	this.gotoFullscreen = gotoFullscreen;
	this.isFullscreen = isFullscreen;
	this.isSubtitled = isSubtitled;
	this.getBitrate = getBitrate;
	this.volumeUp = volumeUp;
	this.setVolume = setVolume;
	this.getVolume = getVolume;
	this.volumeDown = volumeDown;
	this.toggleMute = toggleMute;
	this.mute = mute;
	this.unmute = unmute;
	this.isMuted = isMuted;
	this.getPos = getPos;
	this.getDuration = getDuration;
	this.gotoPos = gotoPos;
	this.shiftPos = shiftPos;
	this.isPlaying = isPlaying;
	this.isLive = isLive;
	
	
	this.disableSubtitles = disableSubtitles;
	this.enableSubtitles = enableSubtitles;
	

	// PUBLIC METHODS
/**
 * ID of the currently playing program
 * @type int
 */
 	//this.progId = progId;
	function getProgId(){return progId};

/**
 * Set to true if video playback is really started (buffering completed)
 * @type boolean
 */
	//this.started = false; // i betydningen bufring fullført, videobilde vises

	function getStarted(){return started};
/**
 * Set to true if video playback is finished (end of video reached)
 * @type boolean
 */
	//this.finished = false;
	function getFinished(){ };


/**
 * Starts video playback from the given url
 * @param {string} url The url pointing to the media file, to be used by Windows Media Player.
 * @param {int} pProgId the video program's id, used for identifying current stream, or to get correct stream in live programs
 * @param {int} pHostProgId optional, as props.progId, but used for sub programs having a real host program
 * @param {boolean} pLive indicates that stream is live if included, and set to true
 * @param {int} startPos when starting playback, jumps playing position to the number of seconds from the start. startPos is optional.
 */
	function start(pUrl,pProgId,pHostProgId,pLive,pTimeBegin,pStartPos,pPaused,pMuted) {	
		live = pLive;
		progId = pProgId;
		hostProgId = pHostProgId;
		metaUrl = pUrl;
		startAsPaused = pPaused;
		timeBegin = pTimeBegin;
		startPos = pStartPos;
		startMuted = pMuted;
		doStart();
	}
	
	function doStart() {
		
		var p2;
				
		if (navigator.userAgent.toLowerCase().indexOf("mac")>0 && navigator.userAgent.toLowerCase().indexOf("gecko")>0) {
			log.debug('SimplePlayer','Creating plugin code for Firefox on MacOS');
			p2 = document.createElement('embed');
			p2.id = elementId;
			p2.setAttribute('class', className + ' controlsBuiltIn');
			p2.setAttribute('type','application/x-mplayer2');
			p2.setAttribute('scale','tofit');
			p2.setAttribute('src',metaUrl);
			p2.setAttribute('filename',metaUrl);
			if (startAsPaused)
				p2.setAttribute('autoplay','false')
			else
				p2.setAttribute('autoplay','true');
			p2.setAttribute('controller','true');
			if (startPos) vpNonIE.setAttribute('currentPosition',startPos);
			p2.setAttribute('pluginspage','http://www.microsoft.com/windows/windowsmedia/player/wmcomponents.mspx');
		} else {
			log.debug('VideoPlayer','Creating standards-compliant plugin code');
			var params;
			p2 = document.createElement('object');
			p2.id = elementId;
			p2.setAttribute('class', className + ' controlsBuiltIn');
			if (navigator.userAgent.toLowerCase().indexOf('opera') >= 0 && navigator.userAgent.toLowerCase().indexOf('win')>0)
				p2.setAttribute('type','application/x-ms-wmp')
			else
				p2.setAttribute('type','application/x-mplayer2');
			params = '<param name="stretchToFit" value="1"><param name="displaySize" value="4"><param name="autoSize" value="0"><param name="allowChangeDisplaySize" value="1">';
			if (options && options.showControls) {
				params += '<param name="showControls" value="1"><param name="showStatusBar" value="1">';
			} else {
				params += '<param name="showControls" value="0"><param name="showStatusBar" value="0">';
			}

			if (startPos) params += '<param name="currentPosition" value="' + startPos + '">';
			if (startMuted)
				params += '<param name="mute" value="1">'
			else
			 params += '<param name="volume" value="90">';
			
			if (startAsPaused)
				params += '<param name="autoStart" value="0"><param name="AUTOPLAY" value="false">'
			else
				params += '<param name="autoStart" value="1"><param name="AUTOPLAY" value="true">';

			params += '<param name="enableContextMenu" value="1">';
			params += '<param name="windowlessVideo" value="0"><param name="src" value="' + metaUrl + '">';
			params += '<param name="SCALE" value="tofit">';

			if (subTitlesElementId) params += '<param name="captioningID" value="' + subTitlesElementId + '">';
			if (!live) {
				params+='<param name="showTracker" value="1">';
			} else {
				params+='<param name="showTracker" value=0">';
			}
			//params += $MR('sumo.web.video.nonIE.missingPlugin');
	
			p2.innerHTML = params;
		}

		parent.replaceChild(p2,p);
		started = true;
		Event.observe(p2,'click',me.onPlayerClick,false);
		p = p2;

	}

	function writeCookie(name, value, expires, path, domain, secure) {
	  logDebug('Util.writeCookie',name + '/' + value + '/' + expires + '/' + domain + '/' + secure);
	  var curCookie = name + "=" + escape(value) +
	      ((expires) ? "; expires=" + expires.toGMTString() : "") +
	      ((path) ? "; path=" + path : "") +
	      ((domain) ? "; domain=" + domain : "") +
	      ((secure) ? "; secure" : "");
	  document.cookie = curCookie;
	}

	function readCookie(name) {
	  var dc = document.cookie;
	  var prefix = name + "=";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  } else
	    begin += 2;
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}


/**
 * Starts playback after pause or stop (requires open media)
 */
	function play() {
		return false;
	}

/**
 * Pauses playback (requires open media)
 */	
	function pause() {
		return false;	
	}

/**
 * Stops playback (requires open media) or attempt for playback, and resets playback position to 0
 */	
	function stop(close) {
		var p2 = document.createElement('div');
		p2.id = elementId;
		p2.setAttribute('class', className + ' controlsBuiltIn');

		if (p) {
			parent.replaceChild(p2,p);
		}
		p = p2;	
	}

/**
 * Toggles between playback or pause (or stop for live streams)
 * @return new state - true if paused, false if playing
 * @type boolean
 */	
	function togglePlay() {
		return false;
	}

/**
 * Makes video playback go fullscreen
 */		
	function gotoFullscreen() {
		return false;
	}

/**
 * Checks if video is playing back in fullscreen
 * @return true if fullscreen, otherwise false
 * @type boolean
 */	
	function isFullscreen() {
		return false;
	}

/**
 * Checks if video has subtitles (sami)
 * @return true if subtitled, otherwise false
 * @type boolean
 */	
	function isSubtitled() {
		return true;
	}

/**
 * Turns on subtitles
 */	
	function enableSubtitles() {
		return false;
	}

/**
 * Turns off subtitles
 */	
	function disableSubtitles() {
		return false;
	}

/**
 * Gets the current bitrate of the video playback
 * @return bitrate, in kbit/s.
 * @type int
 */	
	function getBitrate() {
		return 0;
	}


/**
 * Increases the sound level of the video playback
 * @param {int} step the amount to increase. The volume has a range of 0 to 100.
 * @return new volume setting, between 0 to 100.
 * @type int
 */	
	function volumeUp(step) {
		return false;
	}

/**
 * Sets the sound level of the video playback
 * @param {int} level The new level, in the range 0 - 100.
 */	
	function setVolume(level) {
		return false;
	}

/**
 * Gets the sound level of the video playback
 * @return The volume, in the range 0 - 100.
 * @type int
 */	
	function getVolume() {
		return 100;
	}

/**
 * Decreases the sound level of the video playback
 * @param {int} step the amount to decrease. The volume has a range of 0 to 100.
 * @return new volume setting, between 0 to 100.
 * @type int
 */	
	function volumeDown(step) {
		return false;
	}

/**
 * Toggles the mute state of the video player. If sound is muted, it will be unmuted, otherwise it will be muted.
 * @return the new mute state: True if muted, otherwise false
 * @type boolean
 */		
	function toggleMute() {
		return false;
	}



/**
 * Mutes the video player.
 * @return previous mute state
 * @type boolean
 *  */		
	function mute() {
		return false;
	}

/**
 * Unmutes the video player.
 */		
	function unmute() {
		return false;
	}

/**
 * Returns the mute state of the video player.
 * @return true if muted, otherwise false
 * @type boolean
 */		
	function isMuted() {
		return false;
	}

/**
 * Returns the position of the video player.
 * @return position in seconds
 * @type float
 */		
	function getPos() {
		return false;
	}

/**
 * Returns the length of the video.
 * @return length in seconds
 * @type float
 */		
	function getDuration() {
		return false;
	}

/**
 * Moves playback position to a given time (absolute)
 * @param {int} position the new position in seconds from the start
 */		
	function gotoPos(position) {
		return false;
	}
	
/**
 * Moves playback position by interval (relative)
 * @param {int} delta the interval in seconds
 */		
	function shiftPos(delta) {
		return false;
	}
	
	
/**
 * Returns the playback state of the video player.
 * @return true if video is playing, otherwise false
 * @type boolean
 */		
	function isPlaying() {
		return Element.visible(parent); 
	}

/**
 * Returns the stream mode for the video playing, as given in start
 * @return true if video is a live stream, otherwise false
 * @type boolean
 */		
	function isLive() {
		return live;
	}
	
/**
 * Returns the actual progId for the current clip, as reported from WMP, not the one specified in the start method
 * @return current clip's progId
 * @type integer
 */		
	this.getActualProgId = function() {
		return false;
	};
	
	

	// EVENTS
/**
 * [Callback hook]
 * Fires when status of the video player changes.
 * @param {int} type evaluates to one of the constants listed under
 * @param {string} message the message details. When type is MSG_WMP_STATUS, message contains a status text from Windows Media Player.
 * When type is MSG_WMP_BUFFERING, message contains the buffering progress given in percent.
 * When type is MSG_WMP_ERROR, message contains the error description from Windows Media Player.
 * Otherwise, message is null
 * @param {string} code only given when type is MSG_WMP_ERROR. Contains error code from Windows Media Player.
 * @type function
 */
	this.onStatusChange = null; // (type, message, code)

/**
 * [Callback hook]
 * Fires in a regular interval after video has started (first buffering complete), supplying the time position and duration of the video.
 * @param {int} position current position expressed as number of seconds after the start of the video
 * @param {int} duration the duration of the video file expressed in seconds
 * @param {string} posFormatted current position relative to the start of the video expressed in HH:mm:ss format
 * @param {string} posFormatted duration of the video file expressed in HH:mm:ss format
 * @type function
 */
	this.onPosChange = null; // (position, duration, posFormatted, durFormatted)

/**
 * [Callback hook]
 * Fires in a regular interval during video buffering
 * @param {int} progress the percentage of buffering completeness
 * @type function
 */
	this.onBufferProgress = null; // (position, duration, posFormatted, durFormatted)

/**
 * [Callback hook]
 * Fires when Windows Media Player changes it's play state
 * @param {int} newState the updated state as defined in the WMP SDK.
 * @param {int} oldState the old state before the change, as defined in the WMP SDK.
 * @type function
 */
	this.onPlayStateChange = null; // (newState, oldState)

/**
 * [Callback hook]
 * Fires when Windows Media Player changes it's open state
 * @param {int} newState the updated state as defined in the WMP SDK.
 * @param {int} oldState the old state before the change, as defined in the WMP SDK.
 * @type function
 */
	this.onOpenStateChange = null; // (newState, oldState)

/**
 * [Callback hook]
 * @ignore
 * @type function
 */
	this.onQualityChange = null; // (newQuality)

/**
 * [Callback hook]
 * Fires when a mouse button is clicked with the cursor within the WMP object.
 * For details, See the WMP SDK documentation for the click event
 * @param {int} button which mouse button being pressed
 * @param {int} shiftState the state of the shift button
 * @param {int} fX the horizontal cursor position
 * @param {int} fY the vertical cursor position
 * @type function
 */
	this.onPlayerClick = null;

/**
 * [Callback hook]
 * Fires when video starts buffering for the first time
 * @type function
 */
	this.onBufferingStart = null;

/**
 * [Callback hook]
 * Fires when video starts playing for the first time, when the first buffering is finished
 * @type function
 */
	this.onVideoStart = null;

/**
 * [Callback hook]
 * Fires when video playback reaches the end. This occurs when an on demand file is finished, or a live stream is being shut down by the publisher.
 * @type function
 */
	this.onVideoEnd = null;

/**
 * [Callback hook]
 * Fires when video object is removed from the DOM (HTML)
 * @type function
 */
	this.onCeased = null;




}