$(function(){

		
	//execute on page load
	var close_timer = false;
	var slide_ok = true;
	
	//use sliding animation for footer dropdown menu
	$('footer.main ul.nav').hover(function() {
		if (close_timer) return clearTimeout(close_timer);
		$('footer ul.nav ul').hide();
		if (slide_ok) {
			slide_ok = false;
			$('footer ul.nav > li > ul').slideDown('slow', function(){ slide_ok = true; });
		}
	}, function() {
		close_timer = setTimeout(footerClose, 1000);
	});

	//fade the home banner tagline, & set up the sliding info tabs
	$('div.banner').prepend('<div class="banner-cover" />');
	
	$('.tile > a').find('.wrapper').each(function() {
		var infoTabOffset = -($(this).outerWidth() - 15);
		$(this).hide().css('right', infoTabOffset);
	});

	$(window).load( function() {
		$('body.home .banner-tagline').delay(2500).fadeOut('slow', function() {
			$('div.banner-cover').remove();
			$('.tile > a').find('.wrapper').fadeIn();
		});
	});	
		
	
	//sliding filters on portfolio page
	$('div.filter ul li').hover(
		function() {
			$(this).find('span.title').addClass('show');
		},
		function() {
			if (!$(this).hasClass('selected')) $(this).find('span.title').removeClass('show');
		}).click(function(){
			$(this).toggleClass('selected');
			
			//get types currently selected
			var selected_types = new Array();
			$(this).parent().children('.selected').each(function(){ selected_types[selected_types.length] = $(this).attr('data-id'); });
			//console.log(selected_types);
			
			var len = selected_types.length;
			if (!len) {
				//show all
				$('ul.map li').show();
			} else {
				//loop through dots and either show or hide depending on what's selected
				$('ul.map li').each(function(){
					if (!$(this).attr('data-fuel-types')) { 
						$(this).hide();
					} else {
						$(this).hide();
						var fuel_types = $(this).attr('data-fuel-types').split('|');
						if (!fuel_types.length) {
							$(this).hide();
						} else {
							for (var i = 0; i < fuel_types.length; i++) {
								if (inArray(fuel_types[i], selected_types)) {
									$(this).show();
								}
							}
						}
					}
				});
			}
		});

	


	//location-based
	$('body.portfolio ul.map li a').mouseover(function(){
		$('ul.side li').hide();
		$('ul.side li.' + $(this).attr('class')).show();
	});


	function footerClose() {
		$('footer ul.nav > li > ul').slideUp();
		close_timer = false;
	}
	
	function inArray(needle, haystack) {
		var length = haystack.length;
	    for(var i = 0; i < length; i++) if (haystack[i] == needle) return true;
	    return false;
	}
	
	
	
	// History Module
	// init
	$('.history-mod .year-slides > li h2').addClass('hidden');
	
	var currentYear,
		selectedYear;
	
	$('.history-mod .year-nav li:last').addClass('selected');
	
	currentYear = $('.history-mod .year-slides > li:last');
	$(currentYear).css('z-index', 50).addClass('current');
	
	// selecting a year
	$('.history-mod .year-nav a').click(function (e){
		e.preventDefault();
		
		$(this).parent().addClass('selected').siblings().removeClass('selected');
			
		var selectedYear = $(this).attr('href');
				

		if(!$(selectedYear).hasClass('current')) {
			$(currentYear).removeClass('current');
			currentYear = $(selectedYear);
			
			$(selectedYear).hide().addClass('current').css('z-index', 100).fadeIn(300, function() {
				
				$(this).siblings().css('z-index', '');
				$(this).css('z-index', 50);
			});
			
		}
		
	});	



	//home page banner sliding animation				
	var $banner = $('div.banner'),
		$tileSet = $('div.tile-container'),
		$tileCount = $('div.tile-collection').length,
		$singleWidth = $('div.tile-collection').outerWidth(),
		 
		tileContent,  
		movementInt,
		movementDirection = 0,
		movementSpeed = 5,
		currentMarginLeft;
		
	
	//Sliding Info boxes for tile images
	$banner.delegate('.tile a', 'hover', function(e) {
		var infoTabOffset = -($(this).find('.wrapper').outerWidth() - 15);
		    if (e.type === 'mouseenter') {
				$('div.info > .wrapper', this).stop().animate({
					right : 0
				}, 400);
		    } else {
				$('div.info > .wrapper', this).stop().animate({
					right : infoTabOffset
				}, 400);
		    }
	});
	

	//if no CSS Transition support, use jQuery hover on banner tile info
/*
	if ($('html').hasClass('no-csstransitions') && !$('html').hasClass('ie7')) {
		
		$('div.tile div.info').css({'opacity': 1, 'visibility': 'visible'}).hide();
	
		$banner.delegate('.tile a', 'hover', function(e) {
		    if (e.type === 'mouseenter') {
				$('div.info', this).fadeIn();
		    } else {
				$('div.info', this).fadeOut();
		    }
		});	
	}
*/

	
	/* TASKS */
		
	// 1. Wrap the tile container in a div
	$tileSet.wrap('<div class="slider clearfix" />');
	$slider = $('div.slider');
	
	
	// 2. add clones of tile sets to the beginning and end
	tileContent = $slider.html();
	
	$tileSet.before(tileContent);
	$tileSet.after(tileContent);
	
	
	// 3. set width on tile containers 
	$tileSet = $('div.tile-container');
	$tileSet.css('width', $singleWidth * $tileCount);
	
	
	// 4. set width on slider
	$slider.css('width', $tileSet.outerWidth() * $tileSet.length);
	
	
	// 5. reset slider to main tile set
	$slider.css('marginLeft', -($tileSet.outerWidth()));
	
	
	// 6. add movement button links, and functionality for these buttons
	$banner.append('<div class="trigger left"><span class="arrow"></span></div><div class="trigger right"><span class="arrow"></span></div>');
	$('div.trigger').hover(
		function() {
			if ($(this).hasClass('left')) {
				movementDirection = 1;
			} else {
				movementDirection = -1;
			}
			movementInt = setInterval(moveBanner, 30);
		}, 
		function() {
			clearInterval(movementInt);
			movementDirection = 0;
		}
	);	

	// 7. if clicking on an iframe video from Vimeo, set click on $slider after hitting play
	//$('div.tile').click(function() {
	//	console.log('tile clicked');
	//});		
	
	function addClone(thisEnd) {
		if(thisEnd == 'left') {
			$tileSet.filter(':first').before(tileContent);
			$tileSet = $('div.tile-container').css('width', $singleWidth * $tileCount);
			currentMarginLeft = currentMarginLeft - $tileSet.outerWidth();
		} else if (thisEnd == 'right') {
			$tileSet.filter(':last').after(tileContent);
			$tileSet = $('div.tile-container').css('width', $singleWidth * $tileCount);
			currentMarginLeft = currentMarginLeft + $tileSet.outerWidth();
		}
		
		$tileSet = $('div.tile-container');
		$slider.css('width', $tileSet.outerWidth() * $tileSet.length);
		
		
	}
	
	function moveBanner() {
		currentMarginLeft = parseInt($slider.css('marginLeft'));
		
		if (currentMarginLeft >= -200) {
			addClone('left');
		} else if (currentMarginLeft <= -($slider.outerWidth() - $(window).width()) + 200) {
			addClone('right');
		}
				
		/*		
		$slider.stop().animate({
				marginLeft : currentMarginLeft += (movementDirection * movementSpeed)
		}, 95);
		*/
		
		$slider.css({
			marginLeft : currentMarginLeft += (movementDirection * movementSpeed)
		});
	}	
	
	
	
});
