$(document).ready(function() {

	/*
		Slider
	*/
	
	$('.bubble[slideselector=0]').addClass('bubble-on');
	
	//find number of slides
	var numberOfSlides = $(".image-container").size();
	
	//get width of slides
	var widthOfSlides = $(".image-container:first").width();
	
	//resize sliding divs
	var totalWidth = widthOfSlides * numberOfSlides;
	$("#project-image-placeholder,#description-container").width(totalWidth);
	
	var interval;
	
	interval = setInterval(selectNextSlide, 10000);

	var playAnimation = true;
			
	//current slide
	var currentSlide = 0;
	
	//tie it all together		
	function selectSlide(index) {
		var leftPosition = - widthOfSlides * index;	
	
		$("#description-container, #project-image-placeholder").animate({
			left: leftPosition + "px"
		}, 200 );
		
		$('.bubble').removeClass('bubble-on');
		$('.bubble[slideselector=' + index + ']').addClass('bubble-on');
	}
		
	//slider control buttons
	function animationPaused() { 
		$("#startStop").addClass("pause");
	};
	
	function animationResumed() { 
		$("#startStop").removeClass("pause");
	};
	
	function animationTempPaused() {
		$("#startStop").css("opacity",".5");
	};
	
	function animationTempResumed() { 
		$("#startStop").css("opacity","1");
	};
	
	$(".bubble").click(function() {
		currentSlide = $(this).attr("slideSelector");
		selectSlide(currentSlide);
		clearInterval(interval);
		playAnimation = false;
		showCurrentSlide();
		animationPaused();
	});

	function selectNextSlide() {
		currentSlide = (currentSlide+1) % numberOfSlides;
		selectSlide(currentSlide);
	}
	
	function selectPreviousSlide() {		
		if (currentSlide == 0) {
			currentSlide = numberOfSlides - 1;
		} else {
			currentSlide--;
		}
		
		selectSlide(currentSlide);
	}

	$("#startStop").click(function() {
		playAnimation = !playAnimation;
		if (playAnimation) {
			selectNextSlide();
			interval = setInterval(selectNextSlide, 10000);
			animationResumed();
			
		} else {
			clearInterval(interval);
			animationPaused();
		}
	});	

	//hover event to reveal project info
	var animationTime = 300;
	$("#slider-base").hover(function() {
		hideNotSelectedSlides();
		$("#slider-door").stop().animate({
			left: '210px'						  
		}, animationTime, 'easeOutBack');
		clearInterval(interval);
		animationTempPaused();
	},
	function() {
		$("#slider-door").stop().animate({
			left: '0px'						  
		}, animationTime, function() {showCurrentSlide()} );
		if (playAnimation) {
			interval = setInterval(selectNextSlide, 10000), function() {  };
			animationTempResumed();
		};
	});
	
	//ensures hover event doesn't show hidden slides
	function hideNotSelectedSlides() {
		$("#project-image-placeholder").children('[slide!=' + currentSlide + ']').css("visibility","hidden");
	}
	function showCurrentSlide() {
		$("#project-image-placeholder").children().css("visibility","visible");	
	}
	

	
	//keyboard nav
	$(document).keydown(function(e){

		if (e.keyCode == 39) {  //right arrow
			selectNextSlide();
			clearInterval(interval);
			animationPaused();
	       	return false;
		
		} else if (e.keyCode == 37) { //left arrow
			selectPreviousSlide();
			clearInterval(interval);
			animationPaused();
			return false;
		}
	
	});


	//Content tab switcher
	function toggleSwitch(selectedSwitch) {
		
		//Figure out where the tab belongs
		var tabPosition = $('#tab-selector').position().left;
		var positionOfSelectedSwitch = $('#' + selectedSwitch ).position().left;
		var newTabPosition = Math.round( positionOfSelectedSwitch - tabPosition + tabPosition );
		
		//Set new tab position
		$('#tab-selector').animate({
				left: newTabPosition
			}, 150, function(){
		});
		
		//Set active state on selected tab
		$('#summary-tabs li').removeClass('selected');
		$("#" + selectedSwitch).addClass('selected');
	} 

	//Content block slider
	var currentBlock = 0;
	function selectBlock(index) {
		var widthOfBlocks = $('.imvince-content-block:first').width();
		var currentBlock = 0;
					
		var leftPosition = - widthOfBlocks * index;
		$('#contentBlockContainer').animate({
			left: leftPosition + "px"
		}, 200 );
	}

	$('.toggle-switch').click(function() {
		var selectedSwitch = $(this).parent().parent().attr("id");
		toggleSwitch(selectedSwitch);
		currentBlock = $(this).attr("blockSelector");
		selectBlock(currentBlock);			
	});



	//Social toggle
	
	function socialToggle(selectedTab) {
		
		//Figure out where the tab belongs
		var tabPosition = $('#social-tab-selector').position().left;
		var positionOfSelectedTab = $('#' + selectedTab ).position().left;
		var newTabPosition = Math.round( positionOfSelectedTab - tabPosition + tabPosition );
	
		//Set new tab position
		$('#social-tab-selector').animate({
				left: newTabPosition
			}, 150, function(){	
		});
		
		//Set active state on selected tab
		$('#social-tabs li').removeClass('selected')
		$("#" + selectedTab).addClass('selected');		
	} 

	//Content block slider
	var currentSocial = 0;
	function selectSocial(index) {
		var widthOfBlocks = $('.socialBlock').width();
		var currentBlock = 0;
					
		var leftPosition = - widthOfBlocks * index;
		$('#socialContainer').animate({
			left: leftPosition + "px"
		}, 200 );
	}
	
	
	$('.social-toggle').click(function() {
		var selectedTab = $(this).parent().parent().attr("id");
		socialToggle(selectedTab);	
		currentSocial = $(this).attr("socialSelector");
		selectSocial(currentSocial);			
				
	});





}); //doc ready
	
















