var is_flip4mac = false;
var is_minimized = false;

function play()
{
	if($.client.os == 'Mac' && !hasFlip4Mac())
	{
		$.historyLoad('/rock/fout/flip4mac-ontbreekt/');
		//loadContent('/rock/fout/flip4mac-ontbreekt/');
		return false;
	}

	if(is_flip4mac)
	{
		document.getElementById('mediaplayer').Play();
	}
	else
	{
		document.getElementById('mediaplayer').controls.play();
	}
}

function stop()
{
	if(is_flip4mac)
	{
		document.getElementById('mediaplayer').Stop();
	}
	else
	{
		document.getElementById('mediaplayer').controls.stop();
	}
}

function setVolume(volume)
{
	if(!is_flip4mac)
	{
		document.getElementById('mediaplayer').Settings.Volume = (volume * 100);
	}

	console.log('Setting volume to: ' + volume);
}

function hasFlip4Mac()
{
	var hasPlugIn = false;
	var plugIn;
	var version;
	var versionPattern = /[0-9\.]+$/;

	for(var i = 0; i < navigator.plugins.length; i++)
	{
		plugIn = $.trim(navigator.plugins[i].name);

	    if(plugIn.indexOf('Flip4Mac') != -1)
	    {
	    	version = parseFloat(versionPattern.exec(plugIn));

	    	if(version >= 2.2)
	    	{
	    		hasPlugIn = true;
				break;
			}
		}
	}

	return hasPlugIn;
}

function playerHeightUpdate(h)
{
	$('.player-box').css({height: h + 'px'});
	resizeContentBg2(h);
	resizeContentBg();
}

function renderPlayer()
{
	var html = '';

	if($.client.os == 'Mac')
	{
		if(hasFlip4Mac())
		{
			 html = '<embed enablejavascript="true" id="mediaplayer" type="application/x-mplayer2" src="' + stream + '" height="1" width="1" />';
		}

		is_flip4mac = true;
	}
	else
	{
		if($.browser.mozilla)
		{
			html += '<object id="mediaplayer" data="' + stream + '" type="application/x-ms-wmp" width="0" height="0">';
      		html += '	<param name="autostart" value="false" />';
			html += '	<param name="volume" value="50" />';
		    html += '	<p>Error - the plugin has not loaded</p>';
    		html += '</object>';
		}
		else
		{
			html += '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" height="1" id="mediaplayer" type="application/x-oleobject" width="1">';
			html += '	<param name="autostart" value="false">';
			html += '	<param name="url" value="' + stream + '">';
			html += '	<param name="showcontrols" value="false">';
			html += '	<param name="autosize" value="false">';
			html += '	<param name="showdisplay" value="false">';
			html += '	<param name="uimode" value="invisible">';
			html += '</object>';
		}
	}

	$('#mediaplayerWrapper').html('<div style="width:0; height:0; overflow:hidden;">' + html + '</div>');
	//$('#mediaplayerWrapper').html(html);
}

function togglePlayer()
{
	if(is_minimized)
	{
		$('#toggle').html('MINIMIZE');
		document.getElementById('player').maximize();
	}
	else
	{
		$('#toggle').html('MAXIMIZE');
		document.getElementById('player').minimize();
	}

	is_minimized = !is_minimized;
}

function init()
{
	//
	var flashVars = {
		isMac: ($.client.os == 'Mac') ? 1 : 0, 
		isIE: ($.client.browser == 'Explorer') ? 1 : 0, 
		player: playervariant
	};

	var params = {
		wmode: 'transparent',
		allowScriptAccess: 'always'
	};

	swfobject.embedSWF(playerswf, 'player', '824', '333', '9', '/swf/expressInstall.swf', flashVars, params, {});

	renderPlayer();
}

$(document).ready(init);