﻿
var m_initialized = true;
var m_configMode = 'arrange';
var m_prepareCapture = false;

var designNeedsResize = true;

function GetClientParameters() {
    var vetArea = document.getElementById( 'ViewpointArea' );
    return 'viewWidth=' + vetArea.clientWidth + ',viewHeight=' + vetArea.clientHeight;
}


function InitializeApplication() {
    initFlash();
    if( pluginInstalled ) 
    {
        var clientParams = GetClientParameters();
        
        if ( m_configid &&  m_configid != null && m_configid.length > 0 ) // MTV 20071119
        {
            clientParams += ",configid='" + m_configid + "'";
        }    
                
        SendMessage( 'initialize', clientParams );
        m_initialized = true;
    }
    else
        SendMessage( 'installViewpoint' );

}
 
function updateClientProperties()
{
    var clientParams = GetClientParameters();
	SendMessage("updateClientProperties", clientParams);
}

function StartApplication() {
}


function ShutdownConfiguration() {
    var clientParams = GetClientParameters();
    SendMessage( 'shutdown', clientParams );
}

/*
deprecated!! 

function CaptureSaveImage()
{
showMessageText( "Luodaan kuvaketta.", false );
setTimeout("ExecuteSaveCapture()", 1000);
}

function SaveFinished(message)
{
var portalNode = message.split(":")[1]
    
if (portalNode < -1 && portalNode > -100)
{
// Virhe POrtaalissa
ShowErrorMessage( "Talletus portaaliin epäonnistui.");
alert("Talletus portaaliin epäonnistui (virhe: " +portalNode+ "). Yritä myöhemmin uudelleen.");
}
else if (portalNode == -1) // Portal error for temporary save
{
// Temporary Save
ShowMessage( "Unelmien palapelisi on talletettu." );
alert("Unelmien palapelisi on talletettu.\nRekisteröidy, että pääset katsomaan sitä uudelleen!");
if (window.opener)
{
setTimeout('window.opener.location="' + portalAddess + '?q=up-edit&saveUnregistered=1"', 1000);
setTimeout('window.close()', 1500);
}
else
{
setTimeout('window.location="' + portalAddess + '"', 1000);
}
}
else if (portalNode < -100)
{
ShowErrorMessage("Unelmien palapelin talletus epäonnistui");
// Virhe Puzzlessa
alert("Unelmien palapelin talletus epäonnistui (virhe: " +portalNode+ "). Yritä myöhemmin uudelleen");
}
else
{
ShowMessage( "Unelmien palapelisi on talletettu." );
alert('Palapeli on tallennettu.');
if (window.opener)
{
//setTimeout('window.opener.location="' + portalAddess + 'node/' + portalNode + '"', 1000);
setTimeout('window.opener.location="' + portalAddess + 'node/' + portalNode + '/edit"', 1000);
setTimeout('window.close()', 1500);
}
else
{
setTimeout('window.location="' + portalAddess + 'node/' + portalNode + '"', 1000);
}
}

}


function ToggleDesignMode()
{
//alert("toggleDesignMode");
SendMessage( 'toggleDesignMode' );
}
*/

var modeTimeout;
var inDesign = false;
function SetDesignMode()
{
    clearTimeout(modeTimeout);
    flashVisible = true;
    SendMessage('selectTopView');

    UpdateFlash();
    
    // Would be smart to wait for confirmation, but...
    modeTimeout = setTimeout("showDesignApp()", 1800);
    
    inDesign = true;
}

function showDesignApp()
{
    // show flash layer.. 
    $("#FlashArea").css("visibility", "visible");
    $("#FlashArea").css("position", "absolute");
    $("#FlashArea").css("left", "auto");

    // at least the viewpoint can always be visible, but not unity - therefore needed
    $("#ViewpointArea").css("visibility", "hidden");
}

function SetPuzzleMode()
{
    if( !inDesign ) // only if we are in design mode..
        return;

    clearTimeout(modeTimeout);

    flashVisible = false;
    SendFlashData();

    // hide flash..
    $("#FlashArea").css("position", "absolute");
    $("#FlashArea").css("left", "-9999");
    
	// show viewpoint
    $("#ViewpointArea").css("visibility", "visible");

    inDesign = false;
    designNeedsResize = true;
}

// callback from the server..
function SetConfigMode(mode) {
    if( mode == 'rooms' )
    {
        switchToRoomState();
    }
    else if( mode == 'arrange' )
    {
        switchToArrangeState();
    }
    else if( mode == 'build' )
    {
        switchToBuildState();
    }
    else if( mode == 'color' )
    {
        switchToColorState();
    }
    else
        alert( 'unknown config mode \''+ mode +'\'!' );   
    
    m_configMode = mode;
}



function LibraryItemSelected( itemName ) {    
    SendMessage( 'libraryItemSelected', 'itemSelected=' + itemName );
}

function CategorySelected( selectedCat ) {  
    var action = 'categorySelected';
    var catName = selectedCat;
    
    if (catName.indexOf(":") != -1) {
        var params = catName.split(":");
        action = params[0];
        catName = params[1];
    }  
    
    SendMessage( action, 'categorySelected=' + catName );
}

