var wildpocketsClsid = 'E07C07F7-6841-419A-BAC5-B02AB7A2B6DB';
var wildpocketsMimeType = 'application/wildpockets-plugin-buildandsmash';
var wildpocketsPluginDllname = 'Wild Pockets Plugin Build and Smash';
var wildpocketsRelease = 1; var wildpocketsMajor = 0; var wildpocketsMinor = 5; var wildpocketsBuild = 10524;
var wildpocketsVersionStr = wildpocketsRelease+','+wildpocketsMajor+','+wildpocketsMinor+','+wildpocketsBuild;
var wildpocketsCabURL = 'http://www.wildpockets.com/common/WildPocketsCubCtrlBuildAndSmash10524.cab';
var wildpocketsXpiURL = 'http://www.wildpockets.com/common/WildPocketsCubCtrlBuildAndSmash10524.xpi';


var wpPlatformUnsupportedImage = 'http://www.wildpockets.com/imagelib/plugin/windowsonly.jpg';
var wpDownloadPluginImage      = 'http://www.wildpockets.com/imagelib/plugin/downloadplugin.jpg';
var wpUpdatePluginImage        = 'http://www.wildpockets.com/imagelib/plugin/updateplugin.jpg';

function createPocket(ObjectID, Width, Height, Mode, Pocket, Service, Uid, SessionKey) {
    document.writeln(createPocketString(ObjectID, Width, Height, Mode, Pocket, Service, Uid, SessionKey));
}

