
	var currentSection = '';
	var currentNavSection = '';
	var nextSection = '';
	var transitioning = false;
	var subNavSections = {
		menu: [ 'food', 'winelist', 'specialtydrinks' ]
	};
	function showSection( section, firstLoad )
	{
		if( subContentShowing )
    {
			nextSection = section;
      func = function(){ showSection( nextSection ); nextSection = ''; };
      hideSubContent( func );
      return;
    }
    
		if( typeof( pageTracker ) != 'undefined' )
		{
			pageTracker._trackPageview( '/' + section + '/' );
		}
		
    if( $( 'content_' + section ) && currentSection != section && !transitioning && ( isWindowLoaded || firstLoad ) )
		{
			if( $( 'content_' + currentSection ) ) $( 'content_' + currentSection ).hide();
			if( $( 'nav_' + currentSection ) ) $( 'nav_' + currentNavSection ).removeClassName( 'cur' );

			nav_section = getParentNav( section ) == false ? section : getParentNav( section );
			if( $( 'nav_' + nav_section ) ) $( 'nav_' + nav_section ).addClassName( 'cur' );
			
			transitioning = true;
			nextSection = 'content_' + section;
			prevSection = 'content_' + currentSection;
			nextSectionImage = 'content_image_' + section;
			prevSectionImage = 'content_image_' + currentSection;
			[ 'nextSection', 'prevSection', 'nextSectionImage', 'prevSectionImage' ].each( function( e ){ eval( e + " = $(" + e + ") ? " + e + " : 'dummy'" ) } );
			
			afterFinish = function()
						{ 
							transitioning = false; 
							currentSection = ''; 
							if( section == 'calendar' ) updateCalendar();
							currentSection = section;
							currentNavSection = nav_section;
						}
			;
			
			if( firstLoad !== true )
			{
		  	new Effect.Parallel(
		     	[
		       	new Effect.Appear( nextSection ),
		       	new Effect.Appear( nextSectionImage ),
		        new Effect.Fade( prevSection ),
		        new Effect.Fade( prevSectionImage )
		      ] ,
					{
						duration: .5, 
						afterFinish: afterFinish
					}
				);
			}
			else
			{
				$( nextSection ).show();
				$( nextSectionImage ).show();
				afterFinish();
			}
		}
	}
	
	var subContentShowing = false;
	function showSubContent( content )
	{
		if ( content == '' )
		{
			return;
		}
		
		$('sub_content').update( content );
		
		transitioning = true;
		Effect.Appear(
			'sub_content_container',
			{
				duration: .5,
				afterFinish: function(){ transitioning = false; myLightbox.initialize('sub_content_container'); }
			}
		);
		subContentShowing = true;
	}

	function hideSubContent( afterFinish )
	{
		transitioning = true;
		Effect.Fade(
			'sub_content_container',
			{
				duration: .5,
				afterFinish: function(){ subContentShowing = false; transitioning = false; if( typeof( afterFinish ) == 'function' ){ afterFinish(); } }
			}
		);
	}

  function closeContentContainer()
  {
		if( transitioning ) return; // protects from accidentally transitioning 2 things at once

		if ( subContentShowing == true) 
		{
			transitioning = true;
	  	new Effect.Fade( 
				'sub_content_container', 
				{ 
					duration: .5,
					afterFinish: function(){ transitioning = false; subContentShowing = false; }
				} 
			);
		}
  }
	
	function showPressItem( pressID )
	{
		content = '';
		Element.show('ajaxLoading');
		new Ajax.Request(
			'/content/press_item.php',
			{
				method: 'get',
				parameters: 'pressID=' + pressID,
				onFailure: function()
				{
					alert('There was an error retrieving the content of the selected press item')
				},
				onSuccess: function(transport)
				{
					showSubContent( transport.responseText );
				},
				onComplete: function()
				{
					Element.hide('ajaxLoading');
				}
			}
		);
	}
	
	function showEvent( eventID )
	{
		content = '';
		Element.show('ajaxLoading');
		new Ajax.Request(
			'/content/event.php',
			{
				method: 'get',
				parameters: 'eventID=' + eventID,
				onFailure: function()
				{
					alert('There was an error retrieving the details for the selected event')
				},
				onSuccess: function(transport)
				{
					showSubContent( transport.responseText );
				},
				onComplete: function()
				{
					Element.hide('ajaxLoading');
				}
			}
		);
	}

	/*******************************************************************************
	Photo Gallery Functions
	*******************************************************************************/

  function pg_toggleYear(year){
    e = 'pg_year_' + year;
    e_t = e + '_toggle';
    if(!$(e) || transitioning) return;
    transitioning = true;
    if($(e).style.display == 'none'){
      Element.addClassName(e_t,'down');
    } else Element.removeClassName(e_t,'down');
    
    //$(e).style.display = $(e).style.display == 'none' ? '' : 'none';
    
    if($(e).style.display == 'none'){
    	new Effect.Parallel(
       	[
          new Effect.BlindDown(e, {duration: .5 }),
         	new Effect.Appear(e, { to: 1.0, from: 0.0, duration: .5, transition: Effect.Transitions.linear } ) 
        ]
        ,{afterFinish: function(){transitioning=false;}});
      
    } else{
    	new Effect.Parallel(
       	[
          new Effect.BlindUp(e, {duration: .5 }),
         	new Effect.Fade(e, { to: 0.0, from: 1.0, duration: .5, transition: Effect.Transitions.linear } ) 
        ]
        ,{afterFinish: function(){transitioning=false;}});
    }
    
  }
	
  function pg_toggleMonth(month){
    e = 'pg_month_' + month;
    e_t = e + '_toggle';
    if(!$(e) || transitioning) return;
    transitioning = true;
    if($(e).style.display == 'none'){
      Element.addClassName(e_t,'down');
    } else Element.removeClassName(e_t,'down');
    
    //$(e).style.display = $(e).style.display == 'none' ? '' : 'none';
    
    if($(e).style.display == 'none'){
    	new Effect.Parallel(
       	[
          new Effect.BlindDown(e, {duration: .5 }),
         	new Effect.Appear(e, { sync: true, to: 1.0, from: 0.0, duration: .5, transition: Effect.Transitions.linear } ) 
        ]
        ,{afterFinish: function(){transitioning=false;}});
      
    } else{
    	new Effect.Parallel(
       	[
          new Effect.BlindUp(e, {duration: .5 }),
         	new Effect.Fade(e, { sync: true, to: 0.0, from: 1.0, duration: .5, transition: Effect.Transitions.linear } ) 
        ]
        ,{afterFinish: function(){transitioning=false;}});
    }

  }  
	
	function showPhotoGallery( galleryID, pageNum )
	{
		content = '';
		Element.show('ajaxLoading');
		new Ajax.Request(
			'/content/photo_gallery.php',
			{
				method: 'get',
				parameters: 'galleryID=' + galleryID + '&pageNum=' + pageNum,
				onFailure: function()
				{
					alert('There was an error retrieving the details for the selected event')
				},
				onSuccess: function(transport)
				{
					showSubContent( transport.responseText );
				},
				onComplete: function()
				{
					Element.hide('ajaxLoading');
				}
			}
		);
	}

	/*******************************************************************************
	Send Message Functions
	*******************************************************************************/

  var sendingMessage = false;
	function sendMessage(){
		if(sendingMessage == true) return;
    
		//Validation
		if($F('message_name') == "") {
			$('message_name').focus();
			alert('Your name is required before submission');
			return;
		}

		if($F('message_email') == "") {
			$('message_email').focus();
			alert('Your email is required before submission');
			return;
		}

		if($F('message_body') == "") {
			$('message_body').focus();
			alert('Body is required before submission');
			return;
		}
		
    sendingMessage = true;
		var url = 'general_ajax_functions.php'
		var pars = 'name=' + $F('message_name') + '&email=' + $F('message_email') + '&body=' + $F('message_body') + "&cmd=sendMessage";
    
    Element.show('ajaxLoading');
		myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars,
				onComplete: processSendMessageResponse
			}
		);	
	}

  var response;
  function processSendMessageResponse(R){
    Element.hide('ajaxLoading');
    sendingMessage = false;

    eval("response = " + R.responseText);
    
    if(response.success){
      Element.hide('form_sendMessage');
      Element.show('messageSent');
    } else {
      alert(response.message);
    }
  }
  
	/*******************************************************************************
	Make Reservation Functions
	*******************************************************************************/

  var makingReservation = false;
	function makeReservation(){
		if(makingReservation == true) return;
    
		//Validation
		if($F('reservation_name') == "") {
			$('reservation_name').focus();
			alert('Your name is required before submission');
			return;
		}

		if($F('reservation_email') == "") {
			$('reservation_email').focus();
			alert('Your email is required before submission');
			return;
		}

		if($F('reservation_date') == "") {
			$('reservation_date').focus();
			alert('Date is required before submission');
			return;
		}
		
    makingReservation = true;
		var url = 'general_ajax_functions.php'
		var pars = $('form_makeReservation').serialize() + "&cmd=makeReservation";

    Element.show('ajaxLoading');
		myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars,
				onComplete: processMakeReservationResponse
			}
		);	
	}

  var response;
  function processMakeReservationResponse(R){


    Element.hide('ajaxLoading');
    sendingMessage = false;

    eval("response = " + R.responseText);
    
    if(response.success){
      Element.hide('form_makeReservation');
      Element.show('reservationSent');
    } else {
      alert(response.message);
    }
  }
  
	/*******************************************************************************
	Join Mailing List Functions
	*******************************************************************************/

	var showingMailingListForm = false;
  function toggleMailingListJoinForm(){
    if( $('form_mailing_list_join').visible() )
		{
			new Effect.Shrink( 'form_mailing_list_join' );
			$('mailing_list').setOpacity( .7 );
			showingMailingListForm = false;
		}
		else
		{
			new Effect.Grow( 'form_mailing_list_join' );
			$('mailing_list').setOpacity( .8 );
			showingMailingListForm = true;
		}
		$('mailing_list').blur();
  }

  var joiningList = false;
  function submitMailingListJoinForm(){
    if(joiningList == true) return;

    if($F('mailing_list_firstname') == "" || $F('mailing_list_email') == ""){
      alert("Please provide at minimum your first name and email address");
      return;
    }

    joiningList = true;
    
    Element.show('ajaxLoading');
    
		var url = 'general_ajax_functions.php'
    pars = Form.serialize('form_mailing_list_join') + "&cmd=joinMailingList";
    myAjax = new Ajax.Request(
      url,
      {
        method: 'get',
        parameters: pars,
        onComplete: processJoinMailingListResponse
      }
    );
  }
  
  function processJoinMailingListResponse(response){
    Element.hide('ajaxLoading');
    joiningList = false;
    if(response.responseText == 'success'){
	    new Effect.Squish( 'mailing_list' );
      alert( "Thank you for joining our list! Watch for emails from The Q Worldly Barbeque!" );
    } else {
      alert( response.responseText );
    }
  }

	/*******************************************************************************
	Navigation Functions
	*******************************************************************************/
	function isSubNav( parent, child)
	{
		return isArray( subNavSections[ parent ] ) && subNavSections[ parent ].inArray( child );
	}
	
	function getParentNav( child )
	{
		keys = Object.keys(subNavSections);
		for( var i = 0; i < keys.length; i++ )
		{
			if( isArray(subNavSections[ keys[i] ]) && subNavSections[ keys[i] ].inArray( child ) )
			{
				return keys[ i ];
			}
		}
		return false;
	}
  
	function updateCalendar(months){
    var options = {method: 'get', parameters: months, onComplete: function(){setMoreinfoTriggers(); Element.hide('ajaxLoading');}};
    Element.show('ajaxLoading');
    new Ajax.Updater('calendar', 'content/event_calendar.php', options);
  }
		
	var stopLogoFade = false;
	function fadeLogoOut()
	{
		Effect.Fade('qLogo',{ to: .3, afterFinish: fadeLogoIn, duration: 2 });
	}

	function fadeLogoIn()
	{
		if (stopLogoFade != true) 
		{
			Effect.Fade('qLogo',{to: .7,afterFinish: fadeLogoOut, duration: 2});
		}
		else
		{
			$( 'qLogo' ).setStyle( { opacity: '' } );
		}
	}
	
	fadeLogoOut();
	
	var currentNavLogo = 'bbq';
	function toggleNavLogo()
	{
	  	next = currentNavLogo == 'blues_jazz' ? 'bbq' : 'blues_jazz';
			prev = currentNavLogo;
			currentNavLogo = next; 
			
			new Effect.Parallel(
	     	[
	       	new Effect.Appear( 'q_nav_logo_' + next ),
	        new Effect.Fade( 'q_nav_logo_' + prev )
	      ] ,
				{
					duration: 1
				}
			);
	}
	
	var isWindowLoaded = false;
	function windowLoaded(){

		if( currentPage == '' )
		{
			currentPage = 'menu';
		}

		//initialize sections that should be showing
		if( currentPage != '' )
		{
			if (currentPage == 'photos' && galleryId != '') 
			{
	  		showPhotoGallery( galleryId );
	  	}
			if (currentPage == 'calendar' && eventId != '') 
			{
	  		showEvent( eventId );
	  	}
			showSection( currentPage, true );
		}

		stopLogoFade = true;
		isWindowLoaded = true;

//		firstContentSection = 'menu';
//		showSection( firstContentSection );

		new Effect.Parallel(
			[
				new Effect.Fade( 'loading_container' ),
				new Effect.Appear( 'content_container' ),
				new Effect.Appear( 'audio_player' ),
				new Effect.Appear( 'header' )
			],
			{ 
				duration: 1,
				afterFinish: 
					function()
					{
						setInterval( toggleNavLogo, 4000 );
					}
			}
		);
	}
	
	Event.observe( window, 'load', windowLoaded )
	
	/*************************************************************************** 
	Setup any needed html element event handling
	***************************************************************************/
	
	// Set general variables needed for 
  var baseurl = window.location.protocol + "//" + window.location.hostname + "/";

	// Change onclick for each nav item to load its respective section.
	$$('#nav li a').each(
		function(e){
			sItem = e.href.replace( baseurl, '' );
			Event.observe( e, 'click', new Function('showSection( "' + sItem + '" ); this.blur(); return false;') );
			e.href = '#';
		}
	);	

	Event.observe( $('mailing_list'), 'mouseover', function(){ $('mailing_list').setOpacity( .8 ); } );
	Event.observe( $('mailing_list'), 'mouseout', function(){ if(!showingMailingListForm) $('mailing_list').setOpacity( .7 ); } );
	
	/*************************************************************************** 
	Preload Images
	***************************************************************************/
	img = new Image;
	img.src = "/images/content_bg.png";

	preloadImageSections = [ 'menu', 'calendar', 'photos', 'worldlybarbeque', 'bluesjazzlounge', 'aboutus', 'contactus', 'specialtydrinks', 'winelist' ];
	preloadImageSections.each( function( s ){ img.src = "/images/headers/" + s + ".gif" } );
