var isIeMac = navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Win")==-1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var NNA = (document.layers) ? 1 : 0;  // Netscape NAvigator
var IEX = (document.all) ? 1 : 0;  // Internet EXplorer
// 0 : STOPPED,  1 : CONNECTING, 2 : BUFFERING, 3 : PLAYING, 4 : PAUSED,  5 : SEEKING
var undefined = -1;
var stopped = 0;
var connecting = 1;
var buffering = 2;
var playing = 3;
var paused = 4;
var seeking = 5;
var iPlayListTimerCount = 0;
var iTimerDelay = 500;


					//////////////////   StartClock Method (General) //////////////////////////

function startClock_gen() {
	if (!isIeMac) {
		this.stopClock();
		this.m_iTimerId = IEX ? window.setTimeout(this.m_sName + '.checkEvents()',iTimerDelay) : window.setTimeout(this.m_sName + ".checkEvents()",iTimerDelay);
	}
}

					//////////////////   StopClock Method (General) //////////////////////////
					
function stopClock_gen() {
	if (!isIeMac) {
	    if (this.m_iTimerId != null) {
	        clearTimeout(this.m_iTimerId);
	        this.m_iTimerId = null;
	    }
    }
}

					//////////////////   InitPlayer Method (General) //////////////////////////
					
function init_gen(p_PlayerId, p_sName, p_bAutoPlay, p_iPosition) {

	 // attributes
     this.m_PlayerId = p_PlayerId;
     this.m_sName = p_sName;
     this.m_iStatus = 0;
     this.m_iLastPosition = -1;				
     this.m_iPosition = -1;
     this.m_iTotalTime = -1;
     this.m_iTimerId = null;
     this.m_sMediaFile = '';
     
     // methods
     this.play_gen = play_gen;
     this.pause_gen = pause_gen;
     this.playPause_gen = playPause_gen;
     this.stop_gen = stop_gen;
     this.goto_gen = goto_gen;
     this.checkEvents_gen = checkEvents_gen;
     
     this.startClock = startClock_gen;
     this.stopClock = stopClock_gen;
     this.init_gen = init_gen;

	if (!isIeMac) {
	
     if (p_bAutoPlay) {

	if (PlayerStatus != null)
		PlayerStatus.refresh(connecting, -1, -1, -1);

	this.play_gen();

	if (p_iPosition != null && p_iPosition != 0)
	    this.goToPosition(p_iPosition);
     }
     }
}
function play_gen(p_sFilename) {
	if (!isIeMac) {
	this.m_iStatus = playing;
	this.m_iLastPosition = -1;
	onPlayerPlay(); /// to see
	this.startClock();
	}
}

function pause_gen() {
	if (!isIeMac) {
	this.m_iStatus = paused;
	onPlayerPause(); /// to see
	}
}

function playPause_gen() {
	if (!isIeMac) {
  		this.m_iStatus == playing ? this.pause() : this.play();
  	}
}
	
function stop_gen() {
	if (!isIeMac) {
  	 this.stopClock();
  	 this.m_iStatus = stopped;
  	 onPlayerStop();
   }
}


function goto_gen(p_iPosition) {
	if (!isIeMac) {
    	if ((this.m_iStatus == stopped) || (this.m_iStatus == paused))
    		this.play(); 
    }
    	
}


function checkEvents_gen(p_iCurrentStatus, p_iCurrentPosition, p_iBufferingValue) {
	if (!isIeMac) {
	
		if (bPlayList == false && this.m_iLastPosition > 0 && p_iCurrentPosition == this.m_iTotalTime)
		{
			this.stop_gen();
			onPlayerPosition(0, this.m_iTotalTime);
			return;
		}

		this.m_iStatus = p_iCurrentStatus;
		
		this.stopClock();
		
	       if (p_iCurrentStatus == buffering)
		   onPlayerBuffering(p_iBufferingValue);
		  
	   	// restart timer
		if (p_iCurrentStatus == stopped)
		{
			p_iCurrentPosition = -1;
			onPlayerStop();
		}
		else
			this.startClock();
		   
		if (p_iCurrentPosition != this.m_iPosition)
		{
			this.m_iPosition = p_iCurrentPosition;
			onPlayerPosition(this.m_iPosition, this.m_iTotalTime);
		}
	
		if (this.m_iPosition > 0 )
			this.m_iLastPosition = this.m_iPosition;
			
		// we use this timer to check playlist changes
		if (bPlayList == true) {
		
			iPlayListTimerCount += iTimerDelay;
			
			if (iPlayListTimerCount >= 30000) {    // every minute
				iPlayListTimerCount = 0;
			
				var req = new XMLHttpRequest(); 
				if (req) { 
					req.onreadystatechange = function() { 
						if (req.readyState == 4 && req.status == 200) { 
							
							// get playlist id and last update from portal id
    						var portals = req.responseXML.getElementsByTagName("portals")[0]; // portal items

							var bPlayListFound = false;
    						for (var i = 0; i < portals.childNodes.length; i++) {
								var portal = portals.childNodes[i];
							
   								if (portal.getAttribute('id') == sPortalId) {
   									var playlist = portal.getElementsByTagName("playlist")[0];  // playlist item
   									var mediaid = playlist.getAttribute('mediaid');
   									var update = playlist.getAttribute('update');
   									
   									if (sPlayListId == '')
   										sPlayListId = mediaid;
   										
    								if (sPlayListUpdate == '')
   										sPlayListUpdate = update;  										
   									
   									if (mediaid != sPlayListId || update != sPlayListUpdate && !bUpdatePlaylist)
   										bUpdatePlaylist = true;

   									sPlayListId = mediaid;
   									sPlayListUpdate = update;
   									
   									bPlayListFound = true;
   								}
 							}
 							
 							// stop player if no more playlist broadcast
 							if (!bPlayListFound)
							{
 								Player.stop();
 								Player.fullScreen(false);
 								bFullScreenDone = false;	
							}
						} 
					}; 
					
					var oDate = new Date();
					req.open('GET', sDomainXmlEventFileUrl + '?date=' + oDate.toString()); 
					req.send(null); 
				}
			}
		}
		
	}
}



