var IonDownload = function() {
  return {
    formCompleted : function() {
      var download_href = $.cookie('ion_download_href');
      var download_target = $.cookie('ion_download_target');
      
      // Set a cookie so we know the form has been filled out
      $.cookie('ion_completed', true, {path: '/'});
      
      // Clear the cookies so they aren't used again
      $.cookie('ion_download_href', null, {path: '/'});
      $.cookie('ion_download_target', null, {path: '/'});
      
      // Only redirect if they clicked a download link
      if(download_href && download_target) {
        if(download_target == "_blank") {
          window.open(download_href);
        }
        else {
          window.location = download_href;
        }
        // Add copy and link directing to download, if popup download didn't work
        $('#thank_you_copy').append(
          "Your file is now downloading.  In case of any problems, you can find it again <a onclick=\"window.open('" + download_href + "', '_top', '', false)\" href='#' id='redownload' style='color: #F5AE3F'>here</a>."
        );
        
      }

    },
    clearCompletedCookie : function() {
      $.cookie('ion_completed', null, {path: '/'});
    }
  };
}();


$(window).load(function(){  
  $('.ion_download').click(function(){	
    if(!$.cookie('ion_completed')) {
      $.cookie('ion_download_href', $(this).attr('href'), {path: '/'});
      $.cookie('ion_download_target', $(this).attr('target'), {path: '/'});
		  $.cookie('ion_download_filename', $(this).attr('file_name'), {path: '/'});
      
      $('#leadgen').jqmShow();
      return false;
    }
  });

  // 
});
