var verticalOffset = 372;
var horizontalOffset = 93;

var DownloadSurveyController = Class.create({

  init:function()
  {
    verticalOffset += $('content-popup').style.height;
    $$('.textarea a.require_popup_form').each(
      function(el){
        el.observe('click',function(event){

          $('content-popup').show();
          $('content-popup').style.left = ((el.positionedOffset()['left'])-horizontalOffset)+'px';
          $('content-popup').style.top = ((el.positionedOffset()['top'])-verticalOffset)+'px';
          
          $('dl-title').innerHTML = el.title;
					
					var link_href = el.href;
					var download_link = link_href.match('javascript') ? el.rel : el.href;

          basename = download_link.replace( /.*\//, "" );
          $('cp-form-basename').value = basename;
          $('dl-basename').innerHTML = basename;
          $('dl-href').href = download_link;
          event.stop();
        });
      }
    );

    $('map-close-button').observe('click',function(event){
      $('content-popup').hide();
      event.stop();
    });
    $('download-form-submit').observe('click', this.surveyOptionClick.bind(this));
  },
  setFormAsActive:function()
  {
    $('cp-download').hide();
    $('cp-form').show();
  },
  setDownloadAsActive:function()
  {
    $('cp-form').hide();
    $('cp-download').show();
  },
  surveyOptionClick:function(theEvent)
  {
    // kill the click event
    theEvent.stop();
    if(!Validate('download-form',false))
      return false;
    
    var form_string = $('download-form').serialize();
    // send off the survey answer
    new Ajax.Request('downloadsurvey?do=record', {
      method: 'post',
      parameters: form_string
    });
    
    // show download form
    this.setDownloadAsActive();
  }

});

document.observe('dom:loaded', function() {
  new DownloadSurveyController().init();
});


