// equal heights function
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
};

// image preloader
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
};

// custom fadeToggle effect
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);

};

$(document).ready(function() {
	
	//  Modal windows
	$(".popModal").colorbox();
	// special for the form modals
	$(".btnApply").colorbox();
	
	// rounded corners -- IEs get div strips, others get border-radius applied
	$('#mainNav').corner('5px');
	$('.module').corner('4px');
	$('.band').corner('3px');
	$('a.btnStyle').corner('3px');
	$('.callout, .pullquote').corner('6px');
	$('.callout h3').corner('6px top');
	// note: IE doesn't do anything visible with values less than 3px
	
	// Insights main page -- make items equal height
	equalHeight($('.eqHeight01'));
	
	// Insights main page -- changing arrow on hover for RSS subscribe button
	$('.rssSubscribe').hover(function() {
		$(this).children('a').children('img').attr('src','/images/img_more_on.gif');
	}, function() {
		$(this).children('a').children('img').attr('src','/images/img_more.gif');
	});
	
	// footer -- change out the graphic in the "how can we help" module
	$('#footContact p a').hover(function() {
		$(this).children('img').attr('src','/images/img_foothelp-on.png');
	}, function() {
		$(this).children('img').attr('src','/images/img_foothelp.png');
	});
	
	// no dotted border on last .module item
	$('.module p:last-child').addClass('noBorder'); // for FF, Chrome, Safari
	$('.module div.jquery-corner').prev('p').addClass('noBorder'); // for IE
	$('.module p.more').addClass('noBorder');
	
	// no dotted border on last #management item
	$('#management .mgtMember:last').css('background','none').css('paddingBottom','0').css('marginBottom','0');
	
	// no dotted border on last #mgtInsights item
	$('.mgtInfo li:last-child').css('background','none').css('paddingBottom','0').css('marginBottom','0');
	
	// show/hide for the About Us Management info
	$('.showBio').click(function() {
		$(this).parents('p').siblings('.mgtInsights').hide().siblings('.mgtBio').fadeToggle('fast');
		$(this).siblings('.showInsights').removeClass('controlOn');
		$(this).toggleClass('controlOn');
		return false;
	});
	$('.showInsights').click(function() {
		$(this).parents('p').siblings('.mgtBio').hide().siblings('.mgtInsights').fadeToggle('fast');
		$(this).siblings('.showBio').removeClass('controlOn');
		$(this).toggleClass('controlOn');
		return false;
	});
	
	// Right-hand 'Feed' module show/hide
	$('#sidebar .feed .feedBtns a').click(function() {
		$(this).addClass('feedOn').siblings('a').removeClass('feedOn');
		var feedType = $(this).attr('id');
		$('#sidebar .feed #' + feedType + 'Content').fadeIn('fast').siblings('.fContent').hide();
		return false;
	});
	
	// History/timeline module
	$('#timeline a:last').css('marginRight','0');
	$('#timeline a').hover(function() {
		var histTarget = $(this).html();
		if ( histTarget != '1999') {
			$('#history #hist' + histTarget).show();
			$('#history #hist1999').hide();
		} else {
			$('#history #hist' + histTarget).show();
		}
	}, function() {
		var histTarget = $(this).html();
		$('#history #hist' + histTarget).hide();
	});
	$('#timeline a').click(function() {
		return false;
	});
	
	// what we do page show/hide
//	$('#whatNav a').click(function() {
//		$(this).addClass('whatOn').parents('li').siblings('li').children('a').removeClass('whatOn');
//		var whatSelection = $(this).attr('href').replace('#','');
//		$('#whatContent #' + whatSelection).fadeIn('fast').siblings('div').hide();
//		$('#sidebar div.' + whatSelection).fadeIn('fast').siblings('div.whatSidebar').hide();
//		return false;
//	});
	
	// "more"-type links in right-hand #sidebar modules
	// adds in the triangle graphic, plus hover state
	$('#sidebar .more a').append('<img src="/images/img_more.gif" alt="">');
	$('#sidebar .more a').hover(function() {
		$(this).children('img').attr('src','/images/img_more_on.gif');
	}, function() {
		$(this).children('img').attr('src','/images/img_more.gif');
	});
	
	// contact page, and other form elements
	$("input.formText, textarea.formTextarea").labelify({ text: "label" });
	
	// initialize pagination for sidebar events/news
	// (will only do something if div.sidePage is used to wrap other divs in a sidebar module)
	var wrap = $('#sidebar .module .sidePage, #content .articlePages');
		// set up click events to trigger the pagination plugins' behaviour 
		$('#sidebar .module .prev, #content .articlePaging .prev').click(function(){
			wrap.trigger('prev.evtpaginate');
			//return false;
		});
		$('#sidebar .module .next, #content .articlePaging .next').click(function(){
			wrap.trigger('next.evtpaginate');
			//return false;
		});
		// listen out for events triggered by the plugin to update the counter
		wrap.bind( 'initialized.evtpaginate', function(e, startnum, totalnum ){
			$('.count').text(startnum);
			$('.total').text(totalnum);
		}); 
		wrap.bind( 'finished.evtpaginate', function(e, num, isFirst, isLast ){ $('.count').text(num); } );
		wrap.evtpaginate({ perPage: 1, atEnd: 'loop' }); // call the plugin!
		
		$('.btmPaging, .btmPaging img').click(function() {
        window.location = '#top';
         });

		
		// hover effect for carousel logos
		$('#carouselViewport img').hover(function(){
			imgsrc = $(this).attr('src');
			matches = imgsrc.match(/_on/);
			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.jpg$/ig,'_on.jpg'); // strip off extension
				if ( !$('#carousel ul').is(':animated') ) { // don't do hover until carousel slide in is complete
					$(this).attr('src', imgsrcON);
				}
			}
		}, function(){
				$(this).attr('src', imgsrc);
		});
		
		// initialize the client case study carousel
		// uses serialScroll and scrollTo plugins
		$('#carousel ul').show("slide", { direction: "left" }, 2000);
			$('#carouselViewport').serialScroll({
				items:'li',
				prev:'#carousel #carouselPrev',
				next:'#carousel #carouselNext',
				offset:0,
				start:0,
				step: 3,
				duration:500,
				force:false,
				stop:true,
				lock:false,
				cycle:false, 
				jump: false
			});
			
		// blog filter page "menu" behaviors
		$('#blogFilter ul li, #blogFilter ul li ul').hover(function() {
			$(this).addClass('sortHover');
			$(this).children('ul').slideDown('fast');
		}, function() {
			$(this).removeClass('sortHover');
			$(this).children('ul').slideUp('fast');
		});
		$('#blogFilter ul li ul li').hover(function() {
			$(this).addClass('sortListHover');
		}, function() {
			$(this).removeClass('sortListHover');
		});
				
	
		// clients page "menu" behaviors
		$('#clientSort #btnAll').addClass('allOff');
		$('#clientSort ul li, #clientSort ul li ul').hover(function() {
			$(this).addClass('sortHover');
			$(this).children('ul').slideDown('fast');
		}, function() {
			$(this).removeClass('sortHover');
			$(this).children('ul').slideUp('fast');
		});
		$('#clientSort ul li ul li').hover(function() {
			$(this).addClass('sortListHover');
		}, function() {
			$(this).removeClass('sortListHover');
		});
		
		$('#clientSort ul li ul li').click(function() {
			var sortFilter = $(this).attr('id');
			$('#clientSort #btnAll').removeClass('allOff');
			$('#clientList ul li').addClass('sortOff');
			switch(sortFilter)
			{
			case 'sortFinSvc':
			  $('#clientList ul li.finSvc').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortHealth':
			  $('#clientList ul li.health').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortDist':
			  $('#clientList ul li.dist').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortNonProf':
			  $('#clientList ul li.nonProf').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortProfSvc':
			  $('#clientList ul li.profSvc').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortAppDev':
			  $('#clientList ul li.appDev').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortAppLife':
			  $('#clientList ul li.appLife').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortBusInt':
			  $('#clientList ul li.busInt').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortMsCRM':
			  $('#clientList ul li.msCrm').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortMobApp':
			  $('#clientList ul li.mobApp').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortPort':
			  $('#clientList ul li.port').removeClass('sortOff').addClass('sortOn');
			  break;
			case 'sortUxDes':
			  $('#clientList ul li.uxDes').removeClass('sortOff').addClass('sortOn');
			  break;			
			default:
			  return false;
			}
			return false;
		});
		
		$('#btnAll').click(function() {
			$('#clientList ul li').removeClass('sortOn').removeClass('sortOff');
			$(this).addClass('allOff');
			return false;
		});
		
		// if .pageNavExtras exists (used for breadcrumb and article pagination),
		// adjust margins for #sidebar for proper alignment
		if ($('.pageNavExtras').length > 0) { // exists in the document
			$('#sidebar').css('marginTop','138px');
		} else {
			$('.pageNavExtras').length = 0; // IE breaks without this else statement
		}
		
		// Insights main page -- positioning of right-hand module relative to content modules
		if ($('.insights #posCheck').length > 0) { // exists in the document
			var checkPos = $('.insights #posCheck').position();
			$('.insights #sidebar .feed').css('top',checkPos.top-112);
		}	else {
				$('.insights #posCheck').length = 0; // IE breaks without this else statement
			}
		
		// what we do page -- URL hash activation
		// hash value should be same as hrefs on whatNav + 'Link'
		var whatURL = location.hash;
		whatURL = whatURL.replace('Link','');
		var whatClass = whatURL.replace('#','');
		if ($(whatURL).length > 0) { // exists in the document
			$('#whatNav li a#' + whatURL + 'Nav').addClass('whatOn').parents('li').siblings('li').children('a').removeClass('whatOn');
			$('#whatContent div#' + whatURL).show().siblings('div').hide();
			$('#sidebar div.' + whatClass).show().siblings('div.whatSidebar').hide();
		} else {
					$($(whatURL).length > 0).length = 0; // IE breaks without this else statement
		}
				
		// home page - remove dotted rules from last module items
		$('.home #content .module li:last-child').addClass('noBorder');
		
		// for insights articles, make right-hand column "share" links open in a new window
			$('.sidebarShare a').attr('target','_blank');	
	
});

