var formatsWithWebcamArchive = new Object();

String.prototype.startsWith = function(str) {
	return (this.match("^" + str) == str);
}

/**
 * Event Handler when icons in 'Console Options' column is clicked.
 */
function launchWebcast(format, isSilverLightEnabled) {	
	var playerLaunchURL = playerURL + "&format=" + format;	
	var isSupportedFlashFormat = isFlashConsoleEnabled && (format.indexOf("fh") == 0 || format.indexOf("nonstreaming") != -1);			
	if (isSupportedFlashFormat && hasStandardHtmlPlayerURL) {		
		playerLaunchURL = flashConsoleURL + playerLaunchURL.substring(playerURL.indexOf("?"));
	}				
	if (isSilverLightEnabled) {
		playerLaunchURL += "&silverlight=true";
	}
	if (launchButtonBehaviour == 'replacelobby') {
		window.open(playerLaunchURL, "player", "width=" + playerWidth + ",height=" + playerHeight + ",status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1,scrollbars=1");
	} else {
		window.open(playerLaunchURL, "player", "width=" + playerWidth + ",height=" + playerHeight);
	}
	return false;
}

function launchMobileWebcast(format) {
	var playerLaunchURL = '';	
	if (!isSimuliveEvent && contentType == 'A' && isIOSDevice && mobileArchiveExists) { // OD - IOS - SFA
		playerLaunchURL = location.protocol + '//' + host + '/eventRegistration/eventRegistrationServlet';
		//playerLaunchURL = location.protocol + '//eventqa.on24.com/eventRegistration/eventRegistrationServlet';		
		playerLaunchURL += '?eventid=' + eventId + '&sessionid=' + sessionId;
		playerLaunchURL += '&key=' + audienceKey;
		playerLaunchURL += "&mobs=" + mobs + "&playerurl=" + encodeURIComponent(mobileDownloadURL);	
		playerLaunchURL += "&eventuserid=305999";	
	} else if (contentType == 'L' || (contentType == 'A' && !isIOSDevice) || (contentType == 'A' && isIOSDevice && (faaArchiveExists || webcamArchiveExists || isSimuliveEvent))) { // Live/OD - Android/OD - IOS FAA
		playerLaunchURL = location.protocol + '//' + host + '/clients/default/presentation/default.html';			
		//playerLaunchURL = location.protocol + '//eventqa.on24.com/clients/default/presentation/default.html';
		playerLaunchURL += "?format=" + format + '&mobile=true&flashsupportedmobiledevice=' + (isIOSDevice ? 'false' : 'true');
		playerLaunchURL += "&eventuserid=305999";
		playerLaunchURL += playerURL.indexOf('?') != -1 ? '&' + playerURL.substring(playerURL.indexOf('?') + 1) : '';
		if (isSimuliveEvent) {
			playerLaunchURL += "&simulive=y";			
		}
		playerLaunchURL = playerLaunchURL.replace(/&key=[a-z,0-9,A-Z]*/g,"&key=" + audienceKey);
		if (isSimuliveEvent) {
			playerLaunchURL = playerLaunchURL.replace(/&contenttype=[a-z,0-9,A-Z]*/g, "&contenttype=L");
		}		
	} 	
	if (playerLaunchURL != '') {
		window.open(playerLaunchURL, "player");
	}	
}

/**
 * Function that represents a Preview Lobby Format.
 */
function PreviewLobbyFormat(formatCode, formatName, formatDetails, disable, mobileFormat) {
	this.formatCode = formatCode;	
	this.formatName = formatName;
	this.formatDetails = formatDetails;
	this.formatImgSource = '';
	this.disable = disable;	
	this.mobileFormat = mobileFormat;
	this.setDisabled = function(disabled) {
		this.disable = disabled;
	}
	this.setFormatImgSource = function(imgSource) {
		this.formatImgSource = imgSource;
	}
	this.setFormatName = function(name) {
		this.formatName = name;
	}	
	this.setFormatDetails = function(details) {
		this.formatDetails = details;
	}
	this.setFormatCode = function(code) {
		this.formatCode = code;
	}	
	this.setMobileFormat = function(mobileFormat) {
		this.mobileFormat = mobileFormat;
	}	
}

