

/**
 * Helper function which returns the higher value of the document height.
 * @param {Object} doc 
 * @return {Number} height
 */
function getDocHeight(doc) {
    doc = doc || document;
    // stackoverflow.com/questions/1145850/
    var body = doc.body, html = doc.documentElement;
    var height = Math.max( body.scrollHeight, body.offsetHeight, 
        html.clientHeight, html.scrollHeight, html.offsetHeight );
    return height;
}

/**
 * Defines the height of the iframe in the onload event.
 * @param {String} subject
 * @return None
 */
function load_iframe(subject){
	var iframe = window.parent.document.getElementById("b2note_iframe");
	var doc = iframe.contentDocument? iframe.contentDocument: 
        iframe.contentWindow.document;
	iframe.style.visibility = 'hidden';
	iframe.style.height = "10px"; // reset to minimal height ...
	// IE opt. for bing/msn needs a bit added or scrollbar appears
	iframe.style.height = getDocHeight( doc ) + 4 + "px";
	iframe.style.visibility = 'visible';
	iframe.subject_tofeed=subject;
}

/**
 * Shows the iframe.
 * @return None
 */
function show_iframe() {
	window.parent.document.getElementById('b2note_iframe').style.visibility = "visible";
	window.parent.document.getElementById('b2note_iframe').style.width = "350px";
	window.parent.document.getElementById('b2note_iframe').style.border = "2px solid gray";
}

// http://stackoverflow.com/questions/6754935/how-to-close-an-iframe-within-iframe-itself
/**
 * Hides the iframe.
 * @return None
 */
function hide_iframe() {
	window.parent.document.getElementById('b2note_iframe').style.width = "0px";
	window.parent.document.getElementById('b2note_iframe').style.border = "0px";
	window.parent.document.getElementById('b2note_iframe').style.visibility = "hidden";
}

/**
 * Get the free text when creating a annotation
 * 
 */
function get_free_text() {
    var text = document.getElementById("free-text").value;
    var elem = document.getElementById("section_subject");
    var subject = "";
    if (elem) {
        subject = elem.getElementsByTagName("a")[0].innerHTML;
    }
    // abremaud@esciencefactory.com, 20160129
    // gets the pid selected to provide the pid_tofeed field
    var pid = "test";
    if (elem) {
        pid = elem.getElementsByTagName("a")[1].innerHTML;
    }

    if (document.getElementById("section_subject")) {
        $.redirect('create_annotation',
            {
               free_text: text,
               subject_tofeed: subject,
               pid_tofeed: pid,
               // abremaud@esciencefactory.com, 20160926
               // retrieve Django csrf token from html hidden input element
               csrfmiddlewaretoken: document.getElementById('free-text').parentElement.firstChild.nextSibling.value,
            });
    }
}
/*
* Give a different css class to the button corresponding to the file we're currently annotating.
*
*/
$(document).ready( function() {
    document.getElementById('button1').classList.add('activated');
    last_id = 'button1'
    var reply_click = function(){
        if (last_id){
            document.getElementById(last_id).classList.remove('activated');
        }
        document.getElementById(this.id).classList.add('activated');
        last_id = this.id
    }
    n = 1
    id = 'button'+ n
    while (document.getElementById(id)){
        document.getElementById(id).onclick = reply_click;
        n = n + 1;
        id = 'button' + n;
    }
});
@@ -2907,24 +2907,73 @@
 unction() %7B%0A
+    if(document.getElementById('button1')) %7B%0A    
     document
@@ -3027,24 +3027,28 @@
 ated');%0A    
+  
+  
 last_id = 'b
@@ -3055,16 +3055,20 @@
 utton1'%0A
+    
     var 
@@ -3097,24 +3097,28 @@
 ()%7B%0A        
+ 
+   
 if (last_id)
@@ -3119,16 +3119,20 @@
 st_id)%7B%0A
+    
         
@@ -3203,26 +3203,34 @@
 ');%0A        
-%7D%0A
+    %7D%0A    
         docu
@@ -3286,32 +3286,36 @@
 ated');%0A        
+    
 last_id = this.i
@@ -3316,26 +3316,34 @@
 this.id%0A    
+   
+ 
 %7D%0A
+    
     n = 1%0A  
@@ -3340,24 +3340,28 @@
   n = 1%0A    
+  
+  
 id = 'button
@@ -3365,16 +3365,20 @@
 ton'+ n%0A
+    
     whil
@@ -3410,32 +3410,36 @@
 d(id))%7B%0A        
+ 
+   
 document.getElem
@@ -3473,16 +3473,20 @@
 _click;%0A
+    
         
@@ -3496,32 +3496,36 @@
  n + 1;%0A        
+    
 id = 'button' + 
@@ -3527,16 +3527,26 @@
 n' + n;%0A
+        %7D%0A
     %7D%0A%7D)

fix the button-coloring function being used at the wrong time