// wmp detect

var _wmpinfo = null;

function getWmpInfo()
{
	if ( _wmpinfo==null )
		_wmpinfo = buildWmpInfo();
	return _wmpinfo;
}

function buildWmpInfo()
{
	var wi = new Object();
	wi.installed = false;
	wi.version = 0;
	wi.platform = "";
	
	// set platform ( win or linux )
	var agt=navigator.userAgent.toLowerCase();
	if ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) ) wi.platform = "win";
	if (agt.indexOf("mac")!=-1) wi.platform = "mac";
	if (agt.indexOf("linux")!=-1) wi.platform = "linux";
	
	// try activexobject 7.1
	var axobj = null;
	axobj = getActiveXObject("WMPlayer.OCX.7");
	
	if ( axobj==null )
	{
		// try activexobject 6.4
		axobj = getActiveXObject("MediaPlayer.MediaPlayer.1")
	}

	if ( axobj!=null )
	{
		wi.installed = true;
		parseFloat(axobj.versionInfo);
		wi.version=parseFloat(axobj.versionInfo);
	}
	else if (navigator.plugins && navigator.plugins.length)
	{
		
		for (var i=0; i<navigator.plugins.length; i++)
		{
			if (navigator.plugins[i].name.toLowerCase().indexOf('windows media') != -1)
			{
				
				wi.installed=true;
				break;
			}
		}
	}
	else if (navigator.mimeTypes)
	{
		
      	wi.installed = navigator.mimeTypes['application/x-mplayer2'].enabledPlugin;
	}
	return wi;
}

function getActiveXObject(id)
{
	// "try" is reserved word in NS4, use eval to hide
	var obj = null;
	if (window.ActiveXObject)
		eval ( "try { obj = new ActiveXObject(id); } catch ( e ) {}" );
	else if (window.GeckoActiveXObject)
		eval ( "try { obj = new GeckoActiveXObject(id); } catch ( e ) {}" );
	return obj;
}

function checkWmp()
{
	return ( getWmpInfo().installed );
}

function checkVersionWmp()
{
	return getWmpInfo().version;
}

// player functions

function drawVideo(src, objid, width, height, hidecontrols)
{
	if ( checkWmp() )
	{
		var h = "";
		
		h = '<embed hidden="false" ' +
		'name="' + objid + '" ' +
		'pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" ' +
		'src="' + src + '" ' +
		'type="video/x-ms-wvx" ' +
		'autostart="true" ' +
		'showcontrols="1" ' +
		'showpositioncontrols="0" ' +
		'showaudiocontrols="1" ' +
		'showtracker="0" ' +
		'showdisplay="0" ' +
		'autosize="true" ' +
		'displaysize="4" ' +
		'showstatusbar="1" ' +
		'showgotobar="0" ' +
		'showcaptioning="0" ' +
		'autorewind="1" ';
		if (src == "playlist/videohome") h += 'playcount="8" ' 
		h +=
		'animationatstart="0" ' +
		'transparentatstart="1" ' +
		'allowscan="0" ' +
		'enablecontextmenu="1" ' +
		'clicktoplay="1" ' +
		'invokeurls="0" ' +
		'defaultframe="datawindow"' + 
		'height="' + height + '" ' +
		'vspace="0" ' +
		'width="' + width + '">';
		
// 		if ( getWmpInfo().version>=7 )
// 		{
// 			h =
// 			'<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
// 			'id="' + objid + '" ' +
// 			'width="' + width + '" ' +
// 			'height="' + height + '"> ' +
// 			'<param name="url" value="' + src + '"> ' +
// 			'<param name="AutoStart" VALUE="True"> ' +
// 			'<param name="uiMode" value="' + ( hidecontrols ? "none" : "full" ) + '"> ' +
// 			'<param name="stretchToFit" value="True"> ' +
// 			'</object>';
// 		}
// 		else
// 		{
// 			h =
// 			'<object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' +
// 			'id="' + objid + '" ' +
// 			'width="' + width + '" ' +
// 			'height="' + height + '"> ' +
// 			'<param name="Filename" value="' + src + '"> ' +
// 			'<param name="AutoStart" value="1"> ' +
// 			'<param name="ShowControls" value="' + ( hidecontrols ? "0" : "1" ) + '"> ' +
// 			'<param name="ShowDisplay" value="0"> ' +
// 			'<param name="ShowStatusBar" value="0"> ' +
// 			'<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" ' +
// 			'	name="' + objid + '" ' +
// 			'	width="' + width + '" ' +
// 			'	height="' + height + '" ' +
// 			'	filename="' + src + '" ' +
// 			'	autostart="1" ' +
// 			'	showcontrols="' + ( hidecontrols ? "0" : "1" ) + '" ' +
// 			'	showdisplay="0" ' +
// 			'	showstatusbar="1"></embed> ' +
// 			'</object>';
// 		}

		document.write ( h );
	}
	else
	{
		drawWmpDownload(src);
	}
}

function fullscreenVideo( objid )
{
	var objref = getVideoObjRef(objid);
	if ( objref.playState == 3 && getWmpInfo().version>=7 )
		objref.fullScreen = "true";
}

function getVideoObjRef( objid )
{
	var objref = document[objid];
	if ( document.getElementById )
		objref = document.getElementById(objid);
	return objref;
}

function drawWmpDownload(url)
{
	document.write("<span class='masmalllightbold'><font color=white>Attenzione.<br>Windows Media Player non e' installato.<br>");
	
	if ( getWmpInfo().platform == "linux" )
	{
		document.write("<br /><a href='" + url + "'><font color='yellow'>Clicca qui</font></a><br />" +
					"per visualizzare il video<br />" +
					"con un player differente.");
	}
	else
	{
		document.write("<font color=white>Clicca qui sotto per installare<br>gratuitamente Windows Media Player.<br><br>");
		document.write("<a href='http://www.microsoft.com/windows/windowsmedia/' target='_new'><font color=yellow>Scarica Windows Media Player</font></a></span>");
	}
	
	
}

// test functions

function testWmp()
{

		alert ( "Windows Media Player rilevato: " + checkVersionWmp());

}
