/**
 * Scripts
 *
 * @author Ed Bynum, Matt Bainton
 * @link http://www.erwinpenland.com
 *
 */

$(function() {
/**
 * Globals
 *
 *
 */

	var animating = false;

	if($('body').hasClass('homepage')) {
		
		$('.next_button').live('click', function(e) {
			e.preventDefault();
		
			if(animating) return false;
			animating = true;
		
			var $currentSlide = $('.slide.current');
			var $nextSlide = $currentSlide.next();

			if($nextSlide.length) {
				// There is a /next/ slide to show
			
			} else {
				// No /next/ slide to show; show the first slide
				$nextSlide = $('.slide').first();
			}
		
			$currentSlide.removeClass('current');
			$nextSlide.addClass('current').css({'top':'100%'}).children().hide();
			$currentSlide.add($nextSlide).animate({'top':'-=100%'}, function() {animating = false;}).children().fadeIn(1000);
			$('#main_header').removeClass($currentSlide.attr('data-header-colorscheme')+'_colorscheme').addClass($nextSlide.attr('data-header-colorscheme')+'_colorscheme');
			$('#main_footer').removeClass($currentSlide.attr('data-header-colorscheme')+'_colorscheme').addClass($nextSlide.attr('data-header-colorscheme')+'_colorscheme');
		});

		$('.previous_button').live('click', function(e) {
			e.preventDefault();
		
			if(animating) return false;
			animating = true;

			var $currentSlide = $('.slide.current');
			var $previousSlide = $currentSlide.prev();

			if($previousSlide.length) {
				// There is a /next/ slide to show
			
			} else {
				// No /next/ slide to show; show the first slide
				$previousSlide = $('.slide').last();
			}
		
			$currentSlide.removeClass('current');
			$previousSlide.addClass('current').css({'top':'-100%'}).children().hide();
			$currentSlide.add($previousSlide).animate({'top':'+=100%'}, function() {animating = false;}).children().fadeIn(1000);
			$('#main_header').removeClass($currentSlide.attr('data-header-colorscheme')+'_colorscheme').addClass($previousSlide.attr('data-header-colorscheme')+'_colorscheme');
			$('#main_footer').removeClass($currentSlide.attr('data-header-colorscheme')+'_colorscheme').addClass($previousSlide.attr('data-header-colorscheme')+'_colorscheme');
		});
	
		// Add .current class to the already-on-homepage .slide; it will be added on slide hereafter
		$('#main .home_features .slide').addClass('current');
		
		// Load-in homepage panels
		$.get('/home/homepage_features', function(data) {
			var slideNav = [
				'<ul class="slide_controls vertical_controls">',
					'<li><a class="previous_button slide_nav" title="Go to the previous feature">Previous</a></li>',
					'<li><a class="next_button slide_nav" title="Go to the next feature">Next</a></li>',
				'</ul>'
			].join("");
		
			$('#main .home_features').append(innerShiv(data)).after(slideNav);
		});
	}
	
/**
 * Open external links in new window
 *
 *
 */
	$('html a:not(.popupwindow)').filter(function(){
		var theHref = this;
		if (theHref.hostname && theHref.hostname !== location.hostname) {
			$(theHref).not(".noAutoIcon").addClass("offSite");
			$(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13)){

					// Build tracking url
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/outgoing/' + fixedLink;

					// Track
					//pageTracker._trackPageview(fixedLink);
				};
			});
		};
	});

/**
 * Animations
 *
 *
 */
	// Add in JS animations for things where CSS can't handle it
	// if(!Modernizr.csstransitions) {
		$('html').live('click', function() {
			if($(this).has('.quick_contact.open').length) {
				$('.quick_contact').fadeOut().removeClass('open');
			}
		});
		
		$('.quick_contact').click(function(e) {
			e.stopPropagation();
		})

		$('#main_header .contact_nav a.nav_link').click(function(e) {
			// Clicked on contact nav, show the bubble
			// Not sure how to clear it
			e.preventDefault();
			
			if(e.target == this) {
				$qc = $('.quick_contact');
			
				if($qc.css('opacity') == 1) {
					// Hide the box
					// $qc.animate({'opacity':0}).removeClass('open');
					$qc.fadeIn(function() {
						$(this).addClass('open');
					})
				} else {
					// Show the box
					$qc.css('visibility','visible').animate({'opacity':1}, function() {
						$(this).addClass('open');
					});
				}
			}
		});

		$('.work_list .listing li').hover(function() {
			$('.figcaption', $(this)).delay(150).animate({'opacity':1, 'top':'0%'}, 200);
		}, function() {
			$('.figcaption', $(this)).stop().clearQueue().animate({'opacity':0.3,'top':'100%'});
		});
		
		$('.work_list .listing li').click(function(e) {
			// Show .figcaption on touch devices
			if($('.figcaption', $(this)).css('top') == '100%') {
				e.preventDefault();
				$('.figcaption', $(this)).delay(150).animate({'opacity':1, 'top':'0%'}, 200);
			}
		});
		
		$('.work_list.people .listing li').click(function(e) {
			// Show .figcaption on touch devices
			if($('.figcaption', $(this)).css('top') == '100%') {
				e.preventDefault();
				$('.figcaption', $(this)).delay(150).animate({'opacity':1, 'top':'0%'}, 200);
			}
			e.preventDefault();
		});

	// }

