// Section transitions
var s = {
	zSubnav: 11,
	
	// if there's an anchor be sure to parse and update properly.
	cur: function() {
	//	alert($('body')[0].section);
		return $('body').attr('section');
		
		/*for (i in n.sects)
			if ($('body').hasClass(n.sects[i]))
				return n.sects[i];*/
	},
	
	// Array of main sections
	list: ['home', 'portfolio', 'about', 'services', 'contact'],

	// Is sect a main section?
	is: function(sect) {
		return $.inArray(sect, s.list) > -1;
	},

	// What's a subpage of what?
	sub: {
		// Is sect a subsection?
		is: function(sect) {
			for (key in s.sub.list)
				if (key == sect) return true;
				
			return false;
		},
		
		// Subsection: Main section it belongs to
		list: {
			'details_noisefreak': 'portfolio',
			'details_girlgamer': 'portfolio'
		}
	},

	
	
	// Section switch
	sswitch: function(newSection) {
			//mainSection = s.sub[newSection];

		//alert('!');
		
		// If switching to or from a subsection...
		if (typeof(s.sub[s.cur()]) != 'undefined' || typeof(s.sub[newSection]) != 'undefined')
			s.switchAAA(newSection);
		else
			s.switchNormal(newSection);

		// Change the body attribute to reflect the new section.
		$('body').attr('section', newSection);

	
		
	},
	
	
	switchAAA: function(newSection) {
		var info = {
			intro: false,
			newId:	newSection,
			curId: s.cur()
		};

		// Don't go anywhere if that's where you are already!
		if (info.curId == info.newId) return false;

		
		// Speed depends on how far away
	//	info.coeff = (1 + (Math.abs(n.blips[info.newId][0] - n.blips[info.curId][0]) - 1) * n.anim.prettyCoeff);
		info.coeff = (1 + (Math.abs(n.sects[info.newId].index - n.sects[info.curId].index) - 1) * n.anim.prettyCoeff);
		
		
		
		animate.close(info, function() {
			$('#header').css('display', 
				typeof(s.sub[newSection]) != 'undefined' ?
				'none' :
				'block'
			);
			$('.c-section').css('display', 'none');
			
			$('.content_' + newSection).css('display' ,'block');
			
			animate.open(info);	
		});
	/*
		pseudocode: 
		
			figure out the main section.
			if that section's subnav is showing, all's well.
			
			if it isn't, then
				fade in that subnav after z-indexing it to the top
					[in 3a, this means to do it as opening up; look at the timings for the intro anim]
					[in normal, this means to slowly fade it in after the other animations are complete.]
			
	*/
		
		setTimeout(function() {
			animate.line(info);
			animate.navBlip(info);
		}, n.anim.timeout);
	},
	
	switchNormal: function(newSection) {

		var info = {
			intro: false,
			newId:	newSection,
			curId: s.cur()
		};

		// Don't go anywhere if that's where you are already!
		if (info.curId == info.newId) return false;
		
		// Speed depends on how far away
		info.coeff = (1 + (Math.abs(n.sects[info.newId].index - n.sects[info.curId].index) - 1) * n.anim.prettyCoeff);

		animate.openSesame(function() {
			loadSect(info.newId);
			
			var waitForLoad = setInterval(function() {
				if ($('.c-section[section=' + info.newId + ']').length == 0) return;

				setTimeout(function() {
					$('#header').css('display', 'block');
					
					//console.log(info.newId);
					
					$('.c-section').not('.c-section[section=' + info.newId + ']').css('display', 'none');
					
					$('.c-section[section=' + info.newId + ']').css('display', 'block');
					
					animate.closeSesame(function() {
				//		alert('!!!');
			//			s.subnav(info.newId);
					});
				
				}, 100);
				clearInterval(waitForLoad);
			}, 100);

			
			//!!!
			//TODO: make it show the new one
			
			//s.sub.list(info.newId);				


			
		}); 

		setTimeout(function() {
			animate.line(info);
			animate.navBlip(info);
			animate.heading(info); 
		}, n.anim.timeout);
	},
	
	subnav: function(sect) {
		$(
			($('.sc_' + sect).length > 0) ?
				'.sc_' + sect:
				'.subnav_coverup'
		).not(
			$('.subnav_coverup').css('z-index') == s.zSubnav - 1 ?
				'.subnav_coverup' :
				'.porpoise'
		).css({ 
				
			'display': 'block',
			'opacity': 0,
			'z-index': s.zSubnav++
		}).stop().animate({
			'opacity': 1
		}, 1000, 'easeOutExpo');
	
	}
};
