(function(){
   var downloadInfo, authorized, subscription;

   DUMAS.handleDownloadEvents = handleDownloadEvents;

   function handleDownloadEvents() {
      $('a.downloadBook, #downloadBook').click(function(e){
         //downloading book from book page or bookTrack from profile page
         downloadInfo = {
            target : $(this),
            href : $(this).attr('href'),
            mediaType : 'book',
            title : $('#bookInfo div.info h1').text() != '' ? $('#bookInfo div.info h1').text() : $('#bookTitle').text(),
            e : e
         };
         return DUMAS.dlmInstallCheck(downloadInfo);
      });
      
   $('a.downloadTrack').each(function(i){
         $(this).click(function(e){
            var jq = $(this);

            songName = $('p.songName')[i]; //is this stupid, couldn't it return the wrong name at some point?
            songName = $(songName);
            downloadInfo = {
               target : $(this),
               href : $(this).attr('href'),
               mediaType : 'song',
               title : songName.text(),//need to change this
               i : i,
               e : e
            };
         return DUMAS.dlmInstallCheck(downloadInfo);
         });

      });


   }


   $(function() {
      init(DUMAS.download);
   });

   function init(download) {
      if (download) {
         subscription = download.hasSubscription;
         authorized = download.authorized;
         if (authorized && subscription) {
            handleDownloadEvents();
         }
      }

   }
})();