
/* First principles */

var isIE = (document.getElementById && document.all)?true:false;
var isNS4 = (document.layers)?true:false;
var isNS6 = (document.getElementById && !document.all)?true:false;

function myGetElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function myGetElementById (id)
{
    if ( isIE )
    {
	var str = "document.all('" + id + "')";
	var o = eval(str);
	return o;
    }

    return document.getElementById(id);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function searchInput(text,input) {  
	
  if (!document.getElementById(input)) return false;
  document.getElementById(input).onfocus = function() {
  	// Bugdb 0034: persist the value in here but revert to poem/poet 
    // if empty
    if (this.value == text) {
    	this.value = "";
    }
  }
  document.getElementById(input).onblur = function() {
    if(this.value == "") this.value = text;
    else this.select();
  }
}



/* Nav Stuff */

function findTabs() {

	// apply our mouse functions to the search tab/menu...
	/* myGetElementById('search_tab').onmouseover = sOver;
	myGetElementById('search_tab').onmouseout = sOut;
	myGetElementById('search_menu').onmouseover = sOver;
	myGetElementById('search_menu').onmouseout = sOut; */
	
	// and to the browse tab/menu...
	myGetElementById('browse_tab').onmouseover = bOver;
	myGetElementById('browse_tab').onmouseout = bOut;
	myGetElementById('browse_menu').onmouseover = bOver;
	myGetElementById('browse_menu').onmouseout = bOut;
	
	// position browse dropdown
	var b = myGetElementById('browse_tab');
	myGetElementById('browse_menu').style.top = findPos(b)[1] + b.offsetHeight + "px";
	myGetElementById('browse_menu').style.left = findPos(b)[0] - 5 + "px";
	
	// if we're on a search results page, do some height jiggering
	if ( myGetElementById('results_sidebar') ) {
		if ( myGetElementById('results_sidebar').offsetHeight > myGetElementById('results_content').offsetHeight ) {
			myGetElementById('results_content').style.height = myGetElementById('results_sidebar').offsetHeight + "px";
		}
	}
		
}

function sOver() {
	myGetElementById('search_menu').style.display = "block";
}

function sOut() {
	myGetElementById('search_menu').style.display = "none";
}

function bOver() {
	myGetElementById('browse_menu').style.display = "block";
}

function bOut() {
	myGetElementById('browse_menu').style.display = "none";
}


/* For turning on/off the poem structure view... */

function togglePoemText() {
	if ( myGetElementById('poem_structure').style.display == "none" ) {
		myGetElementById('poem_structure').style.display = "block";
		myGetElementById('text_toggle').innerHTML = "View Poem Only";
		myGetElementById('poem_text').style.width = "45em";
		myGetElementById('poem_wrapper').style.backgroundColor = "#f1f1ea";
	} else {
		myGetElementById('poem_structure').style.display = "none";
		myGetElementById('text_toggle').innerHTML = "View Poem Structure";
		myGetElementById('poem_text').style.width = "63em";
		myGetElementById('poem_wrapper').style.backgroundColor = "#fff";
	}
}

function findToggle() {
	myGetElementById('text_toggle').onclick = togglePoemText;
}


/* Expandables... */

function findStructure() {
	var togglers = myGetElementsByClass('toggler',null,'a');  				// find all of our toggle links
	var toggle = function() { toggleStructure(this); }        				// define the toggle function
	for ( var i=0; i<togglers.length; i++ ) {
		var mom = togglers[i].parentNode;									// find the LI that contains this toggle
		var sisters = mom.getElementsByTagName('ul');						// find all ULs that follow this toggle
		for (var x = 0; x < sisters.length; x++) {
			sisters[0].style.display = "none";								// hide the first UL that follows this toggle
	    }
		togglers[i].onclick = toggle;						  				// assign toggle function to toggle links' onClick
	}
	if ( myGetElementById('poem_structure') != null ) {							// first see if we even have a poem structure to find.  If so...
		var structureHeight = myGetElementById('poem_structure').offsetHeight;	// find height of fully-expanded structure nav
		var poemText = myGetElementById('poem_text');
		if (!poemText) return;
		if ( structureHeight > poemText.offsetHeight ) {
			poemText.style.height = structureHeight + "px";						// apply this height to our poem text div 
		}

	} else {																	// If not, turn off the column
		if (myGetElementById('poem_text') != null) {
			myGetElementById('poem_text').style.width = "50em";
			myGetElementById('poem_text').style.marginLeft = "0";
		}
	    if (myGetElementById('poem_wrapper') != null)
	        myGetElementById('poem_wrapper').style.backgroundColor = "#fff";
	}
}

function toggleStructure(what) {

    var obj;

    if ( typeof (what) == "string" )
	obj = myGetElementById(what);
    else
	obj = what;
	
	var mom = obj.parentNode;								  // find the LI that contains this toggle
	var sisters = mom.getElementsByTagName('ul');			  // find all ULs that follow this toggle

	for (var x = 0; x < sisters.length; x++) {
		var sister = sisters[0];			  				  // find the first UL that follows this toggle
    }

    if ( sister.style.display=="block" ) {                    // if it's on, turn if off
		sister.style.display="none";
		obj.className="toggler closed";
	} else {             								      // if it's off, turn if on
		sister.style.display="block";
		obj.className="toggler open";
	}
}

function findOpens() {

    var obj = myGetElementsByClass('open',null,'li');          // find all of our open toggle switches

	for ( var i=0; i<obj.length; i++ ) {
		obj[i].parentNode.style.display = 'block';
		var as = obj[i].parentNode.parentNode.getElementsByTagName('a');
	    as[0].className = 'toggler open';
	}
}

function findSubjectOpens() {

    var obj = myGetElementsByClass('open',null,'a');          // find all of our open toggle switches
	
	for ( var i=0; i<obj.length; i++ ) {
		var mom = obj[i].parentNode;                         // find all LIs that contain the open toggle switche in question
		var sister = mom.getElementsByTagName('ul');         // find all ULs that follow the open toggle switch in question
			for ( var j=0; j<sister.length; j++ ) {
				sister[0].style.display = "block";           // open 'em
			}
		}
}

function findHighlighter() {
     if (document.getElementById('highlight_toggle') != null) {
		var linx0r = myGetElementById('highlight_toggle').getElementsByTagName('a');
		for ( var i=0; i<linx0r.length; i++ ) {
			linx0r[i].onclick = highlightOff;
		}
	}
}


function highlightOff() {
	
    var on = myGetElementsByClass('highlighted',null,'span');          // find all of our highlighted words
	for ( var j=0; j<on.length; j++ ) {
		on[j].className = "lowlighted";
	}
	
	var linx0r = myGetElementById('highlight_toggle').getElementsByTagName('a');
	for ( var i=0; i<linx0r.length; i++ ) {
			linx0r[i].onclick = highlightOn;
		}
}

function highlightOn() {
	
    var off = myGetElementsByClass('lowlighted',null,'span');          // find all of our could-be-highlighted words
	for ( var j=0; j<off.length; j++ ) {
		off[j].className = "highlighted";
	}
	
	var linx0r = myGetElementById('highlight_toggle').getElementsByTagName('a');
	for ( var i=0; i<linx0r.length; i++ ) {
			linx0r[i].onclick = highlightOff;
		}
}

function getHeight(d) {
	var height = 0;
	if(d.offsetHeight){
         height=d.offsetHeight;
    }
    else if(d.style.pixelHeight){
         height=d.style.pixelHeight;
    }
	return height;
}

function loadHome() {

	var height1 = getHeight(myGetElementById('home1'));
	var height2 = getHeight(myGetElementById('home2'));

	if ( height1 < height2 ) {
		if ( isIE ) {
			myGetElementById('rss').style.marginTop = ( height2 - height1 ) + "px";
		} else {
			myGetElementById('rss').style.marginTop = ( ( height2 - height1 ) + 12  ) + "px"
		}
			
	}
	
}

function clearForm(formIdent) { 
  var form, elements, i, elm, opt, j; 
  form =  document.getElementById(formIdent);
  form.reset();
  elements = form.getElementsByTagName('input');
  for( i=0, elm; elm=elements.item(i++); ) {
    if (elm.getAttribute('type') == "text")	{
	  elm.value = '';
	}
	else if(elm.getAttribute('type') == "radio") {
	  if(elm.getAttribute('value') == "0") elm.setAttribute("checked", "checked");
	}
  }
  elements = form.getElementsByTagName('select');
  for( i=0, elm; elm=elements.item(i++); ) {
    for(j=0; j<elm.options.length; j++){
	  if(elm.options[j]) elm.options[j].selected=false;
	}
  }
}

function popup(url, w, h){
	var l = Math.round((window.screen.width - w)/2);
	var t = Math.round((window.screen.height - h)/2);
	var win = window.open(url, "", "toolbar=0,location=0,status=1,menubar=0,scrollbars=0,resizable=0,width=" + w + ",height=" + h + ",left=" + l + ",top=" + t + "");
	win.focus();
}

function gup( name )//this function is used to parse query string!
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}


