/*
	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 ul, #footer-nav ul, .subpage #nav-crumb ul, #whiteboard-nav ul').find('> li:last').addClass('end_nav');
	$('.subpage #nav-crumb ul').find('> li:first').addClass('first_nav');
	
		//dropdowns
		$('#main-nav .dropdown').each(function(){
			$(this).hide().find('li:last').addClass('end_nav').end();
			
			//show dd, hide other dd's
			$(this).parent().find('> a').hover(function(){
				$(this).parent().siblings().find('.dropdown').hide();
				$(this).parent().find('.dropdown').show(); 
			},function(){});
			
			//hide dd
			$(this).hover(function(){}, function(){ $(this).hide(); });
		});
		
		$('#main-nav > ul > li')
		.each(function(){				
			$(this).hover(function(){
				$(this).parent().siblings().find('.dropdown').hide();
			}, function(){
				$(this).find('.dropdown').hide();
			} );
		});
		
	//CSS
	$(':header + :header').addClass('subhead');
	$(':header + p, :header + ul, :header + ol, :header + blockquote').css({ marginTop:'10px' });
	
	$('#home-buckets, .x3columns, .x2columns').equalHeights();
}

//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();
	});
}

//flash activated quicktour link
function activeTourFromFlash(){
	$('#flash-tour-button').trigger('click');
}

//whiteboard setup
function setupWhiteboard(){
	if($('#whiteboard').length){
		$('#whiteboard').each(function(){
			var wb_nav 	 = $(this).find('#whiteboard-nav');
			var wb_content = $(this).find('#whiteboard-content');
			
			//generate turnstile links
			$(wb_content).find('> div').each(function(){
				var len = $(wb_content).find('> div').length;
				var idx = $(wb_content).find('> div').index(this);
				idx = (idx+1 == len)?0:idx+1;
				
				//add link, only if it's not the last link
				if(idx != 0){
					$(this)
					.append('<p class="arrowed"><a href="#" title="">'+$(wb_nav).find('li:eq('+idx+') a').text()+'</a></p>')
					.find('p:last a').click(function(){
						$(wb_nav).find('li:eq('+idx+') a').trigger('click');
						return false;
					});
				}
			});
			
			//show first tab, hide other tabs / content
			$(wb_content)
			.find('> div').hide().end()
			.find('> div:first').addClass('selected').fadeIn('slow');
			
			$(wb_nav).find('li:first').addClass('selected');
			
			//get initial height (to prevent height-jerking)
			var h = $(wb_content).find('> div.selected').innerHeight(true);
			$(wb_content).css({ height:h+"px" });
			
			//setup tab links
			$(wb_nav).find('a').click(function(){
				
				$(this).parent().addClass('selected').siblings().removeClass('selected');
				
				var tab_index = $(wb_nav).find('a').index(this);						//get tab index
				
				$(wb_content)	.find('> div.selected').fadeOut('fast', function(){
					
					$(this).removeClass('selected');							//deselect old content
					$(wb_content).find('> div:eq('+tab_index+')').addClass('selected').end()	//select new content
					
					var h = $(wb_content).find('> div.selected').innerHeight(true);		//get new height of content
					$(wb_content).animate({ height:h+"px" }, 700, function(){			//animate new content
						$(wb_content).find('> div.selected').fadeIn('slow');							      
					});
				});
				
				return false;
			});
		});
	}
}

// diagrams + charts
function setupEPSDiagram(){
	$('#traditional-process, #coachware-process').hover(
		function(){ $(this).animate({ top:125 }); },
		function(){ $(this).animate({ top:265 }); }
	);
}

function setupComponentDiagram(){
	$('#component-diagram').prepend('<a id="r1" href="#designer-desc">designer</a><a id="r2" href="#console-desc">console</a><a id="r3" href="#coach-desc">coach</a>');
	$('#component-diagram a#r1').hover(
		function(){ $('#component-diagram #designer-desc').animate({ left:300, top:0, opacity:1 }, 400); },
		function(){ $('#component-diagram #designer-desc').animate({ left:300, top:-300, opacity:0 }, 300); }
	);
	$('#component-diagram a#r2').hover(
		function(){ $('#component-diagram #console-desc').animate({ left:100, top:0, opacity:1 }, 400); },
		function(){ $('#component-diagram #console-desc').animate({ left:400, top:-200, opacity:0 }, 300); }
	);
	$('#component-diagram a#r3').hover(
		function(){ $('#component-diagram #coach-desc').animate({ left:200, top:-250, opacity:1 }, 400); },
		function(){ $('#component-diagram #coach-desc').animate({ left:640, top:-650, opacity:0 }, 300); }
	);
	$('#component-diagram a').click(function(){ return false; });
}

/*function setupIndustryAppsInterface(){
	
	var wb_content   = $('#whiteboard-content');
	var table 	   = $('#industry-application-table');
	var industries   = $('#industry-application-table .industries');
	var applications = $('#industry-application-table .applications');
	
	$(table).find('p.arrowed').remove();
	$(applications).find('dl').hide();
	$(industries).find('li').each(function(){
		$(this)
		.wrapInner('<a href="#" title=""></a>')
		.find('a').click(function(){
					     
			var li = $(this).parent();
			var apps = String($(li).attr('class')).split("|");
			
			$(li).addClass('selected').siblings().removeClass('selected');
			
			$(applications).find('dl').fadeOut('slow', function(){
				$(applications).find('dt, dd').hide();
				for(var x=0; x<apps.length; x++){
					$(applications).find('dt:eq('+apps[x]+'), dd:eq('+apps[x]+')').show();
				}
				
				var h1 = $(applications).find('dl').outerHeight(true);	//get new height of content
				var h2 = $(industries).outerHeight(true);			//get new height of content				
				h1 = (h2 > h1)?h2:h1;
				
				$(wb_content).animate({ height:h1+"px" }, 700, function(){	//animate new content
					$(applications).find('dl').fadeIn('slow');
				});
			});
						
			return false;
		});
	})
	$(industries).find('li:first a').trigger('click');
	
}*/

function setupFilterInterface(){
	
	$('.filter-listing').each(function(){
		
		var wb_content = $('#whiteboard-content');
		var container  = $(this);
		var filter   	 = $(this).find('.filter');
		var listing 	 = $(this).find('.listing');
	
		$(listing).find('> ul').hide();
		$(filter).find('li').each(function(intIndex){
			
			$(this)
			.wrapInner('<a href="#" title=""></a>')
			.find('a').click(function(){
				
				var index = intIndex;
				
				$(this).parent()
				.addClass('selected')
				.siblings().removeClass('selected');
				
				$(listing).find('> ul').fadeOut('slow', function(){
					
					var element = $(this).find('> li:eq('+intIndex+')');
					$(element).show().css({ opacity:0 }).siblings().hide();
					$(listing).find('> ul').fadeIn('slow');
					
					var h = $(wb_content).find('> div.selected').innerHeight(true);		//get new height of content
					$(wb_content).animate({ height:h+"px" }, 700, function(){			//animate new content
						$(element).animate({opacity:1}, 'slow');
					});
				});
							
				return false;
			});
		})
	
		$(filter).find('li:first a').trigger('click');
	});
}

/* IE RELATED */
function applyIE6FlickerFix(){
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}


/* INITIALIZATION */
$(document).ready(function(){
	setupPage();
});