// social bookmarking module
// note, there should only be ONE rel=shareit element on the page, otherwise this won't behave properly
$(document).ready(function() {
	var winLoc = window.location.href;
	//assign the value to variables and encode it to url friendly
	var url = encodeURIComponent(winLoc);
	var title = $('title').html();
	var title = encodeURIComponent(title);
	//Setup the bookmark media url and title
	// these link classes are used both for the footer share links and for those appear in the sidebar of articles
	$('a.shareit-facebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + url + '&t=' + title);
	$('a.shareit-delicious').attr('href', 'http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=' + url + '&title=' + title);
	$('a.shareit-stumbleupon').attr('href', 'http://www.stumbleupon.com/submit?url=' + url + '&title=' + title);
	$('a.shareit-twitter').attr('href', 'http://twitter.com/?status=' + title + '%20-%20' + url);
	$('a.shareit-linkedin').attr('href', 'http://www.linkedin.com/shareArticle?mini=true&url=' + url + '&title=' + title);
	
	//grab the anchor tag with rel set to shareit, show the menu, etc.
	$('a[rel=shareit], #shareit-box').mouseenter(function() {		
		
		$('a[rel=shareit]').addClass('shareHover');
		
		//get the left and find the center value
		var position = $('a[rel=shareit]').position();
		var posTop = position.top - 145;
		var posLeft = position.left - 20;
		
		//display the box
		$('#shareit-box').show();
		
		//set the position, the box should appear under the link and centered
		$('#shareit-box').css({'top':posTop, 'left':posLeft});
		
		//make the bookmark media open in new tab/window
		$('#shareit-box a').attr('target','_blank');
		
		
	});

	//onmouse out hide the shareit box
	$('#shareit-box, a[rel=shareit]').mouseleave(function () {
		$('a[rel=shareit]').removeClass('shareHover');
		$('#shareit-box').hide();
	});
	
});
