/*
	functions.dom.js
	JQuery / DOM Interaction Functions
	Created: Nov. 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)
	setupDropDownMenu();
	
	//navigation
		
		if($('#sub-nav-home-link').length){
			$('.subpage div.leadin-content').append('<p id="floating-home-link">'+$('#sub-nav-home-link').html()+'</p>');
		} else {
			$('.subpage div.leadin-content').append('<p id="floating-home-link"><a href="index.php" title="">Home</a></p>');
		}
		
		$('.subpage #sub-nav ul')
		.find('li#sub-nav-home-link').remove().end()
		.find('li:last').addClass('end_nav');
		
		//.prepend($('.subpage #sub-nav ul li.selected'))
		
		$('#sub-nav').show();
		$('#main-content > div.columns2_1 > div.right').prepend($('#sub-nav'));
	
	//helpers
		// fix header heights
		/*$('#leadin-content h2').each(function(){
			if($(this).outerHeight() < 76){
				$('#sub-nav ul').css({ paddingTop:'10px' });
				$(this).css({ paddingBottom:'17px' });
			} else {
				//$(this).css({ paddingTop:'30px' });
			}
		});*/
		
		//contact header
		$('.contact_header').prepend('<img src="images/contact_header_icon.jpg" align="left" alt="" />');
		
		//member_message
		if(BrowserDetect.browser == "Firefox" && BrowserDetect.version < 3){
			$('.member_message blockquote, .quote_message blockquote').append('<span class="end_quote">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
		} else {
			$('.member_message blockquote, .quote_message blockquote').append('<span class="end_quote">&nbsp;</span>');
		}
		
		//success story link addition
		$('#success-stories img').each(function(){
			var a = $(this).parent().find('p:last > a');
			$(this).attr('border', '0').wrap('<a href="'+$(a).attr('href')+'" title="'+$(a).attr('title')+'"></a>');
		});
		
	//support styles
		$('div + :header, p + :header, ul + :header, ol + :header, blockquote + :header').css({ marginTop:'20px' });
}

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

//setup drop down menu system
function setupDropDownMenu(){
	
	$('.dropdown').each(function(){
		$(this).css({ opacity:0.9 });
		$(this).find('li:last').addClass('end_nav');
	 });
	
	$('#main-nav > ul > li > a').hover(
		function(){ $('.dropdown').slideUp(100); $(this).parent().find('.dropdown').slideDown(100); },
		function(){}
	);
	$('#main-nav .dropdown').hover(
		function(){},
		function(){ $(this).slideUp(100); }
	);
}

