/*


Dynamic Application of Functions to DOM
*/
var _debugMode = false;

document.observe('dom:loaded', init);
Event.observe(window, 'load', initAfterImages);

function init () {
  new GetStartedForm();
  new BlurredBackground();
  
  if ($('flash_container')){
      $('plain_text_area').setStyle('width: 350px');
  }
  
  // google analytics tracking the get started form submission
  document.observe('form:submitted', function(e){
    if (pageTracker){
      pageTracker._trackPageview('/getStartedForm:submitted');
    }
  });
  
  document.observe('getStartedForm:open', function(e){
    if (pageTracker){
      pageTracker._trackPageview('/getStartedForm:clicked');
    }
  });
  
}

function initAfterImages() {
  var firstHeader = $$('#primaryColumn > h1');
  if (!firstHeader[0]) return;
  if (firstHeader[0].innerHTML.length < 44 ) {
    firstHeader.invoke('insert', { after: '<div id="ieFix"></div>' });
    if (Prototype.Browser.IE) $('ieFix').pngHack();
  }
}



var BlurredBackground = Class.create({
  initialize: function(){
    this.root = $('feature_background');
    if (!this.root) return;
    this.insertBlurredImage();
    document.observe('getStartedForm:open', this.blurImage.bind(this));
    document.observe('getStartedForm:closed', this.unblurImage.bind(this));
  },
  insertBlurredImage: function(){
    this.root.insert({
      after: '<img src="' + String(this.root.src).gsub('.jpg', '_blurred.jpg') + '" class="feature_background" id="feature_background_blurred" style="display: none; opacity: 0;" />'
    });
    this.blurredImage = $('feature_background_blurred');
  },
  blurImage: function(){
    new Effect.Parallel([
      new Effect.Appear(this.blurredImage),
      new Effect.Fade(this.root)
    ]);
  },
  unblurImage: function(){
    new Effect.Parallel([
      new Effect.Fade(this.blurredImage),
      new Effect.Appear(this.root)
    ]);
  }
});






/*
from: http://mislav.caboo.se/js/when-available-in-prototype/
When object is available, do function fn.
*/
function when(obj, fn) {
  if (Object.isString(obj)) obj = /^[\w-]+$/.test(obj) ? $(obj) : $(document.body).down(obj)
  if (Object.isArray(obj) && !obj.length) return
  if (obj) fn(obj)
}


/* Utility Functions */
function cl(str){
  if(_debugMode) Try.these(
    function(){ console.log(str) },
    function(){ alert(str) }
  );

}

function t(f) {
  Try.these(f);
}



/*


Makes 32 bit PNG's transparency work in Internet Explorer 6
 * Dependent on Prototype 1.6
 * Works on img elements and on background images of elements
 * Image tiling will not work
 * Refer to the readme

Example Usages:
 
 $('yourPNG').pngHack();
 $$('div#fixMe', 'img#andMe', 'img.andUsTo').invoke('pngHack');
 
*/
Element.addMethods({
  pngHack: function(el){
    var el = $(el);
    var transparentGifPath = '/images/transparent.gif';
    if (Prototype.Browser.IE){
      /* if it's an img and a png */
      if ((el.match('img')) && (el.src.include('png'))){
        var alphaImgSrc = el.src;
        var sizingMethod = 'scale';
        el.src = transparentGifPath;
        /* if it's an element with a background png */
      } else if (el.getStyle('backgroundImage').include('png')){
        var bgColor = el.getStyle('backgroundColor') || '';
        var elBg = el.getStyle('backgroundImage');
        var alphaImgSrc = elBg.slice(5, elBg.length - 2);
        var sizingMethod = 'crop';
        el.setStyle({ 'background': [bgColor, ' url(', transparentGifPath, ')'].join('') });
      }
      if (alphaImgSrc) el.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{al}",sizingMethod="#{sz}")'.interpolate({ al: alphaImgSrc, sz: sizingMethod });
    }
    return el;
  }
});

