/**
 * vxFlashPlayer.js 
 *
 * Helper functions for ROO Media Player
 *
 * (c) 2007 ROO Group
 *
 */

function vxLoadIMU(sURL)
{
	var IMUDiv = document.getElementById("vxFlashPlayerSyncIMUFrame");
	if( IMUDiv != null ) 
	{
		IMUDiv.innerHTML = '<iframe src="' + sURL + '" id="vxFlashPlayerIMUContent" style="width:100%; height:100%;	position:relative; border:0px; overflow:hidden;" scrolling="no" border="0" frameborder="0" marginheight="0" marginwidth="0" noresize"></iframe>';
		IMUDiv.style.display = "block";
	}
}

function vxLoadStaticIMU(sURL)
{
	var IMUDiv = document.getElementById("vxFlashPlayerStaticIMUFrame");
	if( IMUDiv != null ) 
	{
		IMUDiv.innerHTML = '<iframe src="' + sURL + '" id="vxFlashPlayerStaticIMUContent" style="width:100%; height:100%; position:relative; border:0px; overflow:hidden;" scrolling="no" border="0" frameborder="0" marginheight="0" marginwidth="0" noresize"></iframe>';
		IMUDiv.style.display = "block";
	}
}

function vxCloseIMU()
{
	vxCloseIMUById("vxFlashPlayerSyncIMUFrame");
}

function vxCloseAllIMU()
{
	vxCloseIMUById("vxFlashPlayerSyncIMUFrame");
	vxCloseIMUById("vxFlashPlayerStaticIMUFrame");
}

function vxCloseIMUById(id)
{
	var IMUDiv = document.getElementById(id);
	if( IMUDiv != null ) 
	{
		IMUDiv.style.display = "none";
		IMUDiv.innerHTML = "";
	}
}

/**
 * Shows or Hides all IMUs.  This is used during Fullscreen operation.
 */
function vxShowIMU(bShow)
{
	var sVisibility = bShow ? "visible" : "hidden";
	var IMUDiv = document.getElementById("vxFlashPlayerSyncIMUFrame");
	if( IMUDiv != null ) 
	{
		IMUDiv.style.visibility	= sVisibility;
	}
	var staticIMUDiv = document.getElementById("vxFlashPlayerStaticIMUFrame");
	if( staticIMUDiv != null ) 
	{
		staticIMUDiv.style.visibility	= sVisibility;
	}
}

function vxSetPlayerSize(w, h)
{
	// The top level.
	var vxp = document.getElementById("vxFlashPlayerContent");
	var vxfp = document.getElementById("vxFlashPlayer");
	
	// Size the Player and the area surrounding it based on the Player dimensions.
	if (g_vxEmbedMode == "fullsize" || g_vxEmbedMode != "scaletofit")
	{
		vxp.style.width	= w;
		vxp.style.height = h;
		vxfp.style.width = '';
		vxfp.style.height = '';
	}
	// Size the Player based on the size of its surrounding HTML elements and Stage (loader.swf.)
	else if (g_vxEmbedMode.indexOf("scaletofit") != -1)
	{
		// Make sure the integrator or client has provided us with a width & height.
		if (vxfp.style.width != "" && vxfp.style.height != "")
		{
			vxp.style.width = '100%';
			vxp.style.height = '100%';
		}
		else
		{
			// Otherwise alert the integrator or client.
			alert('To use the VXFlashPlayer in ' + g_vxEmbedMode + ' mode, you must provide a width and height for the <vxFlashPlayer> HTML element in the ROO embed code.'); 
		}
	}
	
	// call our client initialization callback (if it exists) to let our client know that the vxFlashPlayer has beed loaded.
	if( typeof(vxClientInitialize) == "function" ) 
	{
		vxClientInitialize();
	}
}


function vxSetIMUSizeAndPosition(x, y, w, h)
{
	vxSetSizeAndPosition(document.getElementById("vxFlashPlayerSyncIMUFrame"), x, y, w, h);	
	vxSetSizeAndPosition(document.getElementById("vxFlashPlayerStaticIMUFrame"), x, y, w, h);
	
	// save our IMU position so that we can recalculate when browser window resizes
	_vxIMUPos_x = x;
	_vxIMUPos_y = y;
}


