//<!--BEGIN GLOBAL PLAYLIST VARIABLES-->
//media player
var mp = null;
//playlist
var pl = null;
//media item
var mi = null;
//url array for subcategory work
var subCat_urlArr = null;
//search result array for search results
var searchResults_urlArr = null;
////url array for playlist work
var pl_urlArr = null;
//clear all?
var bRemoveAll = false;
//CHANGE
var cl_mediaFileID = null;
////splash screen image
//var sSplashImage = 'http://localhost/LiveChannel/images/media/man_jumping.gif';
//var sSplashImage = 'images/media/man_jumping.gif';
//<!--END GLOBAL PLAYLIST VARIABLES-->

function GrantMediaAccess(){
	try
	{
		if (mp.settings.mediaAccessRights != 'full')
		{
			return mp.settings.requestMediaAccessRights('full')
		}
		else
		{
			return true
		}
	}
	catch(e)
	{
		return false
	}

}		
function initializeMediaPlayer(mpElement)
{
	//create mp object
	mp = mpElement; //document.mPlayer;
	
	//check media access rights, prompting for access if needed
	
    var bMediaAccess = null //GrantMediaAccess()
	
	//initial settings
	mp.settings.autoStart = 'false';
	mp.stretchToFit = 'true';
	mp.uiMode = 'full';	
	//mp.uiMode = 'full';	
	
	////show splash screen
	//showSplashScreen();
		
	return bMediaAccess
}
function showSplashScreen()
{
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//play a non playlist media item
		var mo = mp.newMedia(sSplashImage);
		
		mp.currentMedia = mo
		mp.controls.play();				
	}   
}		
function initializePlaylistInMemory(playlistName)
{
	//create empty in-memory playlist
	pl = mp.newPlaylist(playlistName, '');
	return pl;
}
function createPlaylistFromMemory(urlArr)
{
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		appendMediaItems(urlArr);			
		//assign playlist to media player
		mp.currentPlaylist = pl;			
	}
}
function playPlaylistMediaItemAuto(playlistItemIndex)
{
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//check playState and play or wait until ready
		setTimeout('checkPlayStateThenPlayAuto(' + playlistItemIndex + ')', 5);			
	}
}
function checkPlayStateThenPlayAuto(playlistItemIndex)
{

	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//playState goes from 9 to 0 to 10...if player not ready, set timer and re-check
		if (mpPlayState != 10)
		{
			setTimeout('checkPlayStateThenPlayAuto(' + playlistItemIndex + ')', 5);
		}else{ 
			//player ready; assign and play media item
			mi = mp.currentPlaylist.item(playlistItemIndex);
			mp.controls.playItem(mi);
		}				
	}

}
function playPlaylistMediaItemManual(playlistItemIndex)
{

	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//assign playlist to media player
		mp.currentPlaylist = pl;
		//assign and play media item
		mi = mp.currentPlaylist.item(playlistItemIndex);
		mp.controls.playItem(mi);
	}
}
function appendMediaItems(urlArr)
{
	var bMediaAccess = GrantMediaAccess()
	if (!bMediaAccess) return false
    if (urlArr == null) return false;
    //loop through array of URLs and append each to playlist
	for (var i=0;i<urlArr.length;i++)
	{
		appendMediaItem(urlArr[i]);
	}		
}
function appendMediaItem(aMP)
{   
	var bMediaAccess = GrantMediaAccess()
	if (!bMediaAccess) return false
	//create media object (aMP[0] = url; aMP[1] = id)
	var mo = mp.newMedia(aMP[0]);
	if(mo.isReadOnlyItem('MediaFileID') == false)
	{
		mo.setItemInfo('MediaFileID',aMP[1])
	}
	//insert into playlist
	pl.appendItem(mo);	
}
function insertMediaItem(aMP, itemIndex)
{
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//CHANGE
		//inserting 1st item into playlist
		if (pl.count < 1)
		{
			//create empty in-memory playlist
			cl_mediaFileID = aMP[1];
			itemIndex = 0 
		}else{
			cl_mediaFileID = null;
		}
				
		//create media object
		//var mo = mp.newMedia(url);
		var mo = mp.newMedia(aMP[0]);
		if(mo.isReadOnlyItem('MediaFileID') == false)
		{
			mo.setItemInfo('MediaFileID',aMP[1])
		}

		//insert into playlist
		pl.insertItem(itemIndex, mo);				
	}

}
function moveMediaItem(oldItemIndex, newItemIndex)
{
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		if (parseInt(oldItemIndex) == parseInt(newItemIndex)) { return };
		
		if (parseInt(oldItemIndex) < parseInt(newItemIndex)) //moving item low to high
		{
		    //move existing media item to new position
            pl.moveItem(parseInt(oldItemIndex), parseInt(newItemIndex) - 1);
		}else{ //moving item high to low
            //move existing media item to new position
			pl.moveItem(parseInt(oldItemIndex), parseInt(newItemIndex));
		}
    }
}
function removeMediaItems(idxArr)
{   
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//remove each entry from playlist
		for (var i=pl.count-1; i >= 0; i--)
		{
			//make sure next media item is not played
        	bRemoveAll == true;
			removeMediaItem(i);
		}			
	}
}
function removeMediaItem(itemIndex)
{
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//removing all items one at a time (called from removeMediaItems)
		if (bRemoveAll == true)
		{
			//remove media item from playlist
	    	pl.removeItem(pl.item(itemIndex));	
		}else{ 
			//removing one item
			bRemoveAll = false;
			//get play state
    		var playStateEnum = mp.playState;
    		//remove media item from playlist
    		pl.removeItem(pl.item(itemIndex));	
    		//media is currently playing
			if (playStateEnum == 3)
			{
				mpPlay(); 
			}
		}			
	}	
}
function playMediaUrl(url,MediaFileID)
{
	mp.URL = url;
	mp.controls.play();
	return;
	
	var bMediaAccess = GrantMediaAccess()
	if (bMediaAccess)
	{
		//play a non playlist media item
		var mo = mp.newMedia(url);
		if(mo.isReadOnlyItem('MediaFileID') == false)
		{
			mo.setItemInfo('MediaFileID',MediaFileID)
		} 
		
		mp.currentMedia = mo
		mp.controls.play();				
	}
}
function mpPlay()
{
	mp.controls.play();
}
function mpPause()
{
	mp.controls.pause();
	//alert(mp.controls.isAvailable('step'));
}		
function mpStop()
{
	mp.controls.stop();
}
function mpNext()
{
	mp.controls.next();
	mpPlay();
}
function mpPrev()
{
	mp.controls.previous();
	mpPlay();
}
function mpMute()
{   
	if (mp.settings.mute == true)
	{   
		mp.settings.mute = false;
	}else{
		mp.settings.mute = true;
	}
	//alert(mp.settings.mute);
	//mpPlay();
}
function mpFullScreen()
{
	// alert(mp.playState)
	if (mp.playState == 3)
	{
		mp.fullScreen = true;		        
	}
}
function mpFastReverse()
{
    //if fast reverse, switch to play
    if (mp.playState == 5)
    {
        mp.controls.play();
	}else{
	    mp.controls.fastReverse();
	}
}
function mpFastForward()
{
    //if fast forward, switch to play
    if (mp.playState == 4)
    {
        mp.controls.play();
	}else{
	    mp.controls.fastForward();
	}	
}
///////////////////////////////////////
var posDragApproved=false;
var volDragApproved=false;
var z,x,y;
var timeoutID = null;
var sliderPixelLeft = 0;
var bFirstTime = true;

