/******************************************************************************
This is a part of the ieHIP Software Code.
Copyright (C) 2003-2007 A5tek Corp.
All rights reserved.
The contents of this file are subject to the ieHIP Software License Agreement Version 1.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.iehip.com/legal/sw-license/1.0/
********************************************************************************/
/*******************************************************************************
Original Author: Allan Chan
Date Created: No documented since day one.

Modified Author: Ted Lee
June 7, 2007
version 2.0.107
Modification: Added Copy Right Statement and also Function description.
********************************************************************************/
var WMP_STATE_UNDEFINED = 0;
var WMP_STATE_STOPPED = 1;
var WMP_STATE_PAUSED = 2;
var WMP_STATE_PLAYING = 3;
var WMP_STATE_SCANFORWARD = 4;
var WMP_STATE_SCANREVERSE = 5;
var WMP_STATE_BUFFERING = 6;
var WMP_STATE_WAITING = 7;
var WMP_STATE_MEDIAENDED = 8;
var WMP_STATE_TRANSITIONING = 9;
var WMP_STATE_READY = 10;
var WMP_STATE_RECONNECTING = 11;

function CWMP( oWMP, oMedia )
{
	this.WMP = oWMP;
	this.Media = oMedia;
	this.cbFunction = null;	
	this.PlayerState = null;
	
	this.mmFunction = null;
	this.retI = -1;
	this.bReverse;
	this.pbRetI = -1;
	this._intCurPosID = -1;
	this.BProgRetI;
	
	CWMP.prototype.Play = WMP_Play;
	CWMP.prototype.Stop = WMP_Stop;
	CWMP.prototype.Pause = WMP_Pause;
	CWMP.prototype.Rewind = WMP_Rewind;
	CWMP.prototype.Forward = WMP_Forward;
	CWMP.prototype.Seek = WMP_Seek;
	CWMP.prototype.Replay = WMP_Replay;
	CWMP.prototype.Skip = WMP_Skip;
	CWMP.prototype.CB = WMP_CB;
	CWMP.prototype.ShowMessage = WMP_ShowMessage;
	CWMP.prototype.PlaybackChange = WMP_PlaybackChange;
	CWMP.prototype.MM = WMP_MM;
	CWMP.prototype.Resume = WMP_Resume;
	CWMP.prototype.Hide = WMP_Hide;
	CWMP.prototype.Show = WMP_Show;
	CWMP.prototype.PlayLength = WMP_PlayLength;
	CWMP.prototype.PlayState = WMP_PlayState;
	CWMP.prototype.statusArea = WMP_statusArea;
	CWMP.prototype.AudioVolume = WMP_AudioVolume;
	CWMP.prototype.Mute = WMP_Mute;
	CWMP.prototype.Event = WMP_Event;
	
	//added by melvin
	CWMP.prototype.mediaInfo=WMP_mediaInfo;
	CWMP.prototype.durationInfo=WMP_durationInfo;
	
	if(typeof(globalVolume)!='undefined') { // failsafe
		if(globalVolume!=null) 
			this.AudioVolume(globalVolume); // apply current user prefered volume level 
		else
			this.AudioVolume(); // get the current default player volume
	}
}

function WMP_Event(i_jonstring)
{
	/*
	alert("get it");
	var t_jonstring=i_jonstring.toJSONString();
	YAHOO.log(NewState); 
	*/
}
// callback
function WMP_CB( obj )
{
	//if(this.cbFunction == null ) return;
	if(obj.PlayerState == undefined ) return;
	UNI_CB( obj.PlayerState, obj );
	//this.cbFunction(obj);
}

function WMP_MM( o )
{
	if(this.mmFunction == null ) return;
	
	this.mmFunction( o );
}

function WMP_Play()
{
	
					YAHOO.log("WMP_Play");
	try {
		var WMP = this.WMP;
		/*
		var oUni = oUniPlayer;
				oUni.dsInfoArea('visible');
			*/	
		switch(this.PlayerState) {
			case 'pause':
				WMP.controls.play();
				break;
			case 'play':
				WMP.controls.pause();
				break;
			case 'forward':
			case 'rewind':
				clearInterval(this.retI);
				WMP.controls.play();
				break;
			default:
			//	WMP.duration=2000;

				WMP.URL = this.Media.URL;
				WMP.uiMode = 'none';				
				WMP.Play();				
			
		}
	}
	catch(er) {
	}
}

