// JavaScript Document

$(document).ready(function(){ 
       // $(".boxbody").pngFix(); 
	   // $(document).pngFix(); 
	   
	   // show everything
	   //$("body").fadeIn("slow");

	   
	   
	   if(!jQuery.support.opacity) {
	   		
	   }


		// add extra markup to mainbody
		$("#mainbody > *").wrapAll('<div id="inner"></div>');
		// add extra markup to nav
		$("#nav").append('<div class="bottom"></div>');
		
		
		
		// IE <=6 Conditional Stuff
		var ie6 = false;
		var ie = false;
		
		if (jQuery.browser.msie) {
		  
		  ie = true;
		  
		  if(parseInt(jQuery.browser.version) <= 6) {
			
			
			ie6 = true;
			
			// reposition the book image, if we're on the home page
			
			// test if titleimage exists
			if ( $('#titleimage').length ) {

				var bim = '<div id="titleimage-repositioned">' + $("#titleimage").html() + '</div>';
				$("#titleimage").remove();
				$("#wrap").prepend(bim);
				$("#titleimage").addClass("repositioned");  
			}
			
		  }
		} 
		
		
		
		
		// add styling to all external links
		$("a[href^='http://']").addClass("external");
		$("a[href^='http://'][title]").each(function (i) { 
			
			var current_title = $(this).attr('title');
			$(this).attr('title', current_title + ' (external link)');
		
		
		});
		// add styling to all dynamic popup links
		$("a[href*='TB_iframe']").addClass("popup");
		$("a[href*='TB_iframe'][title]").each(function (i) { 
			
			var current_title = $(this).attr('title');
			$(this).attr('title', current_title + ' (Pop-up window)');
		
		
		});
		
		if(ie) {
			$('#speak_panel a.popup').before('<br />');
		}
		
		
		// Manage conertina on home page
		// test if there's a sidebar
		if ( $('#sidebar').length ) {
			
			
			// make shadow text
			
			//$("#sidebar .statement h3").clone().insertAfter();
			
			$("#sidebar .statement h3.foreground").each(function (i) { 
				
				//$(this).clone().removeClass('foreground').addClass('shadowText').insertBefore(this);
				//.opacity(0.5);
				
				//$(this).fadeTo("slow", 0.33);
			});
			
			
			if(!ie6) {	$("#sidebar .statement h3, #sidebar .statement div").css('opacity', '0.8');   }
			
			$("#sidebar .statement h3.foreground").click(function () { 
				
				var target = $(this).parent().contents("div");
				
				if(target.css("display") != "block") {
					// target is hidden, so let's show it
					// but first we need to hide other sections that might be open
					var othertargets = $(this).parent().siblings().contents("div:visible"); 
					// close other sections
					othertargets.slideUp();
					// return other sections colours to normal
					//$(this).parent().siblings().animate( { color:"auto" } , 500 );
					if(!ie6) {	$(this).parent().siblings().find("h3, div").animate( { opacity:"0.8" } , 500 ); }
					
					
					// show target
					target.slideDown();
					// change this section's bgcolour to a darker shade
					//target.parent().animate( { color:"#ffffff" } , 500 );
					
					if(!ie6) {	$(this).parent().find("h3, div").animate( { opacity:1 } , 500 );  }
				
				} else {
					// close this tab
					target.slideUp();
					// return this section's bgcolour to normal
					if(!ie6) {	target.parent().find("h3, div").animate( { opacity:"0.8" } , 500 );  }
				}
			
			});
			
			
		}
		
		
		

		
		
		// do advanced styling for table of contents
		// check if there's a toc on this page
		if ( $('ul.toc_chapters').length ) {
			
			// remove numbering style for chapters
			$('ul.toc_chapters').css('list-style', 'none');
			$('ul.toc_other').css('list-style', 'none');
			
			// add chapter numbers to proper place in markup
			var i = 0;
			var chapter = $('ul.toc_chapters > li').eq(0);
			
			for(i=0; chapter.length; i++) {
				var chapter = $('ul.toc_chapters > li').eq(i);
				chapter.prepend('<div class="number">' + (i+1) +'.</div>');
			}



			var i = 0;
			var chapter = $('ul.toc_other > li').eq(0);
			
			for(i=0; chapter.length; i++) {
				var chapter = $('ul.toc_other > li').eq(i);
				
				var roman = '';
				if(i==0) { roman = 'i'; }
				if(i==1) { roman = 'ii'; }
				if(i==2) { roman = 'iii'; }
				if(i==3) { roman = 'iv'; }
				if(i==4) { roman = 'v'; }
				if(i==5) { roman = 'vi'; }
				if(i==6) { roman = 'vii'; }
				if(i==7) { roman = 'viii'; }
				if(i==8) { roman = 'ix'; }
				if(i==9) { roman = 'x'; }
				
				chapter.prepend('<div class="number">' + roman +'</div>');
			}
			
			
			
		}
		
		
		// do advanced styling for sample passages
		// check if there's a sample passages section on this page
		if ( $('p > span.pull_quote_text').length ) {
			
			
			// $('p:has(span.pull_quote_text)').css("color", "blue"); //$('span.pull_quote_text')
			//$('p:has(span.pull_quote_text)').before('<div class="pull_quote pq_left">' + $(this).contents('span.pull_quote_text').eq(0).html() + '</div>');
			// $('p:has(span.pull_quote_text)').contents('span.pull_quote_text').css("color", "blue");
			
			$('p > span.pull_quote_text').each(function(index) {
					
				var parentparagraph = $(this).parent();
				var pullquote_text = $(this).html();
				//alert(pullquote_text);
				parentparagraph.before('<div class="pull_quote pq_right">' + pullquote_text + '</div>');
				
			});

						
			$('ul + div.pull_quote').css("margin-top", "1.1em");
			
		}
		
		
		
		
		
		var sm_top = $("#sample_menu").offset().top;
		var sm_clone = $("#sample_menu").clone().insertAfter("#sample_menu");
		sm_clone.attr('id', 'sample_menu_clone');
		
		
		
		
		$(window).scroll(function () { 

			//alert($("html").scrollTop());
			if($("html").scrollTop() > 200 && $("#back_to_top:hidden").length ) {
				
				// make sure the "back to top" link has fixed position before unhiding it
				if($("#back_to_top").css('position') != 'fixed' ) {
					$("#back_to_top").css('position', 'fixed');
				}
				
				$("#back_to_top").fadeIn();
				
			} else {
			
				if($("html").scrollTop() < 100 && $("#back_to_top:visible").length ) {
					//
					$("#back_to_top").fadeOut();
				}
			
			}
			
			
			// 
			// alert($(window).width());
			//alert($("#sample_menu").offset().left);
			//var leftpos = (($(window).width() - 963)/2);
			//$("#sample_menu").offset(10, leftpos);
			//var leftpos = (   ($(window).width()/2) );
			// 
			
			//var leftpos = ($("#sample_menu").offset().left - ($(window).width()/2));
			//alert(leftpos);
			
			
			// IE <=6 Conditional Stuff
			if ((jQuery.browser.msie) && (parseInt(jQuery.browser.version) <= 6)) {
			    
				// IE <=6
				var sm_top = $("#sample_menu").offset().top;
				var sm_abs_top = $("#sample_menu_clone").offset().top;
				var sm_top_clearance = 70;		
				var new_top = $("html").scrollTop() - sm_top_clearance;
				
				//alert($("html").scrollTop());
				//alert(sm_top);
				
				
				if($("html").scrollTop() > (sm_abs_top + sm_top_clearance)) {
					
					$("#sample_menu").css({'visibility' : 'hidden'});
					$("#sample_menu").css({'top' : + new_top + 'px'});
					$("#sample_menu").css({'visibility' : 'visible'});
					
				} else {
					
					$("#sample_menu").css({'visibility' : 'hidden'});
					$("#sample_menu").css({'top' : + (sm_abs_top - 101) + 'px'});
					$("#sample_menu").css({'visibility' : 'visible'});
				
				}
				
				
				
			} else {
				
				// Compliant Browser
				
				var sm_top = $("#sample_menu").offset().top;
				var sm_left = $("#sample_menu").offset().left;
				var sm_postype = $("#sample_menu").css('position');
				var sm_top_clearance = 20;					
				
				
				if($("html").scrollTop() > (sm_top - sm_top_clearance)) {
					
					if(sm_postype!='fixed') {
						
						$("#sample_menu").css({'visibility' : 'hidden'});
						$("#sample_menu").css({'left' : + sm_left + 'px', 'top' : + sm_top_clearance + 'px'});
						$("#sample_menu").css({'position' : 'fixed'});
						$("#sample_menu").css({'visibility' : 'visible'});
					
					}
				
				} else {
				
					var sm_off_top = $("#sample_menu_clone").offset().top;
					
					if($("html").scrollTop() < (sm_off_top) && sm_postype=='fixed') {
						
						var sm_abs_left = $("#sample_menu_clone").css('left');
						var sm_abs_top = $("#sample_menu_clone").css('top');
						
						$("#sample_menu").css({'visibility' : 'hidden'});
						$("#sample_menu").css({'position' : 'absolute', 'top' : sm_abs_top, 'left' : sm_abs_left});
						$("#sample_menu").css({'visibility' : 'visible'});
						
						
					
					}
				}
					
			}
				
				

		});
		
		
		
		// fix placement of ULs directly beneath main heading
		//$("div#page_header + ul li").eq(0).css({'padding-top' : '0px'});
		if($("div#page_header + ul.toc_other").length) {
			$("div#page_header").eq(0).css({'margin-bottom' : '0px'});
		}
		
		
		
		$('body').css('background', 'url(media/budburst5.jpg) top center no-repeat #e9edee');
		
	   
}); 
