/*
 * Copyright (c) 2009 Michael Pratt
 * Licensed under GPL3.0 (http://opensource.org)
 */
 
$(function(){
	var pageTitle = 'Michael Pratt - Entertainment Design', height_mod = 500;
	
	////////////////////////////////////////////////////
	// Change tabs
	$.extend({
		'changeTab' : function(tab) {
			//Retract any currently displayed tab
			$('.section-displayed').retractSection();
			
			document.title = pageTitle;
			
			if(tab != '')
				window.location.hash = tab;
			
			if (tab == '#' || tab == '')
			//	window.location.hash = "#";
				return;
			
			$(tab).css({'top' : '-' + ($(tab).height() + height_mod) + "px", 'z-index' : '100'}).animate({top: '0px'}, 1000, 'swing').addClass('section-displayed');
		}
	});
	
	///////////////////////////////////////////////////
	// Define some section handling functions
	$.fn.extend({
		'retractSection' : function(){ 
			return this.each(function(){ 
				var el = $(this); 
				el.animate({'top' : "-" + (el.height() + height_mod)}, 1000).css('z-index', 50).removeClass('section-displayed'); 
			});
		} 
	});
	
	//////////////////////////////////////////////////
	// Following code deals with the section expanding/contracting
	$('.section')
		.find('h1.expandable').append(' <span class="section-expandall">- Contract All -</span>').end()
		.find('h2').append(' <span class="section-expand">- Contract -</span>');
	
	$('.section-expand').click(function() {
		var el=$(this), dir = 'Up', collapse='section-collapsed', text='+ Expand +', state='add';
		
		if(el.hasClass(collapse)) {
			text='- Contract -';
			dir='Down';
			state = 'remove';
		}
		
		el.text(text)[state + 'Class'](collapse).parent().next('.section-box')['slide' + dir]();
	});
	
	$('.section-expandall').click(function(){
		var el=$(this), dir = 'Up', collapse='section-collapsed', text='+ Expand All +', state='add', text2='+ Expand +';
			
		if(el.hasClass(collapse)) {
			text='- Contract All -';
			text2='- Contract -';
			dir='Down';
			state = 'remove';
		}
		
		el.text(text)[state + 'Class'](collapse)
			.parent().nextAll('.section-box')['slide' + dir]().end()
			.parent().find('h2 .section-expand').text(text2)[state + 'Class'](collapse);
			
	});
	
	gotoSection(window.location.hash);
});

// Wrapper for changeTab, in the global namespace for ease of access by ActionScript ExternalInterface.
function gotoSection(tab) 
{	
	$.changeTab(tab);
};