/**
 * Global site object for page functionality
 * http://www.bennyschuetz.com
 * @author benny
 */
function Bs()
{
	//
	// Properties
	//	
	this.headHtml 			= "";
	this.headHtmlIndex		= 0;
	this.headHtmlSpeed  	= 25;	// msecs
	this.mainContent		= null;
	this.mainContentIndex 	= 0;
	this.mainContentSpeed	= 100;	// msecs
	this.activeElement		= null;
	this.headTimeout		= null;
	this.contentTimeout		= null;
	
	// PageId (needs to be sync'd with backend)
	this.indexPageId 	= 1;
	this.faqPageId   	= 2;
	this.linksPageId	= 3;
	this.imprintPageId 	= 9;

	//
	// Private member variable
	//	
	var thisObj = this;		    // Self reference	

	
	//
	// Methods	
	//	
	/**
	 * Initialized main navigation
	 */
	this.initNavi = function()
	{
		// Get all li's
		var li = $('#mainnav ul > *');

		// Loop through all them and attach the onclick events
		for (var i=0; i < li.length; i++ )
		{
			jQuery( li[i] ).bind( "click", function(e) { thisObj.doNavi( this ); } );
		}
		
		// Add click handler for logo
		jQuery( '#logo' ).bind( "click", function(e) { thisObj.doNavi( $( '#mainnav ul > *:first' ) ); } );
	}
	
	/**
	 * Action called on menu item click
	 * 
	 * @param {Object} element	HTML element which was clicked
	 */
	this.doNavi = function( element )
	{
		// Fail silently
		if ( !element ) return;
		
		// Get title attribute of element
		var title = $( element ).attr( 'title' );
		
		// Do nothing if current element is already selected
		if ( title === this.activeElement ) return;		
		
		// Set update active element		
		this.activeElement = title;
		
		// Scroll to top
		window.scrollTo( 0, 0);
		
		// Toggle active class			
		$( "#mainnav .active" ).removeClass( "active" );
		if ( element )
		{
			$( element ).addClass( "active" );
		}			
				
		// Load new content
		switch (title) 
		{
			// Imprint				
			case "MenuItemImprint":
				$('#headContent').html("");
				thisObj.fetchContent( this.imprintPageId );
				SWFAddress.setValue('/imprint/');
				break;
				
			// Frequently asked questions	
			case "MenuItemFaq":
				$('#headContent').html( "" );
				thisObj.fetchContent( this.faqPageId );
				SWFAddress.setValue('/faq/');
				break;
				
			// Traces on the net
			case "MenuItemLinks":				
				$('#headContent').html( "" );
				thisObj.fetchContent( this.linksPageId );
				SWFAddress.setValue('/links/');				
				/*
				var html = "An incomplete list about the traces I left on the net.<br /><br />";
				html += "Travel without to move!<br />You suck!";
				// bs.setHeadHtml( html );
				thisObj.showHeadContent(html);
				*/
				break;
			
			// Default, Home tweet home				
			default:
			case "MenuItemHome":
				$('#headContent').html( "" );
				thisObj.fetchContent( this.indexPageId );
				SWFAddress.setValue('/news/');
				break;
		}
	}
	
	/**
	 * Fetches site content of given page via ajax request.
	 * 
	 * @param int pageId	PageId - needs to be sync'd with backend.
	 */
	this.fetchContent = function( pageId )
	{
		// Clear all previous content
		$('#left').html( "" );
		$('#right').html( "" );
		$('#headContent').html( "" );

		// Teaser is written - time to hide cursor
		// $('#imgCursor').css( 'display', 'block' );
		
		// Ajax load new content
        $.getJSON( "?c=ajax&a=GetSiteContent", 
				   { pageId: pageId },
				   thisObj.updateContent );
	}
	
	/**
	 * Callback function called by ajax response
	 * 
	 * @param {Object} data		Ajax response json data
	 */
	this.updateContent = function( data )
	{
		// If teaser is set
        if ( data.teaser )
        {
			// show it
        	if ( null != this.headTimeout )
        	{
        		clearTimeout( this.headTimeout );
        	}
        	$('#headContent').html( "" );
			thisObj.showHeadContent( data.teaser );
        }
		
        if ( data.main )
        {
        	if ( null != this.contentTimeout )
        	{
        		clearTimeout( this.contentTimeout );
        	}
        	$('#left').html( "" );
			thisObj.mainContent = data.main;
			thisObj.mainContentIndex = 0;
			thisObj.showMainContent();
        }
		
		// Set sitebar content if set
        if ( data.sitebar )
        {
            $('#right').html( data.sitebar );
        }
	}
	
	/**
	 * Shows head content of current page
	 * 
	 * @param {Object} headHtml
	 */
	this.showHeadContent = function( headHtml )
	{
		this.headHtmlIndex = 0;
		this.headHtml 	   = headHtml;

		thisObj.headHtmlIndex++;
		if ( thisObj.headHtmlIndex < thisObj.headHtml.length )
		{
			// If we encounter a tag
			if ( "<" === thisObj.headHtml.substr( thisObj.headHtmlIndex, 1 ) )
			{
				// We skip to the end of it
				while ( ">" !== thisObj.headHtml.substr( thisObj.headHtmlIndex, 1) &&
					    thisObj.headHtmlIndex < thisObj.headHtml.length )
				{
					thisObj.headHtmlIndex++;
				}
			}
			else
			{
				while ( " " !== thisObj.headHtml.substr( thisObj.headHtmlIndex, 1) &&
						thisObj.headHtmlIndex < thisObj.headHtml.length )
				{
					thisObj.headHtmlIndex++;
				}
			}			
			// Show headline
			$('#headContent').html( thisObj.headHtml.substr(0, thisObj.headHtmlIndex) );			
		}
		
		// Only recall itself when there is more to print
		if ( thisObj.headHtmlIndex < thisObj.headHtml.length )
		{
			this.headTimout = setTimeout( thisObj.showHeadContent, thisObj.headHtmlSpeed );
		}
		else
		{
			this.headTimeout = null;
		}
	} 
	
	/**
	 * Shows main content of current page
	 */
	this.showMainContent = function()
	{
        if ( thisObj.mainContent === null ) return;
        
        if ( thisObj.mainContent.length > thisObj.mainContentIndex )
        {
            $('#left').append( thisObj.mainContent[ thisObj.mainContentIndex ] );            
        }
        
        thisObj.mainContentIndex++;
        if ( thisObj.mainContent.length > thisObj.mainContentIndex )
        {
            this.contentTimeout = setTimeout( thisObj.showMainContent, thisObj.mainContentSpeed );
        }
        else
        {
        	this.contentTimeout = null;
        }
	}
	
	/**
	 * Callback when hashLink changed.
	 * This callback extract hash value and calls navigation accordingly.
	 * 
	 * @param event
	 */
	this.onHashChange = function( event )
	{
	  var page = event.pathNames[0] ? event.pathNames[0] : 'index';
	  
	  // Handle pages on a case by case basis
	  switch ( page.toLowerCase() )
	  {
  	    case 'faq':
  	     thisObj.doNavi( $( '.faqMenu' ) );
  	     break;
	     
        case 'imprint':
          thisObj.doNavi( $( '.imprintMenu' ) );
          break;
          
        case 'links':
        	thisObj.doNavi( $( '.linksMenu' ) );
        	break;

        case 'news':
        default:
         thisObj.doNavi( $( '.tweetMenu' ) );
         break;
	  }
	}
		
	//
	// Constructor code
	//
	thisObj.initNavi();
}
