var isIeMac = navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Win")==-1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

					//////////////////   Player object  //////////////////////////
function Player_wm(p_PlayerId, p_sName, p_bAutoPlay, p_iPosition) {
    
     // specfic methods
     this.play = play_wm;
     this.pause = pause_wm;
     this.playPause = playPause_wm;
     this.stop = stop_wm;
     this.goToPosition = goto_wm;
     this.fullScreen = fullScreen_wm;
     this.checkEvents = checkEvents_wm;
     this.startPlayTimer = startPlayTimer_wm;
     this.getCurrentMediaName = getCurrentMediaName_wm;
     this.setMediaLoop = setMediaLoop_wm;
	 
     //specific attributes
     this.m_iPlayTimerId = null;
	 
	 this.m_oPlayList = new PlayList(p_PlayerId);
	 
     this.init_gen = init_gen;
     
     // general init
     this.init_gen(p_PlayerId, p_sName, p_bAutoPlay, p_iPosition);
}
					//////////////////   Play Method (windows media)    //////////////////////////

function play_wm(p_sFilename) {
  
  if (!isIeMac) {
	if (p_sFilename != null)
	{
		this.m_sMediaFile = p_sFilename;
		this.m_PlayerId.URL = p_sFilename;    
	}
	
	if (this.m_iPlayTimerId != null) {
		clearTimeout(this.m_iPlayTimerId );
		this.m_iPlayTimerId = null;
	}

			
			
	//if (this.m_PlayerId.openState != 4 && this.m_iStatus != paused)
	//	this.startPlayTimer();
	//else {
		this.m_PlayerId.controls.play();
		this.play_gen(p_sFilename);
	//}
	}
	
}

function startPlayTimer_wm() {
  if (!isIeMac) {
	this.m_iPlayTimerId = IEX ? window.setTimeout(this.m_sName + '.play()',500) : window.setTimeout(this.m_sName + ".play()",500);
	}
}

					//////////////////   PlayPause Method (windows media)    //////////////////////////


function playPause_wm() {
  if (!isIeMac) {
  this.playPause_gen();
  }
}
					//////////////////   Pause Method (windows media)    //////////////////////////
					
function pause_wm() {
  if (!isIeMac) {
	if ((this.m_PlayerId.playState < 3)/*&&(!this.m_PlayerId.controls.isBroadcast)*/) !!! warning
	{
	    this.m_PlayerId.controls.pause();
    	    this.pause_gen();
        }	
    }
}


					//////////////////   Stop Method (windows media)    //////////////////////////
				
function stop_wm() {
  if (!isIeMac) {
    this.goToPosition(0);
    this.m_PlayerId.controls.stop();  
    this.m_iStatus = stopped;
    this.stop_gen();
    }
}

					//////////////////   Goto Method (windows media)    //////////////////////////
					
function goto_wm(p_iPosition) {
  if (!isIeMac) {
     // if (this.m_PlayerId.canSeek) !!! warning
        this.m_PlayerId.controls.currentPosition = p_iPosition/1000; // pos in seconds and not ms
    this.goto_gen(p_iPosition);
    }
}

					//////////////////   CheckEvents Method (windows media)    //////////////////////////

function checkEvents_wm() {
     if (!isIeMac) {
	// check status events
	var currentstatus = this.m_iStatus;
	
	
	var iOpenState = this.m_PlayerId.openState;
	if (currentstatus != stopped && currentstatus != paused)
	{
		currentstatus = iOpenState == 4 ? connecting : currentstatus;	
		currentstatus = iOpenState == 6 ? buffering : currentstatus;
	}

	
	var bufferingvalue = this.m_PlayerId.network.bufferingProgress;
	if (currentstatus == buffering && (bufferingvalue == 100 || bufferingvalue <= 0))
	{
		bufferingvalue = -1;
		currentstatus = playing;
	}
	
	// Position event
	var currentposition =  this.m_PlayerId.controls.currentPosition*1000;
	this.m_iTotalTime = this.m_PlayerId.currentMedia.duration*1000;
	
		if (this.m_PlayerId.playState == 0)
		{
			currentposition = 0;
			currentstatus = stopped;
		}
		
	this.checkEvents_gen(currentstatus, currentposition, bufferingvalue);
	}
}

					//////////////////   FullScreen Method (Windows Media)    //////////////////////////

function fullScreen_wm(p_bDoIt)
{
  if (!isIeMac) {
  	if (p_bDoIt == false)
		this.m_PlayerId.fullScreen = 'false';
	else
		this.m_PlayerId.fullScreen = 'true';
	}
}

function getCurrentMediaName_wm()
{
	return this.m_PlayerId.currentMedia.name;
}

function setMediaLoop_wm()
{
	this.m_PlayerId.settings.setMode('loop', true);
}

function addMedia(p_oMedia)
{	
	var iIndex = 0;
	while (this.m_aoMediaList[iIndex] != null)
		iIndex++;
		
	this.m_aoMediaList[iIndex] = p_oMedia;
	
	if (iIndex == 0) // first element => create playlist
	{
		var  NomPlayList = this.m_PlayerId.newPlaylist("Playlist", "");
		this.m_PlayerId.currentPlaylist = NomPlayList;	
	}
	
	var oMedia= this.m_PlayerId.mediaCollection.Add(p_oMedia.m_sUrl);
	this.m_PlayerId.currentPlaylist.appendItem (oMedia);
	p_oMedia.m_sId = oMedia.getiteminfo("TrackingID");			// to fill id
}

function getCurrentMediaIndex()
{
	var iIndex = 0;
	while (this.m_aoMediaList[iIndex] != null)
	{
		if (this.m_aoMediaList[iIndex].m_sId == this.m_PlayerId.currentMedia.getiteminfo("TrackingID"))
			return iIndex;
		iIndex++;
	};
	
	return -1;
}

function PlayList(p_PlayerId) {

    // attributes
	this.m_aoMediaList = new Array(); 
	this.m_PlayerId = p_PlayerId;	

    //methods
	this.addMedia = addMedia;
	this.getCurrentMediaIndex = getCurrentMediaIndex;
}

function Media(p_sUrl) {
     //specific attributes
     this.m_sUrl = p_sUrl;
	 this.m_sId = "";
	 this.m_sTitle = "";
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

