var SB = SB || {};

SB.Portfolio = (function (window, document) {

	var _scrollThreshold = 20,
		_initialSection = 0,
		_totalSections,
		_scrollTracker,
		_pageCount,
		_prevPageCount,
		_$sections,
		_$collapsedSection = null,
		_scrollingEnabled = true,
		_isFallback;

	var self = {
		init: function() {
			_isFallback = $('html').hasClass('fallback');
			_$sections = $('#scrollable_content > section');
			$("a[rel=external]").attr('target', '_blank');
			
			SB.SectionCarousel.init(_$sections, _initialSection);
			
			if(!_isFallback) {
				_initScrollerVars();
				_initSections();
				_bindAnchors();
				_initWindowScroll();
				_initWindowResize();
			}
			
			_initSubSectionNavs();
			$(window).hashchange();
		},
		
		switchSections: function($from, $to, forcedDeeplinkOnComplete) {
			if($from.attr('id') === $to.attr('id')) return;
			
			_scrollingEnabled = false;
			
			SB.SectionCarousel.setCurrentSection($to.attr('id'));
		
			var smallHeight = $to.css('height');
			var tallHeight = $from.css('height');
			var animTime = _isFallback ? 0 : 200;
			
			$from.animate({height: smallHeight}, animTime, 'swing', function() {
				_onCollapseComplete($(this));
			});
		
			$to.animate({height: tallHeight}, animTime, 'swing', function() {
				_onExpansionComplete($(this), forcedDeeplinkOnComplete);
			});
		},
		
		switchFallbackProject: function(id) {
			var $toLink = $('#link-' + id);
			var $toProject = $('#project-' + id);
			var $toSection = $toLink.closest('section');
			
			$toSection.find('.active').removeClass('active');
			$toLink.addClass('active');
			$toProject.addClass('active');
		},
		
		getSections: function() {
			return _$sections;
		}, 
		
		setInitialSection: function(value) {
			_initialSection = value;
			_scrollTracker = value * _scrollThreshold;
			_pageCount = _prevPageCount = value;
		}
	};

	var _initScrollerVars = function() {
		_scrollTracker = _scrollThreshold * _initialSection;
		_pageCount = _prevPageCount = _initialSection;
	}

	var _bindAnchors = function() {
		$('#scrollable_content > section h2').delegate('a', 'click', function(e) {
			e.preventDefault();

			var $parent = $(this).parent().parent();
			
			if(!$parent.hasClass('selected')) {
				self.switchSections($('.selected'), $parent, undefined);
			}

			_pageCount = _prevPageCount = $parent.data('numericIdentifier');
			_scrollTracker = _pageCount * _scrollThreshold + (_scrollThreshold / 2);
		});
	};

	var _initWindowScroll = function() {
		$('body').mousewheel(function(e, delta) {
			if(_scrollingEnabled) {
				_scrollTracker -= delta;
	
				if(_scrollTracker < 0)
					_scrollTracker = 0;
				else if(_scrollTracker > _totalSections * _scrollThreshold)
					_scrollTracker = _totalSections * _scrollThreshold;
				
				_pageCount = Math.floor(_scrollTracker/_scrollThreshold);
				if(_pageCount > _totalSections - 1) _pageCount = _totalSections - 1;
	
				if(_pageCount != _prevPageCount) {
					self.switchSections($(_$sections[_prevPageCount]),$(_$sections[_pageCount]), undefined);
					_prevPageCount = _pageCount;
				}
			}
		});
	};

	var _initWindowResize = function() {
		$(window).resize(_onWindowResize);
		
		//todo: make this run only after javascript.css has loaded
		setTimeout(_onWindowResize, 500);
	};

	var _onWindowResize = function() {
		if(_$collapsedSection == null || _$collapsedSection.hasClass('selected')) {
			_$collapsedSection = $(_$sections[0]).hasClass('selected') ? _$sections[1] : _$sections[0];
			_$collapsedSection = $(_$collapsedSection);
		}

		var collapsedHeight = _$collapsedSection.height();
		
		if(collapsedHeight < 20) collapsedHeight = 20;
		
		$('#scrollable_content section h2').css('height', collapsedHeight + 'px');
		$('#about img').css('top', '-' + collapsedHeight + 'px');
		$('.visual').css('top', -49 - collapsedHeight + 'px');
	};

	var _onCollapseComplete = function($collapsed) {
		$collapsed.removeClass('selected').css('height', '5%');	
	};

	var _onExpansionComplete = function($expanded, forcedDeeplinkOnComplete) {
		$expanded.addClass('selected').css('height', '80%');
		
		var selectedHref = $expanded.find('nav a[class="active"]').attr('href');
		
		if(selectedHref === undefined)
			location.hash = '';
		else if(forcedDeeplinkOnComplete !== undefined) {
			location.hash = '';
			location.hash = forcedDeeplinkOnComplete;
		}
		else
			location.hash = selectedHref.substr(1);
			
		_scrollingEnabled = true;
	};

	var _initSections = function() {
		_totalSections = 0;

		$.each(_$sections, function(key, value){
			$(this).data('numericIdentifier', key);
			_totalSections++;
		});
	};

	var _initSubSectionNavs = function() {
		var $subNavs = $('nav ul');
		var $subContent = $('.project_descriptions');

		$subNavs.find('li').each(function() {
			var $this = $(this);

			if(!$this.hasClass('last')) $this.append(' / ');
		});
	};

	return self;
})(this, this.document);

$(document).ready(function(){
	SB.Portfolio.init();
});
