$(document).ready(function() {
	// IE CSS fixes
	if ($.browser.msie) {
		$('#main #slider .slide:first-child').css('display', 'block');
		$('#main .team_accordion .category .team_member:first-child').css({ 'border-top': '0 none', 'padding-top': '0' });
		$('#main .pages_breakdown .page:first-child').css('border-top', '1px solid #e4e4e4');
	}
	
	// Image preloading
	function preload(sources) {
		var images = [];
		for (i = 0; i < sources.length; ++i) {
			images[i] = new Image();
			images[i].src = sources[i];
		}
	}
	
	// home slider
	(function($) {
		$.fn.slider = function() {
			var $this = $(this);
			var $slides = $this.children('#slides').children();
			var timer = 0;
			
			$this.find('h3').text($($slides[0]).attr('data-heading'));
			$this.find('p').text($($slides[0]).attr('data-text'));
			
			$slides.each(function(index) {
				$this.find('#slider_navigation ul').append('<li><a href="#" rel="' + index + '"></a></li>');
			});
			
			$this.find('#slider_navigation ul li:first-child').addClass('active');
			
			$this.children('.navigation_left, .navigation_right').click($.debounce(250, function() {
				loopSlide($(this).attr('data-direction'));
			})).click(function(event) {
				event.preventDefault();
			});
			
			function loopSlide(direction) {
				window.clearTimeout(timer);
				
				var slide = $this.find('.slide.active');
				slide.fadeOut(500);
				$slides.removeClass('active');
				
				if (direction == undefined || direction == 1) {
					slide = slide.next();
					if (slide.length === 0) {
						slide = $($slides[0]);
					}
				} else if (direction == -1) { 
					slide = slide.prev();
					if (slide.length === 0) {
						slide = $($slides[$slides.length - 1])
					}
				}
				
				slide.addClass('active');
				
				$this.find('#slider_navigation ul li').removeClass('active');					
				$this.find('#slider_navigation ul li a[rel="' + $.inArray(slide[0], $slides) + '"]').parent().addClass('active');
				
				$this.find('h3').text(slide.attr('data-heading'));
				$this.find('p').text(slide.attr('data-text'));
				
				slide.fadeIn(500, function() {
					timer = window.setTimeout(loopSlide, 4500, undefined);
				});
			}
			
			timer = window.setTimeout(loopSlide, 4500, undefined);
		};
	})(jQuery);
	$('.home #slider').slider();
	
	// Tombstone Slider
	(function($) {
		$.fn.tombstoneSlider = function() {
			var $this = $(this);
			
			var sources = Array();
			$this.find('img').each(function(index) {
				sources[index] = $(this).attr('src');
			});
			preload(sources);
			
			function loopSlide() {
				var $active_slide = $this.find('.active');
				$active_slide.fadeOut(500).css('z-index', '1').removeClass('active');
				
				$active_slide = $active_slide.next();
				if ($active_slide.length == 0) {
					$active_slide = $($this.find('.slide')[0]);
				}
				
				$active_slide.addClass('active').css('z-index', '50').fadeIn(500, function() {
					window.setTimeout(loopSlide, 4000);	
				});
			}
			
			window.setTimeout(loopSlide, 4000);
		}
	})(jQuery);
	$('.tombstone_slider').tombstoneSlider();
	
	
	// Client Slider
	(function($) {
		$.fn.clientSlider = function() {
			var $this = $(this);
			
			var $slides = $this.find('.slide');
			
			function loopSlide() {
				// Pick three new logos to fade in
				$slides.filter('.active').each(function() {
					var $slide = $(this);
					
					var index = $.inArray($slide[0], $slides);
					var newIndex = index + 3;
					
					if (!$slides[newIndex]) {
						newIndex = newIndex - $slides.length;
					}
					
					$slide.fadeOut(300, function() {
						$(this).removeClass('active');
						$($slides[newIndex]).fadeIn(300, function() {
							$(this).addClass('active');
						});
					});
				});
				
				window.setTimeout(loopSlide, 3000);
			}
			
			window.setTimeout(loopSlide, 3000);
		}
	})(jQuery);
	$('#client_slider').clientSlider();
	
	// our-firm/team accordion
	(function($) {
		$.fn.team_accordion = function() {
			var $categories = $(this).children('.category');
			
			$categories.each(function() {
				$(this).find('.team_members').each(function() {
					$(this).data('original_height', $(this).outerHeight());
					$(this).height(0);
				});
			});
			
			$categories.click(function(event) {
				var $this = $(this);
				var $team_members = $this.find('.team_members');
				
				$categories.removeClass('category_open');
				
				if ($.browser.msie) {
					// Use jQuery animations
					if ($team_members.height() != 0) {
						// We're clicking an open category, close it
						$categories.find('.team_members').stop(true, true).animate({ height: 0 }, { duration: 300, queue: false });
					} else {
						// We're clicking a new category, close the rest
						$categories.find('.team_members').stop(true, true).animate({ height: 0 }, { duration: 300, queue: false });	
						$team_members.stop(true, true).animate({ height: $team_members.data('original_height') }, { duration: 300, queue: false });
						$this.addClass('category_open');
					}
				} else {
					// Use CSS3 transitions
					if ($team_members.height() != 0) {
						// We're clicking an open category, close it
						$categories.find('.team_members').height(0);
					} else {
						// We're clicking a new category, close the rest
						$categories.find('.team_members').height(0);
						$team_members.height($team_members.data('original_height'));
						$this.addClass('category_open');
					}	
				}
				
				event.preventDefault();
			});
		}
	})(jQuery);
	$('.team_accordion').team_accordion();
	
	// contact-us map
	if ($('#map_canvas').length > 0) {
		var latlng = new google.maps.LatLng(42.366402, -71.074864);
		var options = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById('map_canvas'), options);
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title: "CSP Associates"
		});
	}
	
	// Careers full description
	$('#careers .open_description').click(function(event) {
		$(this).parent().next('.full_description').slideToggle();
		event.preventDefault();
	});
	
	// Google Analytics
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-25710893-1']);
	_gaq.push(['_trackPageview']);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
});
