jQuery.fn.onImagesLoaded = function(_cb) { 
  return this.each(function() {
 
    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };
 
    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
          }
        }; // _checki \\
 
        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};

$(function() {
	$(".modaloverlay").hide();
	$("#TriggerBarrelNowForm").click(function(){
		$("#BarrelNowForm").show();
		$(".modaloverlay").show();
	});
	
	$(".modaloverlay").click(function(){
		$("#BarrelNowForm").hide();
		$(".modaloverlay").hide();
	});
	$("#BarrelNowForm .closebox").click(function(){
		$("#BarrelNowForm").hide();
		$(".modaloverlay").hide();
	});
	
	$("#HireBarrelForm").validate();
	
	
	$("#ReferredBy").change(function(){
		$("#ReferredOther").hide();
		if($("#ReferredBy").val().indexOf("A friend") != -1) {
			$("#ReferredOtherLabel").html("Friend's Name");
			$("#ReferredOther").fadeIn();
		}
		if($("#ReferredBy").val().indexOf("Other") != -1) {
			$("#ReferredOtherLabel").html("Other");
			$("#ReferredOther").fadeIn();
		}
	});
	
	$("#Budget").change(function(){
		$("#BudgetBarrelNow").hide();
		if($("#Budget").val().indexOf("Under $3,000") != -1) {
			$("#BudgetBarrelNow").fadeIn();
		}
		if($("#Budget").val().indexOf("$3,000 - $5,000") != -1) {
			$("#BudgetBarrelNow").fadeIn();
		}
	});
	
		   
	$('#NewHomeBanner').cycle({
		fx: 'fade',
		speed: 'slow',
		timeout: 8000,
		pager: '#NewHomePager',
		pagerAnchorBuilder: function(){ return '<li>&nbsp;</li>'; }
	});
		
	$('#BarrelGrantsSlideshow').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 8000, 
	    next:   '.next', 
	    prev:   '.prev' 
	});
	
	$("#BarrelGrantsSlideshowContainer").mouseenter(function() {
	    $("#BarrelGrantsSlideshowContainer .nav").fadeIn();
	  }).mouseleave(function() {
	    $("#BarrelGrantsSlideshowContainer .nav").fadeOut();
	  });
	
	$("#BarrelGrantsSlideshowContainer .next").mouseenter(function() {
	    $(this).fadeTo("fast", 1.0);		
	  }).mouseleave(function() {
	     $(this).fadeTo("fast", 0.7);
	  });
	
	$("#BarrelGrantsSlideshowContainer .prev").mouseenter(function() {
	    $(this).fadeTo("fast", 1.0);		
	  }).mouseleave(function() {
	     $(this).fadeTo("fast", 0.7);
	  });
	
	$(".projectListLargePhoto").mouseenter(function() {
	    $(this).fadeTo("fast", 1.0);		
	  }).mouseleave(function() {
	     $(this).fadeTo("fast", 0.7);
	  });
	
	$('div#AboutSlideshowImages').onImagesLoaded(function(_this){
		$(_this).fadeIn(1000);
		$('#AboutSlideshowImages').cycle({ 
		    fx:     'fade', 
		    speed:  'slow', 
		    timeout: 4000, 
		    next:   '.next', 
		    prev:   '.prev' 
		});
	});
	
	$('#CareersSlideshowImages').onImagesLoaded(function(_this){
		$(_this).fadeIn(1000);
		$('#CareersSlideshowImages').cycle({ 
		    fx:     'fade', 
		    speed:  'slow', 
		    timeout: 4000, 
		    next:   '.next', 
		    prev:   '.prev' 
		});
	});
	
	$('div#HomepageBanner').onImagesLoaded(function(_this){
		$(_this).fadeIn(1000);
	});
	
	$('div#NewHomeBanner').onImagesLoaded(function(_this){
		$(_this).fadeIn(1000);
	});
	
	$('#NewHomeFeatured li .entry').onImagesLoaded(function(_this){
		$(_this).fadeIn(1000);
	});

	$("#AboutSlideshowContainer").mouseenter(function() {
	    $("#AboutSlideshowContainer .nav").fadeIn();
	  }).mouseleave(function() {
	    $("#AboutSlideshowContainer .nav").fadeOut();
	  });
	
	$("#AboutSlideshowContainer .next").mouseenter(function() {
	    $(this).fadeTo("fast", 1.0);		
	  }).mouseleave(function() {
	     $(this).fadeTo("fast", 0.7);
	  });
	
	$("#AboutSlideshowContainer .prev").mouseenter(function() {
	    $(this).fadeTo("fast", 1.0);		
	  }).mouseleave(function() {
	     $(this).fadeTo("fast", 0.7);
	  });
	
	$("#CareersSlideshowContainer").mouseenter(function() {
	    $("#CareersSlideshowContainer .nav").fadeIn();
	  }).mouseleave(function() {
	    $("#CareersSlideshowContainer .nav").fadeOut();
	  });

	$("#CareersSlideshowContainer .next").mouseenter(function() {
	    $(this).fadeTo("fast", 1.0);		
	  }).mouseleave(function() {
	     $(this).fadeTo("fast", 0.7);
	  });

	$("#CareersSlideshowContainer .prev").mouseenter(function() {
	    $(this).fadeTo("fast", 1.0);		
	  }).mouseleave(function() {
	     $(this).fadeTo("fast", 0.7);
	  });
	
	// new about page team Playa
	
	$('#BodyText ul.team_members li').css('margin', '0px 10px 10px 10px'); 
	$('#BodyText ul.team_members li:nth-child(1)').css('margin', '0px 10px 10px 0px'); 
	$('#BodyText ul.team_members li:nth-child(6n + 1)').css('margin', '0px 10px 10px 0px'); 
	$('#BodyText ul.team_members li:nth-child(6n)').css('margin', '0px 0px 10px 10px'); 
	
	// ie6 margins 
	
	$('#HomeBottomFour li:nth-child(4)').css('margin-right', '0px'); 
	$('#MoreBarrel li:nth-child(4)').css('margin-right', '0px'); 
	
});

$(document).keydown(function(e) {
		switch(e.keyCode){
			case 39:	$('#BarrelGrantsSlideshow').cycle('next');
						break;	
			case 37:	$('#BarrelGrantsSlideshow').cycle('prev');
						break;
		}
});

$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
    $(pager).find('li').removeClass('activeLI')
        .filter('li:eq('+currSlideIndex+')').addClass('activeLI');
};
