<?php

Yii::import('bootstrap.widgets.input.TbInputHorizontal');

class PlInputHorizontal extends TbInputHorizontal
{

	public function run()
	{
		if($this->type == PlInput::TYPE_COLOR){
    		$this->colorField();
		}else if($this->type == PlInput::TYPE_DATE){
            $this->dateField();
        }
		else{
    		parent::run();
		}
	}
	
	/**
	 * Renders a text field.
	 * @return string the rendered content
	 */
    //FIXME !
    public function dateField() {
        //$attribute='title';
        CHtml::resolveNameID($this->model,$this->attribute,$this->htmlOptions);
        $dpId = $this->htmlOptions['id'];
        $cs=Yii::app()->clientScript;
        $cs->registerScriptFile(Yii::app()->baseUrl . '/js/bootstrap-datepicker.js', CClientScript::POS_HEAD);
        $cs->registerScript('datePicker','$("#dp_'.$dpId.'").datepicker()'); 
        
        echo '<div class="control-group">';
        echo $this->getLabel();
        echo '<div class="controls">';
        echo '<div class="input-append">';
        echo $this->getPrepend();


        if (isset($this->htmlOptions['class'])) {
            $this->htmlOptions['class'] = 'plDatePickerInput ' . $this->htmlOptions['class'];
        } else {
            $this->htmlOptions['class'] = 'plDatePickerInput ';
        }

        $attribute = $this->attribute;
        echo '<div class="input-append date" id="dp_'.$dpId.'" data-date="'.$this->model->$attribute.'" data-date-format="yyyy-mm-dd">';
                echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
                //'<input id="'.$this->id.'" class="span2" size="16" type="text" value="'.date('d-m-Y').'" >';
                echo '<span class="add-on"><i class="icon-calendar"></i></span>';
        echo '</div>';


        echo '</div>';
        echo $this->getError().$this->getHint();

        echo '</div>';
        echo '</div>';


    }

	protected function colorField()
	{
		echo '<div class="control-group">';
		
		echo $this->getLabel();
		echo '<div class="controls">';
		echo '<div class="input-append">';
		echo $this->getPrepend();
		
		$this->htmlOptions['class'] = 'plColorPicerInput ' . $this->htmlOptions['class'];
		
		
		echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
		
		
		echo '<span class="add-on"><div class="plColorPicerPreview"></div></span>';
		//echo $this->getAppend();
		echo '</div>';
		echo $this->getError().$this->getHint();
		
		echo '</div>';
		echo '</div>';
	}
}
