<?php
function throwBoxes( ){
	$oDB = DataBase::getInstance( );
	$oTpl = TplParser::getInstance( );
	$aPageOrig = $oTpl->getTemplateVars( 'aPage' );
	$iStatus = throwStatus( );
	
	foreach( $GLOBALS['aBoxesDisplay'] as $iBoxesType => $sBoxesDisplay ){
		$iPage = intval( $GLOBALS['iContent'] );
		$aBoxes = Array();
		do{
			$sql = 'SELECT `iBox`, `sName`, `sContent`, `iType`, `iStatus`, `iPage`
								FROM `' . $oDB->throwTableName( DB_BOXES ) . '` `boxes`
									LEFT JOIN `' . $oDB->throwTableName( DB_BOXES_PAGES ) . '` `boxes_pages` USING( `iBox` )
								WHERE `iType` = ' . $iBoxesType . ' AND `iStatus` >= ' . intval( $iStatus );
			if( $iPage > 0 ){
				$sql.= ' AND ( `boxes_pages`.`iPage` = ' . intval( $iPage ) . ' )';
			} else{
				$sql.= ' HAVING ( `iPage` IS NULL )';
			}
			$sql.= ' ORDER BY `iPosition`, `iBox`;';
			$aBoxes = $oDB->executeSQL( $sql, true );
			if( $iPage > 0 ){
				$iPage = intval( $GLOBALS['oPage']->aPages[$iPage]['iPageParent'] );
			} else{
				break;
			}
		} while( empty( $aBoxes ) );
		foreach( $aBoxes as &$mBox ){
			$mBox['sContent'] = changeTxt( $mBox['sContent'], 'NdsNl' );
			$mBox['sContent'] = parseSpecials( $mBox['sContent'] );
			$oTpl->assign( 'aBox', $mBox );
			$aReturn[$mBox['iType']][] = $oTpl->parse( 'box_' . $GLOBALS['aBoxesDisplayBlock'][$mBox['iType']] . '.tpl' );
			$oTpl->unassign( 'aBox' );
		}
	}
	$oTpl->assign( 'aPage', $aPageOrig );
	return $aReturn;
}
?>