/*global define,$, setTimeout */
define(['marionette', 'underscore', 'models'], function(m, _, models) {
    'use strict';
    var EOL = '\r\n',
        lineRegex = /^\d+$/,
        timestampRegex = (/(\d{2}:\d{2}:\d{2},\d{3})\s+-->\s+(\d{2}:\d{2}:\d{2},\d{3})/),
        srt, tsToMs = models.Subtitle.prototype.timestampToMillis, lib;
    srt = function(strings, collection) {
        var d = $.Deferred(),
            inner;
        inner = function() {
            var nolines, foundrealend, i, obj, inSrt, matches, line;
            if (collection && collection.models) {
                console.log('Got a collection with models in it');
            }
            nolines = strings.length;
            foundrealend = false;
            for (i = nolines - 1; i > -1; --i) {
                if (strings[i] === EOL && strings[i - 1] === EOL) {
                    foundrealend = true;
                    nolines = i - 1;
                    break;
                }
            }
            if (!foundrealend) {
                d.reject(new Error('Does not look like a valid file'));
                return;
            }

            obj = {};
            inSrt = false;
            for (i = 0; i < nolines; i++) {
                line = strings[i];
                line = line.length > 2 ? line.substring(0, line.length - 2) : line;
                console.log('line ' + i + ' @ ' + (line || ''));
                if ((!inSrt) && lineRegex.test(line)) {
                    inSrt = true;
                    console.log('found an new subtitle');
                    obj.lineno = line;
                    continue;
                }
                if (inSrt) {
                    if (line === '\r\n') {
                        collection.add({
                            'text': obj.text,
                            'start': obj.start,
                            'end': obj.end
                        });
                        obj = {};
                        inSrt = false;
                        continue;
                    }
                    matches = timestampRegex.exec(line);
                    if (matches) {
                        obj.start = tsToMs(matches[1]);
                        obj.end = tsToMs(matches[2]);
                        continue;
                    }
                    if (obj.text) {
                        obj.text.push(line);
                    } else {
                        obj.text = [line];
                    }
                }
            }
            d.resolve(nolines, collection);
        };
        setTimeout(inner, 0);
        return d.promise();
    };
    lib = {
        fromSrt: srt
    };
    return lib;
});
// vim: set shiftwidth=4 softtabstop=4 tabstop=4 expandtab:

@@ -323,22 +323,30 @@
 oMillis,
+%0A       
  lib;%0A
-
     srt 
@@ -1139,32 +1139,70 @@
          obj = %7B
+%0A                text: %5B%5D%0A            
 %7D;%0A            i
@@ -1682,41 +1682,8 @@
   %7D%0A
-                if (inSrt) %7B%0A    
     
@@ -1737,20 +1737,16 @@
         
-    
 collecti
@@ -1778,20 +1778,16 @@
         
- 
-   
 'text': 
@@ -1796,20 +1796,16 @@
 j.text,%0A
-    
         
@@ -1864,20 +1864,16 @@
         
-   
- 
 'end': o
@@ -1903,20 +1903,12 @@
     
-    %7D);%0A    
+%7D);%0A
     
@@ -1930,23 +1930,73 @@
  obj = %7B
-%7D;
 %0A    
+                    text: %5B%5D%0A                    %7D;%0A
         
@@ -2034,36 +2034,32 @@
                 
-  
-  
 continue;%0A      
@@ -2064,34 +2064,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2135,28 +2135,24 @@
             
-    
 if (matches)
@@ -2150,28 +2150,24 @@
 (matches) %7B%0A
-    
             
@@ -2222,28 +2222,24 @@
             
- 
-   
 obj.end = ts
@@ -2268,36 +2268,32 @@
                 
-    
 continue;%0A      
@@ -2294,36 +2294,32 @@
                 
-  
-  
 %7D%0A              
@@ -2324,184 +2324,28 @@
     
-    if (obj.text) %7B%0A                        obj.text.push(line);%0A                    %7D else %7B%0A                        obj.text = %5Bline%5D;%0A                    %7D%0A                %7D
+obj.text.push(line);
 %0A   

Address comments