/**
 * Init Play All
*/

function mediamanager_playall_Init() {
	gIsPlayAllActive = true;
	gMediaManager_currentlyPlayingMediaTab = gMediaManager_currentlyOpenTab;

	// reset the array
	gPlayAllList.length = 0;
	gNextSelectionMediaList.length = 0;

	// i clone js arrays like this, otherwise they are passed by reference
	gPlayAllList = gCurrentMediaList.slice();

	// set the current params as play all params
	gPlayAllCurrentPage			= gCurrentPage;
	gPlayAllCurrentChannel 	= gChannel;
	gPlayAllCurrentCategory = gCategory;
	gPlayAllCurrentFeed			= gFeed;
	gPlayAllFavorites				= gFavorites;
	gPlayAllSearch					= gSearch;
	gPlayAllSearchType			= gSearchType;
	gPlayAllView						= gView;
	gPlayAllMediaHash				= gMediaHash;

	// if no video is playing play the next one
	if (!gCurrentlyPlayingMediaIncAds)	{
		mediamanager_playall_playNextMedia();
	}

	mediamanager_playall_setPlayStopAllLink();
}

/**
 * Play next available media
*/
function mediamanager_playall_playNextMedia() {

	mediaId = mediamanager_playall_getNextMedia();

	var playFormat;

	if (mediaId) {

		if (gMediaInfo[mediaId]['mediatype'] == 4) {
			playFormat = 8;
		}
		else {
			playFormat = 3;
		}

		if (gMediaInfo[mediaId]['mp4Url'] != '') {
			playFormat = 9;
		}

		mediamanager_playlist2_play(mediaId,'',playFormat,true);
	}
	else {
		mediamanager_playall_loadNextSelection();
	}
}

/**
 * Determine next media
*/
function mediamanager_playall_getNextMedia() {
	mediaId = 0;

	returnNextIndex = false;


	for (i=0;i<gPlayAllList.length;i++) {
		listMediaId = gPlayAllList[i];
		if (gMediaInfo[listMediaId]['showasinactive']) {
			continue;
		}

		if (gResetPlayAll || gPrevioslyPlayedMedia == 0 && gCurrentlyPlayingMediaIncAds == 0) {
			mediaId = listMediaId;
			gResetPlayAll = false;
			break;
		}

		if (returnNextIndex) {
			mediaId = listMediaId;
			break;
		}

		if (listMediaId == gPrevioslyPlayedMedia) {
			returnNextIndex = true;
		}
	}

	return mediaId;
}




/**
 * Stop Play All
*/

function mediamanager_playall_Stop() {
	gIsPlayAllActive = false;

	mediamanager_playall_setPlayStopAllLink();
}


/**
 * Reset Play All
*/

function mediamanager_playall_Reset() {
	if (gIsPlayAllActive) {
		// reset the array
		gPlayAllList.length = 0;

		// i clone js arrays like this, otherwise they are passed by reference
		gPlayAllList = gCurrentMediaList.slice();
	}
}


/**
 * Set Play All / Stop All link
*/

function mediamanager_playall_setPlayStopAllLink() {
	document.getElementById('mediamanager_playAllStopSpan').style.display = '';

	if (gIsPlayAllActive) {
		document.getElementById('mediamanager_playAllStart').style.display = 'none';
		document.getElementById('mediamanager_playAllStop').style.display = '';
	}
	else {
		// if it's live or deliveries don't show PLAY ALL
		if (gMediaManager_currentlyOpenTab == 5 || gMediaManager_currentlyOpenTab == 7) {
			document.getElementById('mediamanager_playAllStopSpan').style.display = 'none';
			document.getElementById('mediamanager_playAllStart').style.display = 'none';
			document.getElementById('mediamanager_playAllStop').style.display = 'none';
		}
		else {
			document.getElementById('mediamanager_playAllStart').style.display = '';
			document.getElementById('mediamanager_playAllStop').style.display = 'none';
		}
	}
}

function mediamanager_playall_loadNextSelection() {

	gPlayAllCurrentPage++;

	gPlayAllNextSelectionIsLoading = true;

	xajax_getMedia(gPlayAllCurrentPage,gPlayAllView,gPlayAllSearch,gPlayAllCurrentChannel,gPlayAllCurrentCategory,gPlayAllMediaHash,gPlayAllSearchType,gPlayerLocationId,0,gPlayAllCurrentFeed,0,gPlayAllFavorites,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'',0,gAffiliateSystemUrl,gUploadedByMember,gUrlAdOn,null,gCategoriesInTab);

	mediamanager_playall_checkIfNextSelectionIsLoaded();
}

function mediamanager_playall_checkIfNextSelectionIsLoaded() {
	if (!gPlayAllNextSelectionIsLoading) {
		if (gNextSelectionMediaList.length == 0) {
			mediamanager_playall_Stop();
			gResetPlayAll = true;
		}
		else {
			gPlayAllList = gPlayAllList.concat(gNextSelectionMediaList);
			mediamanager_playall_playNextMedia();
		}
	}
	else {
		setTimeout('mediamanager_playall_checkIfNextSelectionIsLoaded()',1000);
	}
}