﻿var contentHandler = "ContentHandler.ashx";
var adoptionCount = 0;

var contentLoading = false;


function UpdateContent()
{   
    var time = ( new Date() ).getTime();
    var command = contentHandler + "?CMD=update&TIME=" + time;
    
    contentLoading = true;
    vmp.LoadMTX( command );
}

function RefreshContent()
{       
    vmp.ClearScene();
    
    var time = ( new Date() ).getTime();
    var command = contentHandler + "?CMD=refresh&TIME=" + time;
    
    contentLoading = true;
    vmp.LoadMTX( command );
}

function RefreshDone()
{
}

function UpdateDone()
{
    waitForContent = false;
    contentLoading = false;
}

function EnableGrid( enabled )
{
    if( enabled )
        vmp.SetProperty('MTSInstance.InitialScene_0','clps','0');
    else
        vmp.SetProperty('MTSInstance.InitialScene_0','clps','1');
}

function HideMeasures()
{
    m_measureVisible = false;
    EnableGrid( true );
    vmp.PostEvent('HideMeasureSticks',0);
}

function InitializeContent()
{
    vmp.ClearScene();
    vmp.LoadMTX('mts/scene.mtx');
}

function ExecuteAction( action, param )
{
    switch( action )
    {
        case 'PlacerClicked':
            InstantiatePlacer( param );
        break;
        
        case 'ElementClicked':
            SelectElement( param );
        break;
        
        default:
            alert( 'unknown content action!' );
    }
}

function ShowRootLevelNodes()
{
    var childCount = getChildCount( 'MTSRootInstance' );
    
    for( i = 0; i < childCount; i++ )
    {
        var name = vmp.GetProperty( 'MTSRootInstance', 'SceneChilds['+ i +'].name' );
        alert( i + ' : '+ name );
    }
  
}

function DoAdoption( parentName, childName )
{
	var startTime = ( new Date() ).getTime();

	// adopt..
	var parentChildCount = getChildCount( 'MTSInstance.' + parentName );
	vmp.SetProperty( parentName, 'SceneChilds['+ parentChildCount +']', 'MTSInstance.' + childName );

	// call the server generated action to clean up the shit..
	var rootChildCount = getChildCount( 'MTSRootInstance' );

	// switch loaded instances to adopters..
	/*
	for( i = 2; i < rootChildCount; i ++ )
	{
		var adopterName = 'MTSInstance.ADOPTER'; //+ childName +'_ADOPTER';
		vmp.SetProperty( 'MTSRootInstance', 'SceneChilds['+ rootIndex +']', adopterName );
	}
	*/


	// var rootIndex = getIndexForRootChild( childName );
	// var adopterName = 'MTSInstance.ADOPTER'; //+ childName +'_ADOPTER';

	// vmp.SetProperty( 'MTSRootInstance', 'SceneChilds['+ rootIndex +']', adopterName );

	// vmp.SetProperty( 'MTSScene','GarbageInstance', adopterName );
	// vmp.PostEvent( 'RemoveGarbage','0' );

	var endTime = ( new Date() ).getTime();

}

/*
function DoAdoption( parentName, childName )
{
    //var adopterInstance = '<MTSScene><MTSInstance Name=\\"ADOPTER\\" /></MTSScene>';
    //vmp.Execute( adopterInstance );

	var startTime = ( new Date() ).getTime();

	var parentChildCount = getChildCount( 'MTSInstance.' + parentName );
	vmp.SetProperty( parentName, 'SceneChilds['+ parentChildCount +']', 'MTSInstance.' + childName );

	var rootIndex = getIndexForRootChild( childName );
	var adopterName = 'MTSInstance.ADOPTER'; //+ childName +'_ADOPTER';

	vmp.SetProperty( 'MTSRootInstance', 'SceneChilds['+ rootIndex +']', adopterName );

	// vmp.SetProperty( 'MTSScene','GarbageInstance', adopterName );
	// vmp.PostEvent( 'RemoveGarbage','0' );
	
	var endTime = ( new Date() ).getTime();

}
*/

function getIndexForRootChild( childName )
{
	var childCount = getChildCount( 'MTSRootInstance' );
	for( var i = 0; i < childCount; i++ ) {
		if( vmp.GetProperty( 'MTSRootInstance', 'SceneChilds['+ i +'].name' ) == childName )
			return i;
	}
	return -1;
}

function getChildCount( mtsPath )
{
	var result = -1;
	var prop = vmp.GetProperty( mtsPath, 'SceneChilds.count' );

	if( prop != null )
		result = parseInt( prop );

	return result;
}
