var gunfightInstalled = false;
var gunfightDownloading = false;
var downloadTimer = null;
var gunfightDownloadXpiUrl = "";
var gunfightDownloadCabUrl = "";

function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
        var pluginsArrayLength = navigator.plugins.length;
        // for each plugin...
        for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
            // loop through all desired names and check each against the current plugin name
            var numFound = 0;
            for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
                // if desired plugin name is found in either plugin name or description
                if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
                    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
                    // this name was found
                    numFound++;
                }
            }
            // now that we have checked all the required names against this one plugin,
            // if the number we found matches the total number provided then we were successful
            if(numFound == daPlugins.length) {
                pluginFound = true;
                // if we've found the plugin, we can stop looking through at the rest of the plugins
                break;
            }
        }
    }
    return pluginFound;
} // detectPlugin

function gunfightInstallComplete(url, status) {
//    log('gunfightInstallCompleted CALLED '+url+' status = '+ status);
    // refresh the page
    if (status == 0) {
        doDownloadComplete();
        document.write("Gunfight Plugin Install Complete");
//        window.location.reload(false); // false = don't pull from server again
    } else if (status == -210) {
        document.write("Gunfight Plugin Install Cancelled! You must install the plugin before you can play!");
        doDownloadFailed("User canceled install!");
    } else {
        document.write("Gunfight Plugin Install Failed: "+status);
        doDownloadFailed("Plugin Install Failed: "+status);
    }
}

function triggerDownload(onPlayPage) {
    if (window.ActiveXObject) {
        // MSIE (or any browser that supports Active X)
        // install the plugin, but only if we aren't doing status checks,
        // because status checks indicate that we are on the play page
        if (!onPlayPage) {
            log('GFPLUGIN ACTIVEX NOT DETECTED!');
            document.write('<object classid="CLSID:601543EE-C08C-11DC-8EA0-848C56D89593"');
            document.write(' codebase="'+gunfightDownloadCabUrl+'"');
            document.write(' width="0" height="0" id="theplugin">');
            document.write('<\/object>');
        } else {
            // we are on the play page, so keep checking till we
            // find this has been installed
            downloadTimer = setInterval('checkForGunfighterPlugin('+doStatusCheck+')', 1000);
        }
    } else if (navigator.appName == 'Opera') {
        // FIXME: how to trigger install on Opera
        document.write('TODO: trigger download on Opera '+navigator.platform);
        gunfightDownloading = false;
        return;
    } else if (navigator.appVersion.indexOf('Safari') != -1) {
        // FIXME: how to trigger install on Safari
        document.write('TODO: trigger download on Safari '+navigator.platform);
        gunfightDownloading = false;
    } else {
        // Firefox or other Netscape browser
        var url = gunfightDownloadXpiUrl;
        if (navigator.platform == "MacPPC") {
            url = url.replace('_setup', '_macppc');
        } else if (navigator.platform == "MacIntel") {
            url = url.replace('_setup', '_macx86');
        } else if (navigator.platform != "Win32") {
            document.write('Unsupported Platform '+navigator.platform);
            gunfightDownloading = false;
            return;
        }
        xpi={'Wild West Gunfighter Plugin':url};
        InstallTrigger.install(xpi, gunfightInstallComplete);
        log(' DOWNLOAD START!');
    }
}

function checkForGunfighterPluginNoDownload() {
    log('checkForGunfighterPluginNoDownload():');
    if (!gunfightInstalled) {
        log(' NotInstalled');
        if (window.ActiveXObject) {
            detected = detectGunfighterActiveXControl();
        } else {
            detected = detectPlugin('Tenderfoot');
        }
        if (detected == true) {
            log(' DETECTED!');
            // plugin is present
            gunfightInstalled = true;
        } else {
            log(' NotDetected');
            // plugin not present
            gunfightInstalled = false;
        }
    }
    return gunfightInstalled;
} // check for Gunfighter Plugin, but do not download

function checkForGunfighterPlugin(doStatusCheck) {
    log('checkForGunfighterPlugin():');
    var onPlayPage = doStatusCheck;
    if (!gunfightInstalled) {
        log(' NotInstalled');
        if (window.ActiveXObject) {
            if (doStatusCheck) {
                // we are on the play page for IE, so we have to just assume it's detected
                // otherwise the attempt to detect it will screw up the plugin
                detected = true;
            } else {
                detected = detectGunfighterActiveXControl();
            }
        } else {
            detected = detectPlugin('Tenderfoot');
        }
        if (detected == true) {
            log(' DETECTED!');
            // plugin is present
            gunfightInstalled = true;
            if (gunfightDownloading == true) {
                log(' DOWNLOAD COMPLETE!');
                // we were downloading but the download is now complete
                gunfightDownloading = false;
                doDownloadComplete();
            }
        } else {
            log(' NotDetected');
            // plugin not present
            if (!gunfightDownloading) {
                // we haven't started the download yet
                // setup check for download completion
                log(' NotDownloading');
                gunfightDownloading = true;
                // now trigger the download
                triggerDownload(onPlayPage);
           }
        }
    }
    if (gunfightInstalled) {
        log('GFPLUGIN INSTALLED!');
        // plugin is installed, start up the game over check
        //setInterval('check(gameId)',3000);
        if (doStatusCheck) {
            checkGameStatus(gameId);
        }
        if (downloadTimer != null) {
            clearInterval(downloadTimer);
        }
    }
} // check for Gunfighter Plugin

function canDetectPlugins() {
    if( window.ActiveXControl || (navigator.plugins && navigator.plugins.length > 0) ) {
        return true;
    } else {
        return false;
    }
}

function detectGunfightPlugin() {
    var pluginFound = detectPlugin('Tenderfoot');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && window.ActiveXObject) {
        pluginFound = detectGunfighterActiveXControl();
    }
    return pluginFound;
}

function detectGunfighterActiveXControl() {
    var newObj = null;
    try {
        newObj = new ActiveXObject('npTFGLaunchPlugin.axTFGLaunchPlugin.1');
        return true;
    }
    catch(e) {
        log(e.description);
        return false;
    }
    if (newObj != null) {
        delete newObj;
    }
}

function log(s) {
//    break_error
    // append the statement
/*    div = document.getElementById('gflogger');
    var p = document.createElement('p');
    var text = document.createTextNode(s);
    p.appendChild(text);
    div.appendChild(p); */
}