
/*  =Page
  ----------------------------------------------- */
  var Page = {
    initialize: function() {
      
      // Add a text manager to the page
      $$(".section-body", "#home-body").each(function(nextElement) {
        new TextManager(nextElement);
      }.bind(this));
      
      // Change the main image into a Flash movie
      if (typeof(swfobject.embedSWF) == "function") {
        swfobject.embedSWF("assets/movies/home.swf", "home-image", "375", "225", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("assets/movies/about.swf", "about-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("../assets/movies/private-accounts.swf", "private-accounts-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("../assets/movies/retirement-accounts.swf", "retirement-accounts-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("assets/movies/contact.swf", "contact-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("assets/movies/faqs.swf", "faqs-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("assets/movies/glossary.swf", "glossary-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("assets/movies/disclosures.swf", "disclosures-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("assets/movies/privacy.swf", "privacy-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
        swfobject.embedSWF("assets/movies/client-login.swf", "client-login-image", "584", "150", "9.0.0", null, null, {
          wmode: "transparent",
          quality: "high"
        });
      }
      
      // Simulate CSS dynamic content, since IE6/7 don't support it.
      if (Prototype.Browser.IE) {
        var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
        if (version < 8) {
          var before = function(selector, content) {
            $$(selector).each(function(nextElement) {
              var contentElement = new Element("b").update(content);
              nextElement.insert({top: contentElement});
            });
          };
          var after = function(selector, content) {
            $$(selector).each(function(nextElement) {
              var contentElement = new Element("b").update(content);
              nextElement.insert({bottom: contentElement});
            });
          };
          before("#aux-nav li", "» ");
          before(".section-head li li a", "» ");
          before("#home .portfolios li", "» ");
          after("a.more", " <span style=\"padding-left: 0.25em\">»</span>");
        }
      }
    }
  };
  document.observe("dom:loaded", Page.initialize.bind(Page));

/*  =Cookie
  -----------------------------------------------
  KUDOS: http://www.red91.com/2008/02/04/cookies-persistence-javascript
  ----------------------------------------------- */
  var Cookie = {
    set: function(name, value, daysToExpire) {
      if (daysToExpire == null || daysToExpire == undefined) {
        daysToExpire = 365 * 2;
      }
      var expire = '';
      if (daysToExpire != undefined) {
        var d = new Date();
        d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
        expire = '; expires=' + d.toGMTString();
      }
      return (document.cookie = escape(name) + '=' + escape(value || '') + expire + '; path=/');
    },
    get: function(name) {
      var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
      return (cookie ? unescape(cookie[2]) : null);
    },
    erase: function(name) {
      var cookie = Cookie.get(name) || true;
      Cookie.set(name, '', -1);
      return cookie;
    },
    accept: function() {
      if (typeof navigator.cookieEnabled == 'boolean') {
        return navigator.cookieEnabled;
      }
      Cookie.set('_test', '1');
      return (Cookie.erase('_test') === '1');
    }
  };

/*  =TextManager
  ----------------------------------------------- */
  var TextManager = Class.create({
    SIZE: {
      SMALL   : "11px",
      MEDIUM  : "12px",
      LARGE   : "13px"
    },
    element: null,
    buttons: null,
    initialize: function(element) {
      if (!element) return;
      this.element = element;
      this.buttons = $A([]);
    
      this.render();
      var size = Cookie.get("textsize");
      if (size == null) size = this.SIZE.SMALL;
      this.resizeText(size);
    },
    render: function() {
      var container = new Element("div").addClassName("text-manager");
      var headline = new Element("h2").update("Text Size:");
      var buttonContainer = new Element("ul");
      container.appendChild(headline);
      container.appendChild(buttonContainer);
      this.element.appendChild(container);
      for (var prop in this.SIZE) {
        var button = new TextManager.Button(this.resizeText.bind(this), buttonContainer, this.SIZE[prop], prop.toLowerCase());
        this.buttons.push(button);
      }
    },
    resizeText: function(size) {
      if (size == null || size == "") size = this.SIZE.SMALL;
      $$(".section-body").each(function(sectionBody) {
        $$(".section-body-inner").invoke("setStyle", {
          "fontSize": size
        });
        $$(".section-head").invoke("setStyle", {
          "height": (sectionBody.offsetHeight + 15) + "px"
        });
      }.bind(this));
      Cookie.set("textsize", size);
      this.buttons.each(function(nextButton) {
        if (nextButton.size == size) {
          nextButton.activate();
        } else {
          nextButton.deactivate();
        }
      }.bind(this));
    }
  });
  TextManager.Button = Class.create({
    size    : null,
    element : null,
    callback : null,
    initialize: function(callback, container, size, label) {
      this.size = size;
      this.callback = callback;
    
      this.element = new Element("li").addClassName(label);
      container.appendChild(this.element);
      var link = new Element("a", {"href": "#" + label});
      link.update("A");
      this.element.appendChild(link);
      link.observe("click", this.onClick.bindAsEventListener(this));
    },
    activate: function() {
      this.element.addClassName("active");
    },
    deactivate: function() {
      this.element.removeClassName("active");
    },
    onClick: function(evt) {
      evt.preventDefault();
      this.callback(this.size);
    }
  });
