<?php 
class VstFilaMovil{
	protected $_elementos = array();
	
	
	protected $_clase_color = '';
	
	const APP_COLOR_ROJO = 'alert alert-danger no-margin';
	const APP_COLOR_AZUL = 'alert alert-info no-margin';
	const APP_COLOR_VERDE = 'alert alert-success no-margin';
	const APP_COLOR_AMARILLO = 'alert alert-warning no-margin';
	
	public function __construct(){
		
	}
	
	public function setColor($color){
		$this->_clase_color = $color;
	}
	
	public function render(){
		$salida = '<div class="row '.$this->_clase_color.'">';
		$ancho_total = 0;
		$automaticos = 0;
		foreach ($this->_elementos as $key => $elemento){
			
			
			if(is_string($elemento['elemento'])){
				$render = $elemento['elemento'];
				
			}else{
				$render = $elemento['elemento']->render();
			}
			
			$this->_elementos[$key]['render'] = $render;
			if(strlen($render)>0){
				if(is_null($elemento['ancho'])){
					$automaticos +=1;
				}else{
					$ancho_total += $elemento['ancho'];
				}
			}
		}
		$anchomax = 12;
		$anchomin = 1;
		if($automaticos > 0){
			$anchomax = ceil((12- $ancho_total)/$automaticos);
			$anchomin = floor((12- $ancho_total)/$automaticos);
		}
		$primero = true;
		
		foreach ($this->_elementos as $key => $elemento){
			
			if(strlen($elemento['render'])>0){
				if(is_null($elemento['ancho'])){
					if($primero){
						$primero = false;
						$this->_elementos[$key]['ancho'] = $anchomax;
					}else{
						$this->_elementos[$key]['ancho'] = $anchomin;
					}
					$automaticos +=1;
				}
			}
		}
		
		
		foreach ($this->_elementos as $elemento){
			if(strlen($elemento['render'])>0){
				$salida .='<div class="col-xs-'. $elemento['ancho'].' '.$elemento['clase'].'">';
				$salida .= $elemento['render'];
				$salida .='</div>';
			}
			
			 
			
		}
		$salida .='</div>';
		return $salida;
	}
	
	public function __toString(){
		return $this->render();
	}
	
	/**
	 * 
	 * @param unknown $elemento
	 * @param string $ancho numero entre 1 y 12
	 */
	public function addElemento(&$elemento, $ancho = null, $clase = ''){
		$this->_elementos[]= array('elemento'=> $elemento, 'ancho' => $ancho, 'clase'=> $clase);
	}
}