<?php
add_action('widgets_init', 'serpentsoft_tweets_widget_init');

function serpentsoft_tweets_widget_init() {
    register_widget('serpentsoft_tweets_widget');
}

class serpentsoft_tweets_widget extends WP_Widget {

    function serpentsoft_tweets_widget() {
        $widget_ops = array('classname' => 'w-tweets');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-tweets');
        $this->WP_Widget('id-widget-tweets', theme_name . ' - Twitter', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        extract($args);

        $title = apply_filters('widget_title', $instance['widget_title']);

        echo $before_widget;
        if ($title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif; ?>

        <div class="clearfix">
            <ul class="tweet_list">   
                <?php $consKey = serpentsoft_get_data('serpentsoft_twitterAPI_consumerkey');
                $conSecret = serpentsoft_get_data('serpentsoft_twitterAPI_consumersecret');
                $accToken = serpentsoft_get_data('serpentsoft_twitterAPI_accesstoken');
                $accSecret = serpentsoft_get_data('serpentsoft_twitterAPI_accesstokensecret');
                $scrName = serpentsoft_get_data('serpentsoft_twitterAPI_screenName');
                $tweetsCount = serpentsoft_get_data('serpentsoft_TwitterTickerFooter_slider_TweetsNumber');
                $tweets_avatar_size = serpentsoft_get_data('serpentsoft_TwitterTickerFooter_slider_AvatarSize');

                if( !empty($consKey) && !empty($conSecret) && !empty($accToken) && !empty($accSecret) && !empty($scrName) ){
                    serpentsoft_twitter_ticker($consKey, $conSecret, $accToken, $accSecret, $scrName, $tweetsCount, 16);
                }
                else{ echo '<li>It seems like you forget type any of your Twitter OAuth Data.</li>'; } ?>

            </ul>
            
            <script type="text/javascript">
                //<![CDATA[
                jQuery(document).ready(function() {
                    "use strict";
                    var slideSpeed = <?php echo serpentsoft_get_data('serpentsoft_TwitterTickerFooter_slider_SlideSpeed'); ?>;
                    var pauseInSeconds = <?php echo serpentsoft_get_data('serpentsoft_TwitterTickerFooter_slider_pauseInSeconds'); ?>;

                    var ul = jQuery(this).find('#<?php echo $args['widget_id']; ?> .tweet_list');
                    var ticker = function() {
                        setTimeout(function() {
                            var top = ul.position().top;
                            var h = ul.height();
                            var incr = (h / ul.children().length);
                            var newTop = top - incr;
                            if (h + newTop <= 0)
                                newTop = 0;
                            ul.animate({top: newTop}, slideSpeed);
                            ticker();
                        }, (pauseInSeconds * 1000) );
                    };
                    ticker();
                });
                //]]>
            </script>
            
        </div>

        <?php
        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['widget_title'] = strip_tags($new_instance['widget_title']);
        return $instance;
    }

    function form($instance) {
        $defaults = array('widget_title' => __('Search', 'serpentsoft'));
        $instance = wp_parse_args((array) $instance, $defaults);
        ?>

        <p>
            <label for="<?php echo $this->get_field_id('widget_title'); ?>"><?php _e('Title', 'serpentsoft'); ?></label>

            <input id="<?php echo $this->get_field_id('widget_title'); ?>" name="<?php echo $this->get_field_name('widget_title'); ?>" value="<?php echo $instance['widget_title']; ?>" type="text" />
        </p>

<?php } } ?>