var nextBlockToShow = 1;
var maxBlocks;

///////////////////////////////////////////////////////////////////////////////
// common functions

function HighliGht( id )
{
	var elem = document.getElementById(id);
    elem.className='pod_on';
}

function Shadow( id )
{
    var elem = document.getElementById(id);
    elem.className='pod_off';
}

function showObj( obj )
{
	obj.style.visibility = 'visible';
	obj.style.display = 'block';
}

function hideObj( obj )
{
	obj.style.visibility = 'hidden';
	obj.style.display = 'none';
}

function showPopup( name )
{
	var elem = null;
	if ( document.getElementById )
	{
		elem = document.getElementById(name);
	}
	else
	{
		elem = eval("document.all." + name );
	}
	elem.style.visibility = "visible";
	elem.style.display = 'block';
}

function hidePopup( name )
{
	var elem = null;
	if ( document.getElementById )
	{
		elem = document.getElementById(name);
	}
	else
	{
		elem = eval("document.all." + name );
	}
	elem.style.visibility = 'hidden';
	elem.style.display = 'none';
}

function isObjVisible( obj )
{
	return obj.style.visibility == 'visible';
}

// Example: obj = findObj("image1");
function findObj( theObj, theDoc )
{
	var p, i, foundObj;
	if( !theDoc ) theDoc = document;

	if( (p = theObj.indexOf("?")) > 0 && parent.frames.length )
	{
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}

	if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];

	for( i=0; !foundObj && i < theDoc.forms.length; i++ )
		foundObj = theDoc.forms[i][theObj];

	for( i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++ )
		foundObj = findObj(theObj,theDoc.layers[i].document);

	if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

	return foundObj;
}

function findAllObj( theObj, theDoc )
{
	var p, i, foundObj, foundObjects, count;
	if( !theDoc ) theDoc = document;

	if( (p = theObj.indexOf("?")) > 0 && parent.frames.length )
	{
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}

	count = 0;
	foundObjects = new Array();
	if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
	if (foundObj)
		foundObjects[count++] = foundObj;



	for( i=0; i < theDoc.forms.length; i++ )
	{
		foundObj = theDoc.forms[i][theObj];
		if (foundObj) {
			foundObjects[count++] = foundObj;
		}


	}

	for( i=0; theDoc.layers && i < theDoc.layers.length; i++ )
	{
		foundObj = findObj(theObj,theDoc.layers[i].document);
		if (foundObj) {
			foundObjects[count++] = foundObj;
		}
	}

	if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
	if (foundObj) {
		foundObjects[count++] = foundObj;
	}

	return foundObjects;
}



///////////////////////////////////////////////////////////////////////////////
// code that retain compatibility with non-oop usage

function getBoxBlockPrefix()
{
	return this.boxBlockPrefix ? this.boxBlockPrefix : "box_";
}


function getAddButtonName()
{
	return this.addButtonName ? this.addButtonName : 'addButton';
}


function getMaxBlocks()
{
	return this.maxBlocks ? this.maxBlocks : maxBlocks;
}


function getNextBlockToShow()
{
	return this.nextBlockToShow != null ? this.nextBlockToShow : nextBlockToShow;
}


function incrementNextBlockToShow()
{
	this.nextBlockToShow != null ? this.nextBlockToShow++ : nextBlockToShow++;
}


function decrementNextBlockToShow()
{
	this.nextBlockToShow != null ? this.nextBlockToShow-- : nextBlockToShow--;
}


function getFirstBlockNumber()
{
	return this.firstBlockNumber != null ? this.firstBlockNumber : 1;
}


///////////////////////////////////////////////////////////////////////////////
// low level accessory code

function showBoxBlockNumber( num )
{
	showObj( findObj( this.getBoxBlockPrefix() + num ) );
}

function hideBoxBlockNumber( num )
{
	hideObj( findObj( this.getBoxBlockPrefix() + num ) );
}


function isBoxBlockNumberVisible( num )
{
	return isObjVisible( findObj( this.getBoxBlockPrefix() + num ) );
}




///////////////////////////////////////////////////////////////////////////////
// middle level accessory code

function showNextBlock()
{
	if( this.getNextBlockToShow() > this.getMaxBlocks() ) return;
	this.showBoxBlockNumber( this.getNextBlockToShow() );
	this.incrementNextBlockToShow();
	this.toggleAddButton();
}


function hideLastVisible()
{
	this.decrementNextBlockToShow();
	this.hideBoxBlockNumber( this.getNextBlockToShow() );
	this.toggleAddButton();
}


function toggleAddButton()
{
	if( this.getNextBlockToShow() > this.getMaxBlocks() )
	{
		hideObj( findObj( this.getAddButtonName() ) );
	}
	else
	{
		showObj( findObj( this.getAddButtonName() ) );
	}
}


function fillGaps()
{
	var limit = this.getNextBlockToShow() <= this.getMaxBlocks()?
							this.getNextBlockToShow():
							this.getMaxBlocks();

	for( var i = this.getFirstBlockNumber(); i < limit; i++ )
	{
		if( this.isBoxBlockNumberEmpty( i ) )
		{
			for( var j = i+1; j <= limit; j++ )
			{
				if( !this.isBoxBlockNumberEmpty( j ) )
				{
					this.copyBoxBlockFromTo( j, i );
					this.clearBlockNumber( j );
					break;
				}
			}
		}
	}
}




///////////////////////////////////////////////////////////////////////////////
// MUST BE IMPLEMENTED BY SUBCLASSES

function getBoxBlockNumberValue( number )
{
	alert( "getBoxBlockNumberValue() must be defined by  subclass" );
}


function setBoxBlockNumberValue( number, value )
{
	alert( "setBoxBlockNumberValue() must be defined by  subclass" );
}


function clearBlockNumber( number )
{
	alert( "clearBlockNumber() must be defined by  subclass" );
}


function isBoxBlockNumberEmpty( number )
{
	alert( "isBoxBlockNumberEmpty() must be defined by  subclass" );
}


function copyBoxBlockFromTo( from, to )
{
	alert( "copyBoxBlockFromTo() must be defined by  subclass" );
}




///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// UI entry points

function zapBoxBlockNumber( number )
{
	this.clearBlockNumber( number );
	this.fillGaps();
	this.hideLastVisible();
}


function showInitialBoxBlocks()
{
	for( var i = this.getFirstBlockNumber(); i <= this.getMaxBlocks(); i++ )
	{
		if( this.isBoxBlockNumberEmpty( i ) ) 	break;
		this.showNextBlock();
	}
}
