// handle the active state on the top-most navigation bar
if (AE.pathSegments[1] == 'gray-ice-builders') {
  var activeBtn = jQuery('#gray-sites-nav-2').css('background-image').replace('_off', '_on');
  jQuery('#gray-sites-nav-2').css('background-image', activeBtn);
}
else if (AE.pathSegments[1] == 'ws-construction') {
  var activeBtn = jQuery('#gray-sites-nav-3').css('background-image').replace('_off', '_on');
  jQuery('#gray-sites-nav-3').css('background-image', activeBtn);
}

else if (AE.pathSegments[1] != 'introduction' && AE.pathSegments[1] != 'food-intro' && AE.pathSegments[1] != 'food-intro2' && AE.pathSegments[1] != 'se-food-intro' && AE.pathSegments[1] != 'datacenter' && AE.pathSegments[1] != 'jones-lang-lasalle-event') {
  var activeBtn = jQuery('#gray-sites-nav-1').css('background-image').replace('_off', '_on');
  jQuery('#gray-sites-nav-1').css('background-image', activeBtn);
}

// printable projects
if (AE.pathSegments[1] == 'projects' || AE.pathSegments[1] == 'green-building') {
  jQuery(document).bind('projectImageLoaded', function() {
    var bioContent = jQuery('div.view-id-sidebar_team_members .view-content').html();
    jQuery('#team-leaders-print').html(bioContent);
  });
}

if (AE.pathSegments[1] == 'gray-ice-builders' && AE.pathSegments[2] == 'projects') {
  jQuery(document).bind('projectImageLoaded', function() {
    jQuery('#team-leaders-print #top').html(jQuery('#bob-moore').html());
    jQuery('#team-leaders-print #bottom').html(jQuery('#ice-contact-print').html());
  });
}

if (AE.pathSegments[1] == 'ws-construction' && AE.pathSegments[2] == 'projects') {
  jQuery(document).bind('projectImageLoaded', function() {
    jQuery('#team-leaders-print #top').html(jQuery('#steve-renshaw').html());
    jQuery('#team-leaders-print #bottom').html(jQuery('#ws-contact-print').html());
  });
}

jQuery(document).ready(function() {
	
	jQuery('.subsvendors-dropdown h2').click(function() {
		jQuery(this).siblings('.collapsed').slideToggle();
	});
	
	//Fancybox popup window for Contact forms.
	
	jQuery("a.fancybox-load").fancybox({
		onClosed: function() {
			jQuery(document).trigger('fancybox-closed');
		}
	
	});


	jQuery('#market-contact-form').submit(function(e) {
		e.preventDefault();
	
		var selected_inputs = jQuery('#market-contact-form input[type=text], #market-contact-form textarea, #market-contact-form input[name=form-name], #market-contact-form input[name=market]');
	
		if (AE.pathSegments[1] == 'datacenter') { 
			var target = '/ajax/datacenter-contact-form'; 
		} 
		else if (AE.pathSegments[1] == 'news') {
			selected_inputs = jQuery('.email-article-form input, #market-contact-form textarea');
			var target = '/ajax/email-article-form';
		}
		else { 
			var target = '/ajax/project-contact-form'; 
		}
		
		jQuery.post(target, selected_inputs.serialize(), function(data) {
			if (data['errors'] == '') {
				jQuery('#market-contact-form form').hide().get(0).reset();
				jQuery('#market-contact-form #success-message').show();
			} else {
				jQuery.each(data['errors'], function(index, val) {
					var input_field = jQuery('#market-contact-form .required[name=' + val + ']');
					if(input_field.length > 0){
						input_field.addClass('failed').siblings('span').html(input_field.attr('data-error'));
					}
				});
	
				for (var i = 0 in data['errors']) {
					AE.console.print(data['errors'][i]);
					
				}	
				selected_inputs.focus(function() {
					jQuery(this).removeClass('failed').siblings('span').html('');
				});
			}
		}, 'json');
		
	});
	
	jQuery(document).bind('fancybox-closed' , function() {
		jQuery('#market-contact-form #success-message').hide();
		jQuery('#market-contact-form form').show();
	});

	
});