/* Lock Comparison Scrolls */

function findScrolls() {
	if ( !myGetElementById('compare_two') ) return true;
	if (
			myGetElementById('compare_one').scrollHeight > myGetElementById('compare_one').clientHeight &&
			myGetElementById('compare_two').scrollHeight > myGetElementById('compare_two').clientHeight
		) {
		myGetElementById('scroll_controls').style.display = "block";
		scrollOff();
	} else return;
}

function scrollOn() {
var divOne = myGetElementById('compare_one');
var divTwo = myGetElementById('compare_two');
	divOne.onscroll = function() {
		divTwo.scrollTop = divOne.scrollTop;
	}
	divTwo.onscroll = function() {
		divOne.scrollTop = divTwo.scrollTop;
	}
myGetElementById('scroll_on').style.display = "none";
myGetElementById('scroll_off').style.display = "block";
}

function scrollOff() {
var divOne = myGetElementById('compare_one');
var divTwo = myGetElementById('compare_two');
	divOne.onscroll = function() { null }
	divTwo.onscroll = function() { null }
myGetElementById('scroll_on').style.display = "block";
myGetElementById('scroll_off').style.display = "none";
}


/*  Commenting this out and moving the labels outside the input boxes. 
 $(document).ready(function(){           
      
     $("#top_login_user").attr("value","Username"); 
     $("#top_login_pass").attr("value","Password"); 
      
     $("#top_login_user, #top_login_pass").focus( 
          function () { 
               $(this).attr("value",""); 
          } 
     ); 
      
 });
*/