function MaterialItemSelected( itemName ) {
    SendMessage( 'materialItemSelected', 'itemSelected=' + itemName );
}

function UpdateActiveGroups( itemNames )
{
    UpdateActiveContentGroups(itemNames);
}

function InstantiatePlacer( placerName ) {
    SendMessage( 'instantiatePlacer', 'contentSelected=' + placerName );
}

function CreateNewProduct(productName) {
    var cameraParameters = GetCameraParameters();
    SendMessage('createEmptyProductGroup', 'product=' + productName + ',' + cameraParameters);
}

function ZoomInGroup( elementName ) {
    var cameraParameters = GetCameraParameters();
    SendMessage( 'zoomInGroup', 'contentSelected=' + elementName + ',' + cameraParameters );
}

function ZoomOutGroup( elementName ) {
    SendMessage( 'zoomOutGroup', 'contentSelected=' + elementName );
}

function ZoomToDesignState( elementName ) {
    SendMessage( 'zoomToDesignState', 'contentSelected=' + elementName );
}

function ZoomToRoomState( elementName ) {
    SendMessage( 'zoomToRoomState', 'contentSelected=' + elementName );
}

function CenterView( elementName ) {
    if(flashVisible)
    {
        flash.centerCamera();
    } else {
        SendMessage( 'centerView', 'contentSelected=' + elementName );
    }
        
}

// SAVE
/*

deprecated!! 

function SavePuzzle( ) {
if (flashVisible)
{
alert("Palaa 3D -moodiin ensin klikkaamalla \"Takaisin\" -nappia.");
return;
}
writeInfoText("Palautetaan Puzzle -näkymä...");
ResetRoomState("");
// Start Capture
// savePuzzle();
}
function ResetRoomState( ) {
SendMessage( 'prepareViewForSave', 'contentSelected=' );
}
*/
/*
function ExecuteSaveCapture()
{
  setTimeout("Actual_ExecuteSaveCapture()", 500); // MTV20080808: image is not correct
}

function Actual_ExecuteSaveCapture()
{
    // Called from state: "prepareViewForSave"

	if( vmp == null || !pluginInstalled )
		return;

    var path = applicationUrl + 'CaptureHandler.ashx?id='+sessionId+"&target=portal";
    //alert("CaptureSaveImage: " + path);
	vmp.SetProperty('CaptureComponent', 'Path', path);
	vmp.PostEvent('executeCapture',0);
	
	setTimeout("SaveConfigInfo()", 1500);
}
*/

function SaveConfigInfo()
{
    //alert("savePuzzle");
    SendMessage( 'savePuzzle', 'sessionId=' + sessionId  );
}

function Deselect() {
    SendMessage( 'deselect' );
}

function SelectElement( elementName ) {
    SendMessage( 'selectElement', 'contentSelected=' + elementName );
}

function SelectWall( elementName ) {
    SendMessage( 'selectWall', 'contentSelected=' + elementName );
}

function Clear() {
    //    var confirmNewInfo = "Haluatko aloittaa uuden huoneen? Nykyinen huone menetetään.";
    var confirmNewInfo = j_clearConfirm;
    var agree = confirm(confirmNewInfo);
    if( agree ) {
        window.location.reload( false );
    }
}

function ClearQuietly() {
    window.location.reload(false);
}

function ConfirmNew( elementName )
{
    SendMessage( 'confirmNew' );
}

function NewConfiguration()
{
    SendMessage( 'newConfig' );
}

function InformRemoveLegality( message )
{
    var vals = message.split(':');
    
    if( vals[ 0 ] == '0' ) // not ok to remove..
    {
        showMessageText( vals[ 1 ], true );
    }
    else if( vals[ 0 ] == '1' )// ok to remove.. -> just remove..
        SendMessage('remove');
    else if( vals[ 0 ] == '2' ) // confirm that we are about to start a new configuration..        
        ConfirmNew( '' );
    else
        alert( 'unknown remove command..' );
}
    
function Remove( elementName ) {
    if( elementName == null )
        SendMessage( 'checkRemove' ); // we need to confirm first..
    else
        SendMessage( 'remove', 'contentSelected=' + elementName );
}

function writeInfoText (text)
{
    document.getElementById( 'info' ).innerHTML = text;
}

function CreateMeasureSticks() {
    if (!pluginInstalled)
        return;

    if (!m_measureVisible) {
        SendMessage('showMeasurements');
    }
    else {
        HideMeasures();
    }
}

function OpenSaveDialog() {
    if (pluginInstalled) {
        saveWin = window.open('SaveConfiguration.aspx', 'saveWin', 'scrollbars=0;menubar=no,height=290,width=450,resizable=no,toolbar=no,location=no,status=no');
        saveWin.focus();
    }
    return false;
}

function OpenLoadDialog() {
    if (pluginInstalled) {
        loadWin = window.open('loadSavedConfiguration.aspx', 'loadWin', 'scrollbars=1;menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=yes');
        loadWin.focus();
    }
    return false;
}

