var consoleTest = /(^.|\b)console\./;

var useCustomConsole = !(function () {
  var ok = typeof window.console !== 'undefined';
  try {
    window.console.log('jsbin init test');
  } catch (e) {
    ok = false;
  }
  return ok;
})();

function getPreparedCode() {
  var parts = [],
      source = '',
      js = '';
  
  try {
    source = editors.html.getCode();
  } catch (e) {}
  
  try {
    js = editors.javascript.getCode();
  } catch (e) {}
  

  // redirect JS console logged to our custom log while debugging
  if (consoleTest.test(js)) {
    if (useCustomConsole) {
      js = js.replace(/(^.|\b)console\./g, '_console.');
    } else {
      js = js.replace(/(^.|\b)console\./g, 'window.top.console.');
    }
  }
  
  // escape any script tags in the JS code, because that'll break the mushing together
  js = js.replace(/<\/script/ig, '<\\/script');

  // note that I'm using split and reconcat instead of replace, because if the js var
  // contains '$$' it's replaced to '$' - thus breaking Prototype code. This method
  // gets around the problem.
  if (!$.trim(source)) {
    source = "<pre>\n" + js + "</pre>";
  } else if (/%code%/.test(source)) {
    parts = source.split('%code%');
    source = parts[0] + js + parts[1];
  } else if (js) {
    parts.push(source.substring(0, source.lastIndexOf('</body>')))
    parts.push(source.substring(source.lastIndexOf('</body>')));

    source = parts[0];
    
    var close = parts.length == 2 && parts[1] ? parts[1] : '';
    
    if (useCustomConsole) {
      source += "<script src=\"http://jsbin.com/js/render/console.js\"></script>\n<script>\n";
    }
    // source += "<script>\ntry {\n" + js + "\n} catch (e) {" + (window.console === undefined ? '_' : 'window.top.') + "console.error(e)}\n</script>\n" + close;
    // source += "<script>\n" + js + "\n</script>\n" + close;
	source += "<style type=\"text/css\">\n" + js + "\n</style>\n" + close;
  }

  // specific change for rendering $(document).ready() because iframes doesn't trigger ready (TODO - really test in IE, may have been fixed...)
  if (/\$\(document\)\.ready/.test(source)) {
    source = source.replace(/\$\(document\)\.ready/, 'window.onload = ');
  } 
  
  // read the element out of the source code and plug it in to our document.title
  var newDocTitle = source.match(/<title>(.*)<\/title>/i);
  if (newDocTitle !== null && newDocTitle[1] !== documentTitle) {
    documentTitle = newDocTitle[1];
    updateTitle(!/ \[unsaved\]/.test(document.title));
  }

  return source;
}

function renderPreview() {
  var doc = $('#preview iframe')[0], 
      win = doc.contentDocument || doc.contentWindow.document,
      source = getPreparedCode();
  
  win.open();
  
  if (debug) {
    win.write('<pre>' + source.replace(/[<>&]/g, function (m) {
      if (m == '<') return '&lt;';
      if (m == '>') return '&gt;';
      if (m == '"') return '&quot;';
    }) + '</pre>');
  } else {
    win.write(source);
  }
  win.close();
}

@@ -309,19 +309,17 @@
 s = '';%0A
-  
 %0A
+
   try %7B%0A
@@ -372,19 +372,17 @@
  (e) %7B%7D%0A
-  
 %0A
+
   try %7B%0A
@@ -437,19 +437,17 @@
  (e) %7B%7D%0A
-  
 %0A
+
 %0A  // re
@@ -710,19 +710,17 @@
   %7D%0A  %7D%0A
-  
 %0A
+
   // esc
@@ -1297,28 +1297,28 @@
 tIndexOf('%3C/
-body
+head
 %3E')))%0A    pa
@@ -1369,12 +1369,12 @@
 ('%3C/
-body
+head
 %3E'))
@@ -1400,21 +1400,17 @@
 rts%5B0%5D;%0A
-    
 %0A
+
     var 
@@ -1468,13 +1468,9 @@
 '';%0A
-    
 %0A
+
     
@@ -2161,20 +2161,17 @@
  ');%0A  %7D
- %0A  
+%0A
 %0A  // re
@@ -2549,17 +2549,16 @@
 me')%5B0%5D,
- 
 %0A      w
@@ -2647,19 +2647,17 @@
 Code();%0A
-  
 %0A
+
   win.op
@@ -2662,18 +2662,16 @@
 open();%0A
-  
 %0A  if (d

move script element from body to head