<?php
function __autoload($className)
{
	

	$className = str_replace(chr(0), '', $className);
	
	$destino = array();
	
	$destino[] = array('normal' => dirname(__FILE__).'/models/',
	'over' => dirname(__FILE__).'/../models/');
	
	$destino[] = array('normal' => dirname(__FILE__).'/../app/',
	'over' => dirname(__FILE__).'/../appoverride/');
	
	$destino[] = array('normal' => dirname(__FILE__).'/layouts/',
			'over' => dirname(__FILE__).'/../layouts/');
	
	
	$destino[] = array('normal' =>  dirname(__FILE__).'/../app/forms/',
	'over' =>  dirname(__FILE__).'/../forms/');
	
	$destino[] = array('normal' => null,
	'over' =>  dirname(__FILE__).'/../grids/');
	
	$destino[] = array('normal' => null,
	'over' =>  dirname(__FILE__).'/../app/objetos/');
	
	$destino[] = array('normal' => dirname(__FILE__).'/../app/vst_elementos/' ,
			'over' =>  dirname(__FILE__).'/../vst_elementos/' );
		
	$destino[] = array('normal' =>  null,
	'over' =>  dirname(__FILE__).'/../app/elementos/');
	
	$destino[] = array('normal' => dirname(__FILE__).'/../app/controllers/',
	'over' =>  dirname(__FILE__).'/../controllers/');
	
	
	$destino[] = array('normal' => dirname(__FILE__).'/../app/eventos/',
	'over' =>  dirname(__FILE__).'/../eventos/');
	
	$destino[] = array('normal' => dirname(__FILE__).'/../app/columna/',
	'over' =>  dirname(__FILE__).'/../columna/');
		
	$destino[] = array('normal' => dirname(__FILE__).'/../app/validadores/',
			'over' =>  null);
	
	$destino[] = array('normal' =>  dirname(__FILE__).'/../app/listados/',
			'over' =>  dirname(__FILE__).'/../listados/');
	
	
	$destino[] = array('normal' =>  null,
			'over' =>  dirname(__FILE__).'/../');
	
	//objetos
	
	foreach ($destino as $elemetno){
		if(compuevaOver($elemetno['normal'], $elemetno["over"], $className))
			return true;
	}
	
	
	
	
	
}

function compuevaOver($normal, $overrideDir, $className){
	
	$file_in_app = file_exists($normal.$className.'.php');
	$file_in_appoverride = file_exists($overrideDir.$className.'.php');
	
	if (substr($className, -4) == 'Core'){
		
		if(file_exists($normal.substr($className, 0, -4).'.php')){
			require_once($normal.substr($className, 0, -4).'.php');
		}
			
	}else
	{
		
			
			if ($file_in_appoverride && $file_in_app)
			{
				require_once($normal.str_replace(chr(0), '', $className).'.php');
				require_once($overrideDir.$className.'.php');
				return true;
			}
			elseif (!$file_in_appoverride && $file_in_app)
			{
				require_once($normal.str_replace(chr(0), '', $className).'.php');
				$classInfos = new ReflectionClass($className.((interface_exists($className, false) or class_exists($className, false)) ? '' : 'Core'));
				if (!$classInfos->isInterface() && substr($classInfos->name, -4) == 'Core')
					eval(($classInfos->isAbstract() ? 'abstract ' : '').'class '.$className.' extends '.$className.'Core {}');
				return true;
			}
			elseif ($file_in_appoverride && !$file_in_app){
				
				require_once($overrideDir.$className.'.php');
				return true;
			}
	}
	return false;
}

