var	windowWidth		= window.outerWidth || document.body.clientWidth,
	windowHeight	= window.outerHeight || document.body.clientHeight,
	start			= null,
	secs			= 0;

/**
 *  Handles the changes in the video player status.  Used to keep track of how many seconds
 *  a user spends watching the video.
 *
 *  @param		string		status		The new status of the video player.
 */
function playChange ( status )
{
	var	tmp	= new Date (),
		now	= tmp.getTime ();

	switch ( status )
	{
		case 'playing':

			if ( start == null )
			{
				start = now;
			}
			break;

		case 'stopped':

			if ( start != null )
			{
				//  Find the difference between the start time and now in seconds, and add to total.
				secs	+= Math.ceil ( ( now - start ) / 1000 );
				start	= null;
			}
			break;

		case 'complete':
		case 'completed':

			if ( start != null )
			{
				//  Find the difference between the start time and now in seconds, and add to total.
				secs	+= Math.ceil ( ( now - start ) / 1000 );
				start	= null;
			}

			if ( Ext.get ( 'hfOverlayRel' ).getValue () != '' )
			{
				if ( Ext.get ( 'hfTemplateId' ).getValue () == '' )
				{
					parent.document.getElementById ( 'divPlayer' ).style.display = 'none';
				}
				else
				{
					Ext.get ( 'divPlayer' ).hide ();
				}
			}

			break;

		case 'close':

			if ( Ext.get ( 'hfTemplateId' ).getValue () == '' )
			{
				parent.document.getElementById ( 'divPlayer' ).style.display = 'none';
			}
			else
			{
				Ext.get ( 'divPlayer' ).hide ();
			}

			break;
	}
}

//  Used to report back to the server the total video viewing duration in seconds when the user
//  leaves the page.  This will activate on browser close, tab close, new URL, back/forward buttons,
//  and Alt+F4 key combination (possibly others, as well).
window.onbeforeunload =
	function ()
	{
		playChange ( 'completed' );

		if ( secs > 0 )
		{
  if (window.XMLHttpRequest) {
    AJAX=new XMLHttpRequest();
  } else {
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
     AJAX.open("POST", 'nd_VideoActivity.php', false);
	AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

     AJAX.send(Ext.urlEncode (
					{
						labelId:	encodeURI( Ext.get( 'hfLabelId' ).dom.value ),
						seconds:	encodeURI( secs ),
						sendId:		encodeURI( Ext.get( 'hfSendId' ).dom.value ),
						acctId:		encodeURI( Ext.get( 'hfAcctId' ).dom.value ),
						templateId:	encodeURI( Ext.get( 'hfTemplateId' ).dom.value ),
						videoId:	encodeURI( Ext.get( 'hfVideoId' ).dom.value ),
						clickId:	encodeURI( Ext.get( 'hfClickId' ).dom.value )
					}	));
  }
/*
			Ext.Ajax.request
			(
				{
					url:	'nd_VideoActivity.php',
					params:
					{
						labelId:	Ext.get ( 'hfLabelId' ).dom.value,
						seconds:	secs,
						sendId:		Ext.get ( 'hfSendId' ).dom.value,
						videoId:	Ext.get ( 'hfVideoId' ).dom.value
					}
				}
			);
*/
		}
	}

//  Resize and position the browser window if it isn't already maximized and this is not website video.
if ( Ext.get( 'hfTemplateId' ).dom.value != '' &&
	windowWidth < screen.availWidth &&
	windowHeight < screen.availHeight )
{
	window.moveTo ( 0, 0 );
	window.resizeTo ( screen.availWidth, screen.availHeight );
}