(function() {
  'use strict';

  var gerritRef = '';

  if (document.getElementById('macos-dock-extension')) {
    return;
  }

  const apps = [
    { name: 'Web Terminal', iconPath: 'dock-icons/terminal-web-256.png', action: () => {
      var currentHost = window.location.hostname;
      currentHost = currentHost.replace(/^canvas-redis-term-/, '');
      currentHost = currentHost.replace(/^canvas-redis-term/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term-/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term-/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin-/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer-/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer/, '');
      currentHost = currentHost.replace(/^canvas-redis-/, '');
      currentHost = currentHost.replace(/^canvas-redis/, '');
      currentHost = currentHost.replace(/^canvas-postgres-/, '');
      currentHost = currentHost.replace(/^canvas-postgres/, '');
      currentHost = currentHost.replace(/^canvas-webpack-/, '');
      currentHost = currentHost.replace(/^canvas-webpack/, '');
      currentHost = currentHost.replace(/^canvas-jobs-/, '');
      currentHost = currentHost.replace(/^canvas-jobs/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs/, '');
      currentHost = currentHost.replace(/^canvas-web-/, '');
      currentHost = currentHost.replace(/^canvas-web/, '');
      currentHost = currentHost.replace('.inseng.test', '');
      currentHost = currentHost.replace('.instructure.cloud', '');

      var suffix = ".inseng.test";
      if (window.location.href.includes('inseng.test')) {
        suffix = ".inseng.test";
      } else {
        suffix = ".instructure.cloud";
      }
      if (currentHost.length > 0) {
        currentHost = "-" + currentHost;
      }
      const url = `http://canvas-web-term${currentHost}${suffix}/`;
      window.open(url, '_blank');
    }},
    { name: 'PGAdmin', iconPath: 'dock-icons/pgadmin-256.png', action: () => {
      var currentHost = window.location.hostname;
      currentHost = currentHost.replace(/^canvas-redis-term-/, '');
      currentHost = currentHost.replace(/^canvas-redis-term/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term-/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term-/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin-/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer-/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer/, '');
      currentHost = currentHost.replace(/^canvas-redis-/, '');
      currentHost = currentHost.replace(/^canvas-redis/, '');
      currentHost = currentHost.replace(/^canvas-postgres-/, '');
      currentHost = currentHost.replace(/^canvas-postgres/, '');
      currentHost = currentHost.replace(/^canvas-webpack-/, '');
      currentHost = currentHost.replace(/^canvas-webpack/, '');
      currentHost = currentHost.replace(/^canvas-jobs-/, '');
      currentHost = currentHost.replace(/^canvas-jobs/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs/, '');
      currentHost = currentHost.replace(/^canvas-web-/, '');
      currentHost = currentHost.replace(/^canvas-web/, '');
      currentHost = currentHost.replace('.inseng.test', '');  
      currentHost = currentHost.replace('.instructure.cloud', '');

      var suffix = ".inseng.test";
      if (window.location.href.includes('inseng.test')) {
        suffix = ".inseng.test";
      } else {
        suffix = ".instructure.cloud";
      }
      if (currentHost.length > 0) {
        currentHost = "-" + currentHost;
      }
      const url = `http://canvas-pgadmin${currentHost}${suffix}/`;
      window.open(url, '_blank');
    }},
    { name: 'Redis', iconPath: 'dock-icons/redis-256.png', action: () => {
      var currentHost = window.location.hostname;
      currentHost = currentHost.replace(/^canvas-redis-term-/, '');
      currentHost = currentHost.replace(/^canvas-redis-term/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term-/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term-/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin-/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer-/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer/, '');
      currentHost = currentHost.replace(/^canvas-redis-/, '');
      currentHost = currentHost.replace(/^canvas-redis/, '');
      currentHost = currentHost.replace(/^canvas-postgres-/, '');
      currentHost = currentHost.replace(/^canvas-postgres/, '');
      currentHost = currentHost.replace(/^canvas-webpack-/, '');
      currentHost = currentHost.replace(/^canvas-webpack/, '');
      currentHost = currentHost.replace(/^canvas-jobs-/, '');
      currentHost = currentHost.replace(/^canvas-jobs/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs/, '');
      currentHost = currentHost.replace(/^canvas-web-/, '');
      currentHost = currentHost.replace(/^canvas-web/, '');
      currentHost = currentHost.replace('.inseng.test', '');
      currentHost = currentHost.replace('.instructure.cloud', '');

      var suffix = ".inseng.test";
      if (window.location.href.includes('inseng.test')) {
        suffix = ".inseng.test";
      } else {
        suffix = ".instructure.cloud";
      }
      if (currentHost.length > 0) {
        currentHost = "-" + currentHost;
      }
      const url = `http://canvas-redis-term${currentHost}${suffix}/`;
      window.open(url, '_blank');
    }},
    { name: 'Logs Terminal', iconPath: 'dock-icons/terminal-logs-256.png', action: () => {
      var currentHost = window.location.hostname;
      currentHost = currentHost.replace(/^canvas-redis-term-/, '');
      currentHost = currentHost.replace(/^canvas-redis-term/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term-/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term-/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin-/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer-/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer/, '');
      currentHost = currentHost.replace(/^canvas-redis-/, '');
      currentHost = currentHost.replace(/^canvas-redis/, '');
      currentHost = currentHost.replace(/^canvas-postgres-/, '');
      currentHost = currentHost.replace(/^canvas-postgres/, '');
      currentHost = currentHost.replace(/^canvas-webpack-/, '');
      currentHost = currentHost.replace(/^canvas-webpack/, '');
      currentHost = currentHost.replace(/^canvas-jobs-/, '');
      currentHost = currentHost.replace(/^canvas-jobs/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs/, '');
      currentHost = currentHost.replace(/^canvas-web-/, '');
      currentHost = currentHost.replace(/^canvas-web/, '');
      currentHost = currentHost.replace('.inseng.test', '');
      currentHost = currentHost.replace('.instructure.cloud', '');

      var suffix = ".inseng.test";
      if (window.location.href.includes('inseng.test')) {
        suffix = ".inseng.test";
      } else {
        suffix = ".instructure.cloud";
      }
      if (currentHost.length > 0) {
        currentHost = "-" + currentHost;
      }
      const url = `http://canvas-web-dev-logs-term${currentHost}${suffix}/`;
      window.open(url, '_blank');
    }},
    { name: 'Jobs Terminal', iconPath: 'dock-icons/terminal-jobs-256.png', action: () => {
      var currentHost = window.location.hostname;
      currentHost = currentHost.replace(/^canvas-redis-term-/, '');
      currentHost = currentHost.replace(/^canvas-redis-term/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term-/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term-/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin-/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer-/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer/, '');
      currentHost = currentHost.replace(/^canvas-redis-/, '');
      currentHost = currentHost.replace(/^canvas-redis/, '');
      currentHost = currentHost.replace(/^canvas-postgres-/, '');
      currentHost = currentHost.replace(/^canvas-postgres/, '');
      currentHost = currentHost.replace(/^canvas-webpack-/, '');
      currentHost = currentHost.replace(/^canvas-webpack/, '');
      currentHost = currentHost.replace(/^canvas-jobs-/, '');
      currentHost = currentHost.replace(/^canvas-jobs/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs/, '');
      currentHost = currentHost.replace(/^canvas-web-/, '');
      currentHost = currentHost.replace(/^canvas-web/, '');
      currentHost = currentHost.replace('.inseng.test', '');
      currentHost = currentHost.replace('.instructure.cloud', '');

      var suffix = ".inseng.test";
      if (window.location.href.includes('inseng.test')) {
        suffix = ".inseng.test";
      } else {
        suffix = ".instructure.cloud";
      }
      if (currentHost.length > 0) {
        currentHost = "-" + currentHost;
      }
      const url = `http://canvas-jobs-term${currentHost}${suffix}/`;
      window.open(url, '_blank');
    }},
    { name: 'Webpack Terminal', iconPath: 'dock-icons/terminal-webpack-256.png', action: () => {
           var currentHost = window.location.hostname;
      currentHost = currentHost.replace(/^canvas-redis-term-/, '');
      currentHost = currentHost.replace(/^canvas-redis-term/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term-/, '');
      currentHost = currentHost.replace(/^canvas-webpack-term/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term-/, '');
      currentHost = currentHost.replace(/^canvas-jobs-term/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-term/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin-/, '');
      currentHost = currentHost.replace(/^canvas-pgadmin/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer-/, '');
      currentHost = currentHost.replace(/^canvas-githook-installer/, '');
      currentHost = currentHost.replace(/^canvas-redis-/, '');
      currentHost = currentHost.replace(/^canvas-redis/, '');
      currentHost = currentHost.replace(/^canvas-postgres-/, '');
      currentHost = currentHost.replace(/^canvas-postgres/, '');
      currentHost = currentHost.replace(/^canvas-webpack-/, '');
      currentHost = currentHost.replace(/^canvas-webpack/, '');
      currentHost = currentHost.replace(/^canvas-jobs-/, '');
      currentHost = currentHost.replace(/^canvas-jobs/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs-/, '');
      currentHost = currentHost.replace(/^canvas-web-dev-logs/, '');
      currentHost = currentHost.replace(/^canvas-web-/, '');
      currentHost = currentHost.replace(/^canvas-web/, '');
      currentHost = currentHost.replace('.inseng.test', '');
      currentHost = currentHost.replace('.instructure.cloud', '');

      var suffix = ".inseng.test";
      if (window.location.href.includes('inseng.test')) {
        suffix = ".inseng.test";
      } else {
        suffix = ".instructure.cloud";
      }
      if (currentHost.length > 0) {
        currentHost = "-" + currentHost;
      }
      const url = `http://canvas-webpack-term${currentHost}${suffix}/`;
      window.open(url, '_blank');
    }}
  ];

  function createDock() {
    const triggerArea = document.createElement('div');
    triggerArea.className = 'dock-trigger-area';
    triggerArea.id = 'macos-dock-trigger';

    const dock = document.createElement('div');
    dock.id = 'macos-dock-extension';

    apps.forEach(app => {
      const dockItem = document.createElement('div');
      dockItem.className = 'dock-item';

      const appIcon = document.createElement('div');
      appIcon.className = 'app-icon';

      const iconImg = document.createElement('img');
      iconImg.src = chrome.runtime.getURL(app.iconPath);
      iconImg.alt = app.name;
      iconImg.style.width = '100%';
      iconImg.style.height = '100%';
      iconImg.style.borderRadius = '12px';
      appIcon.appendChild(iconImg);

      const tooltip = document.createElement('div');
      tooltip.className = 'tooltip';
      tooltip.textContent = app.name;

      dockItem.appendChild(appIcon);
      dockItem.appendChild(tooltip);
      dock.appendChild(dockItem);

      dockItem.addEventListener('click', (e) => {
        e.preventDefault();
        e.stopPropagation();
        app.action();
      });
    });

    document.body.appendChild(triggerArea);
    document.body.appendChild(dock);

    return { dock, triggerArea };
  }

  function createDeployButton() {
    // Remove existing deploy button if it exists
    console.log('Creating deploy button if on Gerrit page');
    const existingButton = document.getElementById('gerrit-deploy-button');
    if (existingButton) {
      existingButton.remove();
    }

    if (!window.location.href.startsWith('https://gerrit.instructure.com/c/canvas-lms/+')) {
      return;
    }

    const deployButton = document.createElement('button');
    deployButton.id = 'gerrit-deploy-button';
    deployButton.className = 'gr-button';

    // Position the button in the top right
    deployButton.style.position = 'absolute';
    deployButton.style.top = '94px';
    deployButton.style.right = '10px';
    deployButton.style.zIndex = '9999';
    deployButton.style.color = 'white';
    deployButton.style.border = 'none';
    deployButton.style.borderRadius = '4px';
    deployButton.style.padding = '6px 6px';
    deployButton.style.fontSize = '13px';
    deployButton.style.fontFamily = 'Roboto, sans-serif';
    deployButton.style.cursor = 'pointer';
    deployButton.style.boxShadow = '0 2px 4px rgba(0,0,0,0.2)';
    deployButton.style.display = 'none'; // Hide initially until status is checked

    var changeId = '';
    if (window.location.href.lastIndexOf("/") == window.location.href.length - 1) {
      //url ends with /[0=9]*/, in other words, number and a slash, now extract just the number
      changeId = window.location.href.split('/').slice(-2, -1)[0];
    } else {
      changeId = window.location.href.substring( window.location.href.lastIndexOf("/") + 1)
    }

    if (changeId) {
      console.log('Detected changeId:', changeId);
      fetch('instructure://status?refs=' + changeId)
        .then(response => response.json())
        .then(data => {
          console.log('Deployment status response:', data);
          gerritRef = data.ref || '';
          console.log('Extracted gerritRef:', gerritRef);
          deployButton.style.display = 'block'; // Show button after status check
          deployButton.textContent = data.label || 'DEPLOY';
          deployButton.style.backgroundColor = '#2e7d32'; // Green
          deployButton.dataset.buttonType = data.label.toLowerCase() === 'run' ? 'run' : 'deploy';

          if ( deployButton.textContent === 'RUN') {
            // 200 OK - show RUN button with green styling
            deployButton.textContent = 'RUN';
            deployButton.style.backgroundColor = '#2e7d32'; // Green
            deployButton.dataset.buttonType = 'run';

            // Update hover states for green button
            deployButton.addEventListener('mouseenter', () => {
              deployButton.style.backgroundColor = '#1b5e20'; // Darker green on hover
            });

            deployButton.addEventListener('mouseleave', () => {
              deployButton.style.backgroundColor = '#2e7d32'; // Back to green
            });

            // deployButton.addEventListener('mousedown', () => {
            //   deployButton.style.backgroundColor = '#1b5e20'; // Darker green on click
            // });

            // deployButton.addEventListener('mouseup', () => {
            //   deployButton.style.backgroundColor = '#2e7d32'; // Back to green
            // });
          } else {
            // Non-200 response - show regular DEPLOY button
            deployButton.textContent = 'DEPLOY';
            deployButton.style.backgroundColor = '#1565c0'; // Blue
            deployButton.dataset.buttonType = 'deploy';

            // Update hover states for blue button
            deployButton.addEventListener('mouseenter', () => {
              deployButton.style.backgroundColor = '#0056b3';
            });

            deployButton.addEventListener('mouseleave', () => {
              deployButton.style.backgroundColor = '#1565c0';
            });
          }
        })
        .catch(error => {
          // On error, default to DEPLOY button
          console.log('Error checking deployment status:', error);
        });
    }

    deployButton.addEventListener('click', () => {
      if (gerritRef) {
        if (deployButton.textContent === 'RUN') {
          var targetUrl = 'https://' + gerritRef.split("refs/changes/").join("").split("/").join("-") + '-canvas-web.instructure.cloud/';
          window.open(targetUrl, '_blank');
        } else {
          var targetUrl = 'instructure://deploy?refs=' + encodeURIComponent(gerritRef);
          window.open(targetUrl, '_blank');
          console.log('Opening URL with detected patchset:', targetUrl);
        }
      }
    });

    document.body.appendChild(deployButton);
  }

  // Function to create and inject the Solve button
  function createSolveButton() {
    const solveButton = document.createElement('button');
    solveButton.id = 'jira-solve-button';
    solveButton.className = 'solve-button';
    solveButton.textContent = 'Solve';
    solveButton.alt = 'Solve Ticket';

    // Open google.com in a new tab when clicked
    solveButton.addEventListener('click', () => {
      var url = window.location.href;
      // https://instructure.atlassian.net/browse/LX-3477
      // Extract the ticket ID from the URL
      var ticketId = url.split('/browse/')[1].split('?')[0];
      // console.log('Extracted ticket ID:', ticketId);
      window.open('instructure://solve?id=' + ticketId, '_blank');
    });

    return solveButton;
  }

  // Function to inject button on ticket detail page
  function injectButtonOnTicketPage() {
    // Check if button already exists
    if (document.getElementById('jira-solve-button')) {
      return;
    }

    // Look for the "Improve Story" button
    const improveButton = document.querySelector('[data-testid="issue-improve-issue-dropdown.improve-issue-dropdown--trigger"]');

    if (improveButton) {
      // Insert the Solve button right after the Improve Story button
      const solveButton = createSolveButton();
      // improveButton.parentElement.insertBefore(solveButton, improveButton.nextSibling);
      improveButton.parentElement.parentElement.appendChild(solveButton);
      console.log('Solve button injected next to Improve Story button');
      return;
    }

    // Fallback: Look for other action containers if Improve Story button not found
    const actionsContainer = document.querySelector('[data-testid="issue.views.issue-base.foundation.quick-add.quick-add-item.add-button"]')?.parentElement?.parentElement;

    if (actionsContainer) {
      const solveButton = createSolveButton();
      actionsContainer.parentElement.appendChild(solveButton);
      console.log('Solve button injected on ticket page (fallback)');
      return;
    }

    // Another fallback
    const headerActions = document.querySelector('[data-testid="issue.views.issue-base.foundation.breadcrumbs.breadcrumb-current-issue-container"]')?.closest('[data-component-selector="issue-view-header"]');

    if (headerActions) {
      const buttonContainer = headerActions.querySelector('[class*="actions"]') || headerActions;
      const solveButton = createSolveButton();
      buttonContainer.parentElement.appendChild(solveButton);
      console.log('Solve button injected on ticket page (fallback 2)');
    }
  }

  // Main function to detect page type and inject button
  function injectSolveButton() {
    const url = window.location.href;

    // Check if we're on a ticket detail page
    if (url.includes('/browse/') && url.includes('instructure.atlassian.net')) {
      injectButtonOnTicketPage();
    }
  }

  function setupDockBehavior() {
    injectSolveButton();
    // Create deploy button for Gerrit pages
    createDeployButton();

    // Listen for URL changes in SPAs like Gerrit
    let lastUrl = window.location.href;
    const observer = new MutationObserver(() => {
      if (window.location.href !== lastUrl) {
        lastUrl = window.location.href;
        injectSolveButton();
        createDeployButton(); // Re-evaluate button visibility on URL change
      }
    });
    observer.observe(document.body, { childList: true, subtree: true });
    const observer2 = new MutationObserver((mutations) => {
      // Debounce the injection to avoid too many calls
      clearTimeout(window.solveButtonTimeout);
      window.solveButtonTimeout = setTimeout(() => {
        injectSolveButton();
      }, 1000);
    });
    observer2.observe(document.body, { childList: true, subtree: true });

    // Also listen for popstate events (back/forward buttons)
    window.addEventListener('popstate', () => {
      setTimeout(createDeployButton, 100); // Small delay to ensure URL is updated
    });

    // Only show dock on instructure.cloud domains
    if (!window.location.hostname.endsWith('.instructure.cloud') && !window.location.hostname.endsWith('.inseng.test')) {
      return;
    }
    const { dock, triggerArea } = createDock();
    let hoverTimeout;
    let hideTimeout;

    function showDock() {
      clearTimeout(hideTimeout);
      dock.classList.add('visible');
    }

    function hideDock() {
      dock.classList.remove('visible');
    }

    function scheduleDockHide() {
      clearTimeout(hideTimeout);
      hideTimeout = setTimeout(hideDock, 300);
    }

    triggerArea.addEventListener('mouseenter', showDock);

    dock.addEventListener('mouseenter', () => {
      clearTimeout(hideTimeout);
      showDock();
    });

    dock.addEventListener('mouseleave', scheduleDockHide);

    document.addEventListener('mousemove', (e) => {
      const windowHeight = window.innerHeight;
      const mouseY = e.clientY;
      const isNearBottom = mouseY > windowHeight - 50;

      if (isNearBottom && !dock.classList.contains('visible')) {
        clearTimeout(hoverTimeout);
        hoverTimeout = setTimeout(showDock, 100);
      } else if (!isNearBottom && !dock.matches(':hover')) {
        clearTimeout(hoverTimeout);
        if (dock.classList.contains('visible')) {
          scheduleDockHide();
        }
      }
    });

    document.addEventListener('scroll', () => {
      if (!dock.matches(':hover')) {
        scheduleDockHide();
      }
    });
  }

  // Wait for full page load to ensure Gerrit content is available
  if (document.readyState === 'complete') {
    setupDockBehavior();
  } else {
    window.addEventListener('load', setupDockBehavior);
  }
})();