<?php

class Intense_Audio extends Intense_Shortcode {

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

        global $intense_visions_options;

        $this->title = __( 'Audio', 'intense' );
        $this->description = __( 'The audio shortcode lets you play mp3 and ogg files on your WordPress pages. You can play audio from your own site or use a URL instead. The audio shortcode’s colors are very customizable so it is easy to fit it in with your WordPress site design.<br><br>
        The are also options to control the playback. You can choose to loop the audio so it will restart once it gets to the end. You can also choose to automatically play the audio when the page loads.', 'intense' );
        $this->category = __( 'Media', 'intense' );
        $this->show_preview = true;
        $this->is_container = false;
    }

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

        intense_add_script( 'jquery.jplayer' );
        intense_add_script( 'intense.audio' );
        
        $audio_width = ( $width !== 'auto' ) ? 'max-width:' . $width : '';
        $play_icon = intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_play_icon']['source'], 'type' => $intense_visions_options['intense_play_icon']['type'], 'color' => $button_color, 'width' => '15' ) );
        $pause_icon = intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_pause_icon']['source'], 'type' => $intense_visions_options['intense_pause_icon']['type'], 'color' => $button_color, 'width' => '15' ) );
        $backgroundcolor = ' background:' . intense_get_plugin_color( $background_color ) . ';';
        $buttonbackground = 'style="background:' . intense_get_plugin_color( $button_background_color ) . ';"';
        $textcolor = 'style="color:' . intense_get_plugin_color( $text_color ) . ';"';
        $progresscolor = 'style="background:' . intense_get_plugin_color( $progress_color ) . ';"';
        $progressbackground = 'style="background:' . intense_get_plugin_color( $progress_background_color ) . ';"';

        if ( is_numeric( $url ) ) {
            $url = wp_get_attachment_url( $url );
        }

        if ( !empty ( $id ) ) {
            $id_attr = ' id="' . $id . '"';
            $id = 'audio_' . $id;            
        } else {            
            $id_attr = '';
            $id = 'audio_' . rand();
        }        

        $output = '<div' . $id_attr . ' class="intense audio' . ( !empty( $class ) ? ' ' . $class : '' ) . '" data-id="' . $id . '" data-audio="' . $url . '" data-swf="' . INTENSE_PLUGIN_URL . '/assets/js/jquery.jplayer/Jplayer.swf' . '" data-autoplay="' . $autoplay . '" data-loop="' . $loop . '" style="' . $backgroundcolor . '"><div id="' . $id . '" class="jp-jplayer"></div><div id="' . $id . '_container" class="jp-audio"><div class="jp-type-single"><div class="jp-gui jp-interface"><div class="jp-controls"><span class="jp-play" ' . $buttonbackground . '>' . $play_icon . '</span><span class="jp-pause" ' . $buttonbackground . '>' . $pause_icon . '</span><span class="jp-stop"></span><span class="jp-mute"></span><span class="jp-unmute"></span><span class="jp-volume-max"></span></div><div class="jp-progress" style=""><div class="jp-seek-bar" ' . $progressbackground . '><div class="jp-play-bar" ' . $progresscolor . '></div></div></div><div class="jp-volume-bar"><div class="jp-volume-bar-value"></div></div><div class="jp-current-time" ' . $textcolor . '></div><div class="jp-duration" ' . $textcolor . '></div></div><div class="jp-title">' . $title . '</div></div></div></div>';

        return $output;
    }

}