//-----------------------------------------------
//The Virgilian Tradition Website Base Javascript
//Author:   Michael Sohn
//Version:  April 09 2009
//-----------------------------------------------
  $(document).ready(function() {

    // add target new to external links
    $("#subcol a[href^='http://'], #article-text a[href^='http://']").each(function() {
    // check the DOM attribute 'host' on this
        if (this.host != window.location.host) {
        $(this).attr('target', '_new');
        $(this).attr('rel', 'external');
        }
    });

    // add first word of title as class
		var pagetit_first_word = $("title").text().split(" ")[0].toLowerCase();
      	$("body").addClass("vt" + pagetit_first_word);

    // append back to top snippets to end of article
        var back_to_top = "<p><small><a href='#'>Back to Top</a></small></p>";
        var article_text = $("#article-text").text().length;
        var image_eq_thousand_words = ($("#article-text img").length)*550;
        var amount_p = ($("#article-text p").length)*30;
        if (article_text+image_eq_thousand_words+amount_p> 1500) {$("#article-text").append(back_to_top);};

    // append back to top snippets to entries but not the last one
		$("#article-text .entry").not(":last").append(back_to_top);

    // append back to top snippets and entry class to top level li but not the last one
		$("#iofm>li").not(":last").addClass("entry").append(back_to_top);

    // add class noprint to back to top snippets
    $("p:has(small:has(a[href='#']))").each(function() {
        $(this).addClass("noprint");
    });
    // append Updated snippet to footer
        $("div#footer p").append(" <span id='update'>Updated: " + document.lastModified + "</span>");
    // remove border for link (make invisible)
    $("a:has(img), a:has(img):hover, a:has(img):visited").css("border-bottom","0 transparent");

    // add class current to current menu item
    $("#nav li,#nav li#left,#nav ul:first-child").each(function() {
		var menuetit = $(this).text();
		var pagetit0 = $("title").text().split(" | ")[0];
		var pagetit1 = $("title").text().split(" | ")[1];
		var pagetit2 = $("title").text().split(" | ")[2];
		var pagetit3 = $("title").text().split(" | ")[3];
		var pagetit4 = $("title").text().split(" | ")[4];
		var sectionh1 = $("#section h1").text();
    	if (menuetit==pagetit0||menuetit==pagetit1||menuetit==pagetit2||menuetit==pagetit3||menuetit==pagetit4) {
      	$(this).addClass("current");
   		}
    // replace a with strong at top level current menu item
    	if (menuetit==pagetit0) {
      	$(this).find("a").replaceWith("<strong>" + menuetit + "</strong>");
   		}
    	if (menuetit==pagetit2) {
      	$(sectionh1).prepend(pagetit1 + ": ");
   		}
    });
    // add left line at most left menu item
    $("#nav li:first-child a,#nav li:first-child strong").css("border-left-width","1px");

    // toggle visibility of entries with Additions & Corrections
	$(function(){
		$('.addcorr_c').siblings('h4')
		.click(function(event){
			if (this == event.target) {
				$(this).parent().find(".addcorr_c").toggle('fast');
			}
		return false;
		})
	.click();
	});

	$(function(){
		$('.addcorr_c').parent().find("h4 em, h4 q")
		.click(function(event){
			if (this == event.target) {
				$(this).parent().parent().find(".addcorr_c").toggle('fast');
			}
		return false;
		})
//	.click();
	});


	$(function(){
		$('.addcorr_b').siblings('h3')
		.click(function(event){
			if (this == event.target) {
				$(this).parent().find(".addcorr_b").toggle('fast');
			}
		return false;
		})
	.click();
	});
		$('.addcorr_c, .addcorr_b').siblings('h4, h3').css('color','#446699').css('cursor','pointer');
		$('.addcorr_c').parent().find('h4 em, h4 q').css('color','#446699').css('cursor','pointer');

	$("#addcorr").before("<p>Below are the subjects treated in <em>The Virgilian Tradition</em>. Please click on the items in <strong style='color: #446699;'>blue</strong> text if you would like to view additions or corrections to the book.</p>");

    //alert('something');
	

 });
