<?php

/**
 * Description of NivoSLider
 *
 * @author Mohammed Osman  <osmancode@gmail.com>
 */
class NivoSlider extends CWidget {

    public $items = array();
    public $itemTemplate = '<a href="{url}"><img src="{src}" alt="{title}" title="#{title}" /></a>';

    /**
     * @var array the HTML attributes for the portlet container tag.
     */
    public $htmlOptions = array('class' => 'portlet');
    public $options = array(
        'effect' => 'fade', // Specify sets like'=> 'fold,fade,sliceDown'
//        'slices' => 15, // For slice animations
//        'boxCols' => 8, // For box animations
//        'boxRows' => 4, // For box animations
        'animSpeed' => 500, // Slide transition speed
        'pauseTime' => 2500, // How long each slide will show
//        'startSlide' => 0, // Set starting Slide (0 index)
        'directionNav' => false, // Next & Prev navigation
        'controlNav' => false, // 1,2,3... navigation
        'controlNavThumbs' => false, // Use thumbnails for Control Nav
//        'pauseOnHover' => true, // Stop animation while hovering
//        'manualAdvance' => false, // Force manual transitions
//        'prevText' => 'Prev', // Prev directionNav text
//        'nextText' => 'Next', // Next directionNav text
//        'randomStart' => false, // Start on a random slide
//        'beforeChange' => 'js:function(){}', // Triggers before a slide transition
//        'afterChange' => 'js:function(){}', // Triggers after a slide transition
//        'slideshowEnd' => 'js:function(){}', // Triggers after all slides have been shown
//        'lastSlide' => 'js:function(){}', // Triggers when last slide is shown
//        'afterLoad' => 'js:function(){}', // Triggers when slider has loaded
    );
    public $registerCssFiles = true;
    public $theme = 'default';

    function init() {
        if (array_key_exists('class', $this->htmlOptions)) {
            $this->htmlOptions['class'] .= " nivoSlider ";
        } else {
            $this->htmlOptions['class'] = " nivoSlider ";
        }
        $cs = Yii::app()->getClientScript();
        $id = $this->getId();
        $assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets');
        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');
        $options = CJavaScript::encode($this->options);

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

    public function run() {
        return parent::run();
    }

}
