/**
 * Identifies the different versions of mootools Pogo.com supports
 */
var MootoolsVersion = {
  'ONE_ELEVEN' : 1,
  'ONE_TWO' : 2,
  'NONE' : 0
};

/**
 * Set of utility methods that allow seamless integration with all coexisting
 * mootools versions within Pogo.
 */
var MootoolsUtils = {
  getMootoolsVersion: function() {
    if (typeof Ajax != 'undefined') {
      return MootoolsVersion.ONE_ELEVEN;
    } else if (typeof Request != 'undefined') {
      return MootoolsVersion.ONE_TWO;
    } else {
      return MootoolsVersion.NONE; 
    }
  },

  /**
   * Performs an ajax invocation with the given url and options.
   */
  request: function(url, options) {
    if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_ELEVEN) {
      // use Ajax to make the call
      var request = new Ajax(url, options).request();

    } else if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_TWO) {
      // use Request to make the call
      options.url = url;
      var request = new Request(options).send();

    } else {
      // this should only happen on a devel environment
      alert('No known mootools version available');
    }
  },

  /**
   * Injects the element within the parent at the bottom.
   */
  inject: function(element, parent) {
    if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_ELEVEN) {
      element.injectInside(parent);
    } else if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_TWO) {
      element.inject(parent);
    } else {
      // this should only happen on a devel environment
      alert('No known mootools version available');
    }
  },

  /**
   * Removes the element from within the page.
   */
  dispose: function(element) {
    if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_ELEVEN) {
      element.remove();
    } else if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_TWO) {
      element.dispose();
    } else {
      // this should only happen on a devel environment
      alert('No known mootools version available');
    }
  }
};

DomUtils = {
  getParentById: function(element, id) {
    while (element && id != element.id) {
      element = element.parentNode;
    }
    return element;
  }
};

/*--based off of shareapage.js--*/

if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_ELEVEN) {
  /**
   * Popover class implementation Mootools 1.11 compliant
   */
  var Popover = new Class({

    /**
     * Creates the popover with the given inner HTML and displays it.
     */
    initialize: function(innerHTML) {
      var backgroundDrop = new Element('div', {'id' : 'fbconnect-backgroundDrop'});
        backgroundDrop.setHTML('&nbsp;');
        backgroundDrop.injectInside(document.body);

        var newDiv = new Element('div', {
            'id' : 'fbconnect-popover',
            'styles' : {'visibility': 'hidden'}
        });
        newDiv.injectAfter('fbconnect-backgroundDrop');
        newDiv.setHTML(innerHTML);
        this._openModal();
    },

    _openModal: function() {
      var fbconnectBackgroundDrop = $('fbconnect-backgroundDrop'); //activates overlay
      fbconnectBackgroundDrop.injectInside(document.body); //inserts into body
      fbconnectBackgroundDrop.setStyles({ //overlay css attribute values
        opacity: '.1',
        visibility: 'hidden',
        left: 0,
        top: 0,
        width: '100%',
        height: '100%'    });
      var fbconnectBackgroundDropFX = fbconnectBackgroundDrop.effects({duration: 300, transition: Fx.Transitions.Quint}); //fx
      fbconnectBackgroundDropFX.start({
        'opacity': .5
      }).chain(function() { // executes immediately after completion of above effect
        var fbconnectWrapper = $('fbconnect-popover');
        fbconnectWrapper.setStyle('marginTop', -(fbconnectWrapper.getSize().size.y / 2) + 'px');
        fbconnectWrapper.setStyle('visibility','visible');
        fbconnectWrapper.injectAfter('fbconnect-backgroundDrop');
        //disable scrollbars for IE6
        if(window.ie6){
          window.scrollTo(0,0);
          document.body.parentNode.style.overflow = 'hidden'
        }
      });
    },

    closeModal: function() { //close Modal Dialog Box
      $('fbconnect-popover').remove();
      $('fbconnect-backgroundDrop').remove();
      document.body.parentNode.style.overflow = '';
      // refresh the wildfire state back to its original screen
    }
  });

  Popover.close = function(element) {
    var popup =  DomUtils.getParentById(element, 'fbconnect-popover');
    if (popup) {
      $(popup).remove();
      $('fbconnect-backgroundDrop').remove();
    }
  };

} else if (MootoolsUtils.getMootoolsVersion() === MootoolsVersion.ONE_TWO) {

  var Popover = new Class({

    initialize: function(innerHTML) {
      var backgroundDrop = new Element('div', {'id' : 'fbconnect-backgroundDrop'});
        backgroundDrop.set('html', '&nbsp;');
        backgroundDrop.injectInside(document.body);

        var newDiv = new Element('div', {
            'id' : 'fbconnect-popover',
            'styles' : {'visibility': 'hidden'}
        });
        newDiv.injectAfter('fbconnect-backgroundDrop');
        newDiv.set('html', innerHTML);
        this._openModal();
    },

    _openModal: function() {
      var fbconnectBackgroundDrop = $('fbconnect-backgroundDrop'); //activates overlay

      fbconnectBackgroundDrop.setStyles({ //overlay css attribute values
        opacity: '.1',
        visibility: 'visible',
        left: 0,
        top: 0,
        width: '100%',
        height: '100%'
      });

      var myFx = new Fx.Tween(fbconnectBackgroundDrop, {duration : '300'});
      myFx.start('opacity', '.5').chain(function(){
        var fbconnectWrapper = $('fbconnect-popover');
        fbconnectWrapper.setStyle('marginTop', -(fbconnectWrapper.getSize().y / 2) + 'px');
        fbconnectWrapper.setStyle('visibility','visible');
        fbconnectWrapper.injectAfter('fbconnect-backgroundDrop');
        //disable scrollbars for IE6
        if(window.ie6){
          window.scrollTo(0,0);
          document.body.parentNode.style.overflow = 'hidden'
        }
      });
    },

    closeModal: function() { //close Modal Dialog Box
      $('fbconnect-popover').dispose();
      $('fbconnect-backgroundDrop').dispose();
      document.body.parentNode.style.overflow = ''
      // refresh the wildfire state back to its original screen
    }
  });

  Popover.close = function(element) {
    var popup =  DomUtils.getParentById(element, 'fbconnect-popover');
    if (popup) {
      $(popup).dispose();
      $('fbconnect-backgroundDrop').dispose();
    }
  };


}
