$(document).ready(function() {

//hide search text input box when loading
$("#fulltext").hide();

//////   toggle the email address submission form   //////
$("a.emailicon").toggle(
    function() { 
        $("#emailpage").show("fast");
    },
    function() { 
        $("#emailpage").hide("fast");
    }
);
    
//////   process email address submission form   //////
$(".emailerror").hide();
$(".button").click(function() {

     $(".emailerror").hide();
     
     // validate the form
     var emailaddress = $("input#emailaddress").val();
     //var pagelink = $("input#pagelink").val();
     //var pageyear = $("input#pageyear").val();
     //var pageseason = $("input#pageseason").val();
     //var pagedate = $("input#pagedate").val();
     if (emailaddress == "") {
        $("label#emailaddress_error").show();
        $("input#emailaddress").focus();  
       return false;
     }
     
     //var dataString = 'emailaddress=' + email + '&pagelink=' + pagelink + '&pageyear=' + pageyear + '&pageseason=' + pageseason + '&pagedate=' + pagedate;
     var dataString = 'emailaddress=' + emailaddress;
     //alert (dataString); return false; 
     $.ajax( {
        type: "POST",
        url: "email.php",
        data: dataString,
        success: function() {
            $("#emailpage").html("<div id=\"confirm\"></div>");  
            $("#confirm").html("Email sent to " + emailaddress + ". Reload this page to send another email.");
        }
     }); // end .ajax
     return false;
});  // end .button.click

//////   hide and show in faq page   //////
//adapted from simple jQuery show/hide script: http://screwlewse.com/?p=30
// hide all blocks that have class faqhide
$('.hidetxt').hide();
// toggle aboutlink with abouttext
// using chaining for performance and ease
$('.hideheading').toggle(function(){
    $(this).parent().next().slideDown('fast').removeClass('hidetxt').preventDefault;
	},function(){
    $(this).parent().next().slideUp('fast').addClass('hidetxt').preventDefault;
});  //end .hideheading toggle  


// show and hide year-season  +/- toggle in the tree navigation  ////
//$('.children').hide();
// toggle aboutlink with abouttext
// using chaining for performance and ease
/*$('.yearparent').toggle(function(){
    $(this).parent().next().slideDown('fast').removeClass('children').preventDefault;
    $(this).html('<b> - Year </b>');
	},function(){
    $(this).parent().next().slideUp('fast').addClass('children').preventDefault;
	$(this).html('<b> + Year </b>');

});  */
//end year/season +/- toggle

// tree navigation for the year-season-date infocus area  ////
//$('.childreninfocus').show();
// toggle aboutlink with abouttext
// using chaining for performance and ease
/*$('.yearinfocus').toggle(function(){
    $(this).parent().next().slideUp('fast').addClass('children').preventDefault;
    $(this).html('<b> + Year </b>');
	},function(){
    $(this).parent().next().slideDown('fast').removeClass('children').preventDefault;
	$(this).html('<b> - Year </b>');

}) ;  */ 

// search result display page
$('.showhideswitch').toggle(function(){
    $(this).parent().children('.fulltextbox').slideDown();
	$(this).html("[Hide full text]");
	},function(){
    $(this).parent().children('.fulltextbox').slideUp();
	$(this).html("[Display full text]");
});  //end .fulltextbox toggle

// lections page: change citation hover to hand cursor
$('.lection_citation').hover(function() {
    $(this).css('cursor', 'pointer');
});


// toggle the icalendar file listing 
/*$('#icalendar').toggle(
    function() { $("#icalopts").show("fast"); },
    function() { $("#icalopts").hide("fast");}
);*/

$('#aboutheading').hover(
    function() { $(this).css('cursor', 'pointer'); },
    function() { $(this).css('cursor', 'default'); }
);
$('#linkheading').hover(
    function() { $(this).css('cursor', 'pointer'); },
    function() { $(this).css('cursor', 'default'); }
);
$('#printheading').hover(
    function() { $(this).css('cursor', 'pointer'); },
    function() { $(this).css('cursor', 'default'); }
);

///////////  change the title bar for the lightbox print-options div - didn't work on all pages -jmg 12/1/2009  ///////////
//$('#TB_closeAjaxWindow').html("<a id='TB_closeWindowButton' href='#'>[X]</a>");
//$('#searchbox p.searchopts a.thickbox').click(function() {
//    $('#TB_closeAjaxWindow').html("<a id='TB_closeWindowButton' href='#'>[X]</a>");
//});

$('.yyyy').click(
	function() { 
		$(this).parent().children(".timelinedropdown").toggle();
	});	

///// For faqadmin page, dynamically display blank category input box   ////
$('#btnAdd').click(function() {
    var num     = $('.blankCat').length; // how many "duplicatable" input fields we currently have
	var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
	
	// create the new element via clone(), and manipulate it's ID using newNum value	
	var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

    // manipulate the name/id values of the input inside the new element
       newElem.children(':first').attr('id', 'catorder_new' + newNum).attr('name', 'catorder_new' + newNum);
	   newElem.children(':last').attr('id', 'catname_new' + newNum).attr('name', 'catname_new' + newNum);


    // insert the new element after the last "duplicatable" input field
    $('#input' + num).after(newElem);

   });


	
});  // end document.ready

////////// other js functions ///////////

// AJAX CITATION LOOKUP
// from http://nodstrum.com/2007/09/19/autocompleter/
function lookup(inputString) {
    if(inputString.length == 0) {
        $('#suggestions').hide();  // hide the suggestion box if nothing's been entered yet
    } else {
        $.post("citation-lookup.php", {queryString: ""+inputString+""}, function(data){ 
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    } //end of lookup
}

function fill(name, id) {
    $('#citation').val(name);
    $('#lookupid').val(id);
    $('#suggestions').hide();
}

//togglestatus is to disable the suggestionbox when doing a fulltext search
function toggletextbox() {
    if ($('#search-fulltext').is(':checked')) {
        $('#fulltext').show();
		$('#citation').hide();
		$('#autoSuggestionsList').hide();
		$('#lookupid').hide();
		//$('#suggestionsBox').css('border-style','none'); 
		
    }
	if  ($('#search-citation').is(':checked')){
		$('#citation').show();
		$('#autoSuggestionsList').show();
		$('#fulltext').hide(); 
    }   
}

// show all divs on the faq and faq2 page. this is called if we're the printable page.
function showall() {
    $('#main_text').children("#abouttext").show();
    $('#main_text').children("#linktext").show();
    $('#main_text').children("#printtext").show();
    $('#main_text').children(".hidetxt").show();
}

function showcommentbox() {
    $('#commentbox').show('fadein');
    $('#comments').css('width','400px');
}

//for admin page, display year input box for future lection year 
function togglecalyearA() {
    $("#calyearA").toggle();
}
function togglecalyearB() {
    $("#calyearB").toggle();
}
function togglecalyearC() {
    $("#calyearC").toggle();
}

function togglecalyearinput() {
	if ($("#popA").is(":checked") ) {
		$('#calyearA').show();
	}else{ 
		$('#calyearA').hide(); 
	}
	if ($("#popB").is(":checked") ) {
		$('#calyearB').show();
	}else{ 
		$('#calyearB').hide(); 
	}
	if ($("#popC").is(":checked") ) {
		$('#calyearC').show();
	}else{ 
		$('#calyearC').hide(); 
	}
}