/**
 * Sets an objects position relative to vxFlashPlayerContent div.
 */
function vxSetSizeAndPosition(obj, x, y, w, h)
{
	if( obj != null ) 
	{
		var baseCoords = vxFindPos(document.getElementById("vxFlashPlayerContent"));
		obj.style.left = (baseCoords[0]+x) + 'px';
		obj.style.top = (baseCoords[1]+y) + 'px';
		obj.style.width = w + 'px';
		obj.style.height = h + 'px';			
	}
}

/**
 * Returns the page position of an object
 */
function vxFindPos(obj) {
	var curleft = 0;
	var curtop 	= 0;

	if( obj.offsetParent && shouldUseOffset() ) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		
		//while (obj = obj.offsetParent) {
		//	curleft += obj.offsetLeft;
		//	curtop += obj.offsetTop;
		//}
	}
	
	return [curleft,curtop];
}

/**
 * Callback for window.onresize event
 */
function vxOnResize()
{
	vxRepositionIMUs();
}

function vxRepositionIMUs()
{
	var newPos = vxFindPos(document.getElementById("vxFlashPlayerContent"));
	
	var IMUDiv = document.getElementById("vxFlashPlayerSyncIMUFrame");
	if( IMUDiv != null ) 
	{
		IMUDiv.style.left = (newPos[0]+_vxIMUPos_x) + 'px';
		IMUDiv.style.top = (newPos[1]+_vxIMUPos_y) + 'px';
	}	
	
	var staticIMUDiv = document.getElementById("vxFlashPlayerStaticIMUFrame");
	if( staticIMUDiv != null ) 
	{
		staticIMUDiv.style.left = (newPos[0]+_vxIMUPos_x) + 'px';
		staticIMUDiv.style.top = (newPos[1]+_vxIMUPos_y) + 'px';
	}	
}


function openFullscreenPlayer(siteId,channel,clipId,volume,bitrate)
{
	window.open("vxFullscreen.aspx?vxSiteId="+siteId+"&vxChannel="+channel+"&vxClipId="+clipId+"&vxVolume="+volume+"&vxBitrate="+bitrate,"rooFullscreen","fullscreen","true");
}

function vxGetPlayer()
{	/*	In the event that the player is wrapped in a form - in IE - when we add an ID
		attribute to a HTML element, that element is added to the from itself rather 
		than the window. When this happens the flash / js bridge can no longer locate
		the element within the document. Here we override the getAttribute function 
		within the SWFObject to fix this issue.
	*/
	if(!(navigator.plugins && navigator.mimeTypes.length)) 
	{
		window["vxFlashPlayerEmbed"] = document.getElementById('vxFlashPlayerEmbed');
//		window[so.getAttribute('id')] = document.getElementById(so.getAttribute('id'));
	}

	return vxIsMSIE() ? window['vxFlashPlayerEmbed'] : document['vxFlashPlayerEmbed'];
}

function vxIsMSIE()
{
	return navigator.userAgent.indexOf("MSIE") != -1;
}

function vxMSIEVersion()
{
	if( vxIsMSIE() )
		return parseInt(navigator.userAgent.substr(navigator.userAgent.indexOf("MSIE")+5));
	return 0;
}

function shouldUseOffset()
{
	switch( g_vxIEOffsetFix )
	{
		case "6": 		return vxMSIEVersion() != 6; // don't use offset for MSIE 6 only
		case "7up": 	return vxMSIEVersion() <  7; // don't use offset for MSIE 7+
		case "any":		return vxMSIEVersion() == 0; // don't use offset for any MSIE.
		default:		return true;
	}	
}

function vxIsAbsoluteURL(sURL)
{
	return ( sURL.indexOf("http://")==0 || sURL.indexOf("https://")==0 );
}

// position of IMUs.  Values are set via a call to vxSetIMUSizeAndPosition from Flash and they are saved to that
// we can correctly reposition IMUs when the browser window resizes.
var _vxIMUPos_x = 0;
var _vxIMUPos_y = 0;

// Call back to our parent
vxMain();

