/*************

 shamelessly stolen from http://www.cooliris.com
 Install Helper
 @require prototype.js, livepipeui, scriptaculous


 **************/

var InstallHelper = {
  gbv : function() {
    var b = {
      url            : "http://cache.yoono.com/download/ff/yoono_latest.xpi",
      browser        : Browser.vendor,
      browserversion : Browser.version,
      majorVersion   : "7",
      minorVersion   : "0.10"
    }
    return b;
  },
  build: null,
  firstWindowHeight: window.innerHeight,
  windowHeight: window.innerHeight,

  clickedDownload: function() {

    InstallHelper.build = InstallHelper.gbv();
    
    //dump("Clicked DL \n");
    //dump("Browser: " + InstallHelper.build.browser + "\n");

    /*if (!InstallHelper.ff_barPresent()) {
      InstallHelper.windowHeight = window.innerHeight;
    }*/

    if (InstallHelper.build.browser == 'ff') {
      return InstallHelper.ff_begin();
    } else {
      return InstallHelper.browser_not_supported();
    }
    return false;
  },

  browser_not_supported: function() {

    //dump("Browser Not Supported! \n");

    InstallHelper.displayHelper('na_allproducts');
    return false;
  },
  ie_not_ready: function() {
    InstallHelper.displayHelper('na_ie');
    return false;
  },

  helper: false,
  current: false,
  displayHelper: function(container, title) {


    //dump("Display Helper\n");

    if (!InstallHelper.helper)
      InstallHelper.helper = InstallHelper.window_factory($('divInstallHelper'));

    if (container)
    {
      if (InstallHelper.current)
      {
        Effect.Fade(InstallHelper.current, { duration: 0.5 });
        setTimeout("Effect.Appear('" + container + "', { duration: 0.5 })", 500);
      }
      else
      {
        Effect.Appear(container);
      }
      InstallHelper.current = container;
    }

    if (!InstallHelper.helper.isOpen)
      InstallHelper.helper.open();

    //dump(Object.toJSON(InstallHelper.helper));
  },

  // LivePipe UI, Ryan Johnson
  // styled examples use the window factory for a shared set of behavior
  window_factory: function(container) {
    var w = new Control.Modal(container, Object.extend({
      overlayOpacity: 0.90,
      className: 'installhelper_window',
      closeOnClick: $('installhelper_window_close'),
      width: 337,
      minHeight: 247,
      position: [document.viewport.getWidth() - 350, 10],
      fade: true

    }, {}));

    return w;
  },

  /**************

   FIREFOX

   **************/

  ff_begin: function()
  {
    //dump("FF Begin \n");
    //dump("InstallHelper.build.browserversion "+InstallHelper.build.browserversion+" \n");

    if (InstallHelper.build.browserversion >= 3) {
      return InstallHelper.ff3_begin();

    } else if (InstallHelper.build.browserversion >= 2) {
      return InstallHelper.ff2_begin();
    }
    else {
      return true;
    }
  },

  ff_tryInstall: function()
  {
    var result = false;
    try {
      var params = new Array();
      params["Yoono " + InstallHelper.build.majorVersion + "." + InstallHelper.build.minorVersion + " for Firefox"] = {
        URL : InstallHelper.build.url,
        IconURL: "http://www.yoono.com/favicon.ico"
      };
      result = InstallTrigger.install(params);
    } catch (e) {
    }
    return result;
  },

  ff_barPresent : function()
  {
    //dump("InstallHelper.windowHeight: " + InstallHelper.windowHeight +"\nwindow.innerHeight " + window.innerHeight +"\n");
    //dump("InstallHelper.firstWindowHeight: " + InstallHelper.firstWindowHeight +"\n");
    if (InstallHelper.windowHeight > window.innerHeight)
      return true;

    var diff = InstallHelper.firstWindowHeight - InstallHelper.windowHeight;

    return (diff != 0 && (diff < 35 || diff > -35) );

  },

  /*************
   FF3
   *************/

  ff3_begin: function() {

    //dump("FF3 Begin \n");

    InstallHelper.ff3_waitForBarDown(1);
    return InstallHelper.ff_tryInstall();

  },

  ff3_waitForBarDown: function(iteration) {

    if (iteration > 5) {
      InstallHelper.ff3_allowBarUp(); // Redundant, no? well yes, its cause if you refresh with bar down, then the other check doesnt work.
    } else if (InstallHelper.ff_barPresent()) {
      InstallHelper.ff3_allowBarDown();

    } else {
      setTimeout("InstallHelper.ff3_waitForBarDown(" + (iteration + 1) + ")", 500);
    }
  },

  ff3_allowBarDown: function() {
    $('clickAllow').style.color = '#fff';
    $('clickInstallNow').style.color = '#fff';
    $('restartFirefox').style.color = '#666';
    //setTimeout("Effect.Appear('div_arrow_up', { duration: 1.0 });", 1000);
    InstallHelper.displayHelper('ff3_instructions');

    InstallHelper.ff3_waitForBarUp();
  },


  ff3_waitForBarUp: function() {
    if (InstallHelper.windowHeight == window.innerHeight) {
      InstallHelper.ff3_allowBarUp();
    } else {
      setTimeout("InstallHelper.ff3_waitForBarUp()", 500);
    }
  },

  ff3_allowBarUp: function() {

    //Effect.Fade('div_arrow_up', { duration: 0.5 });
    $('clickAllow').style.color = '#666';
    $('clickInstallNow').style.color = '#666';
    $('restartFirefox').style.color = '#fff';

  },

  /*************
   FF2
   *************/
  ff2_begin: function() {

    //dump("FF2 Begin \n");

    InstallHelper.ff2_waitForBarDown(1);
    InstallHelper.ff_tryInstall();
    return false;
  },

  ff2_waitForBarDown: function(iteration) {
    if (iteration > 5) {
      // InstallHelper.ff2_allowBarDown(); // Redundant, no? well yes, its cause if you refresh with bar down, then the other check doesnt work.
    } else if (InstallHelper.windowHeight > window.innerHeight) {
      InstallHelper.ff2_allowBarDown();

    } else {
      setTimeout("InstallHelper.ff2_waitForBarDown(" + (iteration + 1) + ")", 500);
    }
  },

  ff2_allowBarDown: function() {

    $('ff2_clickEditOptions').style.color = '#fff';
    $('ff2_clickAllow').style.color = '#666';
    $('ff2_clickDownloadAgain').style.color = '#666';
    InstallHelper.displayHelper('ff2_instructions');
    InstallHelper.ff2_waitForBarUp();
  },


  ff2_waitForBarUp: function() {
    if (InstallHelper.windowHeight == window.innerHeight) {
      InstallHelper.ff2_allowBarUp();
    } else {
      setTimeout("InstallHelper.ff2_waitForBarUp()", 500);
    }
  },

  ff2_allowBarUp: function() {
    $('ff2_clickEditOptions').style.color = '#666';
    $('ff2_clickAllow').style.color = '#fff';
    setTimeout("InstallHelper.ff2_clickedAllow()", 4000);
  },

  ff2_clickedAllow: function() {
    // This cannot actually be detected, so we dont log a stat.
    // It is just estimated via time. It's only a nice color change anyway.
    $('ff2_clickAllow').style.color = '#666';
    $('ff2_clickDownloadAgain').style.color = '#fff';
  },

  ff2_clickedDownloadAgain: function() {


    InstallHelper.windowHeight = window.innerHeight;
    InstallHelper.ff2_begin();

  }
};
