$(document).ready(function()
{

	// -------------
	// --- STATE DEFINITIONS (treat them as constants)
	// -------------	
	
	var STATE_SPLASH		= "stateSplash";
	var STATE_ACCOMODATIONS = "stateAccommodations";
	var STATE_LOCAL			= "stateLocal";	
	var STATE_PRICING		= "statePricing";
	var STATE_RESERVATIONS	= "stateReservations";
	
	var states = [ STATE_SPLASH, STATE_ACCOMODATIONS, STATE_PRICING, STATE_RESERVATIONS ];	
	
	var SUBSTATE_ACCOMODATIONS_SLEEPING = "stateAccomSleeping";
	var SUBSTATE_ACCOMODATIONS_CRAFTING = "stateAccomCrafting";
	var SUBSTATE_ACCOMODATIONS_EATING	= "stateAccomEating";
	var SUBSTATE_ACCOMODATIONS_PHOTOS	= "stateAccomPhotos";	
	
	// -------------
	// --- INIT
	// -------------
	
	var currentState = "";	// default

	if ( initStateFromPHP != "" && stateExists( initStateFromPHP ) )
	{
		changeMainState( initStateFromPHP, false );
	}		
	
	// inject markup that we only need if there is javascript
	
	$('#CenteringContainer').append("<div class='ccc-tooltip' id='ccc-create-tooltip'></div><div class='ccc-tooltip' id='ccc-connect-tooltip'></div><div class='ccc-tooltip' id='ccc-celebrate-tooltip'></div>");	

	
	// -------------
	// --- USER INTERACTION EVENTS
	// -------------
	
	
	// --- CONNECT, CREATE, CELEBRATE
	
	$('#MainHeader ul li').click(
		function(event) 
		{
			event.preventDefault();
		}
	);	

	$('#MainHeader ul li').hover(
		function() 
		{
			var selectorCreate = "#" + String( $(this).attr('id') ) + "-tooltip"; 
			$( selectorCreate ).addClass('ccc-tooltip-hover');
		},
		function() 
		{
			var selectorCreate = "#" + String( $(this).attr('id') ) + "-tooltip"; 
			$( selectorCreate ).removeClass('ccc-tooltip-hover');
		}		
	);
	
	// --- MAIN NAVIGATION 	
	
	$('#PrimaryNav ul.primaryNav li').click(
		function(event) 
		{
			event.preventDefault();

			var newStateString = parseTitleVars( $(this).attr('title'), "state" );
			
			if ( currentState != newStateString ) 
			{
				changeMainState( newStateString );
			}
		}
	);
	
	
	// --------------------------
	// --- MAIN CONTENT STATE ---
	// --------------------------	
	
	function changeMainState( stateString, loadContent )
	{
		// sometimes we want to set the state of the app without triggering a load,
		// namely when we go directly to a certian page
		
		if ( loadContent == undefined ) loadContent = true;
		
		$('ul.primaryNav li').removeClass('selected');
		$('body').attr('id',' ');
		
		switch( stateString )
		{
		
		case STATE_SPLASH :
			// currently this is unused because of issues with
			// internet explorer 6 dynamically not updating the style cascade
			// on the background-images used in the main navigation 
			$('#MainContent div#mcSplash').css("display", "block");
			$('#PrimaryNavSplash').css("display", "block");
			$('body').attr('id','splash');
			break;
		
		
		case STATE_ACCOMODATIONS :
			
			if ( loadContent )
			{
				$("#MainContent").load( "/content/accommodations.html", null, accomdationsReady );
			}
			else
			{
				accomdationsReady();
			}
			
			$('#MainContent div#mcAccommodations').css("display", "block");
			//$('#PrimaryNav').css("display", "block");
			$('ul.primaryNav li.pnAccommodations').addClass('selected');
			break;
		
		
		case STATE_PRICING :

			if ( loadContent )
			{
				$("#MainContent").load( "/content/pricing.html", null, null );
			}			
			
			$('#MainContent div#mcPricing').css("display", "block");
			//$('#PrimaryNav').css("display", "block");
			$('ul.primaryNav li.pnPricing').addClass('selected');
			break;
		
		
		case STATE_RESERVATIONS :
		
			if ( loadContent )
			{
				$("#MainContent").load( "/content/reservations.html", null, null );
			}
		
			$('#MainContent div#mcReservations').css("display", "block");
			//$('#PrimaryNav').css("display", "block");
			$('ul.primaryNav li.pnReservations').addClass('selected');
			break;
			
			
		case STATE_LOCAL :
		
			if ( loadContent )
			{
				$("#MainContent").load( "/content/local.html", null, null );
			}
		
			$('#MainContent div#mcLocal').css("display", "block");
			//$('#PrimaryNav').css("display", "block");
			$('ul.primaryNav li.pnLocal').addClass('selected');
			break;			
		}
		
		try { 
			pageTracker._trackPageview( "/dynamic/" + stateString ); 
		} 
		catch( trackerError ) 
		{
			//alert(trackerError);
		}
		
		// Conclude by updating the current application state
		currentState = stateString;
	}


	// -------------
	// --- ACCOMODATIONS SUB-ROUTINE
	// ------------- 
	
	function accomdationsReady()
	{
		$("#mcAccommodations > div").addClass( "jsActive" );
		
		$("#mcAccommodations div a").lightBox();
		
		$("#mcAccommodations div a").prepend( '<span class="expandTab"></span>' );
		
		// Hide all but the first subcontent block
		$('#accomCrafting, #accomEating, #accomSleeping').css("display","none");
		
		// Show the nav and add hyperlinks to the markup
		$('#accommodationsNav').css( "display", "block" );
		$('#accommodationsNav li').each(function()
		{
			$(this).html( "<a href=''>" + $(this).text() + "</a>" );
		});
		
		// quick hack, select the default on accommodations init
		$('#accommodationsNav li.outsideLeft a').addClass('selected');
		
		// Add actions to the subnav
		$('#accommodationsNav li').click(function(event)
		{
			event.preventDefault();
			var newSubState = parseTitleVars( $(this).attr('title'), "state" );
			changeAccomState( newSubState );
			
			// quick hack
			$('#accommodationsNav li a').removeClass('selected'); //2e9684
			$('a', this).addClass('selected');
		});
		
		
		$('#mcAccommodations a.lightboxFrame').hover(
			function() 
			{
				$( ".expandTab", this).css( "display" , "block" );
			},
			function() 
			{
				$( ".expandTab", this).css( "display" , "none" );
			}		
		);		
		
		
		
	}

	function changeAccomState( subStateString )
	{
		$("#mcAccommodations > div").css( "display", "none" );
	
		switch( subStateString )
		{
	
		case SUBSTATE_ACCOMODATIONS_SLEEPING :
			$('#accomSleeping').css("display", "block");
			break;		
		
		case SUBSTATE_ACCOMODATIONS_CRAFTING :
			$('#accomCrafting').css("display", "block");
// 			$('ul.primaryNav li.pnAccommodations').addClass('selected');
			break;
		
		case SUBSTATE_ACCOMODATIONS_EATING :
			$('#accomEating').css("display", "block");
// 			$('ul.primaryNav li.pnAccommodations').addClass('selected');
			break;
		
		case SUBSTATE_ACCOMODATIONS_PHOTOS :
			$('#accomPhotos').css("display", "block");
			break;
		}	
	}
	
	// -------------
	// --- UTILITY FUNCTIONS
	// ------------- 
	
	function mainContentHide() 
	{
		// hide all of the main content
		//$('#MainContent div').css("display", "none");
		
		// hide both version of the main nav
		//$('#PrimaryNav').css("display", "none");
		//$('#PrimaryNavSplash').css("display", "none");
		
		// reset the nav selection
		$('ul.primaryNav li').removeClass('selected');
	}
	
	function stateExists( stateString )
	{
		var wasFound = false;
		$.each( states, function( index, item )
		{
			if ( item == stateString )
			{
				wasFound = true;
			}
		});
		return wasFound;
	}
	
	function parseTitleVars( titleString, specificVar )
	{
 		var titleVars = titleString.split("#");
		
		var returnThis;
		
		// return the value of the variable specified by "specificVar"
		if ( specificVar != undefined )
		{
			$.each( titleVars, function( index, varString )
			{			
				if ( varString.indexOf( specificVar ) > -1 )
				{
					returnThis = varString;
					return false;
				}
			});	
			return returnThis;
		}
		
		// TO IMPLEMENT
		// return an array or JSON object of all of the variables
		
		if ( specificVar == undefined )
		{
			alert( 'object return not implemented' );
		}
		
	
	}
	
});
