/* 
Peter Petrucci.com
http://www.peterpetrucci.com

(c) James Eunson 2009
All Rights Reserved
http://www.jameseunson.com 
*/

(function($) {

var TITLE = "Peter Petrucci";
var TITLE_SEPARATOR = ">";

var tabs = {
	
	hash: null, // hash without preceding #
	
	hashPieces: [], // pieces of hash string delimited by /
	
	titlePieces: [],
	
	// Initialize tabs
	init : function()
	{			
		var self = this;

		self.clearTabs();
		self.bindNav();
		
		$.historyInit(this.changeTab);
		
		// Hide page containers
		$('div.wrapper').each(function(i){
				$(this).css('display', 'none');
		});
		
		if(window.location.hash.length == 0) { // If no address provided, homepage
			self.changeTab('#home');
		}
	},
	
	// Bind navigation hover effect, hover is disabled if nav item is 'open'
	bindNav: function()
	{
		$('#header #nav li a').hover(
			function() {
				if($(this).data('hover') != 'true' && $(this).data('open') != 'true') {
					$(this).data('hover','true')
						   .children('span.hover')
						   .fadeIn(200);							
				}
			},
			function() {
				if($(this).data('open') != 'true') {
					$(this).children('span.hover')
						   .fadeOut(200, function() {
							 $(this).parent().data('hover','false') 
							});
				}		
			}	
		).click(function(e){
			if($.browser.msie && $.browser.version < 8) {
				var hash = $(this).attr('href').replace(/^.*#/, '');
				$.historyLoad(hash);
				return false;
				
			}
		})
	},
	
	// Clears selected state on all tabs
	clearTabs: function()
	{
		$('#header #nav li a').each(function() {
			$(this).data('open','false').data('hover','false')
				.children('span.hover').hide();
		});
	},
	
	// Returns true if the provided link is internal, false otherwise - ***currently inactive
	isInternalLink : function(link)
	{
		if(link.attr('href').indexOf('/',0) != -1 && link.attr('href').substr(0,1) == '#')
			return true;
		return false;
	},
	
	// Places/splits the hash into a globally accessible variable
	processHash: function(hash)
	{
		if(hash.substr(0,1) != '#')
		{
			this.hash = hash;
		} else {
			this.hash = hash.substr(1, hash.length);
		}
		
		this.hashPieces = this.hash.split('/');
	},
	
	setTitle: function(title)
	{
		var titleString = TITLE + " " + TITLE_SEPARATOR;
		
		if(title)
		{
			document.title = titleString + " " + title;
			return;
		}
		
		var self = this;
		var ucfirst = function(word) {
			return word.substr(0,1).toUpperCase() + word.substr(1, word.length);
		};
		
		var titlePieceCount = this.hashPieces.length;
		
		// If disc specific page, pull out the page name from the h3
		if(tabs.hashPieces[1] != null) 
			tabs.hashPieces[1] = $('.wrapper.discography h3').text();
		
		$.each(this.hashPieces, function(i){
			
			titleString += " " + ucfirst(this);
			if(i < self.hashPieces.length-1)
				titleString += " " + TITLE_SEPARATOR;
		});
		
		document.title = titleString;
	},
	
	// Changes tab from one to another - workhorse function
	changeTab: function(hash)
	{
		if(hash == "" || hash == null)
			return;
		
		var self = this;
		
		tabs.processHash(hash);
		tabs.clearTabs();	
		
		// Find the corresponding nav item and 'open' it
		$('#header #nav li#' + tabs.hashPieces[0] + '-tab').find('a').data('open','true')
			.children('span.hover').show();
		
		// Display the loading indicator
		var loader = $('<div />').attr('class','content_loading');
		$('#content').append(loader);
		
		// Hide active content pane
		$('.wrapper').filter(':visible').slideUp('fast', function(e){
			$(this).hide();
		});
		
		// Show new active content pane + load content
		$('.wrapper.'+ tabs.hashPieces[0]).empty();
		$.ajax({
			url: '/' + tabs.hash,
			complete: function() {
				$('#content .content_loading')
					.fadeOut('fast', function(e){ $(this).remove() });
			},
			success: function(data) {
				$('.wrapper.'+ tabs.hashPieces[0]).html(data).slideDown('fast');
				tabs.setTitle();
				tabs.postChangeTab();
				// Find the corresponding nav item and 'open' it
				$('#header #nav li#' + tabs.hashPieces[0] + '-tab').find('a').data('open','true')
					.children('span.hover').show();			
			},
			error: function(request) {
				$.ajax({
					url: '/error',
					success: function(data) {
						$('.wrapper.error').html(data).slideDown('fast');
					}
				})
				tabs.setTitle("Page not found");				
			}
		});
	},
	
	// Loads functionality specific to a given tab
	// Custom functionality, the rest of this class is generic
	postChangeTab: function()
	{	
		var self = this;
		
		if($.browser.msie && $.browser.version < 8) {
			$('#sidebar ul#news a').each(function(i){
					$(this).click(function(e){
						var hash = $(this).attr('href').replace(/^.*#/, '');
						$.historyLoad(hash);
					})
				});
			
			$('.wrapper').filter(':visible').find('a').each(function(i){
				var link = $(this);
				
				if(link.attr('href').indexOf('/') != -1 && link.attr('href').indexOf('#') != -1)
				{
					link.click(function(e){
						var hash = link.attr('href').replace(/^.*#/, '');
						$.historyLoad(hash);
					});
				}
			});
		}
		
		switch(tabs.hashPieces[0])
		{
			case "home":
				pp.initPhotos();
				pp.initDiscs();
				break;
				
			case "discography":
				if(tabs.hashPieces[1] != null) 
				{
					pp.initDiscPage(); // disc specific url in hashPieces[1]
				} else {
					pp.initDiscography();
				} 	
				break;
				
			case "photos":
				$('.wrapper.photos .photo a').colorbox(
					{transition:'elastic', speed:500});
				break;
		}
	}
};

$(function() {
	tabs.init();
});
	
})(jQuery);
