/*=============================================================
| DOWNLOAD THEMEPACK
|
| Author:    Jeremy Caney, Ignia LLC (Jeremy@ignia.com)
| Client     MSN
|
| Purpose :  Downloads a themepack and provides error handling
|
>==============================================================
| Revisions  Date        Author          Comments
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|            05.05.06    Unknown         Imported from template
|            05.05.06    Jeremy Caney    Customized messaging
\------------------------------------------------------------*/

/*=============================================================
| DECLARE PUBLIC VARIABLES
\------------------------------------------------------------*/
var errRequestFailed = "The content installation failed.  Please try again.";
var errUserCanceled  = "";
var errUnknown       = "We could not find the requested themepack or there was a problem with the download.  Please try again later.";
var errLogin         = "Please sign into MSN Messenger and try again";
var errContent       = "We could not find the requested themepack.  Please try again later.";
var errBusy          = "Please wait until the first download process is completed, then try again.";
var strSuccess       = "The theme has been installed and is ready to use in Messenger.";

var errStatus        = 0;

/*=============================================================
| MSN DOWNLOAD CONTENT: Initialize download
\------------------------------------------------------------*/
function installTheme(path) {
  try {
    MessengerContentInstaller.InstallContent(path);
    } 
  catch(error) {
    OnContentInstalled(error.number);
    }
  }

/*=============================================================
| MSN MESSAGE: Provide interface for error messages
\------------------------------------------------------------*/
function installThemeError(msg) {
  alert(msg);
  errStatus = 1;
  }
function installThemeSuccess(msg) {
  alert(msg);
  }

/*=============================================================
| ON CONTENT INSTALLED: Provide follow-up messaging
\------------------------------------------------------------*/
function OnContentInstalled(result) {

  tail = (result & 0xFFFF);
  head = ((result >> 16) & 0xFFFF);

  if (tail == 0x036c) {
  //http request failed
  //MSGR_E_P4_HTTP_DOWNLOAD
    installThemeError(errRequestFailed);
    } 
  else if (tail == 0x038B) {
  //user clicked cancel
  //MSGR_E_USER_CANCEL_DOWNLOAD
    installThemeError(errUserCanceled);
    } 
  else if (tail == 0x4005) {
  //generic fail
  //E_FAIL
    installThemeError(errUnknown);
    } 
  else if (tail == 0xFFFF) {
  //unexpected error
  //E_UNEXPECTED
    installThemeError(errUnknown);
    } 
  else if (tail == 0x031E) {
  //user offline
  //MSGR_E_NOT_LOGGED_ON
    installThemeError(errLogin);
    } 
  else if (tail == 0x0057) {
  //download url empty
  //E_INVALIDARG
    installThemeError(errContent);
    } 
  else if (tail == 0x0005) {
  //there's already a download in place
  //E_ACCESSDENIED
    installThemeError(errBusy);
    } 
  else if (tail == 0 && head == 0) {
    installThemeSuccess(strSuccess);
    }
  else {
    installThemeError(errUnknown);
    }
  }
