/*
	functions.dom.js
	JQuery / DOM Interaction Functions
	Created: Sept. 20, 2008
	Creator: Matt Kircher
*/

/* GENERAL */
function setupPage(){
	
	applyIE6FlickerFix();	//IE6 Flickering issue
	BrowserDetect.init();	//start browser detection object
	translateEmails();		//changes unlinked email address to usable ones (spam protection)
	
	/* CSS Support */
		$('#main-nav')
		.find('li:first').addClass('home_link').end()
		.find('li:last').addClass('contact_link');
		
		$('#content').append('<div class="clear_block">&nbsp;</div>');
		
		$('#footer-main-nav li:last').addClass('nopipe');
		
		$('a.accented_link').append(' &raquo;');
		
		$('#sub-content .module:last').css({ borderBottom:'none' });
	
	/* Authors, Editors, Series Listing */
		if($('#series-listing.archive, #author-listing, #editor-listing, #resources-listing').length){
			$('#series-listing.archive, #author-listing, #resources-listing')
			.find(' > div').hide().end()
			.parent().parent().find('#sub-nav a').bind('click', function(){
				
				$('#sub-nav li').removeClass('selected');
				$('#series-listing.archive > div, #author-listing > div, #resources-listing > div').hide();
				
				$(this).parent().addClass('selected').blur();
				$($(this).attr('href')).show();
				return false;		 
			});
			
			//if no hash on url, or location of hash doesn't exist...
			if($.trim(document.location.hash) == "" || !$('#sub-nav li a[href="'+document.location.hash+'"]').length){
				if($('#sub-nav li.selected').length){
					//if a manual class="selected" has been placed on any menu item, select it...
					$('#sub-nav li.selected:first a').trigger('click');
				} else {
					//otherwise, select first item in the menu
					$('#sub-nav li:first a').trigger('click');
				}
			} else {
				//...otherwise, select specific hash id from url
				$('#sub-nav li a[href="'+document.location.hash+'"]').trigger('click');
			}
			
			$('#series-listing.archive div.series, #editor-listing > div, #author-listing > div').append('<p><a href="#header" title="Jump to the top of the page...">Top of page</a></p>');
		}
		
		//add links to the chapter archive blocks (chapter title and authors)
		if($('#series-listing').length){
			$('#series-listing .series > li').each(function(){
				if($(this).find('h5 a').length){
					var href = $(this).find('h5 a').attr('href');
					var title = $(this).find('h5 a').attr('title');
					
					var chapter = $(this).find('h6');
					$(chapter).html('<a href="'+href+'" title="'+title+'">'+chapter.html()+'</a>');
					
					$(this).find('> ul li').each(function(){
						var a = $(this);
						$(a).html('<a href="'+href+'" title="'+title+'">'+a.html()+'</a>');
					});
				} else {
					return false;	
				}
			});
		}
		
		//add links to the chapter blocks (authors)
		if($('.chapter_listing').length){
			$('.chapter_listing > dt').each(function(){
				if($(this).find('a').length){
					var href = $(this).find('a').attr('href');
					var title = $(this).find('a').attr('title');
					
					var author = $(this).next('dd');
					$(author).html('<a href="'+href+'" title="'+title+'">'+author.html()+'</a>');
				} else {
					return false;	
				}
			});
		}
}

//makes email tags invisible to spiders / spammers
function translateEmails(){
	$('span.email, address.email').each(function(){
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;		
		
		//EXAMPLE: <span class="email" title="link title | email address | email subject"> link content </span>		
		
		var inner_content = $(spt).html();						//inner HTML of span tag
		var t = $(spt).attr('title');						//email, link options from title attribute
		
		var title = t.substring(0, t.indexOf('|'));				//title for the link
		t = t.substring(t.indexOf('|')+1);
		
		var addr = t.substring(0, t.indexOf('|'));				//email address from id attribute
		addr = addr.replace(at,"@").replace(dot,".");				//replace words with chars
		
		var subject = t.substring(t.indexOf('|')+1);				//subject for email, if needed
		var fulladdr = ($.trim(subject) != "")?addr+'?subject='+subject:addr;	//full address formed with subject, if needed
		
		inner_content = ($.trim(inner_content) == "" || $.trim(inner_content) == "&nbsp;")?addr:inner_content;
		
		$(spt).after('<a href="mailto:'+fulladdr+'" title="'+title+'">'+ inner_content +'</a>')
		.hover(function(){window.status="Send an email!";}, function(){window.status="";});
		$(spt).remove();
	});
}

/* IE RELATED */
function applyIE6FlickerFix(){
	try {
	 document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}


/* INITIALIZATION */
$(document).ready(function(){
	setupPage();
});
