﻿var contentHandler = "ContentHandler.ashx";
var adoptionCount = 0;
var contentLoading = false;
var animationStates = new Array();

function UpdateContent()
{
	if( vmp == null || !vetInstalled )
		return;

    var time = ( new Date() ).getTime();
    var command = contentHandler + "?CMD=update&TIME=" + time;

    contentLoading = true;
    vmp.LoadMTX( command );
}

function RefreshContent()
{
	if( vmp == null || !vetInstalled )
		return;

    vmp.ClearScene();
    vmp.LoadMTX( "mts/scene.mtx" );
    
    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;
    
    if( removingGarbage ) {
        setTimeout( 'OpenReportWindow()', 800 );
        removingGarbage = false;
    }
}

function InitializeContent()
{
	if( vmp == null || !vetInstalled )
		return;

    vmp.ClearScene();
    vmp.LoadMTX('mts/scene.mtx');
}

function ExecuteAction( action, param )
{
    switch( action )
    {
        case 'PlacerClicked':
            InstantiatePlacer( param );
        break;
        
        case 'ElementClicked':
            SelectElement( param );
        break;
        
        case 'PlayAnimation':
            HandleAnimations( param );
        break;
        
        default:
            alert( 'unknown content action!' );
    }
}

function HandleAnimations( instanceName )
{
    var animationName = instanceName + '_ShowFunctionalityAnim';
    var idx = GetAnimationIndex( animationName );
    
    if( idx == -1 )
    {
        vmp.SetProperty(animationName,'pdir','1');
        animationStates.push( animationName );
    }
    else
    {
        vmp.SetProperty(animationName,'pdir','0');
        animationStates.splice( idx, 1 );
    }
    
    vmp.TriggerAnim( animationName );
}

function GetAnimationIndex( animationName )
{
    for( var i = 0; i < animationStates.length; i++ )
    {
        if( animationStates[ i ] == animationName )
            return i;
    }
    return -1;
}

function EnableGrid( enabled )
{
	if( vmp == null || !vetInstalled )
		return;

    if( enabled )
        vmp.SetProperty('MTSInstance.InitialScene_0','clps','0');
    else
        vmp.SetProperty('MTSInstance.InitialScene_0','clps','1');
}

function HideMeasures()
{
	if( vmp == null || !vetInstalled )
		return;

    m_measureVisible = false;
    EnableGrid( true );
    vmp.PostEvent('HideMeasureSticks',0);
}

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 )
{
	// 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' );
}

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;
}
