/*
	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)
	
	//navigation
		$('#main-nav, #functional-nav ul, #sub-nav, #footer-main-nav, #footer-functional-nav').find('li:last').addClass('end_nav');
		
		if($('#sub-nav').length){ $('#main-content').addClass('padded-main-content'); }
		
	//helpers
		$('.arrowed').append(' &rsaquo;');
		$('#content :header').filter(':gt(0)').not('.subhead').css({ marginTop:'25px' });
		$('#faq-listing :header').css({ marginTop:'0px' });
}

//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();
	});
}

//FAQ Listing
function initFaqSection(){
	
	$('#faq-listing .section').each(function(){
		
		//hide all parts
		$(this).find('> ul li > :header').siblings().hide();
		$(this).find('h3').css({ opacity:1 }).siblings().hide();
		
		//add section expansion
		$(this).find('h3').wrapInner('<a href="#"></a>').find('a').click(function(){
			
			if($(this).parent().hasClass('selected')){
				$(this).parent().removeClass('selected').animate({ opacity:1 }).siblings().slideUp(700);
				$(this).parent().parent().siblings().each(function(){
					$(this).find('h3').animate({ opacity:1 });
				});
			} else {
				//other sections
				$(this).parent().parent().siblings().each(function(){
					$(this).find('h3').removeClass('selected').animate({ opacity:0.60 }).siblings().slideUp(700);
				});
				
				//this section
				$(this).parent().addClass('selected').animate({ opacity:1 });
				//$('#faq-listing .section').find('h3').siblings().hide();
				$(this).parent().siblings().slideDown(700);
			}
			return false;	
		});
		
		//add section listing expansion
		$(this).find('ul li > h4').wrapInner('<a href="#"></a>').find('a').click(function(){				
			
			//other LIs
			$(this).parent().parent().siblings().each(function(){
				$(this).animate({ marginTop:'0px', marginBottom:'0px' });
				$(this).find('h4').siblings().slideUp(700);
			});
			
			//this LI
			$(this).parent().parent().animate({ marginTop:'15px', marginBottom:'15px' });
			$(this).parent().siblings().slideDown(700);
			return false;			     
		});
		
		//format stuff
		var answer_title = "Answer";
		if($('.french').length)	 { answer_title = "R&eacute;ponse"; }
		else if($('.spanish').length){ answer_title = "Respuesta"; }
		
		$(this).find('ul li > h4').next('p').prepend('<b class="answer_label">'+answer_title+':</b> ');
	});
}

function initProductInfo(){
	if($('#product-listing')){
		
		//get initial product
		var hash = window.location.hash;
		hash = hash.substring(hash.indexOf('#')+1);
		
		var product = $('#product-'+hash);
		product = (hash == "")?$('#'+$('#product-listing > div:eq(0)').attr('id')):product;
		
		//hide all headers
		$('.product').find('> :header:eq(0)').hide();
		$('.product').each(function(){
			$('<hr />').insertAfter($(this));
		});
		
		//append subhead with product title, hide all other listings
		$('<h3 class="subhead">'+$(product).find('> :header:eq(0)').html()+'</h3>').insertAfter($('#main-content > h2'));
		$(product).siblings().hide();
		
		//build side menu
		$('#sub-content').each(function(){
			
			//insert, or empty
			if($('#product-quick-jump').length){ $('#product-quick-jump ul').empty(); }
			else {
				var h = "Quick Links";
				if($('.french').length)	 { h = "&Agrave; voir aussi"; }
				else if($('.spanish').length){ h = "visi&oacute;n r&aacute;pida"; }
				
				$(this).prepend('<div id="product-quick-jump"><h3>'+h+'</h3><ul class="fancy_checklist"></ul></div>');
			}
			
			//add links
			$('#product-listing .product').each(function(){
				$('#product-quick-jump ul').append('<li><a href="#'+$(this).attr('id').substring(8)+'">'+$(this).find(':header:eq(0)').html()+'</a></li>');
				$('#product-quick-jump ul li:last').find('sup').remove();
		   	})
			
			//initialize links
			$('#product-quick-jump ul li a').each(function(){
				$(this).click(function(){
					var id = $(this).attr('href');
					id = id.substring(id.indexOf('#')+1);					
					window.location.hash = id;
					
					$('#main-content > h3.subhead:eq(0)').html($('#product-listing > div#product-'+id+' > :header:eq(0)').html());
					$('#product-listing div#product-'+id).show().siblings().hide();
					
					return false;
				});
			});
		});
	}
}


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


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