
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var is_chromeframe = navigator.userAgent.toLowerCase().indexOf('chromeframe') > -1;

// based on http://stackoverflow.com/questions/838137/jquery-change-height-based-on-browser-size-resize
// answer number (7)

jQuery.event.add(window, "load", resizepanel_right);
jQuery.event.add(window, "resize", resizepanel_right);

function resizepanel_right() 
{
   // var h = $(window).height();
    var w = jQuery(window).width();
		// alert(w);
    jQuery("#panel_right").css('width',( w < 1155) ? 250 : 370);
}
 
function menuLinkClicked(e){				 		
	agent = jQuery.browser;
	if(agent.msie) {
	which = event.srcElement;
		event.cancelBubble = true;
		event.returnValue = false;
	} else {
	  which = e.target;
		e.stopPropagation();
		e.preventDefault();
	}
	
	var href = which.getAttribute("href");	
	
 jQuery.get("getContent.php?wantedFile=" + href, function(data) {
      
	if (href.indexOf("--") > -1){
		   // we have an article to show - articles have a -- in their name
			 jQuery('#panel_body').html(data);
			 load_panel_body();
		}
	 else{	
			// we have a new menu to show - menus don;t have a -- in their name
			
				
			 jQuery('#toc_topic').html(data);
			 jQuery("#toc_topic").scrollTop(0);
        }     
						 });		 
      	
	
	}
 
  function process_biblio()
	        {
					// Available firld names (all as lowercase):
//BibliographyType,ISBN,Identifier,Author,Title,Journal,Volume,Number,Month,Pages,Year,Address,Note,URL,Booktitle,Chapter,Edition,Series,Editor,Publisher,ReportType,Howpublished,Institution,Organizations,School,Annote,Custom1,Custom2,Custom3,Custom4,Custom5
					
					jQuery("[cite=true]").each(function() {
					
					citeRef = jQuery(this).attr("link_text");
					biblioInfo = footnote(citeRef, "author,editor,journal,title,publisher,year,url,note");
					jQuery(this).prepend(biblioInfo);
					
					});
		} 	
   
 
  function load_panel_body()
          {
		 process_biblio();			
		 GreekTransliteration();
		
		//   bible_abbr,  bible_info_fileds,   and  bible_info 
		//   are from data/bible_info/bible_copyrights.js 	
		//   bible_info_fileds array of "name_Full", "abbr", "latex", "owner" , "contact", "contact_email",  "http", "html_copyright"	 
		
	for (thisBible in bible_abbr)
		{ 
	    var  thisBibleAbrr = bible_abbr[thisBible];
			var thisBiblesInfo = bible_info[thisBibleAbrr];
		
		 if (jQuery("#panel_body").html().indexOf(thisBibleAbrr) > -1)
		   {
			  jQuery("#panel_body").append('<div class="side_note" link_text="' + thisBibleAbrr + ' Bible copyright"><div class="copyright"><b>'+thisBibleAbrr+'  &ndash; '+ thisBiblesInfo.name_Full +'</b><br />'+ thisBiblesInfo.html_copyright +'<br />' + thisBiblesInfo.http + '</div></div>\n\n');
			  }
	  }	 

		 load_panel_right();
		 process_showMore();
		 load_toc_current();	
		 
		 jQuery("#panel_right").scrollTop(0);
		 jQuery("#panel_body").scrollTop(0);				
					
		// runAnyScripts(document.getElementById("panel_body"));			
					
					}
					
	function process_showMore()
	       {
				 jQuery("*[showMore]").each( function(){				  
					jQuery(this).click(function() {
					
			showHide = jQuery(this).attr("showMore");
			  
	  if (jQuery(this).text() == "More Information")
		{
		   
			 jQuery("[showMore]=" + showHide ).each( function() // handles multiple occurances of clickable text
			 {
				     jQuery(this).text('Show Less');
		    });
						 
		 jQuery("#" + showHide).show();
    }
	else
	  {	
	    jQuery("[showMore]=" + showHide ).each( function() // handles multiple occurances of clickable text
			 {
        jQuery(this).text('More Information');
		    });				
		 
		  jQuery("#" + showHide).hide();
    }
			load_toc_current();	// builds or removes links depending on display status
				});  // click()
				 }); // each()	 
		
		
				 }				
					
  function  frontpage_loaded()
	  {
		TypeHelpers.insertClasses();
		
		load_panel_body();
		jQuery("#intro").fadeOut(12000,function(){
		
		  jQuery(this).empty(); // keep clear for pre- a- ppending latter stuff

		});
		
	/*	
		// helped by http://jqueryfordesigners.com/scroll-linked-navigation/
		
		$("#panel_body").scroll(function () {
  var inview = '#' + $('#content > section > h1:in-viewport:first').parent().attr('id'),
      $link = $('nav a').filter('[hash=' + inview + ']');
      
  if ($link.length && !$link.is('.selected')) {
    $('#toc_current a').removeClass('selected');
    $link.addClass('selected');    
   }
	 
});
		*/
		
	  }
		
	function  load_toc_current()
	  {
		var tocContents = "";
		
		function makeAnchorAndLinkTocEntry(text, spacer)
		        {
						var linkText = text.substr(0, text.length -1);
						
						tocContents += "<p style=\"margin-bottom: -7px; margin-left: " + spacer + 'px;"><a href="#'+linkText+'">'+text+'</a></p>';
						// alert('<a name="'+linkText+'"></a>');
						
						if (jQuery("a[name="+linkText + "]").attr("name") == undefined) 
						     //  load_toc_current() can be called more than oncem, so only buld anchor once - process_showMore()
              {						
						     return  '<a name="'+linkText+'"></a>'; // -1 to get rid of any possible line break
						  }
						}
		
				
		jQuery("[toc=true]").each(function() {
	
	  var displayText = jQuery(this).text();
	
	  var classNames = jQuery(this).attr("class").split(" ");
	
	  var processThis = "subsection" // cases where toc="true" is used with no heading class at all
	
	   for (className in classNames)
		   {
			  if (classNames[className].indexOf("head") > -1 | classNames[className].indexOf("section") > -1)
				  {
					 var processThis = classNames[className];
					   break
					 }
			 }
	
	  if (jQuery(this).parent().css('display') == 'none')
		  {
			processThis  = "skip";
			 }
	
	   switch	( processThis )
		       {
						case "heading30":
						 jQuery(this).prepend( makeAnchorAndLinkTocEntry(displayText, "0") );
						break;
						
						case "section":
						  jQuery(this).prepend( makeAnchorAndLinkTocEntry(displayText, "5") );						
						break;

						case "subsection":
						  jQuery(this).prepend( makeAnchorAndLinkTocEntry(displayText, "10") );						
						break;

						case "subsubsection":
						  jQuery(this).prepend( makeAnchorAndLinkTocEntry(displayText, "15") );						
						break;
																		
					 }
		
		})
		 jQuery("#toc_current").html(tocContents);
		}	
		
		
	function load_panel_right()
	  {
		var rightPanelHtml = "";
		jQuery("#panel_body").append('<ol>');
		
		jQuery('.side_note').each(function(index) {
		var noteHtml = jQuery(this).html();
		var linkText = jQuery(this).attr("link_text");
		var refNumber = index + 1;
		var newHtml =   '<a id="_'
		              + refNumber
									+ '" href="#' 
									+ linkText 
									+ '">' 
									+ linkText 
									+ ' &nbsp;<sup>(' 
									+ refNumber
									+ ')</sup></a>';
									
		 rightPanelHtml +=   '<div onclick="scrollToId(\''
		                   + '_'
											 + refNumber
											 + '\')" class="rightPanelNoteHeading"><a name="' 
											 + linkText 
											 + '"><span class="rightPanelRefNumber">(' 
											 + refNumber 
											 + ')</span>  &nbsp;&nbsp;<u>' 
											 + linkText 
											 + '</u></a></div>'
		                       + noteHtml;	
  
	   
	
		 jQuery(this).empty().html(newHtml);
		
  
		 jQuery("#panel_body").append('<div class="print_only_footnotes"><b>(' +refNumber+'.)</b> &nbsp;' + linkText +'&nbsp;&nbsp;' + noteHtml + '</div>');
		 
		 });
		 
		 jQuery("#panel_right").html(rightPanelHtml);
		 
//    jQuery("#panel_body").append('');
		
		// alert("Still being written Dion, Paul");
		}	
		
		function scrollToId(id)
		        {
						// scroll to previous/parent element to give vontext in document would be nice
						// but documents are not going to have pattern required
						
           document.getElementById(id).scrollIntoView();
					
						}
	
			
						
						
	  function GreekTransliteration()
	         {
					 
   var GreekAlphaBetRomanOrder =["&#913;","&#945;","&#914;","&#946;","&#926;","&#958;","&#916;","&#948;","&#917;","&#949;","&#934;","&#966;","&#915;","&#947;","&#919;","&#951;","&#921;","&#953;","*","&#962;","&#922;","&#954;","&#923;","&#955;","&#924;","&#956;","&#925;","&#957;","&#927;","&#959;","&#928;","&#960;","&#920;","&#952;","&#929;","&#961;","&#931;","&#963;","&#932;","&#964;","&#933;","&#965;","&#988;","&#989;","&#937;","&#969;","&#935;","&#967;","&#936;","&#968;","&#918;","&#950;"];

   var RomanAlphaBet="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";					 
					 
					 
					 jQuery(".showGreek").each( function(){
					 
					 var LatinType = jQuery(this).text();
					 
					 var replacement = "";
					 
					 for (letter =0; letter < LatinType.length; letter++)
						  {
							    thisLetter = LatinType.substr(letter,1);
								  alphaLocation = RomanAlphaBet.indexOf(thisLetter);
									
								if( alphaLocation > -1)
								  {
									 replacement += GreekAlphaBetRomanOrder[alphaLocation];
								  }
								else
								 { 
								 replacement += thisLetter // pass through not requiring handling
								 }
							}
					 jQuery(this).html(replacement);
					 jQuery(this).attr("title", "A Rough 'Greek-lish' Transliteration");
					 });
					 
	 } 					
	 
	 
	 
	 // a la http://brightbyte.de/page/Loading_script_tags_via_AJAX
	// not in use 
	 function runAnyScripts(holdElement) {
	if (holdElement.nodeType != 1) return; //if it's not an element node, return
 
 // alert(holdElement.tagName);
 
	if (holdElement.tagName.toLowerCase() == 'script') {
		eval(holdElement.text); //run the script
	}
	else {
		var n = holdElement.firstChild;
		while ( n ) {
			if ( n.nodeType == 1 )  runAnyScripts( n ); //if it's an element node, recurse
			n = n.nextSibling;
		}
	}
}
