<html>
  <!-- This page is meant to load inside the host browser like IE/FF -->
  <head><title>Initialize hidden chrome frame</title>
    <script type="text/javascript" src="chrome_frame_tester_helpers.js">
    </script>
    <script type="text/javascript">
      var g_failure_timeout = null;
      var g_test_id = 1;
      var g_test_name = 'InitializeHidden';
      var g_cf3_loaded = false;

      function OnNavigationFailed() {
        onFailure(g_test_name, g_test_id, 'ChromeFrame Navigation failed');
      }

      function OnObjectFocusFailed() {
        appendStatus('chrome frame focus failed');
        onFailure(g_test_name, g_test_id, 'Visibility test failed');
      }

      function OnCF1Loaded() {
        appendStatus('Chrome frame 1 loaded, not visible yet.');
        try {
          // Make chrome frame visible
          var cf1 = document.getElementById('CFDiv1');
          cf1.style.visibility = 'visible';
          appendStatus('Chrome frame 1 visibility - ' + cf1.style.visibility);
          // Set focus to chrome frame.  This should set focus to the
          // first element inside the frame, which a script inside the
          // page will detect and notify us back by sending us a message.
          document.ChromeFrame1.focus();
          g_failure_timeout = window.setTimeout(OnObjectFocusFailed, 10000);
        } catch(e) {
          appendStatus('Error setting focus to CF1. Error: ' + e.description);
          onFailure(g_test_name, g_test_id, 'CF1 focus() error');
        }
      }

      function OnCF1Message(evt) {
        if (evt.data == 'btnOnFocus') {
          window.clearTimeout(g_failure_timeout);
          g_failure_timeout = null;
          appendStatus('CF1 visible and focused');

          // Now make second chrome frame instance visible
          document.getElementById('CFDiv2').style.display = 'block';
          appendStatus('Chrome frame 2 visible, should start loading now');
          g_failure_timeout = window.setTimeout(OnObjectFocusFailed, 10000);
        }
      }

      function OnCF2Loaded() {
        appendStatus('Chrome frame 2 loaded');
        try {
          // Set focus to chrome frame.  This should set focus to the
          // first element inside the frame, which a script inside the
          // page will detect and notify us back by sending us a message.
          // We set focus on a timeout as on IE it takes a while for the window
          // to become visible.
          setTimeout(SetFocusToCF2, 1000);
        } catch(e) {
          appendStatus('Error setting focus to CF2. Error: ' + e.description);
          onFailure(g_test_name, g_test_id, 'CF2 focus() error');
        }
      }

      function SetFocusToCF2() {
        try {
          document.ChromeFrame2.focus();
        } catch (e) {
          appendStatus('SetFocusToCF2: Error: ' + e.description);
          onFailure(g_test_name, g_test_id, 'SetFocusToCF2 error ' +
                                            e.description);
        } 
      }

      function OnCF2Message(evt) {
        if (evt.data == 'btnOnFocus') {
          window.clearTimeout(g_failure_timeout);
          g_failure_timeout = null;
          appendStatus('CF2 visible and focused');
          onSuccess(g_test_name, g_test_id);
        }
      }
    </script>
  </head>
  <body>
    <div id="CFDiv1" style="visibility: hidden;"></div>
    <script type="text/javascript">
insertControl("CFDiv1",
    { "id": "ChromeFrame1",
      "width": "300",
      "height": "80",
      "src": "simple_object_focus_cf.html",
      "eventHandlers": { "onload": "OnCF1Loaded();",
                         "onloaderror": "OnNavigationFailed();",
                         "onmessage": "OnCF1Message(arguments[0]);" },
      "objectAttributes": { "tabindex": "0" },
      "embedAttributes": {"id": null}
    });
    </script>
    <div id="CFDiv2" style="display: none;"></div>
    <script type="text/javascript">
insertControl("CFDiv2",
    { "id": "ChromeFrame2",
      "width": "300",
      "height": "80",
      "src": "simple_object_focus_cf.html",
      "eventHandlers": { "onload": "OnCF2Loaded();",
                         "onloaderror": "OnNavigationFailed();",
                         "onmessage": "OnCF2Message(arguments[0]);" },
      "objectAttributes": { "tabindex": "1" },
      "embedAttributes": { "id": null}
    });
    </script>
    <div id="statusPanel" style="border: 1px solid red; width: 100%">
    Test running....
    </div>
  </body>
</html>