function createPocketString(ObjectID, Width, Height, Mode, Pocket, Service, Uid, SessionKey) {

    htmlout = '';

    var qsParam = new Array();
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            qsParam[key] = val;
        }
    }

    // Try to determine where we are.
    // If InstallTrigger exists, we must be in some sort of Mozilla browser.
    // Otherwise, we'll show the ActiveX control.
    var showMoz = true;
    var showIE = true;
    try {
        if (InstallTrigger) {
            //showIE = false;
            // Moz browsers ignore the object tag, so render it anyways in case we hit
            // some weird browser that supports ActiveX *and* InstallTrigger.
        }
    } catch (e) {
        showMoz = false;
    }

    var supportedArchitecture=true;
    var browserOK = true;
    var pluginInstalled = false;
    var pluginUpToDate = false;

    if (navigator && navigator.platform) {
        var platform=navigator.platform;
        if(!platform.match("Win")) {
            supportedArchitecture=false;
        }
    }
    /*
    if(!supportedArchitecture) {
        htmlout += "We're sorry; Wild Pockets currently only works on " +
                   "Windows operating systems. Stay tuned for versions " +
                   "for other platforms!";
        return;
    }
    */
    // Check what version of the plugin is installed and determine if it is up-to-date
    // with what this page is offering.
    if (navigator && navigator.plugins) {
        var wpPlugin = navigator.plugins[wildpocketsPluginDllname];
        if (wpPlugin) {
            pluginInstalled = true;
            var wpPluginDesc = wpPlugin.description;
            var wpPluginVer = new RegExp(wildpocketsPluginDllname+' (.*)').exec(wpPluginDesc)[1];
            var parts = new RegExp('([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)').exec(wpPluginVer);
            var release = parts[1]; var major = parts[2]; var minor = parts[3]; var build = parts[4];
            if (wildpocketsRelease <= release) {
                if (wildpocketsMajor <= major) {
                    if (wildpocketsMinor <= minor) {
                        if (wildpocketsBuild <= build) {
                            pluginUpToDate = true;
                        }
                    }
                }
            }
        }
    } else {
        browserOK = false;
    }

    if (!supportedArchitecture) {
        htmlout += '<div style="width:'+Width+'px; height:'+Height+'px">';
        htmlout += '<img src="'+wpPlatformUnsupportedImage+'">';
        htmlout += '</div>';
    } else {

        // Show ActiveX block
        if (showIE) {
            htmlout +=
                '<object id="' + ObjectID + '"' +
                ' classid="CLSID:' + wildpocketsClsid + '"' +
                ' codebase="' + wildpocketsCabURL + '#version=' + wildpocketsVersionStr + '"' +
                ' width="' + Width + '"' +
                ' height="' + Height + '">' +
                '<param name="identifier" value="' + ObjectID + '"></param>' +
                '<param name="deferred" value="1"></param>' +
                '<param name="defertimeout" value="5"></param>';
            if (Mode) {
                htmlout +=
                    '<param name="mode" value="' + Mode + '"></param>';
            }
            if (Pocket) {
                htmlout +=
                    '<param name="open" value="' + Pocket + '"></param>';
            }
            if (Service && Uid && SessionKey) {
                htmlout +=
                    '<param name="service" value="' + Service + '"></param>' +
                    '<param name="uid" value="' + Uid + '"></param>' +
                    '<param name="sessionkey" value="' + SessionKey + '"></param>';
            }
        }
        // Show Mozilla block
        if (showMoz) {
            if (!pluginInstalled) {
                // plugin not installed, offer to install fresh
                htmlout +=
                    '<div style="width:' + Width + 'px; height:' + Height + 'px; display:table; #position:relative; overflow:hidden;">' +
                    '<div style="#position:absolute; #top:50%; display:table-cell; vertical-align:middle;">' +
                    '<div style="#position:relative; #top:-50%">' +
                    '<center><image onclick="installWildpocketsFirefoxWin();" src="'+wpDownloadPluginImage+'"></center>' +
                    '</div></div></div>';
            } else if (pluginInstalled && !pluginUpToDate) {
                htmlout +=
                    '<div style="width:' + Width + 'px; height:' + Height + 'px; display:table; #position:relative; overflow:hidden;">' +
                    '<div style="#position:absolute; #top:50%; display:table-cell; vertical-align:middle;">' +
                    '<div style="#position:relative; #top:-50%">' +
                    '<center><image onclick="installWildpocketsFirefoxWin();" src="'+wpUpdatePluginImage+'"></center>' +
                    '</div></div></div>';
            } else {
                htmlout +=
                    '<embed name="' + ObjectID + '"' +
                    ' identifier="' + ObjectID + '"' +
                    ' deferred="1" defertimeout="5"' +
                    ' type="' + wildpocketsMimeType + '"' +
                    ' width="' + Width + '"' +
                    ' height="' + Height + '"';
                if (Mode) {
                    htmlout +=
                        ' mode="' + Mode + '"';
                }
                if (Pocket) {
                    htmlout +=
                        ' open="' + Pocket + '"';
                }
                if (Service && Uid && SessionKey) {
                    htmlout +=
                        ' service="' + Service + '"' +
                        ' uid="' + Uid + '"' +
                        ' sessionkey="' + SessionKey + '"';
                }
                htmlout += '></embed>';
            }
        }
        // End ActiveX block
        if (showIE) {
            htmlout += '</object>';
        }
    }
    
    return htmlout;
}

function installWildpocketsFirefoxWin() {
    eval('params = { "' + wildpocketsPluginDllname + '": { URL: "' + wildpocketsXpiURL + '" } };');
    InstallTrigger.install(params);
}

function pocketPerform(id, command, args) {
    try {
        getPocket(id).perform(command,args);
        return;
    } catch (e) {
        setTimeout("pocketPerform('"+id+"','"+command+"','"+args+"');",100);
        return;
    }
}

function pocketEvaluate(id, command, args) {
    try {
        var result = getPocket(id).evaluate(command,args);
        return result;
    } catch (e) {
        return;
    }
}

function getPocket(id) {
    return window.document[id];
}

function wp_urlencode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}

function wp_urldecode(encodedString) {
    var output = encodedString;
    var binVal, thisString;
    var myregexp = /(%[^%]{2})/;
    while ((match = myregexp.exec(output)) != null
                && match.length > 1
                && match[1] != '') {
        binVal = parseInt(match[1].substr(1),16);
        thisString = String.fromCharCode(binVal);
        output = output.replace(match[1], thisString);
    }
    return output;
}