﻿function PhGallery( anchor_elm )
{
	this.m_oAnchor			= anchor_elm;
	this.m_oCurPanel		= null;
	this.m_fpImageSelected	= null;
	this.m_fpImageAdded		= null;
	this.m_oScrollBar		= null;
	this.m_oWaitDiv			= null;
	
	this.InitObject();
}

PhGallery.prototype.InitObject = function()
{
	this.m_oWaitDiv	= document.createElement( 'div' );
	this.m_oWaitDiv.id					= 'gallery_load_message';
	this.m_oWaitDiv.style.visibility	= 'hidden';
	

	document.body.appendChild( this.m_oWaitDiv );	
	g_oClient.DisableSelection( this.m_oAnchor );
}

PhGallery.prototype.SetImageSelectedCallback = function( callback )
{
	this.m_fpImageSelected = callback;
}

PhGallery.prototype.SetImageAddedCallback = function( callback )
{
	this.m_fpImageAdded = callback;
}

PhGallery.prototype.ClearImages = function()
{
	for( i = this.m_oAnchor.childNodes.length-1; i >= 0; i-- )
	{
		g_oClient.RemoveElement( this.m_oAnchor.childNodes[i] );
	}
}

PhGallery.prototype.ThumbnailSelected = function( pnl )
{	
	this.m_oCurPanel = pnl;
	this.m_fpImageSelected( pnl.SearchResult );
}

PhGallery.prototype.AddImages = function( images )
{
	var me		= this;
	var pnl		= null;
	var i		= 0;
	var x		= 0;
	var y		= 0;
	
	var block_size	= (820)/4;
	var portrait_w	= 0;
	var portrait_h	= 0;
	
	for( i = 0; i < images.length; i++ )
	{	
		var pnl	= new PhPortrait()

		pnl.SetSearchResult( images[i] );
		pnl.SetSelectedCallback( function( pnl ){me.ThumbnailSelected( pnl );} );
		pnl.AttachToElement( this.m_oAnchor );
		pnl.SetInlineStyles( 'position: absolute;' );
		
		portrait_w = pnl.m_oContainer.clientWidth;
		portrait_h = pnl.m_oContainer.clientHeight;

		pnl.SetPosition( x+((block_size-portrait_w)/2), y+(block_size-portrait_h)/2 );
		
		x += block_size;
		
		if( x > block_size*3 )
		{
			x = 0;
			y += block_size;
		}
		
		if( this.m_fpImageAdded != null )
			this.m_fpImageAdded( pnl );
	}
	
	this.m_oAnchor.style.height = (y+block_size)+'px';
	
	if( this.m_oScrollBar != null )
		this.m_oScrollBar.Update();
}

PhGallery.prototype.SetScrollBar = function( sb )
{
	this.m_oScrollBar = sb;
	this.m_oScrollBar.SetScrollPanel( this.m_oAnchor );
}

PhGallery.prototype.DisplayMessage = function( msg )
{
	var gallery	= document.getElementById( 'gallery' );
	var pos		= g_oClient.GetElementPosition( gallery );
	
	this.m_oWaitDiv.innerHTML			= msg;
	this.m_oWaitDiv.style.left			= (pos.X+(gallery.clientWidth-this.m_oWaitDiv.clientWidth)/2)+'px';
	this.m_oWaitDiv.style.top			= (pos.Y+(gallery.clientHeight-this.m_oWaitDiv.clientHeight)/2)+'px';
	this.m_oWaitDiv.style.visibility	= 'visible';
}

PhGallery.prototype.ClearMessage = function( timeout )
{
	var me = this;
	
	if( timeout == 0 )
		this.m_oWaitDiv.style.visibility = 'hidden';
	else
		setTimeout( function(){me.m_oWaitDiv.style.visibility = 'hidden';}, timeout*1000 );
}
