Event.observe(document, 'dom:loaded', function()
{
  var modal_content = $('modal-window').remove().show();

  $$('.nav-requestCall', '.request-call-button').invoke('observe', 'click', function(event) {
    event.stop();
    blackbox.write(modal_content);

    if(!$('request-pixel'))
    {
      var img = new Element('img', { src: 'http://pixel.everesttech.net/2984/t?ev_RequestACall=1', 'width': 1, 'height': 1, 'id': 'request-pixel'});
      $('trigger-modal-submit').insert({before: img});
    }      
  });

  attachModalBehaviours(modal_content);
});

function attachModalBehaviours(modal_content)
{
  var form = modal_content.down('form');

  form.down('.trigger-close').observe('click', blackbox.hide.bindAsEventListener(blackbox));

  modal_content.observe('click', function(event) {
    if(event.target == $('trigger-modal-submit'))
    {  
      event.stop();

      new Ajax.Request(form.action, {
        parameters: form.serialize(),
        evalScripts: true,
        onCreate: function() {
          form.down('img.ajax-loader').show();
        },
        onSuccess: function(transport) {
          modal_content.update(transport.responseText);
          blackbox.positionContainer(null);

          attachModalBehaviours(modal_content);
        },
        on202: function(transport) {
          modal_content.update(transport.responseText);
          modal_content.down('.trigger-close').observe('click', blackbox.hide.bindAsEventListener(blackbox));

          _gaq.push(['_trackEvent', 'Request a Call', 'Submit Call Request', 'Submit Call Request']);
        }
      });
    }
  });
}

