<?php

class Intense_Slider extends Intense_Shortcode {

    function __construct() {
        parent::__construct();

        global $intense_visions_options;

        $this->title = __( 'Slider', 'intense' );
        $this->description = __( 'The slider shortcode has multiple different slider types to choose from. The different slider types have their own strengths and limitations. Each slider is mobile responsive.', 'intense' );
        $this->category = __( 'Elements', 'intense' );
        $this->show_preview = false;
        $this->is_container = true;
    }

    function shortcode( $atts, $content = null ) {
        global $intense_visions_options;
        extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

        global $intense_slider_type;

        $intense_slider_type = $type;
        $wrapper_start = "";
        $wrapper_end = "";
        $shadow_style = "";
        $controls = "";
        $controls_before = true;
        $slider_id = 'slider_' . rand();
        $random = rand();

        if ( !empty( $id ) && $id != '' ) {
            $slider_id = $id;
            $random = $id;
        } 

        $output = "<script> jQuery(function($){";

        $slider = null;

        switch ( $type ) {
        case 'owl-carousel':
            intense_add_style( 'owlcarousel' );
            intense_add_style( 'owlcarousel-theme' );            
            intense_add_script( 'owlcarousel' );

            if ( $transition != 'slide' && !empty( $transition ) ) {
                intense_add_style( 'owlcarousel-transitions' );                
            }

            $navigation_color = intense_get_plugin_color( $navigation_color );
            $navigation_font_color = intense_get_plugin_color( $navigation_font_color );

            $prev = '<span class="prev">' . intense_run_shortcode( 'intense_button', array(
                'size' => $navigation_size,
                'color' => $navigation_color,
                'font_color' => $navigation_font_color,
                'border_radius' => $navigation_prev_border_radius,
                'icon_source' => $navigation_prev_icon_source,
                'icon' => $navigation_prev_icon,
                'link' => 'javascript: void(0)'
            ), $navigation_prev_text ) . '</span>';
            $next = '<span class="next">' . intense_run_shortcode( 'intense_button', array(
                'size' => $navigation_size,
                'color' => $navigation_color,
                'font_color' => $navigation_font_color,
                'border_radius' => $navigation_next_border_radius,
                'icon_source' => $navigation_next_icon_source,
                'icon' => $navigation_next_icon,
                'link' => 'javascript: void(0)',
                'icon_position' => 'right'
            ), $navigation_next_text ) . '</span>';                      

            $title = '';            
            
            $navigation_style = "
                #slider_controls_$random .prev, #slider_controls_$random .next {
                    cursor: pointer;
                    transition: all .2s ease-in;
                }

                #slider_controls_$random {               
                    z-index: 500;                
                }
                #slider_controls_$random .slider-controls {
                    top: 0;
                }
                #$slider_id .owl-controls .owl-page span {
                    background-color: $navigation_color;
                }

                #$slider_id .owl-numbers {
                    color: " . ( empty( $navigation_font_color ) ? intense_get_contract_color( $navigation_color ) : $navigation_font_color ) . ";
                }
            ";

            $controls = '<div id="slider_controls_' . $random . '" style="position: relative;">' . $title  . '<div class="slider-controls">' . $prev . " " . $next . '</div><div class="clearfix"></div></div>';

            $navigation_script = '';

            switch ( $navigation_position ) {
                case 'topleft':
                    $navigation_style .= "#slider_controls_$random .slider-controls { float: left; padding: 0 20px 10px 0; }";
                    break;                
                case 'topcenter':
                    $navigation_style .= "#slider_controls_$random .slider-controls { margin: 0px auto; text-align:center; padding: 0 20px 10px 20px; }";
                    break;
                case 'topright':
                    $navigation_style .= "#slider_controls_$random .slider-controls { float: right; padding: 0 0 10px 20px; }";
                    break;
                case 'middleinside':
                    $navigation_style .= "#slider_controls_$random .slider-controls .prev { left: 0; position: absolute; display: none; opacity: .65; }";
                    $navigation_style .= "#slider_controls_$random .slider-controls .next { right: 0; position: absolute; display: none; opacity: .65; }";
                    $navigation_style .= "#slider_controls_$random .slider-controls .prev:hover { opacity: 1; }";
                    $navigation_style .= "#slider_controls_$random .slider-controls .next:hover { opacity: 1; }";
                    $navigation_script = "                    
                        $('.owl-wrapper-outer, #slider_controls_$random .next, #slider_controls_$random .prev').mouseover(function() {                    
                            $('#slider_controls_$random .next').show();
                            $('#slider_controls_$random .prev').show();
                        }).mouseout(function() {                   
                            $('#slider_controls_$random .next').hide();
                            $('#slider_controls_$random .prev').hide();
                        });
                    ";
                    break;
                case 'middleoutside':
                    $navigation_style .= "#slider_controls_$random .slider-controls .prev { left: 0px; position: absolute; }";
                    $navigation_style .= "#slider_controls_$random .slider-controls .next { right: 0px; position: absolute; }";

                    //wrap the slider in a div and give that div the same amount of padding
                    //that is required to fit the prev and next buttons on both sizes
                    $output .= " 
                        var prevWidth = $('#slider_controls_$random .prev').outerWidth() + 6;
                        var nextWidth = $('#slider_controls_$random .next').outerWidth() + 6 ;  
                        var sliderwrapper = $('<div>');
                        
                        $('#$slider_id').wrap(sliderwrapper);
                        var container = $('#$slider_id').parent();
                        container.css('padding-left', prevWidth + 'px');
                        container.css('padding-right', nextWidth + 'px');
                        var shadow = $('.intense.shadow', container.parent().parent());

                        shadow.appendTo($('#$slider_id').parent());
                    ";
                    break;
                case 'bottomleft':
                    $navigation_style .= "#slider_controls_$random .slider-controls { left: 0; padding: 10px 20px 0 0; }";
                    $controls_before = false;
                    break;
                case 'bottomcenter':
                    $navigation_style .= "#slider_controls_$random .slider-controls { margin: 0px auto; text-align:center; padding: 10px 20px 0 20px; }";
                    $controls_before = false;
                    break;
                case 'bottomright':
                    $navigation_style .= "#slider_controls_$random .slider-controls { float: right; padding: 10px 0 0 20px; }";
                    $controls_before = false;
                    break;
                default:
                    $controls = '';
                    break;
            }     

            $transition = ($transition == 'slide' ? 'false' : "'$transition'" );

            $output .= "
                var owl_$random;
                owl_$random = $('#$slider_id').owlCarousel({                    
                    stopOnHover: " . ( $pause_on_hover ? 'true' : 'false' ) . ",                    
                    items: $items,
                    itemsDesktop: [1000,$items_desktop],
                    itemsDesktopSmall: [900,$items_desktop_small],
                    itemsTablet: [600,$items_tablet],
                    itemsMobile: [600,$items_mobile],
                    autoHeight: " . ( $autoheight ? 'true' : 'false' ) . ",
                    pagination: " . ( !empty( $navigation_pagination_type ) && $navigation_pagination_type == 'none' ? 'false' : 'true' ) . ",
                    paginationNumbers: " . ( $navigation_pagination_type === 'numbers' ? 'true' : 'false' ) . ",                    
                    afterAction: function() {
                        setTimeout(function() {
                            intenseAdjustOwlNavigation();
                        }, 500);
                    },
                    //scrollPerPage: true,
                    autoPlay: $speed,
                    slideSpeed: 1000,
                    transitionStyle: $transition,
                    mouseDrag: " . ( $mousedrag ? 'true' : 'false' ) . ",
                    touchDrag: " . ( $touchdrag ? 'true' : 'false' ) . ",
                    afterMove: intenseAdjustOwlNavigation
                });

                $('#slider_controls_$random .next').click(function(){
                    owl_$random.trigger('owl.next');
                });            

                $('#slider_controls_$random .prev').click(function(){
                    owl_$random.trigger('owl.prev');
                });

                $('#$slider_id').parent().find('.intense.shadow').css('margin-top', '-' + ($('#$slider_id .owl-controls').outerHeight(true)) + 'px');

                function intenseAdjustOwlNavigation() {
                    var prevWidth = $('#slider_controls_$random .prev').outerWidth() + 5;
                    var nextWidth = $('#slider_controls_$random .next').outerWidth() + 5 ;

                    $('#slider_controls_$random .next').css('margin-top', $('.owl-wrapper-outer', $('#$slider_id').parent()).height() / 2 - $('#slider_controls_$random .next').height() / 2);
                    $('#slider_controls_$random .prev').css('margin-top', $('.owl-wrapper-outer', $('#$slider_id').parent()).height() / 2 - $('#slider_controls_$random .prev').height() / 2);                              
                }                
                
                intenseAdjustOwlNavigation();

                $navigation_script
            ";   

            break;
        case 'flexslider':
            intense_add_style( 'intense_flexslider' );
            intense_add_script( 'intense_flexslider' );

            $output .= " $(document).ready(function() {";
            $output .= '    $(".intense.flexslider").hover(function(){
                                $(this).find(".flex-direction-nav a").css("opacity", ".8");
                            }, function(){
                                $(this).find(".flex-direction-nav a").css("opacity", "0");
                            });';
            $output .= " });";

            $output .= "$('#$slider_id').flexslider({
                        slideshowSpeed: " . $speed . ",
                        pauseOnHover: " . ( $pause_on_hover ? 'true' : 'false' ) . ",
                        prevText: '',
                        nextText: '',
                        animation: '$transition',"
                        . ( isset( $item_width ) ? "itemWidth: $item_width, itemMargin: 5, animationLoop: false, " : "" ) .
                        "//controlNav: 'thumbnails',
                        });";

            $wrapper_start = "<ul class='intense slides' $slider>";
            $wrapper_end = "</ul>";
            break;
        case 'basic':
            intense_add_script( 'intense.basicslider' );

            $slider = "data-basicslider='true'";

            $output .= " $(window).load(function() {";
            $output .= "    $('[data-basicslider=\"true\"]').basicslider({
                            speed: " . $speed . ",
                            pauseOnHover: " . ( $pause_on_hover ? 'true' : 'false' ) . "
                            });";
            $output .= " });";
            
            $wrapper_start = "<ul class='intense slides' $slider>";
            $wrapper_end = "</ul>";
            break;
        default:
            break;
        }

        $output .= "}); </script>";

        if ( isset( $navigation_style ) ) {
            $output .= "<style>" . $navigation_style . "</style>";
        }

        if ( $shadow ) {
            $output .= "<div><div style='margin-bottom: 0;'>";
        }

        if ( $controls_before ) {
            $output .= $controls;
        }

        $output .= "<div id='$slider_id' class='intense slider $type " . ( !empty( $class ) ? ' ' . $class : '' ) . " " . ( isset( $item_width ) ? "carousel" : "" ) . "' style='position: relative;'>" .             
            $wrapper_start . do_shortcode( $content ) . $wrapper_end . 
            "</div>";

        if ( $shadow ) {
            $output .= "</div><img src='" . INTENSE_PLUGIN_URL . "/assets/img/shadow{$shadow}.png' class='intense shadow' style='vertical-align: top; border:0px; border-radius: 0px; box-shadow: 0 0 0; $shadow_style' alt='' /></div>";
        }

        if ( !$controls_before ) {
            $output .= $controls;
        }

        return $output;
    }

    
}
