/*
	functions.dom.js
	DOM Interactivity Functions
	Created: Nov. 5, 2008
	Creator: Matt Kircher
*/

function setupPage(){
	
	applyIE6FlickerFix();
	translateEmails();
	initHomeClientViewer();
	
	//navigation
	$('#main-nav ul, #sub-nav ul, #functional-nav ul, #footer-nav ul').find('> li:last').addClass('end_nav');
	
	//links
	$('a').bind('click', function(){ $(this).blur(); });
	$('a.arrowed').append(' &rsaquo;');
	
	//content, columns
	$('#content, .columns').addClass('clearfix');
	//$('#content, .columns').append('<div class="clear_block">&nbsp;</div>');
	//$('p a.fancy_button').parent().append('<div class="clear_block">&nbsp;</div>')
	
	$('.module-casestudy, .module-insight, .module-contact, .module-news-box, .module-client-box')
	.prepend('<div class="top">&nbsp;</div>')
	.append('<div class="bottom">&nbsp;</div>');
	
	$('.home .module-clients ul li:gt(0)').hide();
	$('.home .module-clients ul li:first').addClass('selected');
	
	//styles
	$('#content').hide();
		$('#side-content').find('.module + .module').css({ marginTop:'25px', clear:'both' });
		$('.home #main-content, .subpage #main-content .left').find(':header:gt(0) + p, :header:gt(0) + ul, :header:gt(0) + ol').css({ marginTop:'0px' }).prev().css({ marginBottom:'0px' });
		$('.home #main-content, .subpage #main-content .left').find('p + :header, ul + :header, ol+ :header').prev().css({ marginBottom:'20px' });
		
		if($('.full_page').length){
			$('.subpage #main-content').find(':header:gt(0) + p, :header:gt(0) + ul, :header:gt(0) + ol').css({ marginTop:'0px' }).prev().css({ marginBottom:'0px' });	
			$('.subpage #main-content').find('p + :header, ul + :header, ol+ :header').prev().css({ marginBottom:'20px' });
		}
	$('#content').show();
}

function formatHomeModules(){
	//find 160 words and discard rest of the news entry
	$('.module-news-box p:gt(0)').not('p:last').remove();
	/*$('.module-news-box p:eq(0)').hide();
	
	var t = $('.module-news-box p').text();
	t = t.substring(0,t.indexOf(' ',160));
	$('.module-news-box p:eq(0)').text(t+'...').show();*/
	
	//make module height equal
	$('#module-gallery').equalHeights();
	
	//move bottom image to match each other
	mod1_y = $('.module-insight .bottom').offset().top;
	mod2_y = $('.module-client-box .bottom').offset().top;
	if(mod1_y > mod2_y){
		y = mod1_y-mod2_y+$('.module-client-box .bottom').height();
		$('.module-client-box .bottom').css({ top:eval(y-1)+"px" });
	} else if(mod1_y < mod2_y){
		y = (mod2_y-mod1_y)+$('.module-insight .bottom').height();
		$('.module-insight .bottom').css({ top:eval(y-1)+"px" });
	}
}

function translateEmails(){
	$('span.email, address.email').each(function(){
		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
		var addr_check = addr;
		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;" || 
				   $.trim(inner_content) == " " || 
				   $.trim(inner_content) == $.trim(addr_check))?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 initHomeClientViewer(){
	if($('#client_viewer').length){
		
		//hide all items
		$('#client_viewer li').css({ opacity:0, left:'15px' });
			
		//show first item
		$('#client_viewer li:first').show().animate({ opacity:1, left:0 }, 700);
	
		var interval = setInterval(function(){
				
			//fade image
			$('#client_viewer li:first').animate({ opacity:0, left:'-15px' }, 500, function(){
				
				//hide top (for IE6)
				$('#client_viewer li:first').hide();
				
				//move to bottom
				$('#client_viewer ul').append($('#client_viewer li:first'))
				
				//show next image, now the first in the list
				$('#client_viewer li:first').css({ opacity:0, left:'15px' }).show().animate({ opacity:1, left:0 }, 700);
				
			});
		}, 5000);
	}
}

function setupNewsPagination(){
	
	var currentPage = 0;
	var numPerPage = 8;
	
	$('#news-listing').each(function() {
	
		var $listing = $(this);
		
		$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>');
		var $pager2 = $pager.clone(true);
		
		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');
				
				$('.pager').find('a:eq('+eval(currentPage)+')').addClass('active').siblings().removeClass('active');
				$(this).trigger('blur');
				return false;
			})
			.appendTo($pager).addClass('clickable')
			.clone(true).appendTo($pager2);
		}
		$pager.find('a.page-number:first').addClass('active');
		$pager2.find('a.page-number:first').addClass('active');
		
		$pager.insertBefore($listing);
		$pager2.insertAfter($listing);
		
		$listing.trigger('repaginate');
	});	
}

/* IE RELATED */

function applyIE6FlickerFix(){
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

$(document).ready(function(){
	setupPage();
});