/**
 * Slide open the info_listing
 *
 *
 */
	$('a.entry_title').click(function(e) {
		e.preventDefault();
		var $that = $(this).parent();
		$article = $that.children('article');
		if ($that.hasClass('active')){
			
			$article.animate({'height':0}, function() {
				$that.removeClass('active');			
			});
			
		} else {

			$('.entry.active').removeClass('active').children('article').css('height', '0px');
	
			$that.addClass('active');
			
			var hash = $(this)[0].hash.substr(10);
	
			if(false && !$that.children('article').length) {
				$.get('/htmlstubs/job_positions/'+hash+'.htmlstub', function(data) {
					$that.append(data);
					$article = $that.find('article');
					$article.attr('data-open-height', $article.css('height'));
					$article.css('height',0).animate({'height':$article.attr('data-open-height')});
				
					$('.jobs_listing a.close', $article).click(function(e) {
						//e.preventDefault();
						//e.stopPropagation();
						$article.animate({'height':0});
					});
				});
			} else if(!$article.is('[data-open-height]')) {
				$article
					.show()
					.attr('data-open-height', $article.css('height'))
					.css('height',0)
					.animate({'height':$article.attr('data-open-height')});
			
				$('a.close', $article).click(function(e) {
					e.preventDefault();
					$article.animate({'height':0});
				});
			} else {
				$article.animate({'height':$article.attr('data-open-height')});
			}
		
		}
		
	});
	

/**
 * Placeholder text
 * From: https://gist.github.com/379601
 *
 */
	if(!Modernizr.input.placeholder){

		$('[placeholder]').focus(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
				input.removeClass('placeholder');
			}
		}).blur(function() {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		}).blur().parents('form').submit(function() {
			$(this).find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			})
		});
	}
	

/**
 * About page promo bar
 *
 *
 */
	if($('body').hasClass('about_page')) {
		$('.next_button').live('click', function(e) {
			e.preventDefault();

			if(animating) return false;
			animating = true;
			$slides = $('#main .slides');
			if($slides.position().left <= ('-'+(($slides.children().length - 1) * $slides.children().width()))) {
				// Last Slide
				$slides.css('left','0%').animate({'left':'-=100%'}, function() {
					animating = false;
				});
			} else {
				// Move it on over
				$slides.animate({'left':'-=100%'}, function() {
					animating = false;
				});
			}
		});

		$('.previous_button').live('click', function(e) {
			e.preventDefault();

			if(animating) return false;
			animating = true;
			$slides = $('#main .slides');
			if($slides.position().left >= 0) {
				// First Slide
				$slides.css('left',('-'+(($slides.children().length - 1) * 100) + '%')).animate({'left':'+=100%'}, function() {
					animating = false;
				});
			} else {
				// Move it on over
				$slides.animate({'left':'+=100%'}, function() {
					animating = false;
				});
			}
		});

		// Load-in promos
		$.get('/about/about_features', function(data) {
			$slideContainer = $('#main .slides');
			var currentPromo = $slideContainer.html();
			var slideNav = [
				'<ul class="slide_controls horizontal_controls">',
					'<li><a href="#" class="previous_button slide_nav" title="Previous image">Previous</a></li>',
					'<li><a href="#" class="next_button slide_nav" title="Next image">Next</a></li>',
				'</ul>'
			].join("");

			$slideContainer.append(innerShiv(data)).append(currentPromo).after(slideNav);
			slideCount = $slideContainer.children().length;
			$slideContainer.css({'width': (slideCount * 100)+'%'}).children().css('width', (100 / slideCount)+'%');
		});
	}
});
