<?php

class Intense_Promo_Box extends Intense_Shortcode {

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

		global $intense_visions_options;

		$this->title = __( 'Promotional Box', 'intense' );
		$this->description = __( 'The promo box shortcode is perfect for call-outs and promotions. Easily add a button with link, change the size, set the borders, add a shadow, and more.', 'intense' );
		$this->category = __( 'Layout', 'intense' );
		$this->show_preview = true;
		$this->preview_content = __( "Promotional Box Content", 'intense' );
		$this->is_container = true;
	}

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

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

		$backcolor = "";
		$background_image = "";
		$color_style = "";

		if ( empty( $id ) ) $id = 'promo_' . rand();

		$button_id = $id . '_button';

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

		if ( isset( $button_type ) ) {
			$button_color = $button_type;
		}

		if ( !empty( $background_color ) ) {
			$backcolor = intense_get_plugin_color( $background_color );
			$background_color_style = "background-color: " . $backcolor . " !important;";
		} else {
			$background_color_style = "background-color: " . intense_get_plugin_color( "muted" ) . " !important;";
		}

		if ( isset( $background_opacity ) && strlen( $backcolor ) > 0 ) {
			$background_color_style = "background-color: " . intense_get_rgb_color( $backcolor, $background_opacity ) . " !important;";
		}

		if ( !empty( $imageid ) ) {
			$photo = Intense_Photo_Source::get_wordpress_photo( $imageid, $imagesize );			
			$photo_url = $photo['url'];
		} else if ( !empty( $imageurl ) ) {
			$photo_url = $imageurl;
		}

		if ( isset( $photo_url ) ) {
			if ( $imagemode == 'repeat-x' ) {
				$background_image .= " background-image: url($photo_url); background-repeat: repeat-x;";
			} else if ( $imagemode == 'repeat-y' ) {
				$background_image .= " background-image: url($photo_url); background-repeat: repeat-y;";
			} else if ( $imagemode == 'repeat' ) {
				$background_image .= " background-image: url($photo_url); background-repeat: repeat;";
			} else if ( $imagemode == 'no-repeat' ) {
				$background_image .= " background-image: url($photo_url); background-repeat: no-repeat;";
			}
		}

		if ( isset( $color ) ) {
			$color_style = "color: " . intense_get_plugin_color( $color ) . " !important;";
		} 

		$border_style = null;
		$border_top_style = null;
		$border_right_style = null;
		$border_bottom_style = null;
		$border_left_style = null;
		$radius = '';

		if ( isset( $border ) ) $border_style = "border: " . $border . ' !important;';
		if ( isset( $border_top ) ) $border_top_style = "border-top: " . $border_top . ' !important;';
		if ( isset( $border_right ) ) $border_right_style = "border-right: " . $border_right . ' !important;';
		if ( isset( $border_bottom ) ) $border_bottom_style = "border-bottom: " . $border_bottom . ' !important;';
		if ( isset( $border_left ) ) $border_left_style = "border-left: " . $border_left . ' !important;';		
		if ( isset( $border_radius ) ) $radius = 'border-radius:' . $border_radius . ';';

		$output = '';

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

		switch ( $size ) {
		case 'small':
			$box_class = "well well-sm";
			break;
		case 'medium':
			$box_class = "well";
			break;
		case 'large':
			$box_class = "well well-lg";
			break;
		case 'mega':
			$box_class = "jumbotron";
			break;
		default:
			// code...
			$box_class = "well";
			break;
		}

		$output .= "<div $id class='intense $box_class " . ( !empty( $class ) ? ' ' . $class : '' ) . "' style='$shadow_style $background_color_style $color_style $border_style $border_top_style $border_right_style $border_bottom_style $border_left_style $background_image $radius'>";
		if ( isset( $button_text ) && strlen( $button_text ) > 0 ) {
			if ( is_numeric( $button_link ) ) {
				$button_link = get_permalink( $button_link );
			}

			$output .= intense_run_shortcode( 'intense_button', array(
				'id' => $button_id,
				'color' => $button_color,
				'size' => $button_size,
				'class' => 'promo pull-' . $button_position,
				'target' => $button_target,
				'link' => $button_link,
				'rel' => $button_rel
			), $button_text );
		}
		$output .= do_shortcode( $content );
		$output .= "<div class='clearfix'></div></div>";

		if ( $shadow ) {
			$output .= "<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;' alt='' /></div>";
		}

		return $output;
	}
}
