<?php

class Intense_Animated_Image extends Intense_Shortcode {

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

		global $intense_visions_options;

		$this->title = __( 'Animated Image', 'intense' );
		$this->description = __( 'The animated image shortcode adds animated images that slide left, right, up, or down along the screen. You can also control the speed and position of the image. Multiple images can be added together to create impressive effects.', '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 ) );

		$output = "";

		intense_add_script( 'intense.animated.image' );

		if ( is_numeric( $image ) ) {
			$imageid = $image;
		} else if ( !empty( $image ) ) {
			$imageurl = $image;
		}

		if ( !empty( $imageid ) ) {
			$photo = Intense_Photo_Source::get_wordpress_photo( $imageid, $size );

			$photo_url = $photo['url'];
			$photo_width = $photo['width'];
		} else if ( !empty( $imageurl ) ) {
			$photo_url = $imageurl;
		} else {
			$photo_url = '';
		}

		switch ( $anchor ) {
			case 'top':
				$background_position_percent = "0";
				break;
			case 'bottom':
				$background_position_percent = "100";
				break;
			case 'left':
				$background_position_percent = "0";
				break;
			case 'right':
				$background_position_percent = "100";
				break;
			case 'middle':
				$background_position_percent = "50";
				break;
			default:
				$background_position_percent = "0";
				break;
		}

		$id = ( !empty ( $id ) ) ? ' id="' . $id . '"' : '';

		if ( $type == "slideleft" || $type == "slideright" || $type == "slideup" || $type == "slidedown" ) {
			$output .= '<div' . $id . ' class="intense animated-image slide' . ( !empty( $class ) ? ' ' . $class : '' ) . '" style="' .
				'z-index: ' . $zindex . '; ' .
				'height: 100%;' .
				( isset( $photo_width ) && ( $type == 'slideup' || $type == 'slidedown' ) ? 'width: ' . $photo_width . 'px;' : '' ) .
				'position: absolute; top: 0; bottom: 0; left: 0; right: 0; ' .
				'background-image: url(' . $photo_url . '); background-repeat: repeat-' . ( ( $type == 'slideup' || $type == 'slidedown' ) ? 'y' : 'x' ) . ';' .
				'background-position-' . ( ( $type == 'slideup' || $type == 'slidedown' ) ? 'x:' . $background_position_percent . '%;' : 'y:' . $background_position_percent . '%;' ) . 
				'" data-speed="' . $speed . '" data-direction="' . $type . '" data-anchor="' . $background_position_percent . '"></div>';
		} else if ( $type == "scrollleft" || $type == "scrollright" || $type == "scrollup" || $type == "scrolldown" ) {
			if ( !isset( $height ) ) {
			 	$height = $photo['height'];
			}

			if ( is_numeric( $height ) ) $height .= 'px';
			if ( is_numeric( $width ) ) $width .= 'px';
			
			switch ( $type ) {
				case 'scrollleft':
					$image_css = " left: 0; max-width: none; max-height: " . $height . ';';
					$container_css = 'height: ' . $height . ';width: ' . $width . ';';
					break;
				case 'scrollright':
					$image_css = " right: 0; max-width: none; max-height: " . $height . ';';
					$container_css = 'height: ' . $height . ';width: ' . $width . ';';
					break;
				case 'scrollup':
					$image_css = " top: 0; width:100%;";
					$container_css = 'height: ' . $height . ';width: ' . $width . ';';
					break;
				case 'scrolldown':
					$image_css = " bottom: 0; width:100%;";
					$container_css = 'height: ' . $height . ';width: ' . $width . ';';
					break;
			}

			$output .= '<div' . $id . ' class="intense animated-image scroll' . ( !empty( $class ) ? ' ' . $class : '' ) . '" style="position: relative; overflow: hidden;' .
				$container_css .
				'" data-speed="' . $speed . '" data-direction="' . $type . '" data-anchor="' . $background_position_percent . '">';
			$output .= '<img src="' . $photo_url . '" style="position: absolute;' . $image_css . ' transition: all ' . $speed . 'ms ease-in-out;" />';			
			$output .= '</div>';
		}

		return $output;
	}
}