//Positioning
function PositionDrag(){

    event.cancelBubble = true;
	posDragApproved=true;
	z=event.srcElement;
	
	temp1=z.style.pixelLeft;
	temp2=z.style.pixelTop;
	x=event.clientX;
	y=event.clientY;
	
	document.onmousemove=PositionMove;
}

function PositionDragMouseUp(){

    event.cancelBubble = true;
    bFirstTime = true

	SetDragPosition();
	posDragApproved=false;
}

function PositionMove(){
	if (event.button==1&&posDragApproved){
	
	    event.cancelBubble = true;
	    clearTimeout(timeoutID);
	
        var sbt = document.getElementById('slideBackgroundTable');
        var sc = document.getElementById('slideCell');


	    var nLeftLimit = 0
		var nRightLimit = parseInt(sc.width)
		
	    //z=event.srcElement
		z=document.getElementById('posSlider')
		
        //beyond left bound
    	if (z.style.pixelLeft < 0 ){ 
		    
		    z.style.pixelLeft = 0 
			sbt.style.width = 1 

			////window.status = z.style.pixelLeft
			sliderPixelLeft = z.style.pixelLeft;
			
			return
		}
        //beyond right bound
		if (z.style.pixelLeft > nRightLimit - z.width){
			
			z.style.pixelLeft = nRightLimit - z.width
			sbt.width = z.style.pixelLeft 
			
			////window.status = parseInt(z.style.pixelLeft)
			sliderPixelLeft = z.style.pixelLeft;
			
			return
		}
		//within bounds
		if (temp1+event.clientX-x >= 0)
		{
			z.style.pixelLeft = temp1+event.clientX-x
		}else{
			z.style.pixelLeft = 0
		}
		if (z.style.pixelLeft != 0)
		{
			sbt.style.width = z.style.pixelLeft 
		}else{
			sbt.style.width = 1
		}

		////window.status = parseInt(z.style.pixelLeft)

        sliderPixelLeft = z.style.pixelLeft;
        
		return false
	}
}
function PositionClick()
{
	//if (event.button==1&&dragapproved){
	    var sbt = document.getElementById('slideBackgroundTable');
	    var sc = document.getElementById('slideCell');

	    var nPositionPct = 0 
	    var nRightLimit = parseInt(sc.width) + 2
	    
	    z=document.getElementById('posSlider')
	    
	    //beyond left bound
	    if (event.offsetX < 0)
	    {
            z.style.pixelLeft = 0
            sbt.style.width = z.style.pixelLeft
            
            nPositionPct = 0
            ////window.status = parseInt(z.style.pixelLeft)
            
        }else{
            //beyond right bound
            if (event.offsetX > sc.width - z.width)
            {
                z.style.pixelLeft = sc.width - z.width
                sbt.style.width = z.style.pixelLeft
                
                nPositionPct = z.style.pixelLeft / nRightLimit
                ////window.status = parseInt(z.style.pixelLeft)
                
            //within bounds
            }else{
                z.style.pixelLeft = event.offsetX 
				if (z.style.pixelLeft != 0)
				{
					sbt.style.width = z.style.pixelLeft; 
				}else{
					sbt.style.width = 1;
				}
                nPositionPct = parseInt(z.style.pixelLeft - 10) / nRightLimit            
                ////window.status = parseInt(z.style.pixelLeft)
            }
	    }
	    
    	SetClickPosition();
		
		return false
	//}    
}
function GetPosition(){

	if (mp.currentMedia == null) { return };

	var nMediaDuration = mp.currentMedia.duration;
    var nCurrentPos = mp.Controls.currentPosition;
    
    var completedPct = nCurrentPos / nMediaDuration;
    
    var sbt = document.getElementById('slideBackgroundTable');
    var sc = document.getElementById('slideCell');
    
    //if slider not being dragged
    if (!posDragApproved)
    {
         //set position marker
        z=document.getElementById('posSlider');
        
        z.style.pixelLeft = parseInt(sc.width - z.width) * completedPct;

	    if (z.style.pixelLeft != 0)
	    {
		    sbt.style.width = z.style.pixelLeft; 
	    }else{
		    sbt.style.width = 1;
	    }
	}
	
	if (mp.controls.currentPositionString.length > 1)
	{
	    videoTime.innerText = mp.controls.currentPositionString + ' / ' + mp.currentMedia.durationString;
	}else{
	    videoTime.innerText = '';
	}
			    
    ////window.status = nCurrentPos //completedPct + ' ' + z.style.pixelLeft; 
    
    if (completedPct == 1) { return }
    
    clearTimeout(timeoutID);    
    timeoutID = window.setTimeout('GetPosition()', 10);
}
function SetClickPosition(){
	
	var nMediaDuration = mp.currentMedia.duration;
    var nCurrentPos = null;
    
    var completedPct = null;
    var sc = document.getElementById('slideCell');
        
    //slider
    z=document.getElementById('posSlider');
    
    completedPct = event.offsetX / parseInt(sc.width - z.width);
    
    nCurrentPos = completedPct * nMediaDuration;

    mp.Controls.currentPosition = nCurrentPos;
    
	if (mp.controls.currentPositionString.length > 1)
	{
	    videoTime.innerText = mp.controls.currentPositionString + ' / ' + mp.currentMedia.durationString;
	}else{
	    videoTime.innerText = '';
	}
	
	GetPosition();
	
	////window.status = z.style.pixelLeft;
}
function SetDragPosition(){
	
	var nMediaDuration = mp.currentMedia.duration;
    var nCurrentPos = null;
    
    var completedPct = null;
    
    var sc = document.getElementById('slideCell');
        
    //slider
    z=document.getElementById('posSlider');
    
    //completedPct = event.offsetX / parseInt(slideCell.width - z.width);
    //if slider not being dragged
    completedPct = sliderPixelLeft / parseInt(sc.width - z.width);
    
    //timer needs to be here or media won't sink up.
    if (bFirstTime)
    {
        bFirstTime = false;
        window.setTimeout('SetDragPosition()', 15);
        return
    }
     
    nCurrentPos = completedPct * nMediaDuration;

    mp.Controls.currentPosition = nCurrentPos;
    
	if (mp.controls.currentPositionString.length > 1)
	{
	    videoTime.innerText = mp.controls.currentPositionString + ' / ' + mp.currentMedia.durationString;
	}else{
	    videoTime.innerText = '';
	}

    GetPosition();
    	
	////window.status = sliderPixelLeft
}
////////////////////////////////////////////////
//Volume
function VolumeMove(){
	if (event.button==1&&volDragApproved){
	
	    //z=event.srcElement
	    z=document.getElementById('MediaPlayerVolume')
	    
	    var vcw = document.getElementById('VolumeControlWidth')
	
        if (z.style.pixelLeft < 0){
			z.style.pixelLeft = 0
			SetVolume(0)
			return
		}

		var nRightLimit = 0
		var nLeftLimit = vcw.width - 5

		if (z.style.pixelLeft > nLeftLimit){
			z.style.pixelLeft = nLeftLimit
			SetVolume(100)
			return
		}
		
		var nVolRange = nLeftLimit - nRightLimit

		//within bounds
		if (temp1+event.clientX-x >= 0)
		{
			z.style.pixelLeft = temp1+event.clientX-x
		}else{
			z.style.pixelLeft = 0
		} 
			
		nVolume = Math.round((z.style.pixelLeft / nVolRange) * 100)
		
		if (Math.round((z.style.pixelLeft / nVolRange) * 100) <= 0){
			var nVolume = 0
		}
		if (Math.round((z.style.pixelLeft / nVolRange) * 100) > 100){
			var nVolume = 100
		}
		
		z.title = nVolume + '%'
		
		//alert(nVolume);
		
		SetVolume(nVolume)
		
		//z.style.pixelTop=temp2+event.clientY-y
		return false
	}
}

