<?php

class Intense_Video extends Intense_Shortcode {

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

    global $intense_visions_options;

    $this->title = __( 'Video', 'intense' );
    $this->description = __( 'Add responsive videos to your WordPress website using the video shortcode. Show videos from many different sources.', 'intense' );
    $this->category = __( 'Media', 'intense' );
    $this->show_preview = true;
  }

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

    $lazy_load = isset( $lazy_load ) ? $lazy_load : isset( $intense_visions_options['intense_lazy_load'] ) ? $intense_visions_options['intense_lazy_load'] : true ;

    $vidcontainer = "video-container-" . rand();

    if ( !empty( $id ) ) {
      $video_id = $id;
    } else {
      $video_id = "video-" . rand();
    }

    if ( !empty( $video_size ) ) {
      $size = $video_size;
    } else {
      $size = "auto";
    }

    if ( !empty( $poster_url ) || !empty( $poster_id ) ) {
      intense_add_style( 'intense_video_js_css' );
      intense_add_script( 'intense_video_js' );
    } elseif ( !empty( $video_url ) ) {
      intense_add_script( 'intense_fitvids' );
    }

    if ( is_numeric( $poster ) ) {
      $poster_id = $poster;
    } else if ( !empty( $poster ) ) {
        $poster_url = $poster;
      }

    if ( !empty( $poster_id ) ) {
      $photo = Intense_Photo_Source::get_wordpress_photo( $poster_id, 'large1600' );      
      $poster_url = $photo['url'];
    } else if ( !empty( $poster_url ) ) {
        $poster_url = $poster_url;
      }

    if ( is_numeric( $mp4 ) ) {
      $mp4_id = $mp4;
    } else if ( !empty( $mp4 ) ) {
        $mp4_url = $mp4;
      }

    if ( !empty( $mp4_id ) ) {
      $mp4_url = wp_get_attachment_url( $mp4_id );
    } else if ( !empty( $mp4_url ) ) {
        $mp4_url = $mp4_url;
      }

    if ( is_numeric( $ogv ) ) {
      $ogv_id = $ogv;
    } else if ( !empty( $ogv ) ) {
        $ogv_url = $ogv;
      }

    if ( !empty( $ogv_id ) ) {
      $ogv_url = wp_get_attachment_url( $ogv_id );
    } else if ( !empty( $ogv_url ) ) {
        $ogv_url = $ogv_url;
      }

    if ( is_numeric( $webm ) ) {
      $webm_id = $webm;
    } else if ( !empty( $webm ) ) {
        $webm_url = $webm;
      }

    if ( !empty( $webm_id ) ) {
      $webm_url = wp_get_attachment_url( $webm_id );
    } else if ( !empty( $webm_url ) ) {
        $webm_url = $webm_url;
      }

    $output = "";

    if ( isset( $poster_url ) || isset( $poster_id ) ) {
      $output .= "<style>";
      $output .= ".videocontent {";
      $output .= "width: 100%;";
      $output .= "margin: 0 auto;";
      $output .= "}";
      $output .= ".video-js {";
      $output .= "width: 100%;";
      $output .= "}";
      $output .= ".vjs-fullscreen {";
      $output .= "  padding-top: 0px;";
      $output .= "}";
      $output .= "</style>";

      $output .= "<div id='" . $vidcontainer . "' class='videocontent'>";
      $output .= "<video id='" . $video_id . "' class='video-js vjs-default-skin vjs-big-play-centered" . ( !empty( $class ) ? ' ' . $class : '' ) . "' controls
      preload='auto' width='auto' height='auto' poster='" . $poster_url . "'
      data-setup='{}'>" .
        ( isset( $mp4_url ) ? "<source " . ( $lazy_load ? 'data-' : '' ) . "src='" . $mp4_url . "' type='video/mp4'>," : "" ) .
        ( isset( $ogv_url ) ? "<source " . ( $lazy_load ? 'data-' : '' ) . "src='" . $ogv_url . "' type='video/ogg'>," : "" ) .
        ( isset( $webm_url ) ? "<source " . ( $lazy_load ? 'data-' : '' ) . "src='" . $webm_url . "' type='video/webm'>," : "" ) .
        "</video>";
      $output .= "</div>";
    } elseif ( isset( $video_url ) || isset( $user ) || isset( $search ) ) {
      intense_add_script( 'intense.video' );
      intense_add_script( 'intense_fitvids' );

      if ( isset( $video_url ) ) {
        $embed_code = wp_oembed_get( $video_url );
      } else {
        $embed_code = wp_oembed_get( 'https://www.youtube.com/watch?v=CRtgnF_b2po' );
      }

      switch ( $video_type ) {
        case 'vevo':
          //url - http://www.vevo.com/watch/nirvana/In-Bloom-Live-at-Reading-1992/USUV70902845
          //embed - <iframe width="575" height="324" src="http://cache.vevo.com/assets/html/embed.html?video=USUV70500154&autoplay=0" frameborder="0" allowfullscreen></iframe>
          
          $video_id = substr( $video_url, strrpos( $video_url, '/' )+1 );

          $embed_code = '<iframe width="575" height="324" src="http://cache.vevo.com/assets/html/embed.html?video=' . $video_id . '&autoplay=' . ( $autoplay ? 1 : 0 )  . '" frameborder="0" allowfullscreen></iframe>';
          break;
        case 'youtube':
          $params = '';

          if ( isset( $user ) ) {
            $params = 'listType=user_uploads&list=' . $user;
          }

          if ( isset( $search ) ) {
            $params = 'listType=search&list=' . $search;
          }

          if ( isset( $user ) || isset( $search ) ) {
            if ( empty( $show_related ) ) {
              $embed_code = preg_replace( '/(youtube\.com.embed)(.*)(\?feature=oembed)(.*)/', '$1?' . apply_filters( "intense_extra_youtube_querystring_parameters", "wmode=transparent&amp;" ) . $params . '&rel=0&theme=' . $theme . '$4', $embed_code );
            } else {
              $embed_code = preg_replace( '/(youtube\.com.embed)(.*)(\?feature=oembed)(.*)/', '$1$3&' . $params . '&theme=' . $theme . '$4', $embed_code );
            }
          }

          if ( isset( $playlist ) ) {
            $params = 'playlist=' . $playlist;

            if ( empty( $show_related ) ) {
              $embed_code = preg_replace( '/(youtube\.com.*)(\?feature=oembed)(.*)/', '$1?' . apply_filters( "intense_extra_youtube_querystring_parameters", "wmode=transparent&amp;" ) . $params . '&rel=0&theme=' . $theme . '$3', $embed_code );
            } else {
              $embed_code = preg_replace( '/(youtube\.com.*)(\?feature=oembed)(.*)/', '$1$2&' . $params . '&theme=' . $theme . '$3', $embed_code );
            }
          }

          if ( $params == '' && empty( $show_related ) )
            $embed_code = preg_replace( '/(youtube\.com.*)(\?feature=oembed)(.*)/', '$1?' . apply_filters( "intense_extra_youtube_querystring_parameters", "wmode=transparent&amp;" ) . 'rel=0&theme=' . $theme . '$3', $embed_code );
          
          break;
        case 'vimeo':
          $embed_code = preg_replace( '/(player.vimeo.com.video.\d+)(.*)/', '$1?badge=' . ( $show_badge ? 1 : 0 ) . '&byline=' . ( $show_byline ? 1 : 0 ) . '&portrait=' . ( $show_portrait ? 1 : 0 ) . '&title=' . ( $show_title ? 1 : 0 ) . '&loop=' . ( $loop ? 1 : 0 ) . '&autoplay=' . ( $autoplay ? 1 : 0 ) . '$2', $embed_code );
          break;
      }

      if ( $lazy_load ) {
        intense_add_script( 'lazyloadxt' );
        intense_add_script( 'lazyloadxt.extra' );
        intense_add_script( 'intense.lazyload' );
        intense_add_style( 'lazyloadxt.spinner' );
        $embed_code = str_replace("src", "data-src", $embed_code);
      }

      $output .= "<div id='" . $vidcontainer . "' class='intenseVideoContainer" . ( !empty( $class ) ? ' ' . $class : '' ) . "'" . ( $video_type == "qik" || $video_type == "flickr" ? "style='padding-top:0px'" : "" ) .">";
      $output .= $embed_code;
      $output .= "</div>";

      $output .= "<style>";
      $output .= "#" . $vidcontainer . " {";
      $output .= "position: relative;";
      $output .= "}";
      $output .= "#" . $vidcontainer . " iframe, #" . $vidcontainer . " object, #" . $vidcontainer . " embed {";
      $output .= "position: absolute;";
      $output .= "top: 0;";
      $output .= "left: 0;";
      $output .= "width: 100%;";
      $output .= "height: 100%;";
      $output .= "overflow: hidden;";
      $output .= "}";
      $output .= "</style>";

      $output .= "<script>";
      $output .= "  jQuery(document).ready(function(){";
      $output .= "    jQuery('#" . $vidcontainer . "').fitVids();";
      $output .= "  });";
      $output .= "</script>";
    }

    return $output;
  }
}
