function setExcerpt(excerptid,workid) {
    roland.excerpt.Processor.setParameter(null,"current-excerpt",excerptid);
    roland.excerpt.Processor.setParameter(null,"current-work",workid);
    roland.excerpt.Draw();
    roland.excerpt_parts.Draw();
    //t = document.getElementById(excerptid + ".context_switch");
    //toggleContext(t,excerptid + ".context");
}

function narrowQuery()
{
    query_name = this.firstChild.nodeValue;
    query_type = this.getAttribute('class');
    roland.queryresults.Processor.setParameter(null, "query-name",query_name);
    roland.queryresults.Processor.setParameter(null, "query-type",query_type);
    roland.queryresults.Draw();
}

function hilightSame(e)
{
    ss = document.styleSheets[1]
    rules = ss.cssRules
    h = "."+this.firstChild.nodeValue+" { background-color: yellow; color: black; } ";
    if (e.type == 'mouseover') {
        if (rules.length > 0) {
            ss.deleteRule(rules.length - 1);
        }
        ss.insertRule(h,rules.length);
        
    }
}

function createElementWithText(etype,etext)
{
    el = document.createElement(etype);
    et = document.createTextNode(etext);
    el.appendChild(et);
    return el;
}

function showExcerpt() {
    ex = document.getElementById('excerpt');
    //ex.style.height='53%';
    ex.style.display='block';
    qr = document.getElementById('queryresults'); 
    //qr.style.height='33%';
    qr.style.display='block';
    //res = document.getElementById('results');
    //res.height='33%';
    //res.style.display='block';
}

function zoomImage(image_src) {
    grayout = document.getElementById('grayout');
    zoomdiv = document.getElementById('zoomdiv');
    zoomedimage = document.getElementById('zoomedimage');
    zoomedimage.src = image_src;
    grayout.style.display = "block";
    zoomdiv.style.display = "table";
}

function hideImage() {
    grayout = document.getElementById('grayout');
    zoomdiv = document.getElementById('zoomdiv');
    grayout.style.display = "none";
    zoomdiv.style.display = "none";
}

function toggleTextNotes(textnotesid) {
    		query = "//div[@class='textnotes']"; 
    			// gets all of the divs with class textnotes
    		textnotes = document.evaluate(query, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
    			// we're getting unordered textnotes as a snapshot, which is part of the XPath standard
    		for (i=0;i<textnotes.snapshotLength;i++) {
    			textnotes = textnotes.snapshotItem(i)
    			if (textnotes.id == textnotesid) { // as long as we're looping, might as well make the note we need visible and the rest hidden
    				textnotes.style.display = 'block'; 
    			} else {
    				textnotes.style.display = 'none';
    			}
     		}
}


function toggleNote(noteid) {
    		query = "//div[@class='note']"; 
    			// gets all of the divs with class note
    		notes = document.evaluate(query, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
    			// we're getting unordered notes as a snapshot, which is part of the XPath standard
    		for (i=0;i<notes.snapshotLength;i++) {
    			note = notes.snapshotItem(i)
    			if (note.id == noteid) { // as long as we're looping, might as well make the note we need visible and the rest hidden
    				note.style.display = 'block'; 
    			} else {
    				note.style.display = 'none';
    			}
     		}
}

function toggleContext(node, contextid) {
        c = document.getElementById(contextid);
        if(c.style.display=='block') {
            showtext = document.createTextNode("(more...)");
            c.style.display = 'none';
            node.replaceChild(showtext, node.firstChild);
        } else {
            c.style.display = 'block';
            hidetext = document.createTextNode("(...less)");
            node.replaceChild(hidetext, node.firstChild);
        }
}

// following are two functions that allow a film still to be replaced by the QuickTime movie

function hide(which){
    which.style.display='none';
}

function show(which){
    which.style.display='block';
}

function loadXMLDocument (url) {
   var httpRequest = new XMLHttpRequest();
   httpRequest.open('GET', url, false);
   httpRequest.send(null);
   return httpRequest.responseXML;
}

function createXSLProcessor(url) {
    xslt = loadXMLDocument(url);
    xsltP = new XSLTProcessor();
    xsltP.importStylesheet(xslt);
    return xsltP;
}

function XSLTDiv(div,url) {
    this.Processor = createXSLProcessor(url);
    this.div = div;
    this.Draw = doTransform;
}

    function doTransform()
    {
        var fragment = this.Processor.transformToFragment(roland.corpus, document);
        this.div.innerHTML = "";
        this.div.appendChild(fragment);
    }

function JSDiv(div) {
    this.div = div
    this.Draw = partsList;
}

    function partsList() {
        excerpt_parts = this.div;
        current_excerpt = roland.excerpt.Processor.getParameter(null,"current-excerpt");
        var typearray = new Array('character','imagery','theme');
        var displaytype = new Array('Characters:','Imagery:','Themes:');
        var resultlist = document.createDocumentFragment();
        // i is an index variable
        for (i=0;i<typearray.length;i++)
        {
            var mydict = new Object();
            mytype = typearray[i];
            
            query = "/works/work/excerpt[@id='"+current_excerpt+"']//"+mytype;
            nodes = roland.corpus.evaluate(query, roland.corpus, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
            while (node = nodes.iterateNext())
            {
            		if (node == null) { continue }
                mydict[node.getAttribute('name')] = node;
            }
            part_list = document.createElement('ul');
            mylibolded = part_list.appendChild( createElementWithText('li', displaytype[i]) );
            mylibolded.setAttribute('style','font-weight:bold; text-indent:-1.5em; padding-top:1em');
            
            count = 0;
            for (node in mydict)
            {
            	  count++;
            	  myli = document.createElement('li');
                mylink = createElementWithText("a",node);
                mylink.setAttribute('class',mytype);
                mylink.addEventListener("click", narrowQuery, false);
                mylink.addEventListener("mouseover",hilightSame,false);
                mylink.addEventListener("mouseout",hilightSame,false);
                myli.appendChild(mylink);
                part_list.appendChild(myli);
            }
            if (count > 0) {
            	resultlist.appendChild(part_list);
            }
        }
        excerpt_parts.innerHTML="";
        excerpt_parts.appendChild(resultlist);
    }

//This function allows the bookmarking of an excerpt.
function getWorkAndExcerpt() {
	var workid = '';
	var excerptid = '';
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == 'workid') {
      workid = pair[1];
    }
    if (pair[0] == 'excerptid') {
    	excerptid = pair[1];
    }
  }
  return [excerptid, workid];
}


function Roland() {
    this.start = startRoland;
}
function startRoland() {
    this.corpus = loadXMLDocument('rolandht.xml');
    this.excerpt = new XSLTDiv(document.getElementById('excerpt'),'excerpt.xsl');
    this.queryresults = new XSLTDiv(document.getElementById('queryresults'),'query.xsl');
    this.excerpt_parts = new JSDiv(document.getElementById('excerpt-parts'));
}

var roland = new Roland();

function initialize() {
    roland.start();
    roland.queryresults.Draw();
    urlquery = getWorkAndExcerpt();
    if (urlquery[0] && urlquery[1] != '') {
    	setExcerpt(urlquery[0], urlquery[1]);
    	showExcerpt();
    };
}


window.onload = initialize;


