//document ready
jQuery(function($){
	$('#navigation ul li:last-child, .services-tab-links ul li:last-child, #footer .nav li:last-child').addClass('last');
	
	if (window.navigator.userAgent.match(/Chrome/)) {
		$('.newsletter-form .btn').css('line-height', '20px');
	}
	
	//navigation dd
	$('#navigation ul li').hover(function(){
		$('.dd', this).show();
		if($('.dd',this).length){
			$('a:eq(0)', this).addClass('hover');
		}
	}, function() {
		$('.dd', this).hide();
		if($('.dd',this).length){
			$('a:eq(0)', this).removeClass('hover');
		}
	});
	
	$('#navigation a').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	//home slideshow
	$('#home-slideshow ul, #page-slideshow ul').jcarousel({
		auto: 5,
		scroll: 1,
		visible: 1,
		wrap: 'both',
		buttonPrevHTML: null,
		buttonNextHTML: null,
		initCallback: homeSlideshowConstruct,
		itemFirstInCallback: homeSlideshowFirstIn
	});
	
	// Counter animation
	(function() {
		var counter = $('.counter');
		var lis = counter.find('li');
		lis.each(function() {
			$(this).data('count', parseInt($(this).text()));
			$(this).html('<span>0<br /></span>');
			for (var i=1; i<=$(this).data('count'); ++i) {
				$(this).find('> span').append(i + '<br />').animate({ top: '-' + ($(this).data('count') * 65) + 'px' }, 1500);
			}
		});
	})();
	
	//recent press slider
	$('.recent-press-slider .slider ul').jcarousel({
		auto: 0,
		scroll: 3,
		visible: 3,
		wrap: 'circular',
		setupCallback: pressSliderConstruct
	});
	
	// Blinking Fields
	$('.blink')
		.focus(function() { if(this.title==this.value) { this.value = ''; } })
		.blur(function(){ if(this.value=='') {this.value = this.title; } });
	
	//services slider
	var firstRunFlag = true; 
	$('.services-tab-links ul li a').click(function(){
		var fIdx = $(this).parent().index()+1;
		if ($(this).hasClass('active')){
			$('.services-step-slider').slideUp(function(){
				obj.scroll(1);
			});
			$('.services-tab-links ul li a').removeClass('active');
		}else{
			$('.services-step-slider').slideDown(function(){
				if (firstRunFlag) {
					$('.services-step-slider ul').jcarousel({
						auto: 0,
						visible: 1,
						scroll: 1,
						buttonPrevHTML: null,
						buttonNextHTML: null,
						initCallback: function(carousel){
							obj = carousel;
							$('.services-tab-links ul li a').click(function(){
								carousel.scroll($(this).parent().index()+1);
								return false;
							});
						},
						itemFirstInCallback: {
							onAfterAnimation: function(carousel){
								if (firstRunFlag) {
									carousel.scroll(fIdx);
								}
							}
						}
					});
					firstRunFlag = false;
				}
				
			});
			$('.services-tab-links ul li a').removeClass('active');
			$(this).addClass('active');
		}
		return false;
	});
	
	//call function
	pageHeader();
	
	//maybe sliders
	(function() {
		var video_slider = $('.media .video .slider > ul').data('visible', 4);
		var photo_slider = $('.media .photos .slider > ul').data('visible', 16);
		var team_slider = $('.meet-the-team .slider > ul').data('visible', 9);
		
		var sliders = $(video_slider).add(photo_slider).add(team_slider).each(function() {
			var slider = $(this);
			var lis = slider.find('.hld li');
			if (lis.length > slider.data('visible')) {
				slider.jcarousel({
					auto: 0,
					visible: 1,
					scroll: 1,
					wrap: 'both'
				});
			}
		});
	})();
	
	$('.widget-workers .colorbox, .meet-the-team .slider ul li a.colorbox, .media .video ul .hld li a.video-colorbox').colorbox({
		inline:true,
		opacity: 0.5,
		returnFocus: false
	});
	
	$('#worker-popup .top-c ul li').live('click', function(){
		var idx = $(this).index();
		$('#worker-popup .top-c ul li').removeClass('active');
		$(this).addClass('active');
		$('#worker-popup .tab').hide();
		$('#worker-popup .tab').eq(idx).show();
	});
	
	$('.photos .slider a.colorbox').colorbox({
		opacity: 0.5,
		returnFocus: false
	});
	
	$('.gfield_radio input[type="radio"]').before('<span />');
	//call function
	customRadio();
	
	$('.gform_widget select option').each(function() {
		$(this).attr('value', $(this).attr('value').replace(/ /g, '+!+!+'));
	});
	$('.give-work-form select, .donate-form select, .gform_widget select').selectbox({ animationSpeed: 175 });
		
	$('.case-studies-col > ul li > h5 a').click(function(){
		var idx = $(this).parents('li').index();
		if($(this).parents('li').hasClass('opened')){
			$('.case-studies-col li').removeClass('opened');
			$('#content .overlay').hide();
			$('#content .overlay .tab').hide();
		}else{
			$('.case-studies-col li').removeClass('opened');
			$('#content .overlay').hide();
			$('#content .overlay .tab').hide();
			$(this).parents('li').toggleClass('opened');
			$(this).parents('#content').find('.overlay').toggle().find('.tab').eq(idx).toggle();
		}
		return false;
	});
	
	$('body.page-template-page-knowledge-php').click(function(evt) {
		if ($(evt.target).parents('#content').length == 0) {
			$('#content .case-studies-col li').removeClass('opened');
			$('#content .overlay .tab').hide();
		}
	});
	
	$('.validate').submit(function() {
		var valid = true;
		var errors = '';
		
		$(this).find('input.required').each(function() {
			if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				$(this).addClass('field-error');
				errors += 'The "' + $(this).attr('title') + '" field is required\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		$(this).find('.email').each(function() {
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			
			if (!pattern.test($(this).val()) || $(this).val() == $(this).attr('title')) {
				$(this).addClass('field-error');
				errors += 'The email address you entered is not valid\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		if (!valid) {
			alert(errors + '\n ');
		}
		
		return valid;
	});
	
	if ($('body').hasClass('page-template-page-give-work-php')) {
		$('#sidebar').height($('#content').outerHeight(true));
		console.log($('#sidebar').height());
		console.log($('#content').outerHeight(true));
	}
	
	//png fix
	if ($.browser.msie && parseInt($.browser.version) == 6) {
		DD_belatedPNG.fix('#logo a, .hiring-sticker, .black-btn, .black-btn span, #navigation .dd .t, #navigation .dd .b, #navigation .dd .m, .caption, .carousel-prev, .carousel-next, #navigation ul li ul li ul li, .jcarousel-prev, .jcarousel-next, .black-btn-2, .black-btn-2 span, .black-btn-3, .black-btn-3 span, .black-box, .counter li, .ribbon, .caption, .library .overlay .stickers li');
	}
	
	//home slideshow init callback
	function homeSlideshowConstruct(carousel){
		$('#home-slideshow li, #page-slideshow li').each(function(){
			$('#home-slideshow .nav, #page-slideshow .nav').append('<a href="#"></a>');
		});
		
		$('#home-slideshow .nav a, #page-slideshow .nav a').live('click', function(){
			$('#home-slideshow .nav a, #page-slideshow .nav a').removeClass('active');
			$(this).addClass('active');
			carousel.scroll($(this).index()+1);
			return false;
		});
	}

	//home slideshow firstIn callback
	function homeSlideshowFirstIn(carousel, li, idx){
		$('#home-slideshow .nav a, #page-slideshow .nav a').removeClass('active');
		$('#home-slideshow .nav a, #page-slideshow .nav a').eq(idx-1).addClass('active');
	}

	//press slider init callback
	function pressSliderConstruct(carousel){
		if (carousel.options.size > carousel.options.visible) {
			$('.recent-press-slider .slider').before('<a href="#" class="carousel-prev" /><a href="#" class="carousel-next" />');
			$(".recent-press-slider .carousel-prev").click(function(){
				carousel.prev();
				return false;
			});
			$(".recent-press-slider .carousel-next").click(function(){
				carousel.next();
				return false;
			});
		}
	}
	
	//page header animation
	function pageHeader(){
		var header = $('.page-header').fadeTo(0, 0.001);
		setTimeout(function() {
			header.fadeTo(500, 1);
		}, 500);
	}
	
	//custom radio buttons
	function customRadio() {
		$('.custom-radio span, .gfield_radio span').click(function() {
			var thisName = $(this).siblings('input').attr('name');
			$('.custom-radio, .gfield_radio').each(function(){ 
				if ($(this).find('input').attr('name') == thisName){
					$(this).find('span').removeClass('checked');
					$(this).find('input').attr('checked', false);
				}	
			});
			$(this).addClass('checked');
			$(this).siblings('input').attr('checked', true);
		}).siblings('label').click(function() {
			$(this).siblings('span').click();
		});
	}
	
	//tech diagram
	$('#tech-diagram > #feature-1').addClass('selected');
	$('#tech-diagram > .feature-zone').click(function(){
    var top = $('#tech-diagram').offset().top - 10;
	  $('html, body').animate({scrollTop:top}, 'fast');
		var fbox = $(this).attr('id');
		if ($(this).hasClass('selected')){
		  return false;
		} else {
  		// deactivate all other features
  		$('#tech-diagram > .feature-zone').each(function(index){
  		  $(this).removeClass('selected');
  		})
  		$('#tech-features > .feature-text').each(function(index){
  		  $(this).fadeOut('fast');
  		})
  		// activate this feature
  		$(this).addClass('selected');
  		$('#tech-features > .' + fbox).fadeIn('fast');
  		return false;
  }
	});
	
	
	
});
