$(document).ready(function(){

	$("a[rel=external]").attr("target","_blank");
	// $("a[href*=.pdf], a[rel=external]").attr("target","_blank");
	// $("a[href^='http:']").not("[href*='beenegarter.bitmanic.com']").attr('target','_blank');

	// $("#newsroom .entry").fitted();
	
	$('#nav_main li#nav_main-newsroom a').attr('href','http://beenegarter.com/newsroom');

	/* !Homepage Promotions */
	/* ------------------------------------------------------------------- */
	var numOfPromos = $('#promos .promo').length;
	if(numOfPromos > 1){
		$('#promos')
		.after('<p id="promo-pager">') 
		.cycle({ 
				fx:     'scrollLeft', 
				speed:  750, 
				pager:  '#promo-pager' 
		});
	}

	/* !Header Banner */
	/* ------------------------------------------------------------------- */

	var isBannerList = $('#slides.list');
	if(isBannerList){

		var mainNavLinks = $("#logo a, #nav_main li a"); // Store all links that trigger the header banner animations in a jQuery object
		// Fetch the locations of all images used in the header banner
		var bannerImages = [
'http://beenegarter.com/media/files/pages/home.jpg', 'http://beenegarter.com/media/files/pages/services1.jpg', 'http://beenegarter.com/media/files/pages/services2.jpg', 'http://beenegarter.com/media/files/pages/services3.jpg', 'http://beenegarter.com/media/files/pages/industries1.jpg', 'http://beenegarter.com/media/files/pages/industries2.jpg', 'http://beenegarter.com/media/files/pages/industries3.jpg', 'http://beenegarter.com/media/files/pages/industries4.jpg', 'http://beenegarter.com/media/files/pages/firm.jpg', 'http://beenegarter.com/media/files/pages/newsroom.jpg', 'http://beenegarter.com/media/files/pages/careers.jpg', 'http://beenegarter.com/media/files/pages/contact.jpg', 'http://beenegarter.com/media/files/pages/clients.jpg'
		];
		$(bannerImages).preload(); // Preload all banner images		
	
		if(mainNavLinks.hasClass('current')){ // If one of the main nav links has a class of 'current'...
			var initialGroup = mainNavLinks.filter('.current').first().data('group'); // Then find the current page and get its 'group' value.
		} else { // If none of the main nav links have a class of 'current'...
			var initialGroup = "home"; // Then the homepage should be currently active
		}
	
		var initialSlides = $('#slides li.'+initialGroup); // Fetch all slides in the current page's group.
		var initialSlidesCount = initialSlides.length; // Store the number of target slides that the current page has.
	
		if(initialSlidesCount>1){ // If there's more than one targeted slide...
			var randomSlide = Math.floor(Math.random()*initialSlidesCount); // Select one of the target slides randomly,
			var initialSlide = initialSlides.eq(randomSlide); // And then target that selected slide. 
		} else { // If there's only one targeted slide...
			var initialSlide = initialSlides; // Then copy the slide stored in 'targetSlides'
		}
	
		initialSlide.addClass('current initial'); // Show the current slide by assigning it as active
		var initialSlideImage = initialSlide.data('image'); // Find the current slide's image
		$('#banner-images').append('<img src="'+initialSlideImage+'" alt=""/>'); // Insert the current slide's (hidden) image into the banner
		$('#banner-images img:last-child').fadeIn(1000); // Fade the new image into full opacity.
	
		$('#slides .slide').not('.initial').css('opacity','0'); // Force opacity for IE
		
		// alert(currentGroup);
		mainNavLinks.hover( // Upon hovering over a link in the main nav...
			function(e){ 
				// e.preventDefault();
				var self = $(this); // Store 'this' as a var
				var group = self.data('group'); // Find out what group of slides this link should target
	
				var currentSlide = $('#slides li.current'); // Find the current slide
				var targetSlides = $('#slides li.'+group).not('.current'); // Fetch all slides in the hovered link's group.
	
				if(targetSlides){ // If there are any targeted slides... (hint: there won't be if the hovered link is also the current link in a group with no other links (like the contact page's group))
				
					var targetSlidesCount = targetSlides.length; // Store the number of target slides that the hovered link has.
	
					if(targetSlidesCount >= 2){ // If there's more than one targeted slide...
						var randomSlide = Math.floor(Math.random()*targetSlidesCount); // Select one of the target slides randomly,
						var targetSlide = targetSlides.eq(randomSlide); // And then target that selected slide. 
					} else if(targetSlidesCount == 1) { // If there's only one targeted slide...
						var targetSlide = targetSlides; // Then copy the slide stored in 'targetSlides'
					}
	
					var targetSlideImage = targetSlide.data('image'); // Find the target slide's image
					$('#banner-images').append('<img src="'+targetSlideImage+'" alt=""/>'); // Insert the target slide's (hidden) image into the banner
	
					var otherSlides = targetSlide.siblings(); // Find the 'other' slides - basically, all slides that are *not* the target slide
					otherSlides.stop(true,true).animate({ // Animate the other slides, if there are any, to fade their text away
						opacity: 0,
						top: '-75px'
					},100,function(){
						otherSlides.css('top','75px'); // After the text is hidden, reposition the other slides
					});
	
					currentSlide.removeClass('current'); // Remove the 'current' class from the current slide
					targetSlide.addClass('current'); // Add the 'current' class to the targetSlide
	
					currentSlide.animate({ // Animate the current slide to fade its text away
						opacity: 0,
						top: '-75px'
					},200, function(){
						currentSlide.css('top','75px'); // After the text is hidden, reposition the current slide...
						targetSlide.animate({ // And then start the target slide's animation
							opacity: 1,
							top: 0
						},400);
					});
	
					$('#banner-images img:last-child').fadeIn(500); // While all of this is going on, fade in the target slide's image
					if($('#banner-images').children().length > 4){ // Do some cleanup. If there are more than four images in the banner image div, delete some of the old ones.
						$('#banner-images img:first-child').remove();
					}
	
				}
	
			},
			function(e){e.preventDefault();}
		);
	
	}
	
	/* !Sidebar Navigation */
	/* ------------------------------------------------------------------- */
	var sidebarNav = $('ul#nav-sub');
	sidebarNav.children('li.here, li.parent-here').children('ul').show(); // Show active submenus on page load
	sidebarNav.children('li.sub-level-0').children('a').click(function(){
		var self = $(this);
		if(self.siblings('ul').length != 0){ // If this section has a subnav associated with it, toggle-fy it
			self.siblings('ul').slideToggle();
			self.parent('li').toggleClass('here parent-here');
			return false;
		}
	});
	
	/* !"Connect with an expert" section (on service pages) */
	/* ------------------------------------------------------------------- */
	
	function buildTitle(){
		var partner = $('input#partner').val();
		var topic = $('input#topic').val();
		if (topic != null){
			var title = "Inquiry for "+partner+" about "+topic;
		} else {
			var title = "Inquiry for "+partner;
		}
		$('input#title').val(title);
	}
	$('.connectexpert .expert').hover(
		function(e){
			e.preventDefault();
			$(this).find('h6 a.fancybox').fadeIn(300);
		},
		function(e){
			e.preventDefault();
			$(this).find('h6 a.fancybox').fadeOut(200);
		}
	);
	$('.connectexpert a.fancybox, .email a.fancybox').fancybox({'onComplete':buildTitle});
	
	// Delay visibility of slide text until page is loaded
	$('#banner #slides .slide p').css('visibility','visible');
	
	// Hide search box placeholder text when the box has a value
	$('input#searchbox').val('').blur(function(){
		var hasVal = $(this).val();
		(hasVal) ? $(this).addClass('has-value') : $(this).removeClass('has-value') ;
	});

});