function WMP_Resume() {	
	var WMP = this.WMP;	
	//WMP.controls.currentPosition = parent.intWMP_lastPos;	
	WMP.controls.play();
}

function WMP_Pause()
{
	var WMP = this.WMP;
	
	clearInterval(this.retI);
	WMP.controls.pause();
}

function WMP_Stop()
{
	this.statusArea('');
	clearInterval(this.pbRetI);
	clearInterval(this.BProgRetI);
	clearInterval(this.retI);
	if (this.PlayState() > WMP_STATE_STOPPED)
	{
		var WMP = this.WMP;
		WMP.controls.stop();
		WMP.uiMode = 'invisible';
	}
}

function WMP_Rewind()
{
	var WMP = this.WMP;
		
	if(WMP.controls.isAvailable("FastReverse")) {
		WMP.controls.fastReverse();
	}
	else {
		clearInterval(this.retI);
		this.bReverse = true;
		this.retI = setInterval(function() {
												WMP_Seek(oUniPlayer, 5);
											}, 250);
		this.PlayerState = 'rewind';	
	}
}

function WMP_Forward()
{
	var WMP = this.WMP;
	
	if(WMP.controls.isAvailable("FastForward")) {
		WMP.controls.fastForward();
	}
	else {
		clearInterval(this.retI);
		this.bReverse = false;
		this.retI = setInterval(function() {
												WMP_Seek(oUniPlayer, 5);
											}, 250);
		this.PlayerState = 'forward';	
	}
}

function WMP_Replay()
{
	var WMP = this.WMP;

	clearInterval(this.retI);
	this.bReverse = true;
	this.Seek(oUniPlayer, 30);	
}

function WMP_Skip()
{
	var WMP = this.WMP;

	clearInterval(this.retI);
	this.bReverse = false;
	this.Seek(oUniPlayer, 30);	
}


function WMP_Seek(oUni, iPos)
{
	var CWMP = oUni.CurrPlayer;
	var oWMP = CWMP.WMP;
	if(!oWMP)  return;
	
	// slidebar routine
	if(iPos <= 1) { // slidebar
		var newPos = oWMP.currentMedia.duration * iPos;
		oWMP.controls.currentPosition = newPos;
		return;
	}
	// end slidebar routine
	
	// DMM emulation
	var iSpeed = iPos;
	if(oUni.CurrPlayer.bReverse)  {
		if(oWMP.controls.currentPosition > 1)
			oWMP.controls.currentPosition -= iSpeed; // rewind / replay
		else
			CWMP.Play();
	}
	else
		oWMP.controls.currentPosition += iSpeed; // forward / skip
}

function WMP_Hide()
{
	var WMP = this.WMP;
	
	WMP.uiMode = 'invisible';
}

function WMP_Show()
{
	var WMP = this.WMP;
	
	WMP.uiMode = 'none';
}

function WMP_PlayLength()
{
	var WMP = this.WMP;
	
	return WMP.currentMedia.duration;
}

function WMP_PlayState()
{
	var WMP = this.WMP;
	
	return WMP.playState;
}

function WMP_AudioVolume(val)
{
	var WMP = this.WMP;
	var interval = 5;
	var currVolume = WMP.settings.volume;
	
	
	if(val!=undefined && !isNaN(val)) {
		WMP.settings.volume = val * interval;	
		currVolume = WMP.settings.volume;
	}
	var retVolume = currVolume / interval;
	
	if(typeof(globalVolume)!='undefined') // failsafe
		globalVolume = retVolume; // index volume level as user prefered
					
	return retVolume;
}

function WMP_Mute(bool)
{
	var WMP = this.WMP;
	var currMute = WMP.settings.mute;
		  
	if(bool==undefined)
		return currMute; 
		
	switch(bool) {
		case true:
			WMP.settings.mute = true;
			currMute = WMP.settings.mute;
			break;
		case false:
			WMP.settings.mute = false;
			currMute = WMP.settings.mute;
			break;
		default:
			return currMute; 	
	}
	
	return currMute; 
}

