myLocalizedDownloadInstructions = null;
myLanguageVersion = null;
myDefaultPartner = null;

function isUpgradeableFlashVersion()
{
    var theVersion = swfobject.getFlashPlayerVersion();

    if ( isIEOnVista() )
    {
        return (theVersion.major > 7);
    }

    return ( theVersion.major > 6 || (theVersion.major == 6 && theVersion.release == 65) );
}

function isMac()
{
    return navigator.userAgent.toLowerCase().match(/macintosh/);
}

function isSafariOnMac()
{
    return isSafari() && isMac();
}

function isSafariOnWin()
{
    return isSafari() && !isMac();
}

function isSafari()
{
    return navigator.userAgent.toLowerCase().match(/safari/);
}

function isIEOnVista()
{
    if ( isIE() )
    {
        return navigator.userAgent.match(/Windows NT 6/);
    }
    return false;
}

function isIE()
{
    if (document.all)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function isSupportedClientPlatform()
{
    return navigator.userAgent.match(/Windows|WinNT/);
}

function isClientInstalled()
{
    if (isClientInstallTrueInFlashCookie())
    {
        return true;
    }
    else if (isClientInstallTrueInIECookie())
    {
        return true;
    }
    return false;
}

function showRolloverPendingClientInstall(aShowFlag)
{
    if( !isClientInstalled() && isSupportedClientPlatform() && isIE() )
    {
        theRolloverImage = document.getElementById("rollover");
        theRolloverImage.style.display = aShowFlag ? "inline" : "none";
    }
}

function isClientInstallTrueInFlashCookie()
{
    var theInstalledFlag = getSmileboxProperty("Installed");
    return ("true" == theInstalledFlag);
}

function isClientInstallTrueInIECookie()
{
    var theInstalledFlag = GetCookie("Installed");
    return ("true" == theInstalledFlag);
}

function setTemplateIdAndDownloadOrLaunch( aTemplateId, aDontGotoDownloadInstructionsPageFlag )
{
    setSmileboxProperty( "startupArgs", "smilebox://download?templateId=" + aTemplateId );
    downloadOrLaunch( aTemplateId, aDontGotoDownloadInstructionsPageFlag );
}

function getProtocolCommandForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter )
{
    if( !anAfterInstallAction )
    {
        return null;
    }

    var theUrlEncodedAfterInstallActionParameter = anAfterInstallActionParameter;
    if ( ! isIE() )
    {
        theUrlEncodedAfterInstallActionParameter = encodeURIComponent( anAfterInstallActionParameter );
    }

    if ( anAfterInstallAction.toLowerCase() == "search" )
    {
        return "smilebox://invokesearch?keywords=" + theUrlEncodedAfterInstallActionParameter;
    }
    else if ( anAfterInstallAction.toLowerCase() == "opencatalog" )
    {
        return "smilebox://opencategorybypath?path=" + theUrlEncodedAfterInstallActionParameter;
    }
    else if ( anAfterInstallAction.toLowerCase() == "downloadcreation" )
    {
        return "smilebox://downloadcreation?sendevent=" + theUrlEncodedAfterInstallActionParameter;
    }
    else if ( anAfterInstallAction.toLowerCase() == "giftsubscription" )
    {
        return "smilebox://giftsubscription";
    }
    else if ( anAfterInstallAction.toLowerCase() == "activate" )
    {
        return "smilebox://activate?templateId=" + theUrlEncodedAfterInstallActionParameter;
    }

    return null;
}

function setStartupArgsForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter )
{
    var theProtocolCommand = getProtocolCommandForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter );
    if ( theProtocolCommand )
    {
        setSmileboxProperty( "startupArgs", theProtocolCommand );
    }
}

function downloadOrLaunchForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter, aDontGotoDownloadInstructionsPageFlag )
{
    localizePartnerCode();
    setStartupArgsForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter );
    downloadOrLaunch( null, aDontGotoDownloadInstructionsPageFlag, anAfterInstallAction, anAfterInstallActionParameter );
}

function localizePartnerCode()
{
    if ( myLanguageVersion )
    {
		var thePartnerCode = getSmileboxProperty("partner");
		if( thePartnerCode.search( myLanguageVersion ) == -1 )
		{
			setSmileboxProperty( "partner", myDefaultPartner );
		}
    }
}

function getDownloadOrLaunchUrlAndSetFlashCookiesForPlayback(aPartnerCode, aCampaign, anAfterInstallAction, anAfterInstallActionParameter)
{
    var theUrl;

    setStartupArgsForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter );

    if( isClientInstalled() )
    {
        theUrl = getLaunchSmileboxUrl( null, anAfterInstallAction, anAfterInstallActionParameter );
    }
    else
    {
        setSmileboxProperty( "partner", aPartnerCode );
        setSmileboxProperty( "campaign", aCampaign );
        theUrl = getDownloadUrlAndPingGoogleAnalytics();
    }

    return theUrl;
}

