<?php

class Intense_Hr extends Intense_Shortcode {

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

		global $intense_visions_options;

		$this->title = __( 'Horizontal Ruler', 'intense' );
		$this->description = __( 'The horizontal ruler shortcode can be used to separate portions of the page. With the amount of options available, it can also be a powerful design tool. You have the option to use horizontal or vertical rulers. You can even use your own images or any of our predefined shadows.', 'intense' );
		$this->category = __( 'Elements', 'intense' );
		$this->show_preview = 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;
		}

		$color = intense_get_plugin_color( $color );

		if ( empty( $color ) ) {
			$color = $intense_visions_options['intense_content_box_style']['border-color'];
		}

		$border_color = 'border-color: ' . $color . ';';

		$title_background_color = intense_get_plugin_color( $title_background_color );

		if ( empty( $title_background_color ) ) {
			$title_background_color = $intense_visions_options['intense_title_background_color'];
		}

		$title_color = intense_get_plugin_color( $title_color );

		if ( !empty( $icon_type ) ) {
			$icon_shortcode = intense_run_shortcode( 'intense_icon', array( 
				'source' => $icon_source,
				'type' => $icon_type, 
				'size' => $icon_size, 
				'color' => $icon_color, 
				'spin' => $icon_spin, 
				'rotate' => $icon_rotate, 
				'flip' => $icon_flip,
				'stack_source' => $icon_stack_source,
				'stack_type' => $icon_stack_type,
				'stack_color' => $icon_stack_color ) ) ;		
		}

		if ( !empty( $imageid ) ) {
			$photo = Intense_Photo_Source::get_wordpress_photo( $imageid, $size );			
			$photo_url = $photo['url'];
			$w = $photo['width'];
			$h = $photo['height'];

			if ( empty( $imagedimension ) && $direction == "horizontal" ) {
				$imagedimension = $h;
			}

			if ( empty( $imagedimension ) && $direction == "vertical" ) {
				$imagedimension = $w;
			}
		} else if ( !empty( $imageurl ) ) {
			$photo_url = $imageurl;
		}
		
		if ( empty( $id ) ) {
			$client_id = 'hr_' . rand();
		} else {
			$client_id = $id;
		}

    	$id = ' id="' . $client_id . '"';

    	if ( is_numeric( $margin_top ) ) $margin_top .= 'px';
    	if ( is_numeric( $margin_bottom ) ) $margin_bottom .= 'px';

		$output = "<div" . $id . " class='intense hr $direction" .
			" hr-$title_position " .
			( $direction == 'vertical' ? ' hr-' . $type : '' ) .
			( !empty( $class ) ? ' ' . $class : '' ) .
			" $size' style='" .
			( $direction == 'vertical' ? 'height: ' . $height . 'px; ' . $border_color . ' ' : '' ) .
			( !empty( $photo_url ) && $direction == "vertical" ? " width: " . ( !empty( $imagedimension ) ? $imagedimension : 16 ) . "px; " : "" ) .
			( !empty( $margin_top ) ? ' margin-top:' . $margin_top . ';' : '' ) .
			( !empty( $margin_bottom ) ? ' margin-bottom:' . $margin_bottom . ';' : '' ) .
			"'>";

		if ( !empty( $title ) || !empty( $icon_type ) ) {
			$padding_adjustment = null;

			if ( $size != "large" && ( empty( $title_background_color ) || $title_background_color == $intense_visions_options['intense_title_background_color'] ) ) {				
				if ( $title_position == 'left' ) {
					$padding_adjustment = "padding-left: 0px;";
				} else if ( $title_position == 'right' ) {
					$padding_adjustment = "padding-right: 0px;";
				}
			}

			if ( $direction == 'vertical' ) {
				$padding_adjustment = '';
			}

			$title_radius = !empty( $title_radius ) ? " border-radius: {$title_radius}px;" : '';
			$title_color = !empty( $title_color ) ? " color: {$title_color};" : '';

			$output .= "<$title_tag class='intense hr-title " . ( $title_shadowed ? "hr-title-shadowed" : "" ) . "' style='background-color: $title_background_color;" . $title_radius . $title_color . " $padding_adjustment'>";

			intense_add_script( 'intense.hr' );
		}

		if ( !empty( $icon_type ) && $icon_position == 'left' ) {
			$output .= $icon_shortcode . " ";
		}

		$output .= $title;

		if ( !empty( $icon_type ) && $icon_position == 'right' ) {
			$output .= " " . $icon_shortcode;
		}

		if ( !empty( $title ) || !empty( $icon_type )  ) {
			$output .= "</$title_tag>";
		}

		if ( !empty( $accent_position ) ) {
			if ( is_numeric( $accent_width ) ) $accent_width .= "px";
			if ( is_numeric( $accent_height ) ) $accent_height .= "px";

			$accent_color = intense_get_plugin_color( $accent_color );

			$accent_width_amount = preg_replace( "/[^0-9]/","", $accent_width );

			switch ( $accent_position ) {
				case 'left':
					$accent_position = 'left: 0;';
					break;
				case 'right':
					$accent_position = 'right: 0;';
					break;
				case 'center':
					$accent_position = 'left: calc(50% - ' . $accent_width_amount / 2 . 'px);';
					break;
				default:
					$accent_position = 'left: 0;';
					break;
			}

			$GLOBALS['IntensePluginInlineStyle']['#' . $client_id] = "
				#{$client_id} > .hr-inner::before {
					content: '';
					width: {$accent_width};
					display: inline-block;
					background: {$accent_color};
					height: {$accent_height};
					top: calc(-{$accent_height} - 1px);
					position: absolute;
					{$accent_position}
				} 
			";
		}

		$output .= "<span class='intense hr-inner hr-$type " . ( $shadow ? "shadow " : "" ) . "' style='" .
			( $shadow ? "$border_color border-width: 0; border:0px; border-radius: 0px; box-shadow: 0 0 0; " : "$border_color " ) .
			( !empty( $photo_url ) && $direction == "vertical" ? " width: " . ( !empty( $imagedimension ) ? $imagedimension : 16 ) . "px; " : "" ) .
			"'>" .
			( $shadow ? "<img src='" . INTENSE_PLUGIN_URL . "/assets/img/shadow{$shadow}.png' />" : "" ) .
			( !empty( $photo_url ) && ( $imagemode == 'full' ) ? "<div style='text-align: center;'><img src='" . $photo_url . "' style='max-width:100%;' /></div>" : "" ) .
			( !empty( $photo_url ) && ( $imagemode == 'repeat' ) ? "<div style='text-align: center; " .
			( !empty( $photo_url ) && $direction == "horizontal" ? "height: " . ( !empty( $imagedimension ) ? $imagedimension : 16 ) . "px;" : "" ) .
			( !empty( $photo_url ) && $direction == "vertical" ? "width: " . ( !empty( $imagedimension ) ? $imagedimension : 16 ) . "px; height: " . $height . "px; " : "" ) .
			" background: url(" . $photo_url . ") repeat;'></div>" : "" ) .
			"</span>";
		$output .= "</div>";

		return $output;
	}
}
