//#############################################
// Source: ViewKimiPage.js
// Size: 3748
// Hash: 1304252507
//#############################################
var ViewKimiPageWidth = 841;  // this will auto-resize the viewed kimi-page to this width

window.addEvent('domready', setupKimiPage);

function setupKimiPage() {
  // find any text modules on the page and add their borders
  $$('.TextModule').each(function(tmElement) {
    var borderClass = tmElement.getAttribute('borderclass');

    try
    {
      var borderDecorator = new AbstractBorderDecorator[borderClass]();

      var html = tmElement.innerHTML;
      var container = borderDecorator.injectDecoratedContainer(tmElement);
      container.innerHTML = html;
    }
    catch(e)
    {
      throw 'Specified borderClass ' + borderClass + ' not found (perhaps you need to include the appropriate AbstractBorderDecorator subclass .js file on this page).';
    }
  });
  
    if (window.webkit) 
    {
      // in WebKit, textModules somehow affect the calculated height of the KimiPage.  Fix this by clearing floats (note, this breaks IE, which is why we're not doing it in IE).
      $$('#viewKimiPageContainer').each(function(viewContainer) {
        var clearDiv = $(document.createElement('div'));
        clearDiv.setStyle('clear', 'both');
        viewContainer.appendChild(clearDiv);
      });
    }

  if (typeof ViewKimiPageWidth != 'undefined')
  {
    $$('.KimiPage').each(function(kimiPage) {
      var currentWidth = kimiPage.getSize().size.x;
      kimiPage.setStyle('width', ViewKimiPageWidth);

      var difference = ViewKimiPageWidth - currentWidth;
      if (difference != 0)
      {
        // distribute the difference evenly amongst the columns in each column-set
        kimiPage.getElements('.ColumnSet').each(function(cs) {
          var columns = cs.getElements('.Column');
          var differenceEach = difference / columns.length;

          columns.each(function(col) {
            var w = col.getStyle('width').toInt();
            col.setStyle('width', w + differenceEach);
          });

        });
      }
    });
  }

  $$('.KimiPageBackground').each(function(bgKimi) {
    bgKimi.setStyle('opacity', bgKimi.getAttribute('opacity'));

    var backgroundStyle = bgKimi.getAttribute('background');  // we pass the background-image style in this attribute so it isn't sanitized out by the ruby sanitizer
    backgroundStyle = backgroundStyle.replace(/javascript|vbscript/g, '');  // but we further need to sanitize this before setting it as a style property
    bgKimi.setStyle('background-image', backgroundStyle); // this property gets sanitized out if we put it in the css
    
    var repeatStyle = bgKimi.getAttribute('repeat');  // we pass the background-repeat style in this attribute so it isn't sanitized out by the ruby sanitizer
    repeatStyle = repeatStyle.replace(/javascript|vbscript/g, '');  // but we further need to sanitize this before setting it as a style property
    bgKimi.setStyle('background-repeat', repeatStyle); // this property gets sanitized out if we put it in the css
    
    window.fireEvent('resize'); // positions the bgkimi
  });

  // show the container (helps reduce flicker)
  $$('#viewKimiPageContainer').each(function(viewContainer) {
    viewContainer.setStyle('visibility', 'visible');
  });

  // attempt to fix a troublesome IE6 bug where images appear at 16x16 pixels
  if (window.ie6) setTimeout(function() {
    
    $$('.KimiImage').each(function(kimiImage) {
      kimiImage.setStyles({ width: 'auto', height: 'auto' });
    });
    
  }, 1000);
}

function viewKimiPage_handleResize()
{
  $$('#viewKimiPageContainer').each(function(viewContainer) {
    var bgKimi = viewContainer.getElement(".KimiPageBackground");
    if(bgKimi) {
      bgKimi.setStyles(viewContainer.getCoordinates());
    }
  });
}

window.addEvent('resize', viewKimiPage_handleResize);

