// Sniffer.js must be included before this file

var iPopupWidth = 780;
var iPopupHeight = 601;
var strPopupOptions = 'location=0,menubar=0,toolbar=0,status=1,scrollbars=0,' +
        'resizable=1,directories=0,center:yes';

// must be initialized with a call to SetValues()
var cphost = '';
var strPluginURL = '';

function SetValues(New_cphost, New_strPluginURL) {
    cphost = New_cphost;
    strPluginURL = New_strPluginURL;
}

var SurveyURL = '';

// Embed the "personalize" button on the page
function WritePersButton(strButtonURL, iWidth, iHeight, iBorder)
{
    var strSize = 'width="' + iWidth + '" height="' + iHeight + '"';

    // If IE, or with no Mozilla popup plugin, use the normal button
    //   (which for Mozilla will trigger a javascript call to install plugin)
    if (is_ie || !IsPopupPluginUpToDate(true)) {
        document.write('<a href="Javascript: clickedPersButton();">\n');
        document.write('<img style="border-color:black" src="' + strButtonURL +
                '" ' + strSize + ' border="' + iBorder + 
                '" alt="Personalize and print your project!"></a>\n');
        return;
    }
    
    // If here, we're a Mozilla popup plugin user who is already installed
    var l = 0;
    var t = 0;
    var w = window.screen.availWidth - 30;
    var h = window.screen.availHeight - 60;
    if (!(iBorder >= 1)) {
        iBorder = 0;
    }
    var strPopupSize = 'width=' + w + ',height=' + h + ',screenX=' + l + ',screenY=' + t;

    document.write('<table border="0" cellspacing="0" cellpadding="' + iBorder + '" bgcolor="black">')
    document.write('<tr><td>');
        document.write('<object name="Personalize" type="application/x-popup-plugin" ' +
                          'codebase="'+cphost+'/install/CreateAndPrint.xpi" '+strSize+'>');
        document.write('  <param name="srcurl" value="' + strButtonURL + '"/>');
        document.write('  <param name="url" value="' + strPluginURL + '"/>');
        document.write('  <param name="context" value="' + strPopupSize + ',' + strPopupOptions + '"/>');
        document.write('</object>');
    document.write('</td></tr></table>');
}

// OnClick handler for the "personalize" button - when survey is turned on
function popUnderSurvey()
{
    strWinFeatures="resizable=yes,scrollbars=yes,width=500,height=500";
    popAwayWin=window.open(SurveyURL,"popunder",strWinFeatures);
    popAwayWin.blur()
    window.focus()
    return;
}

// OnClick handler for the normal "personalize" button
function clickedPersButton()
{
    if (is_ie) {
        PopupIEWorkspace();
    }
    else {
        InstallPopupPlugin(false);
    }
}

// Popup the workspace window for IE
function PopupIEWorkspace(strAdditions)
{
    var l = 0;
    var t = 0;
    var w = window.screen.availWidth - 30;
    var h = window.screen.availHeight - 60;
    var strPopupSize = 'width=' + w + ',height=' + h + ',left=' + l + ',top=' + t;
    var strURL = (!strAdditions ? strPluginURL : strPluginURL + strAdditions);
    
    window.open(strURL, '_blank', strPopupSize + ',' + strPopupOptions);
}

// Check the status of the popup plugin for Mozilla
function IsPopupPluginUpToDate(bSilent)
{
    if (is_ie) {
        return true;
    }

    if (is_safari) {
        return false;
    }

    var mimetype = navigator.mimeTypes["application/x-popup-plugin"];
    if (mimetype && mimetype.enabledPlugin) {
        return true;
    }

    var bXPI = ((netscape && netscape.softupdate) ? false : true);
    if (bXPI) // Perform a check to see if the XPInstall stuff is going to fail (ala Compuserve 7.0)
    {
        if (typeof(InstallTrigger) == 'undefined')
        {
            if (bSilent) {
                return false;
            }
            else {
                alert('Your Mozilla-based browser does not support the installation of software plugins.\n\n' +
                    'Create & Print requires a plugin that we cannot install.\n\n' +
                    'As an alternative, you can use Microsoft\'s ' +
                    'Internet Explorer to access all of the functionality of Create & Print.\n\n' +
                    'You might also check with your browser manufacturer to see if an update is available that will fix this problem.');
                return 2; // cancel
            }
        }
    }

    return false;
}

// Direct the browser to the XPI popup plugin, which (as long as the mime type 
//   is "application/x-xpinstall") will trigger the plugin install and eventually
//   ask the user to restart, putting them back on this page
function InstallPopupPlugin(bSilent)
{
    if (confirm('In order to use Create & Print for Mozilla, you must download and install the latest version.'+
            '\n\nThis will only take a few moments. Do you want to install the plugin?')) {
        window.location = cphost + '/install/CreateAndPrint.xpi';
    }
}