var previewLobbyFormats = new Array();

/**
 * Update the Formats depending on the User's configuration
 */
function updateFormats() {
	var imgSourcePath = '/eventManager/images/eventstreams/';
	for (i in previewLobbyFormats) {
		var previewLobbyFormat = previewLobbyFormats[i]; 
		var formatCode = previewLobbyFormat.formatCode;
		if (formatCode.startsWith('rm') && formatCode.indexOf('nonstreaming') == -1) {
			if (isMobile || previewLobbyFormat.disable || !realPlayerDetected) {
				previewLobbyFormat.setDisabled(true);
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon-real_disabled.gif');
			} else {
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon-real.gif');
			}
		} else if (formatCode.startsWith('wm')) {
			if (isMobile || previewLobbyFormat.disable || (!windowsPlayerDetected && isSilverlightEnabled && !silverlightPlayerDetected) || (!windowsPlayerDetected && !isSilverlightEnabled)) {
				previewLobbyFormat.setDisabled(true);
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon-wmp_disabled.gif');
			} else {					
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon-wmp.gif');
			}
		} else if (formatCode.startsWith('fh')) {			
			if (previewLobbyFormat.disable || (!flashPlayerDetected && !isIOSDevice)) {
				previewLobbyFormat.setDisabled(true);
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon-flash_disabled.gif');
			} else {
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon-flash.gif');
			}
		} else if (formatCode.indexOf('nonstreaming') != -1) {				
			if (previewLobbyFormat.disable) {
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon_nonStreaming_disabled.png');
			} else {
				previewLobbyFormat.setFormatImgSource(imgSourcePath + 'icon-webconference.gif');
			}
		}
	}
}

/**
 * Render the format rows.
 */