// EVENTS
function WMP_PlayStateChange(oUni, NewState )
{
	try {
		var CWMP = oUni.CurrPlayer;
		var WMP = CWMP.WMP;
		var o = new Object();
				o.oUni = oUni;
		
		if(WMP.error.errorCount) return;
		
		switch(NewState) {
			case WMP_STATE_UNDEFINED:
				clearInterval(CWMP._intCurPosID);
				break;
			case WMP_STATE_STOPPED:
				CWMP.PlayerState = 'stop';
				o.splashOn = true;
				o.on = false;
				o.PlayerState = CWMP.PlayerState;
				clearInterval(CWMP._intCurPosID);
				CWMP.CB(o);
				break;
			case WMP_STATE_PAUSED:
				CWMP.PlayerState = 'pause';
				clearInterval(CWMP._intCurPosID);
                o.PlayerState = CWMP.PlayerState;
                CWMP.CB(o);
				break;
			case WMP_STATE_PLAYING:
				CWMP.statusArea('');
				clearInterval(CWMP.pbRetI);
				CWMP.pbRetI = setInterval(function () {try{
					var currPos = WMP.controls.currentPosition;
					var duration = WMP.currentMedia.duration;
					var slideValue = currPos / duration;
					CWMP.PlaybackChange(oUniPlayer, slideValue)}catch(errr){}
				}, 1000);

				clearInterval(CWMP._intCurPosID);				
				//CWMP._intCurPosID = setInterval("parent.intWMP_lastPos = getCurrentPosition(WMP)", 50);
				
				CWMP.PlayerState = 'play';
				//WMP_Message('Play');
				o.imgFile = '';
				o.on = false;
                o.PlayerState = CWMP.PlayerState;
				o.splashOn = false;
				CWMP.CB(o);
				break;
			case WMP_STATE_SCANFORWARD:
				CWMP.PlayerState = 'forward';
				clearInterval(CWMP._intCurPosID);
				break;
			case WMP_STATE_SCANREVERSE:
				CWMP.PlayerState = 'reverse';
				clearInterval(CWMP._intCurPosID);
				break;
			case WMP_STATE_BUFFERING:
				CWMP.statusArea('Buffering...');
				o.imgFile = 'buffering.gif';
				o.on = true;
				clearInterval(CWMP._intCurPosID);
				CWMP.CB(o);
				break;
			case WMP_STATE_WAITING:
				CWMP.statusArea('Waiting...');
				o.imgFile = 'connecting.gif';
				o.on = true;
				CWMP.CB(o);
				break;
			case WMP_STATE_MEDIAENDED:
				WMP.uiMode = 'invisible';
				CWMP.PlayerState = 'end';
				o.oUni = oUni;
				o.on = false;
				o.PlayerState = CWMP.PlayerState;
				clearInterval(CWMP._intCurPosID);
				clearInterval(CWMP.pbRetI);
				clearInterval(CWMP.retI);
				CWMP.CB(o);
				break;
			case WMP_STATE_TRANSITIONING:
				CWMP.statusArea('Contacting...');
				o.imgFile = 'connecting.gif';
				o.on = true;
				clearInterval(CWMP._intCurPosID);
				CWMP.CB(o);
				break;
			case WMP_STATE_READY:
				//CWMP.statusArea('Ready...');
				o.imgFile = '';
				o.on = false;
				CWMP.CB(o);
				break;
			case WMP_STATE_RECONNECTING:
				CWMP.statusArea('Reconnecting...');
				o.imgFile = 'connecting.gif';
				o.on = true;
				CWMP.CB(o);
				break;
		}
	}
	catch(er) {
	}
}

function WMP_Buffering(oUni, Start)
{
	var CWMP = oUni.CurrPlayer;
	
	if(Start)
		CWMP.BProgRetI = setInterval("WMP_BufferProgress()", 1000);
	else {
		clearInterval(CWMP.BProgRetI);	
		if(CWMP.statusArea != undefined)
			CWMP.statusArea('')
	}
}

function WMP_BufferProgress()
{
	var CWMP = oUniPlayer.CurrPlayer;
	var WMP = CWMP.WMP;
	
	if(CWMP.statusArea != undefined) {
		if(WMP.network != null)
			CWMP.statusArea('Buffering: ' + WMP.network.BufferingProgress + '%');
	}
}

