<?php

class Intense_Google_Docs_Viewer extends Intense_Shortcode {

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

		global $intense_visions_options;

		$this->title = __( 'Google Docs Viewer', 'intense' );
		$this->description = __( 'Add a document (ex. PDF, PowerPoint, Word, etc.) to your WordPress websites using Google Docs Viewer. Documents can be embedded into the page, added as a link, or as a URL. More information about Google Docs Viewer can be found at <a target="_blank" href="https://docs.google.com/viewer">https://docs.google.com/viewer</a>.
			<br><br>
			Using the Google Docs Viewer shortcode makes it convenient for your users to view documents without having to leave your site or open a document in a separate program.', 'intense' );
		$this->category = __( 'Media', 'intense' );
		$this->show_preview = true;
		$this->preview_content = __( "Link Text", 'intense' );
	}

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

		if ( is_numeric( $document ) ) {
			$url = wp_get_attachment_url( $document );
		} else if ( !empty( $document ) ) {
			$url = $document;
		}

		if ( empty( $width ) ) {
			$width = "100%";			
		}

		if ( empty( $height ) ) {
			$height = "800px";
		}

		if ( !empty ($url) ) {
			switch ($type) {
				case 'url':
					return 'http://docs.google.com/viewer?url=' . urlencode( $url );
					break;
				case 'link':
					if ( !empty( $link_rel ) ) $link_rel = ' rel="' . $link_rel . '"';

					return '<a' . $link_rel . ' href="http://docs.google.com/viewer?url=' . urlencode( $url ) . '" target="' . $target . '">' . do_shortcode( $content ) . '</a>';
					break;
				case 'embedded':
					return '<iframe src="//docs.google.com/viewer?url=' . urlencode( $url ) . '&embedded=true" width="' . $width . '" height="' . $height . '" style="border: none;"></iframe>';
					break;
			}
		}
	}	
}
