// This will test for desired plugin.  If not found, it will test others until an appropriate one is found.

		
function PluginChooser( fmts )
{
	this.init( fmts )
};

PluginChooser.prototype.init = function(fmts)
{
	// These first few definitions point to the detection function for each player
	this.real = detectReal;
	this.qt = detectQuickTime;
	this.wm = detectWindowsMedia;
	
	// If user's preferred plugin is not found, search for other formats in this order.
	this.plugorder = new Array('real','qt','wm');
	
	// These are the friendly name of the plugins
	this.plugnames = new Array("RealMedia","QuickTime (MP3)","Windows Media");
	
	// This holds information about which plugins are installed.
	this.plugstatus = new Object;
	
	// This translates from the CMS's media_formats variable to real, qt and wm
	
	this.mediaformats = {'MP3':'qt', 'QuickTime':'qt', 'RealAudio':'real', 'RealVideo':'real','WindowsMedia':'wm'};

	var newfmts = new Object();
	var thenewfmt;
	for(afmt in fmts)
	{
		if( this.mediaformats[afmt] )	// If we know about this media format...
		{
			thenewfmt = this.mediaformats[afmt]	// Get the simpler media format...
			if(!newfmts[thenewfmt])	// Check to see that we haven't already added this format
			{
				newfmts[thenewfmt] = fmts[afmt];
			}
		}
	}
	
	this.inventory = newfmts;
	// Figure out which plugins we have and put them in plugstatus
	for(x=0;x<this.plugorder.length;x++)	{
		this.plugstatus[this.plugorder[x]] = this.canPlay(this.plugorder[x]);
	}
	this.settingshtml = "";
	this.mypref = "";
};

// This gets the URL for a particular format
PluginChooser.prototype.getFormatURL = function(fmt)	{
	return eval('this.inventory.' + fmt)
};

// This checks to see if a particular plugin is installed.
PluginChooser.prototype.isPluginInstalled = function(fmt)	{
	return eval('this.' + fmt + '()')
};

// For a particular format, test whether the media is in the inventory and whether the plugin is installed
PluginChooser.prototype.canPlay = function (fmt)	{
	var canplay = false;
	if(this.getFormatURL(fmt))	{	// Test for the plugin
		if(this.isPluginInstalled(fmt))	{
			canplay = true;
		}
	}
	return canplay;
};

// This takes as input the user's preference and selects the appropriate format based on inventory.
PluginChooser.prototype.selectFormat = function ( pref )	{
	var selectedformat = "none";
	if(this.plugstatus[pref])	{
		selectedformat = pref;
	}	else	{
		for(x=0;x<this.plugorder.length;x++)	{
			if(this.plugstatus[this.plugorder[x]])	{
				selectedformat = this.plugorder[x];
				alert("The KCRW audio or video you requested is not available in your preferred format.  It will be played using " + this.plugnames[x] + " instead."); 
				break;
			}
		}
	}
	return selectedformat;
};

// Cookie functions

PluginChooser.prototype.getCookie = function(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

PluginChooser.prototype.setCookie = function(name, value, days)	
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

PluginChooser.prototype.setMyPref = function( pref )
{
	this.mypref = pref;
};

PluginChooser.prototype.getMyPref = function()
{
	return this.mypref;
};

// Gets the HTML for the settings pane

PluginChooser.prototype.getSettingsHTML = function()
{
	var missingplugins = false;
	var html = "";
	html += '<div id="player_select">';
	html += '<h2>Choose Your Listening Format:</h2>';
	html += '<div class="player_formats">';
	for(x=0;x<this.plugorder.length;x++)
	{
		if(this.inventory[this.plugorder[x]])
		{
			html += '<div id="' + this.plugorder[x] + '">';
			if(this.plugstatus[this.plugorder[x]] == false)
			{
				html += '<span class="unavail">' + this.plugnames[x] + '*</span>';
				missingplugins = true;
			}	else
			{
				//html += '>';
				html += '<a href="javascript:;" onclick="changeSettings(\'' + this.plugorder[x] + '\');return false;"';
				if(this.plugorder[x]==this.mypref)
				{
					html += ' class="sel"';
				}
				html += '>' + this.plugnames[x] + '</a>';
			}
			html += '</div>';
		}
	}
	html += '</div>';
	if(missingplugins) html += '<div class="format_notes">* This plug-in is not installed on your computer.</div>';
	html += '</div>';
	this.settingshtml = html;
	return this.settingshtml;
}