function WMP_statusArea(msg)
{
	parent.parent.IOclick("statusArea", msg);
}

function WMP_Error(oUni)
{
	var CWMP = oUni.CurrPlayer;
			CWMP.PlayerState = 'end';
			
	var WMP = CWMP.WMP;
	var ErrorCode = parseInt(WMP.error.item(0).errorCode);
	var Description = parseInt(WMP.error.item(0).errorDescription);
	
	var o = new Object();
			o.imgFile = 'FailedToConnect.gif';
			o.on = true;
			o.oUni = oUni;
	var ob = {
		's':''	
	}
	
	switch(ErrorCode) {
		case -1072885385: // media device ejected 
			o.Error = {
				'Code':ErrorCode, 'Description': Description 
			};
			ob.s = 'Disc has been removed';
			break;
		default:
	}
	
	CWMP.CB(o);
	CWMP.ShowMessage(ob);
	WMP_statusArea('');
}

function WMP_PositionChange(oUni, oldPosition, newPosition)
{
	oldPosition;
	newPosition;
}

function WMP_PlaybackChange(oUni, slideValue)
{
	var CWMP = oUni.CurrPlayer;
	var WMP = CWMP.WMP;
	
	//parent.infoArea(WMP.controls.currentPositionString);
	//if(safeToCall('parent.setValue')) {
	//	parent.setValue(parent.NodeObject('slider-input-1'), slideValue);
	//	parent.setValue(parent.NodeObject('slider-input-2'), slideValue);
	//}
}

function WMP_MouseMove(oUni, nButton, nShiftState, fX, fY)
{
	window.focus(); // fix to avoid the wmp video frame float
	var CWMP = oUni.CurrPlayer;
	var o = new Object();
			o.oUni = oUni;
			o.nButton = nButton;
			o.nShiftState = nShiftState
			o.clientX = fX;
			o.clientY = fY;
	
	CWMP.MM(o);
}

function WMP_GotFocus(oUni)
{
	//if (parent.isDS())
	//{
	//	parent.objInputFocus();
	//}
	//else
	//{
	//	parent.objInputFocus.throwFocus();
	//}
}

// Helper functions
function WMP_ShowMessage(o)
{
	//if(parent.infoArea != null)
	//	parent.infoArea(o.s);
	//clearTimeout(this.timerId);
	
	//if(o.suspend==undefined) { 
	//	this.timerId = setTimeout(function() {
	//		parent.infoArea('');
	//	}, 5000);
	//}
}

function WMP_mediaInfo()
{
	var o = WMP;
	if(o==null)
		return null;
	var item = o.currentMedia;
	if(item==null)
		return null;
		
	var m = new cmediaInfo();	
	m.title=item.name;
	m.artist=item.getItemInfo("WM/AlbumArtist");
	m.album=item.getItemInfo("WM/AlbumTitle");
	m.year=item.getItemInfo("WM/Year");
	return m;
}

function WMP_durationInfo()
{
	var o = WMP;
	if(o==null)
		return 0;
	if(o.currentMedia==null)
		return 0;
	var d=o.currentMedia.duration;
	if(m>0)
		return 0;
	
	var k=Math.floor(d-o.controls.currentPosition);	
	return k;
}

function WMP_toggleScreenDisplay() {
	var WMP = this.WMP;
	var blnSilly = WMP.fullScreen; //ge;-, another weird behavior of WMP. Store PARAM FULLSCREEN value to override (somehow) WMP's native toggleFullscreen on DoubleClick
	WMP.fullScreen = false;
	UNI_toggleScreenDisplay();
} //function WMP_toggleScreenDisplay()


//public functions and classes

function toDurString(i)
{
	var d=new Date(2006,1,1,0,0,i);
	var s=d.toTimeString().substr(3,5);
	return s;
}

function cmediaInfo()
{
	this.title='';
	this.artist='';
	this.album='';
	this.year=0;
	this.duration=0;
	this.position=0;
	this.durationString='';
}

function getCurrentPosition(oWMP) {	
	return oWMP.controls.currentPosition;					
}