function setupBioCarousel(){

	if($('#bio-listing').length){
		$('#bio-listing').each(function(){
			
			$('#bio-listing').hide();
			$('<div id="bio-carousel">'+
				'<div id="bio-carousel-controls">'+
					'<a id="bio-carousel-controls-prev" href="#" rel="0">&nbsp;</a>'+
					'<a id="bio-carousel-controls-next" href="#" rel="0">&nbsp;</a>'+
				'</div>'+
				'<ul></ul>'+
				'<div id="bio-content"></div>'+
			  '</div>')
			.insertBefore($('#bio-listing'));
			
			//build image carousel
			$('#bio-listing li').each(function(){ $('#bio-carousel ul').append('<li><a href="#'+$(this).attr('id')+'"><img src="'+$(this).find('img').attr('src')+'" /></a></li>'); });
			
			//setup controls
			$('#bio-carousel-controls a').css({ opacity:0 });
				
				$('#bio-carousel-controls-prev').click(function(){
					var steps = Number($('#bio-carousel-controls-prev').attr('rel'));
					if(($('#bio-carousel ul li').length-4)-steps >= 1){
						$('#bio-carousel-controls-next').show().fadeTo(300, 1);
					}
					if(steps-1 == 0){
						$('#bio-carousel-controls-prev').fadeTo(300, 0, function(){ $(this).hide(); });
					}
					if(steps-1 >= 0){
						$('#bio-carousel-controls-next').attr('rel', steps-1);
						$('#bio-carousel-controls-prev').attr('rel', steps-1);
						var new_step = (steps-1)*89;
						
						$('#bio-carousel ul').animate({ left:'-'+new_step+'px' });
					}
					return false;
				});
			
				$('#bio-carousel-controls-next').click(function(){
					var steps = Number($('#bio-carousel-controls-next').attr('rel'));
					if(($('#bio-carousel ul li').length-4)-steps <= 1){
						$('#bio-carousel-controls-next').fadeTo(300, 0, function(){ $(this).hide(); });
					}
					if(steps+1 > 0){
						$('#bio-carousel-controls-prev').show().fadeTo(300, 1);
					}
					if(steps+1 <= $('#bio-carousel ul li').length-4){
						$('#bio-carousel-controls-next').attr('rel', steps+1);
						$('#bio-carousel-controls-prev').attr('rel', steps+1);
						var new_step = (steps+1)*89;
						
						$('#bio-carousel ul').animate({ left:'-'+new_step+'px' });
					}
					return false;
				});
			
			//setup image carousel actions
			$('#bio-carousel ul li a')
			.css({ opacity:0.20 })
			.hover(
				function(){ if(!$(this).hasClass('selected')){ $(this).fadeTo(150, 0.40); } },
				function(){ if(!$(this).hasClass('selected')){ $(this).fadeTo(150, 0.20); } }
			)
			.click(
				function(){
					var id = $(this).attr('href');
					id = id.substring(id.search('#'));
					var bio_content = $(id).clone().find('img').remove().end().html();
					$('#bio-content').html($(bio_content));
					
					$('#bio-carousel ul li a').fadeTo(1, 0.20); 
					$('#bio-carousel ul li a').removeClass('selected');
					
					$(this).fadeTo(150, 1);
					$(this).addClass('selected');
					return false;
				}
			);
			
			
			//fade in first entry
			var first_id = $('#bio-listing li:first').attr('id');
			var bio_content = $('#'+first_id).clone().find('img').remove().end().html();
			var largest_entry_height = 0;
			$('#bio-listing li').each(function(){
				$('#bio-content').css({ opacity:0 }).html($(this).clone().html());
				var h = $('#bio-content').outerHeight(true);				
				largest_entry_height = (h > largest_entry_height)?h:largest_entry_height;
			});
			
			$('#bio-content').empty().css({ opacity:1, minHeight:eval(largest_entry_height)+'px' }).html($(bio_content));
			$('#bio-carousel ul li:first a').fadeTo(150, 1);
			
			if($('#bio-carousel ul li').length > 5){
				$('#bio-carousel-controls-next').fadeTo(300, 1);	
			}
		});
	}
}

/* news pagination */
function setupNewsPagination(n){

	var currentPage = 0;
	var numPerPage = n;
	var hash = Number(document.location.hash.substring(1)) || 0;
	hash = (hash-1 <= 0)?0:hash-1;
	
	$('#news-listing').each(function() {
	
		var $listing = $(this);
		$listing.find('> li').addClass('news_item');
		
		$listing.bind('repaginate', function() {	
			
			var n = currentPage * numPerPage;
			var x = ((currentPage + 1) * numPerPage - 1);
										
			$listing.find('.news_item').show();
			$listing.find('.news_item:lt('+n+')').hide().end();
			$listing.find('.news_item:gt('+x+')').hide().end();
		});
	
		var numRows = $listing.find('.news_item').length;			
		var numPages = Math.ceil(numRows / numPerPage);			
		var $pager = $('<p class="pager"><b>Page:</b></p>');
		
		for (var page = 0; page < numPages; page++) {			
			$('<a href="#" class="page-number">' + (page + 1) + '</a>')
			.bind('click', {'newPage': page}, function(event) {
			
				currentPage = event.data['newPage'];
				$listing.trigger('repaginate');
				$(this).addClass('active').siblings().removeClass('active');
				$(this).trigger('blur');
				return false;
			})			
			.appendTo($pager).addClass('clickable');			
		}
		if(hash < numPages){
			$pager.find('a.page-number:eq('+hash+')').addClass('active').trigger('click');
		} else {
			$pager.find('a.page-number:first').addClass('active');
		}
		$pager.insertBefore($listing);
		
		$('#news-listing').append('<p class="top_page"><a href="#header" title="Go to the top of the page...">Top of the page</a></p>');
		
		$listing.trigger('repaginate');
	});
}

/* FAQ */
function setupFAQ(){
	
	$('#faq').each(function() {
		
		$('#main-content > .columns2_1 > div.right').append('<h4 class="blocked_header">FAQ quick jump</h4><ul id="faq-quick-jump" class="bulleted_list"></ul>');
		
		var count = 1;
		$(this).find('dt').each(function(){
			$(this).attr('id', 'faq'+count);
			$('#faq-quick-jump').append('<li><a href="#'+$(this).attr('id')+'">'+$(this).text()+'</a></li>');
			count++;
		});
		
		$(this).find('dd:gt(0)').each(function(){
			$(this).append('<p><a href="#header">Back to the top</a></p>');
		});
	});
}

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


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