function VolumeClick(){
	//if (event.button==1&&dragapproved){
	    z=document.getElementById('MediaPlayerVolume')
	    
	    var vcw = document.getElementById('VolumeControlWidth')
	    
	    //left bound
	    if (event.offsetX < 0 ) //z.width)
	    {
            z.style.pixelLeft= 0 //z.width / 2
        }else{
            //beyond right bound
            if (event.offsetX > vcw.width - z.width)
            {
                z.style.pixelLeft = vcw.width - z.width
            //within bounds
            }else{
                z.style.pixelLeft = event.offsetX	
            }
	    }

		var nLeftLimit = 0
		var nRightLimit = vcw.width - z.width

		var nVolRange = nRightLimit - nLeftLimit

		nVolume = Math.round((z.style.pixelLeft / nVolRange) * 100)
		
		if (Math.round((z.style.pixelLeft / nVolRange) * 100) <= 0){
			var nVolume = 0
		}
		if (Math.round((z.style.pixelLeft / nVolRange) * 100) > 100){
			var nVolume = 100
		}
		
		z.title = nVolume + '%'
		
		SetVolume(nVolume)

//		////window.status = z.style.pixelLeft
		
		return false
	//}
}

function SetVolume(nVolume){

	nVolume = parseInt(nVolume);

	if (nVolume < 0)nVolume = 0
	if (nVolume > 100)nVolume = 100
//	if (nVolume == 0)nVolume = -100

	//nVolume = nVolume * 30
	//nVolume = nVolume - 3000

	//if (mp)
	//alert(mp.settings.volume)
		mp.settings.volume = nVolume
	//alert(mp.settings.volume)		
}

function GetVolume(){
	
	var nVolume = -3000
	
	
	if (mp){
		nVolume = mp.settings.volume 
	}
	
	if (nVolume < -3000){
		nVolume = -3000
	}
	
	nVolume = (nVolume + 3000)/30
	
	var nRightLimit = 0
	var nLeftLimit = VolumeControlWidth.width - 5
	var nVolRange = nLeftLimit - nRightLimit
	var nVolLeft = Math.round((nVolume / 100) * nVolRange)
	mp.style.pixelLeft = nVolLeft
}

function VolumeDrag(){

    event.cancelBubble=true;	
	volDragApproved=true
	z=event.srcElement
	
	temp1=z.style.pixelLeft
	temp2=z.style.pixelTop
	x=event.clientX
	y=event.clientY
	document.onmousemove=VolumeMove
}

function VolumeDragMouseUp(){
    event.cancelBubble=true;
	volDragApproved=false
}

 
 		