function getDownloadOrLaunchUrlAndSetFlashCookies(aPartnerCode, aCampaign, anAfterInstallAction, anAfterInstallActionParameter)
{
    var theUrl;

    setSmileboxProperty( "partner", aPartnerCode );
    setSmileboxProperty( "campaign", aCampaign );
    setStartupArgsForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter );

    if( isClientInstalled() )
    {
        theUrl = getLaunchSmileboxUrl( null, anAfterInstallAction, anAfterInstallActionParameter );
    }
    else
    {
        theUrl = getDownloadUrlAndPingGoogleAnalytics();
    }

    return theUrl;
}

function downloadOrLaunch( aTemplateId, aDontGotoDownloadInstructionsPageFlag, anAfterInstallAction, anAfterInstallActionParameter )
{
    setAudible( false );
    if( isClientInstalled() )
    {
        launchSmilebox( aTemplateId, anAfterInstallAction, anAfterInstallActionParameter );
    }
    else
    {
        forceDownload( aDontGotoDownloadInstructionsPageFlag );
    }
}

function navigateToDownloadInstructions()
{
    if ( myLocalizedDownloadInstructions )
    {
        window.location = myLocalizedDownloadInstructions;
        return;
    }
    window.location = "/downloadInstructions.html";
}

function forceDownload( aDontGotoDownloadInstructionsPageFlag )
{
    if ( !aDontGotoDownloadInstructionsPageFlag )
    {
        setTimeout("navigateToDownloadInstructions();", 5000);
    }
    window.location = getDownloadUrlAndPingGoogleAnalytics();
}

function launchSmilebox( aTemplateId, anAfterInstallAction, anAfterInstallActionParameter )
{
    var theLaunchUrl = getLaunchSmileboxUrl( aTemplateId, anAfterInstallAction, anAfterInstallActionParameter );
    pingGoogleAnalytics( theLaunchUrl );
    window.location = theLaunchUrl;
}

function getLaunchSmileboxUrl( aTemplateId, anAfterInstallAction, anAfterInstallActionParameter )
{
    var theLaunchUrl = null;

    if( aTemplateId )
    {
        theLaunchUrl = "smilebox://download?templateId=" + aTemplateId;
    }
    else if ( anAfterInstallAction  )
    {
        theLaunchUrl = getProtocolCommandForPageConfiguration( anAfterInstallAction, anAfterInstallActionParameter );
    }

    if ( theLaunchUrl == null )
    {
        theLaunchUrl = "smilebox://start";
    }

    return theLaunchUrl;
}

function getDownloadUrlAndPingGoogleAnalytics()
{
    setAudible( false );
    var thePartnerCode = getSmileboxProperty("partner");
    thePartnerCode = thePartnerCode ? thePartnerCode : "smilebox";
    var theCampaign = getSmileboxProperty("campaign");

    pingGoogleAnalytics( "http://www.smilebox.com/go/downloadInstaller/" + thePartnerCode + "&campaign=" + theCampaign );

    return "http://secure.smilebox.com/ecom/partnerRedirect/downloadInstaller?partner=" + thePartnerCode + "&campaign=" + theCampaign;
}

function pingGoogleAnalytics( aUrl )
{
    try
    {
        if (pageTracker)
        {
            pageTracker._trackPageview( aUrl );
        }
        else
        {
            urchinTracker( aUrl );
        }
    }
    catch (e)
    {
    }
}

function isStupidUrchinTrackerBug()
{
    return isIE() && window.location.href.indexOf( "iframe.html" ) != -1;
}

var mySoundOnFlag = true;
function toggleMute()
{
    setAudibleAndGraphics( !mySoundOnFlag  );
}

function setAudibleAndGraphics( aMuteFlag )
{
    setMuteGraphics( aMuteFlag );
    setAudible( aMuteFlag );
}

function setMuteGraphics( anAudibleFlag )
{
    var theMuteBackground = "url(/globalImages/muteButton.gif)";
    var theUnMuteBackground = "url(/globalImages/unmuteButton.gif)";
    var theMuteButton = document.getElementById("muteButton");

    if (!theMuteButton)
    {
        theMuteButton = document.getElementById("partnerMuteButton");
        theMuteBackground = "url(/globalImages/partner/muteButton.gif)";
        theUnMuteBackground = "url(/globalImages/partner/unmuteButton.gif)";
    }

    if (theMuteButton)
    {
        if (anAudibleFlag)
        {
            theMuteButton.style.backgroundImage = theMuteBackground;
        }
        else
        {
            theMuteButton.style.backgroundImage = theUnMuteBackground;
        }
    }
}

function setAudible( anAudibleFlag )
{
    mySoundOnFlag = anAudibleFlag;

    var theFlashMovie = document.getElementById("Smilebox");
    if (theFlashMovie)
    {
        theFlashMovie.setAudible( anAudibleFlag );
    }

    try
    {
        theFlashMovie = document.getElementById("DesignPreviewSwf");
        if (theFlashMovie)
        {
            if ( anAudibleFlag )
            {
                theFlashMovie.Play();
            }
            else
            {
                theFlashMovie.StopPlay();
            }
        }
    }
    catch(e)
    {
    }
}

