// https://developer.mozilla.org/en-US/docs/Web/API/window.getSelection

function dogeared_document_init(){

    $(document).mouseup(dogeared_document_onselected);
    $(document).touchend(dogeared_document_onselected);

}

function dogeared_document_onselected(e){

    var target = e.target;

    if (target.nodeName == 'BUTTON'){
	dogeared_document_highlight();
	return;
    }

    var sel = window.getSelection();
    
    if (sel.toString() == ""){
	return;
    }

    // http://stackoverflow.com/questions/3597116/insert-html-after-a-selection

    $(".highlight").remove();

    range = window.getSelection().getRangeAt(0);
    expandedSelRange = range.cloneRange();
    range.collapse(false);

    var el = document.createElement("div");
    el.innerHTML = "<span class=\"highlight\"> <button id=\"highlight\" class=\"btn btn-sm\">Highlight</button> </span>";

    var frag = document.createDocumentFragment(), node, lastNode;

    while ((node = el.firstChild)){
        lastNode = frag.appendChild(node);
    }
    
    range.insertNode(frag);

    if (lastNode){
        expandedSelRange.setEndAfter(lastNode.previousSibling);
        sel.removeAllRanges();
        sel.addRange(expandedSelRange);
    }

}

function dogeared_document_highlight(){

    var doc = $("#document");
    var id = doc.attr("data-document-id");

    var s = window.getSelection();
    var t = s.toString();

    var method = 'dogeared.highlights.addHighlight';

    var args = {
	'document_id': id,
	'text': t
    };

    // TO DO: something...

    var on_success = function(rsp){
	console.log(rsp);
    };

    dogeared_api_call(method, args, on_success);
}

@@ -66,16 +66,31 @@
 ection%0A%0A
+var wtf = %22%22;%0A%0A
 function
@@ -137,16 +137,24 @@
 nt).
+bind('
 mouseup
-(
+', 
 doge
@@ -184,33 +184,214 @@
 d);%0A
-    $(document).touchend(
+%0A    // This does not work well at all yet (20140503/straup)%0A    // http://stackoverflow.com/questions/8991511/how-to-bind-a-handler-to-a-selection-change-on-window%0A%0A    $(document).bind('selectionchange', 
 doge
@@ -417,17 +417,16 @@
 ected);%0A
-%0A
 %7D%0A%0Afunct
@@ -459,24 +459,49 @@
 lected(e)%7B%0A%0A
+    console.log(e.type);%0A
     var targ
@@ -565,32 +565,36 @@
 geared_document_
+add_
 highlight();%0A%09re
@@ -602,24 +602,55 @@
 urn;%0A    %7D%0A%0A
+    $(%22.highlight%22).remove();%0A%0A
     var sel 
@@ -678,25 +678,26 @@
 ();%0A    
-%0A    if (
+var txt = 
 sel.toSt
@@ -702,16 +702,30 @@
 String()
+;%0A%0A    if (txt
  == %22%22)%7B
@@ -737,24 +737,140 @@
 urn;%0A    %7D%0A%0A
+    if (txt == wtf)%7B%0A%09return;%0A    %7D%0A%0A    wtf = txt;%0A%0A    console.log('draw button');%0A    $(%22.highlight%22).remove();%0A%0A
     // http:
@@ -942,39 +942,8 @@
 on%0A%0A
-    $(%22.highlight%22).remove();%0A%0A
     
@@ -1407,24 +1407,88 @@
 ode(frag);%0A%0A
+    // this causes the highlight button to be hidden in iOS...%0A%0A
     if (last
@@ -1636,16 +1636,17 @@
 %0A    %7D%0A%0A
+%0A
 %7D%0A%0Afunct
@@ -1667,16 +1667,20 @@
 ocument_
+add_
 highligh
@@ -1817,24 +1817,68 @@
 oString();%0A%0A
+    // This requires grabbing 'wtf' in ios%0A%0A
     var meth
@@ -1971,24 +1971,48 @@
 : t%0A    %7D;%0A%0A
+    console.log(args);%0A%0A
     // TO DO

make something that sort of works in ios