function renderRows() {	
	
	var mobilePreviewLobbyFormats = new Array();
	
	// Render rows (both desktop and mobile)
	for (i in previewLobbyFormats) {	
		var formatImageSource = '/eventManager/images/eventstreams/';	
		var formatCode = previewLobbyFormats[i].formatCode;		
		var isFormatDisabled = previewLobbyFormats[i].disable;		
		if (isMobile) {
			previewLobbyFormats[i].setDisabled(true); // For Mobile, disable all rows
			preSelectedStreamFormat = ''; // For Mobile - no pre selection 
			if (!isFormatDisabled) {
				previewLobbyFormats[i].setFormatImgSource(formatImageSource + (formatCode.indexOf('nonstreaming') != -1 ? 'icon_nonStreaming_disabled.png' : formatCode.startsWith('fh') ? 'icon-flash_disabled.gif' : ''));
			}	
		}
		new PreviewLobbyFormatRowCreator(previewLobbyFormats[i]).create();	
		
		// If Mobile, add valid formats to mobilePreviewLobbyFormats
		if (isMobile) {			
			if ((formatCode.startsWith('rm') && formatCode.indexOf('nonstreaming') == -1) || formatCode.startsWith('wm') || (formatCode.indexOf('nonstreaming') != -1 && contentType == 'A')) { 
				continue;			
			} 
			previewLobbyFormats[i].setDisabled(isFormatDisabled); // Retain the original disabled
			mobilePreviewLobbyFormats[mobilePreviewLobbyFormats.length] = previewLobbyFormats[i];
		}
	}	
	
	if (!isMobile) {
		return;
	}	
	
	// Mobile Console Pre-Selected Format
	if (mobilePreviewLobbyFormats.length == 1) { // If there is only one format, select that
		preSelectedStreamFormat = mobilePreviewLobbyFormats[0].formatCode;
	} else { // More than 1 format		
		// If 'audio' format is available, select that
		for (k in mobilePreviewLobbyFormats) {
			if (mobilePreviewLobbyFormats[k].formatCode.indexOf('audio') != -1) {
				preSelectedStreamFormat = mobilePreviewLobbyFormats[k].formatCode;				
				break;
			}
		}
		// 'audio' format not available, select video
		if (preSelectedStreamFormat == '') {			
			for (k in mobilePreviewLobbyFormats) {
				if (mobilePreviewLobbyFormats[k].formatCode.indexOf('multi') != -1 || mobilePreviewLobbyFormats[k].formatCode.indexOf('video') != -1) {
					preSelectedStreamFormat = mobilePreviewLobbyFormats[k].formatCode;					
					break;
				}
			}
		}
	}
	
	var mobilePodcastRowCreated = false;
	for (j in mobilePreviewLobbyFormats) {		
		
		var formatImageSource = '/eventManager/images/eventstreams/';	
		var formatCode = mobilePreviewLobbyFormats[j].formatCode;		
		mobilePreviewLobbyFormats[j].setMobileFormat(true);
		
		// For Apple Devices, since Flash is not supported, remove 'FH'
		var formatDetails = mobilePreviewLobbyFormats[j].formatDetails;
		if (isIOSDevice) { 
			formatDetails = formatDetails.replace(/FH/g, '');
			formatDetails = formatDetails.replace(/FH /g, '');
		}	
		mobilePreviewLobbyFormats[j].setFormatDetails(formatDetails);		
		
		// Update Format Name according to device.
		var formatName = 'Mobile Console for ' + (isIOSDevice ? 'iOS' : 'Android') + ' - ';		
		if (formatCode.indexOf('nonstreaming') != -1 && contentType == 'L') { // Non-Streaming only for Live			
			formatImageSource += mobilePreviewLobbyFormats[j].disable ? 'icon_nonStreaming_disabled.png' : 'icon-webconference.gif';
			formatName += 'Non-Streaming';					
		} else if (formatCode.indexOf('audio') != -1 || formatCode.indexOf('video') != -1 || formatCode.indexOf('multi') != -1) {			
			if (contentType == 'A' && isIOSDevice && !isSimuliveEvent) { // If On-Demand and not simulive, if it is Apple Device.... 
				if (!faaArchiveExists && !mobileArchiveExists && !webcamArchiveExists) {
					continue; // No Archive Exists
				}
				if (mobileArchiveExists && mobilePodcastRowCreated) {
					continue; // If Mobile SFA exists, it takes precedence.  So if a row has already been created - skip others
				}
				if (faaArchiveExists && formatCode.indexOf('audio') == -1) {
					continue; // If FAA Archive exists, render row for audio format
				}
				if (webcamArchiveExists) { // If Webcam Archive Exists
					if (formatCode.indexOf('video') == -1 && formatCode.indexOf('multi') == -1) {
						continue; // Not Video format - skip
					} else if (typeof formatsWithWebcamArchive[formatCode] == 'undefined') {
						continue; // Webcam Archive doesn't exist for this format - skip
					}
				}
			}			
			formatImageSource += isIOSDevice ? 'icon-quicktime.png' : mobilePreviewLobbyFormats[j].disable ? 'icon-flash_disabled.gif' : 'icon-flash.gif';			
			formatName = contentType == 'A' && isIOSDevice && mobileArchiveExists ? 'Mobile Podcast' : formatCode.indexOf('audio') != -1 ? formatName + 'Audio' : isIPhone ? formatName + 'Audio' : formatName + 'Video';
			// For Apple Devices, for on-demand, enable format
			if (contentType == 'A' && isIOSDevice) {
				mobilePreviewLobbyFormats[j].setDisabled(false);	
				mobilePreviewLobbyFormats[j].setFormatDetails('');
			}
		}			
		
		mobilePreviewLobbyFormats[j].setFormatImgSource(formatImageSource);
		mobilePreviewLobbyFormats[j].setFormatName(formatName);
		
		new PreviewLobbyFormatRowCreator(mobilePreviewLobbyFormats[j]).create();
		// If On-Demand, if it is Apple Device, if mobile archive exists, if format is audio or video set flag to true
		if (contentType == 'A' && isIOSDevice && (formatCode.indexOf('audio') != -1 || formatCode.indexOf('video') != -1 || formatCode.indexOf('multi') != -1) && mobileArchiveExists) {
			mobilePodcastRowCreated = true;
		}	
				
	}
}

$(document).ready(function(){	
	updateFormats();	
	renderRows();
});
