//*****************************************************************************
//+ Copyright(c) 2007, 20-20 Technologies Inc. All rights reserved.
//*****************************************************************************

var g_VSAPI = null;
var g_Extension = null;
var g_bAPILoadInProgress = false;
var g_bDebugEnabled = false;
var g_sEmailTo = "";
var g_sCustomerID = "";

//=============================================================================
window.onresize = UIAdjustCenteredDIV;

//=============================================================================
function UIGetAPI()
{
   return g_VSAPI;
}

//=============================================================================
function UIGetExtension()
{
   return g_Extension;
}

//=============================================================================
function UIGetPlannerFrame()
{
   var g_VSPlannerFrame = window.frames['MainFrame'].UIGetPlannerFrame();
   return g_VSPlannerFrame
}

//=============================================================================
function OnLoadVPUI()
{
    UIAdjustCenteredDIV();

    g_sEmailTo = UIGetURLArgumentValue("DealerEmail");
    g_sCustomerID = UIGetURLArgumentValue("CustomerId");
   
   
   var l_sDebugMode = UIGetURLArgumentValue( "Debug" );
   g_bDebugEnabled = (l_sDebugMode == "Yes");
   
   g_VSAPI = window.frames['VSAPIFrame'];
   g_Extension = window.frames['ExtensionFrame'];
   g_bAPILoadInProgress = true;

   if( g_bDebugEnabled )
      document.getElementById( 'VSAPIFrame' ).src = "../../UICore/VSAPIFrame_Debug.htm";
   else
      document.getElementById( 'VSAPIFrame' ).src = "../../UICore/VSAPIFrame.htm";
      
}

//=============================================================================
function OnVSAPILoaded()
{
   if( g_bAPILoadInProgress )
   {
      var l_sCultureCode = UIGetURLArgumentValue( "Lang" );
      if( l_sCultureCode == "" )
         l_sCultureCode = UIGetAPI().VSGetUIConfig( 'CultureCode' );   

      var l_BaseURL = UIGetURLArgumentValue( "DataURL" );
      if( l_BaseURL == "" )
         l_BaseURL = window.location.href.split("/UI/")[0];

      var l_BaseCatalogDataURL = UIGetURLArgumentValue( "CatDataURL" );
      if( l_BaseCatalogDataURL == "" )
         l_BaseCatalogDataURL = l_BaseURL + "/CatalogData/";

      var l_BaseUIDataURL = UIGetURLArgumentValue( "UIDataURL" );
      if( l_BaseUIDataURL == "" )
         l_BaseUIDataURL = l_BaseURL + "/UIConfig/Images/";

      var l_BackOfficeURL = UIGetURLArgumentValue( "BOffURL" );
      if( l_BackOfficeURL == "" )
         l_BackOfficeURL = l_BaseURL + "/";
   
      UIGetAPI().VSInitAPI( l_BaseCatalogDataURL, l_BaseUIDataURL, l_BackOfficeURL, "", "", l_sCultureCode );
   }
}

//=============================================================================
function OnVSAPIInitSucceeded()
{
   g_bAPILoadInProgress = false;
   
   // check for autologin   
   var l_sCustomer = UIGetURLArgumentValue( "customer" );
   var l_sSessionID = UIGetURLArgumentValue( "sessionID" );
   if ( l_sSessionID != "" )
   {
      UIGetAPI().VSLoginWithSessionID( l_sCustomer, l_sSessionID,  "" );
   }
   else if ( l_sCustomer != "" )
   {
      UIGetAPI().VSLogin( l_sCustomer, null );
   }

   var l_sSkipIntro = UIGetURLArgumentValue( "SkipIntro" );
   
   if( UIGetAPI().VSGetUIConfig( 'EnableExtension' ) )
      document.getElementById( 'ExtensionFrame' ).src = "../../UIConfig/Pages/VSExtensionFrame.htm";

   if( document.getElementById( 'IEPlayerInstallState' ).value == "InProgress" ) 
   {
      // When installing the player on IE, the browser needs to refresh the page.
      // In this case, don't change the document location, the browser refresh 
      // will take care of it.
      return;
   }
   else if( l_sSkipIntro == "Yes" )
   {
      document.getElementById( 'MainFrame' ).src = "VPUITestRequirements.htm";
   }
   else
   {
      document.getElementById( 'MainFrame' ).src = "VPUIIntro.htm";
   }
   
   document.title = UIGetAPI().VSGetUIConfig( 'PageTitlePrefix' );
}

//=============================================================================
function OnVSAPIInitFailed()
{
   g_bAPILoadInProgress = false;
   document.getElementById( 'MainFrame' ).src = "VPUIError.htm";
}

//=============================================================================
function UISetIEPlayerInstallState( i_sInstallState )
{
   document.getElementById( 'IEPlayerInstallState' ).value = i_sInstallState;
}

//=============================================================================
function UIGetHWAcceleration()
{
   //TODO: This should be top down ... e.g. passed to RPHostRoomFrame.htm in the URL?
   return UIGetAPI().VSGetUIConfig( "HWAcceleration" );
}

//=============================================================================
function UIGetURLArgumentValue( i_sArgName )
{
   var l_sURL = window.location.href;
   var l_nIDX = l_sURL.indexOf( "?" );
   if( l_nIDX >= 0 )
   {
      var l_sAllArgs = l_sURL.substr( l_nIDX + 1 );
      var l_aAllArgs = l_sAllArgs.split( "&" );
      var i, l_aArg;
      for( i = 0; i < l_aAllArgs.length; i++ )
      {
         l_aArg = l_aAllArgs[i].split( "=" );
         if( l_aArg.length == 2 && l_aArg[0] == i_sArgName )
            return l_aArg[1];
      }
   }
   return "";
}

//=============================================================================
function UIAdjustCenteredDIV()
{
   var l_nWindowWidth = document.documentElement.clientWidth;
   var l_nWindowHeight = document.documentElement.clientHeight;
   
   if( l_nWindowWidth <= 1014 )
      document.getElementById( 'centered_div' ).className = 'normal_planner';
   else
      document.getElementById( 'centered_div' ).className = 'centered_planner';
}