/* 
	Class: GalleryControl
	
	This file provides a GalleryControl object to more easily setup, initialize,
	and control bepress image galleries.
	
	Currently the object is hard-coded to look for an element with an id of
	'gallery_items' and manipulate its class.  It also expects an <a> tag or 
	button to exist with the id 'switch_button'.
	
	This could be better abstracted such that the including page could specify
	the gallery element id and various other such page element characteristics
	(styles, classes, etc.) but that is left to future refinement.

*/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*                             Constructor                               */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* 
	Constructor: GalleryControl()

	Instantiate a new control with the jQuery object provided.

	Arguments:
		inJQuery - the jQuery object to use for operations.
		
*/
function GalleryControl( inJQuery )
{
	this.$ = inJQuery;
	
	/*	
		The isPageInitComplete variable should be set to true as the last act of
		the jQuery( "document" ).ready() callback.
	*/
	this.isPageInitComplete = false;
	
	/* 	
		The maximum number of times that setupFinalPageInitWhenReady can schedule
		itself while waiting for floatbox initialization.
	*/
	this.maxPageInitAttempts = 10;
	this.pageInitAttemptsCount = 0;
};


	
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*                            Public Methods                             */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* 
	Method: display()

	Display the image gallery in the user's preferred layout.  This function
	depend on being able to access the $.cookie function and uses it to both
	read and write the user's view preference.
	

*/
GalleryControl.prototype.display  = function()
	{
		// Let's make sure that the cookie plugin in live and available
		if ( typeof this.$.cookie == "function" )
		{
				if ( this.$.cookie( "layout" ) == null )
				{
					this.$.cookie( "layout", "gridView", {expires: 365} );
				}
				
				var container = this.$( "#gallery_items" );
				
				if ( this.$.cookie( "layout" ) == "listView"
					&& ! container.hasClass( GalleryControl.LIST_VIEW_CLASS ) )
				{
					this.$( "#switch_button" ).addClass( "swap" );
					container
						.hide( GalleryControl.TOGGLE_DELAY_MICROSECONDS / 2 )
						.removeClass( GalleryControl.GRID_VIEW_CLASS )
						.addClass( GalleryControl.LIST_VIEW_CLASS )
						.show( GalleryControl.TOGGLE_DELAY_MICROSECONDS / 2 );
				}
				else if ( this.$.cookie( "layout" ) == "gridView"
					&& ! container.hasClass( GalleryControl.GRID_VIEW_CLASS ) )
				{
					this.$( "#switch_button" ).removeClass( "swap" );
					container
						.hide( GalleryControl.TOGGLE_DELAY_MICROSECONDS / 2 )
						.removeClass( GalleryControl.LIST_VIEW_CLASS )
						.addClass( GalleryControl.GRID_VIEW_CLASS )
						.show( GalleryControl.TOGGLE_DELAY_MICROSECONDS / 2 );
				}
					
				// console.log( new Date() + " display in " + this.$.cookie( "layout" ) );
			
		}
		else
		{
			// console.log( "$.cookie is not available" );
		}
	};

/* 
	Method: toggleView()

	Toggles the gallery from one view to the other (list or grid) and sets 
	the view as the user's preference in the 'layout' cookie.
	

*/
GalleryControl.prototype.toggleView  = function()
	{
		// console.log( "toggleView called " + this.isPageInitComplete );
		
		if ( this.isPageInitComplete ) {
		
			if( this.$.cookie( "layout" ) == "gridView" ){
				this.$.cookie( "layout", "listView", {expires: 365} );
				this.display();
			} else {
				this.$.cookie( "layout", "gridView", {expires: 365} );
				this.display();
			}
	
		}
		else
		{
			// console.log( new Date() + " toggleView ignored, page not init'd" );
		}
	};

/* 
	Method: setupFinalPageInitWhenReady()
	
	Checks the page for floatbox initialization completion.  
	If it is not finished setting up, then it will setup a
	timeout to call itself again in 100ms.  The actual final
	initialization should be performed by the 
	'inCallbackFunction' argument which must be a function that
	returns true if it succeeded, false otherwise.
	
	setupFinalPageInitWhenReady should be called from within the 
	jQuery( "document" ).ready() callback.  
	
	Arguments:
		inCallbackFunction - a callback function that performs
				final initializations and returns true|false.
	
*/
GalleryControl.prototype.setupFinalPageInitWhenReady  = function( inCallback )
	{
		// console.log( new Date() + " setupFinalPageInitWhenReady running with " + inCallback );
		
		if ( typeof fb == 'undefined' )
		{
			// console.log( new Date() + " fb == undefined, check later" );
			
			if ( this.pageInitAttemptsCount <= this.maxPageInitAttempts )
			{
				setTimeout( "galleryControl.setupFinalPageInitWhenReady("+inCallback+")", 100 );
				this.pageInitAttemptsCount++;
			}
		}
		else
		{
			// console.log( new Date() + " page init ready to go" );
			// call our final init.
			try
			{
				inCallback();
			}
			catch( e )
			{
				// console.log( new Date() + " page init callback exception: " + e ); 
			}
			
			this.isPageInitComplete = true;
		}
	};

/* 
	Method: toString()
	
	Override. Returns the string representation of the object.
	
	Returns:
	
	A string with class name and count of gallery items.
	
*/
GalleryControl.prototype.toString  = function()
	{
		return "[object GalleryControl (" + this.$( "#gallery_items" ).find( "li" ).size() + ")]";
	};
	
/* 
	Method: valueOf()
	
	Override. Returns a the number of gallery items in the gallery this 
	object controls.
	
	Returns:
	
	An integer count of the number of gallery items contained.
	
*/
GalleryControl.prototype.valueOf  = function()
	{
		return this.$( "#gallery_items" ).find( "li" ).size();
	};


// - - - - - - - - - - - - - - - - - - - 
// "Static" fields

/*
	private.  The CSS class names for list and grid view
*/
GalleryControl.LIST_VIEW_CLASS = 'display';
GalleryControl.GRID_VIEW_CLASS = 'thumb_view';

/*
	private.  the speed/delay in microseconds for a toggle display change.
*/
GalleryControl.TOGGLE_DELAY_MICROSECONDS = 4;

// - - - E N D   C L A S S - - - - - - - - - - - - - - - - - - - - - - - - - - 
