//random name selection
(function($){
  $.fn.randName = function(){
    return this.eq(Math.floor(Math.random()*this.length));
  };
})(jQuery);

$(document).ready(function(){
	
	// $('#keywords').labelify({ text: 'label' });
	
	// cycle with auto-advance, prev/next buttons
	$('#heroImages.cycleMe, #panels.cycleMe').cycle({ 
	    fx:     'fade', 
	    speed:  '750', 
	    timeout: 8000, 
	    pager: '#cycleNav',
			slideExpr: "div.panel"
	});
	$('#heroImages.cycleMe').cycle({ 
	    fx:     'fade', 
	    speed:  '750', 
	    timeout: 8000, 
	    pager: '#cycleNav',
			slideExpr: "img"
	});
	$('.verbalCycle').cycle({ 
	    fx:     'fade', 
	    speed:  '750', 
	    timeout: 8000, 
	    next:   '.micro-next',
			slideExpr: 'div'
	}).cycle('pause');
	
	$('#cycleNav a').click(function(){
		$('#heroImages').cycle('pause'); 
	});
	
	// cycle with auto-advance for case studies - paging
   $('#heroImages.caseCycle').cycle({
       fx:     'fade',
       speed:  '750',
       timeout: 4000,
       pager:  '#casenav',
			 slideExpr: 'img'
   });
	// add classes for image replacment on paging
	// $('#casenav a:nth-child(1)').addClass('page-one');
	// $('#casenav a:nth-child(2)').addClass('page-two');
	// $('#casenav a:nth-child(3)').addClass('page-three');
	// $('#casenav a:nth-child(4)').addClass('page-four');
	
	// wrap the naming logos in groups of 6
	// var $pArr = $('.naming-main #heroImages img');
	// 	var pArrLen = $pArr.length;
	// 	var pPerDiv = 6;
	// 	for (var i = 0;i < pArrLen;i+=pPerDiv){
	// 		$pArr.filter(':eq('+i+'),:lt('+(i+pPerDiv)+'):gt('+i+')').wrapAll('<div class="nameGroups" />');
	// 		}
	var nth = 6;
	$('#heroImages.nameCycle').children('img').each(function(index) {
	  if (index % nth === 0) {
	    $('<div class="cycled"></div>').appendTo('#heroImages');
	  }
	  $('#heroImages').children('div:last').append(this);
	});
	
	// cycle with auto-advance for naming logos - no paging or prev/next
   $('#heroImages.nameCycle').cycle({
       fx:     'fade',
       speed:  '750',
       timeout: 8000,
			 slideExpr: 'div.cycled',
			 pager: '#cycleNav',
			 cleartypeNoBg:  true
   });
	
	// create columns for the case listing module
	if ( $('#caseListing').length ) {
		$('#caseListing ul').ezColumns({columns: 4, colWrapper: '<ul class="col"></ul>', target: $('div#listContainer')});
	}
	
	//randomly show a TG name
	$( '.altnames' ).randName().show();
	
	// select a new TG name on click
	// $('#nameChange').click(function() {
	// 	$('.altnames').hide();
	// 	$( '.altnames' ).randName().show();
	// });
	
	// hover for the previous/next buttons
	$('#prev, #next').hover(function() {
		$(this).addClass('hovered');
	}, function() {
		$(this).removeClass('hovered');
	});
	
	// home page - remove margin from last module
	$('.home .module:eq(5)').css('marginRight','0');
	
 	// add the arrow graphic to the left nav links
	$('.clientCol a').append('<img src="/images/interface/sectnav_arrow.png" class="arrow" alt="">');
	
	// no right margin on third client list column
	$('.clientCol:last').css('marginRight','0');
	
	// make entire home module areas clickable
	$('.home .module').click(function(){
		window.location=$(this).find('a').attr('href');
	  return false;
	});
	
	$('.home .module').hover(function() {
		$(this).find('a').addClass('hovered');
	}, function() {
		$(this).find('a').removeClass('hovered');
	});
	
	// hover state for left nav
	$('#sectNav li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	// nav li click clicks a
	$('#sectNav li').click(function() {
		window.location=$(this).find('a').attr('href'); return false;
	});
	
});

