// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * If there is already a tab with the given URL, switch focus to it. Otherwise,
 * create a new tab and open the URL.
 *
 * @param url The URL to open.
 */
function focusOrCreateTab(url) {
  chrome.windows.getAll({"populate":true}, function(windows) {
      var existing_tab = null;
      for (var i in windows) {
        var tabs = windows[i].tabs;
        for (var j in tabs) {
          var tab = tabs[j];
          if (tab.url == url) {
            existing_tab = tab;
            break;
          }
        }
      }
      if (existing_tab) {
        chrome.tabs.update(existing_tab.id, {"selected": true});
      } else {
        chrome.tabs.create({"url": url, "selected": true});
      }
    });
}

/**
 * In the current tab, navigate to the specified URL.
 *
 * @param url The URL to navigate to.
 */
function navigate(url, callback) {
  chrome.tabs.getSelected(null, function(tab) {
      chrome.tabs.update(tab.id, {url: url}, callback);
  });
}

// Open the Chromoting HostList tab when
chrome.browserAction.onClicked.addListener(function(tab) {
    var hostlist_url = chrome.extension.getURL("hostlist.html");
    focusOrCreateTab(hostlist_url);
  });


function openChromotingTab(hostName, hostJid) {
  var username = getCookie('username');
  var xmppAuth = getCookie('xmpp_auth');
  var newTabUrl = chrome.extension.getURL("chromoting_tab.html");
  var request = {
    username: getCookie('username'),
    xmppAuth: getCookie('xmpp_auth'),
    hostName: hostName,
    hostJid: hostJid,
  };

  console.log("Attempt to connect with" +
              " username='" + request.username + "'" +
              " hostName='" + request.hostName + "'" +
              " hostJid='" + request.hostJid + "'" +
              " auth_token='" + request.xmppAuth + "'");
  navigate(newTabUrl, function(tab) {
      console.log("We're trying now to send to " + tab.id);
      chrome.tabs.sendRequest(
          tab.id, request, function() {
            console.log('Tab finished connect.');
          });
    });
}

@@ -1940,29 +1940,32 @@
 %22);%0A
+%0A
   
-navigate(newTabUrl,
+var sendRequestFunc =
  fun
@@ -1961,32 +1961,33 @@
 tFunc = function
+ 
 (tab) %7B%0A      co
@@ -1978,26 +1978,24 @@
 (tab) %7B%0A    
- 
- 
 console.log(
@@ -2036,26 +2036,24 @@
 ab.id);%0A    
-  
 chrome.tabs.
@@ -2065,18 +2065,16 @@
 equest(%0A
-  
         
@@ -2113,18 +2113,16 @@
         
-  
 console.
@@ -2163,19 +2163,503 @@
     
-  %7D);%0A    %7D
+%7D);%0A  %7D;%0A%0A  // This function will run when after the url for the tab is updated. If%0A  // the tab is not yet loaded it will wait for another 500ms to inspect%0A  // again.%0A  var checkStatusFunc = function (tab) %7B%0A    if (tab.status == %22complete%22) %7B%0A      sendRequestFunc(tab);%0A      return;%0A    %7D%0A%0A    // Wait for 500ms and then get the tab and check its status.%0A    setTimeout(function() %7B%0A        chrome.tabs.get(tab.id, checkStatusFunc);%0A      %7D, 500);%0A  %7D%0A%0A  navigate(newTabUrl, checkStatusFunc
 );%0A%7D

Make chromoting connect always work