<?php

/**
 * Description of NivoSliderWidget
 *
 * @author Mohammed Osman  <osmancode@gmail.com>
 */
class NivoSliderWidget extends ShortCodeWidget {

    protected $_options = array(
        array(
            'name' => 'model_id',
            'label' => 'Slider ID',
            'value' => 0,
        ),
        array(
            'name' => 'layout',
            'label' => 'Widget Layout File',
            'value' => 'default',
        ),
        array(
            'name' => 'effect', 'type' => 'dropdownlist',
            'items' => array(
                'sliceDown' => 'sliceDown',
                'sliceDownLeft' => 'sliceDownLeft',
                'sliceUp' => 'sliceUp',
                'sliceUpLeft' => 'sliceUpLeft',
                'sliceUpDown' => 'sliceUpDown',
                'sliceUpDownLeft' => 'sliceUpDownLeft',
                'fold' => 'fold',
                'fade' => 'fade',
                'random' => 'random',
                'slideInRight' => 'slideInRight',
                'slideInLeft' => 'slideInLeft',
                'boxRandom' => 'boxRandom',
                'boxRain' => 'boxRain',
                'boxRainReverse' => 'boxRainReverse',
                'boxRainGrow' => 'boxRainGrow',
                'boxRainGrowReverse' => 'boxRainGrowReverse',
            ),
            'value' => 'fade',
        ),
        array(
            'name' => 'theme', 'type' => 'dropdownlist',
            'items' => array('bar' => 'Bar', 'dark' => 'Dark', 'default' => 'Default', 'flexcms' => 'Flex CMS', 'light' => 'Light'),
            'value' => 'flexcms',
        ),
        array(
            'name' => 'slices',
            'help' => "For slice animations",
            'value' => 15,
        ),
        array(
            'name' => 'boxCols',
            'help' => "For box animations",
            'value' => 8,
        ),
        array(
            'name' => 'boxRows',
            'help' => "For box animations",
            'value' => 4,
        ),
        array(
            'name' => 'animSpeed',
            'help' => "Slide transition speed",
            'value' => 500,
        ),
        array(
            'name' => 'pauseTime',
            'help' => "How long each slide will show",
            'value' => 2500,
        ),
        array(
            'name' => 'startSlide',
            'help' => "Set starting Slide (0 index)",
            'value' => 0,
        ),
        array(
            'name' => 'directionNav',
            'help' => "Next & Prev navigation",
            'type' => 'dropdownlist',
            'items' => array('false' => 'false', 'true' => 'true'),
            'value' => 'false',
        ),
        array(
            'name' => 'controlNav',
            'help' => "1,2,3... navigation",
            'type' => 'dropdownlist',
            'items' => array('false' => 'false', 'true' => 'true'),
            'value' => 'false',
        ),
        array(
            'name' => 'controlNavThumbs',
            'help' => "Use thumbnails for Control Nav",
            'type' => 'dropdownlist',
            'items' => array('false' => 'false', 'true' => 'true'),
            'value' => 'false',
        ),
        array(
            'name' => 'pauseOnHover',
            'help' => "Stop animation while hovering",
            'type' => 'dropdownlist',
            'items' => array('false' => 'false', 'true' => 'true'),
            'value' => true,
        ),
        array(
            'name' => 'manualAdvance',
            'help' => "Force manual transitions",
            'type' => 'dropdownlist',
            'items' => array('false' => 'false', 'true' => 'true'),
            'value' => 'false',
        ),
        array(
            'name' => 'prevText',
            'help' => "Prev directionNav text",
            'value' => 'Prev',
        ),
        array(
            'name' => 'nextText',
            'help' => "Next directionNav text",
            'value' => 'Next',
        ),
        array(
            'name' => 'randomStart',
            'help' => "Start on a random slide",
            'type' => 'dropdownlist',
            'items' => array('false' => 'false', 'true' => 'true'),
            'value' => 'false',
        ),
        array(
            'name' => 'beforeChange',
            'help' => "Triggers before a slide transition",
            'value' => 'js:function(){}',
        ),
        array(
            'name' => 'afterChange',
            'help' => "Triggers after a slide transition",
            'value' => 'js:function(){}',
        ),
        array(
            'name' => 'slideshowEnd',
            'help' => "Triggers after all slides have been shown",
            'value' => 'js:function(){}',
        ),
        array(
            'name' => 'lastSlide',
            'help' => "Triggers when last slide is shown",
            'value' => 'js:function(){}',
        ),
        array(
            'name' => 'afterLoad',
            'help' => "Triggers when slider has loaded",
            'value' => 'js:function(){}',
        ),
    );
    public $modelClass = "Slider";
    public $registerCssFiles = true;
    public $shortCode = 'nivoslider';
    public $theme = 'default';

    function init() {
        parent::init();
        $this->theme = $this->_options['theme'];

        $cs = Yii::app()->getClientScript();
        $id = $this->getId();
        $assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/assets/nivoslider/');
        if ($this->registerCssFiles == true) {
            $cs->registerCssFile($assets . '/nivo-slider.css');
            $cs->registerCssFile($assets . "/themes/{$this->theme}/{$this->theme}.css");
        }
        $cs->registerScriptFile($assets . '/jquery.nivo.slider.js');
        $cs->registerScriptFile($assets . '/jquery.nivo.slider.pack.js');

        $options = CJavaScript::encode($this->_options);
        $options = str_replace("'false'", "false", $options);

        $js = "jQuery('#{$id}')";

        $js .= ".nivoSlider({$options});";
        $cs->registerScript(__CLASS__ . '#' . $id, $js);
    }

}
