/*
	functions.dom.js
	DOM Interactivity Functions
	Created: 1.29.08
	Creator: Matt Kircher, Mainline Media LLC
*/

function setupPage(){
	
	//email protection
	translateEmails();
	
	//navigation
	$('#main-nav li:last, #functional-nav li:last, #footer-main-nav li:last, #footer-functional-nav li:last').addClass('end_nav');
	
	$('a').bind('click', function(){ $(this).blur(); });				//links
	$('a.arrowed').append(' &rsaquo;');						
	
	//FAQs & "sectionized" pages
	if($('.faq').length){ setupFAQ(); }
	if($('.sectionized').length){ setupSectionizedPage(); }
	
	//inline bulleted lists
	$('.bulleted_inline_list').each(function(){
		$(this).find('li').not(':first').prepend('<b>&bull;</b> ');
	});
	
	//HR tags (for backgrounds in IE)
	swapHRTags();
	
	//content images
	$('#main-content img[@align="left"], #sub-content img[@align="left"]').addClass('floating_image_l');
	$('#main-content img[@align="right"], #sub-content img[@align="right"]').addClass('floating_image_r');
	
	//modules
	$('#sub-content').prepend($('#sub-nav'));
	$('#sub-content div.module:last').addClass('end_module');	
	$('#content').append('<div class="clear_block">&nbsp;</div>');
	
	//header & content spacing
	setHeaderContentSpacing();
	
	//Flash
	if($('.flash-masthead').length){
		$('.flash-masthead').media({ 
		    width:     800, 
		    height:    215, 
		    autoplay:  true, 
		    src:       'flash/masthead_r4.swf', 
		    attrs:     { quality:'high', allowScriptAccess:'sameDomain', scale:'exact', align:'left' }, 
		    params:    { quality:'high', allowScriptAccess:'sameDomain', scale:'exact', bgcolor:'#007CC4' }, 
		    caption:   false // supress caption text 
		});
	}
}

function translateEmails(){
	$('span.email, address.email').each(function(){
		
		//example: <span class="email" title="[title] | [addr at domain dot com] | [subject of email]"> [link text] </span>
							   
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;		
		
		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();
	});
}

function swapHRTags(){
	if(BrowserDetect.OS == "Windows"){
		if(BrowserDetect.browser == "Explorer"){
			$('hr').not('[@class="clear_block"]').before('<div class="hr"></div>').remove();
			$('hr[@class="clear_block"]').before('<div class="hr clear_block"></div>').remove();
		}
	}
}

function setHeaderContentSpacing(){
	$('h1, h2, h3, h4, h5, h6').next().each(function(){
		var node = $(this).get(0);
		n = node.nodeName;
		
		if(n == "P" || n == "UL" || n == "OL" || n == "BLOCKQUOTE" || n == "DL"){
			$(this)
			.css("paddingTop","0px")
			.css("paddingBottom","8px")
		}
	});
	
	$(':header + .subhead').css({ paddingTop:'0px' }).prev().css({ paddingBottom:'0px' });
}
	
function setupHeights(){
	
	//setup content area heights
	var padding = 50;
	var sub_h = 0;
	var m = $('#main-content').height();
	
	if($('#sub-nav').length > 0){
		sub_h = $('#sub-nav').height();
		$('#sub-content').css('paddingTop', eval(sub_h + padding)+"px");
	}
	$('#sub-content').height($('#main-content').height());
	$('#content').height(m + sub_h + padding);
	
	$(document).bind('resize', setupHeights);
}

function setupNewsSelector(){
	if($('#category_select').length){
		var cats = new Array();
		$('.article_listing').each(function(){
			var c = $(this).attr('title');
			if($.inArray(c, cats) == -1) cats.push(c);
		});
		
		for(x in cats){
			$('#category_select select').append('<option value="'+cats[x]+'">'+cats[x]+'</option>');
		}
		$('#category_select select option:first').attr('selected', true);
		
		$('#category_select select').bind('change', function(){
			$('.article_listing').show();
			
			var v = $(this).find('option:selected').val();
			if(v != "all"){
				$('.article_listing').not('[@title="'+v+'"]').hide();
			}
		})
		
		//setupHeights();
	}
}

function setupFAQ(){
	$('.faq').each(function(){
		
		var faq = this;
		
		$(faq)
		.find('dd').hide().append('<p><a class="faq_close" href="#" title="Collapse this answer...">Close answer</a></p>')
		.find('a:last').click(function(){
			$(this).trigger('blur').parent().parent().slideUp(500);
			return false;
		}).end().end()
		.find('dt').each(function(){
			this.innerHTML = '<a href="#" title="Click to read the answer...">' + this.innerHTML + '</a>';			   
		})
		.find('a').click(function(){
			$(this).trigger('blur').parent().next().slideDown(500);
			return false;
		});
	});
}

function setupSectionizedPage(){
	$('#preamble').prepend('<div id="sectionized-nav" class="sidebar"><h5>Quick Jump</h5><ul></ul></div>');
		
	var sCount = 1;
	$('.sectionized > :header:not(.subhead)').each(function(){
		$(this).attr('id', 's'+sCount);  sCount++;
		$('#sectionized-nav ul')
		.append('<li><a href="#'+$(this).attr('id')+'" title="Jump to this section">'+$(this).text()+'</a></li>')
		.find('a:last').click(function(){
			$.scrollTo($($(this).attr('href')), 300);
			return false;
		});
		
	});
	
	$('#sectionized-nav li:last').addClass('end_nav');
}

/* IE RELATED */

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

//DOM loaded
$(document).ready(function(){
	BrowserDetect.init();
	setupPage();
	applyIE6FlickerFix();//IE BG-image flicker fix
	
	//setupHeights();
});