<?php

class Intense_Popover extends Intense_Shortcode {

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

		global $intense_visions_options;

		$this->title = __( 'Popover', 'intense' );
		$this->description = __( 'Add popover tooltips to elements using the popover shortcode. Choose from four different locations: top, bottom, left, right. There are several additional options including the popover color, border radius (rounded corners), border color, opacity, and more.', 'intense' );
		$this->category = __( 'Typography', 'intense' );
		$this->show_preview = true;
		$this->preview_content = __( "Content", 'intense' );
		$this->vc_map_content = 'textfield';		
	}

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

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

		$font_color = intense_get_plugin_color( $font_color );
		$tooltip_title_background_color = intense_get_plugin_color( $tooltip_title_background_color );
		$tooltip_background_color = intense_get_plugin_color( $tooltip_background_color );
		$tooltip_font_color = intense_get_plugin_color( $tooltip_font_color );
		$tooltip_border_color = intense_get_plugin_color( $tooltip_border_color );

		intense_add_style( 'qtip2' );
		intense_add_script( 'qtip2' );
		intense_add_script( 'imagesloaded' );
		intense_add_script( 'intense.tooltip' );
		intense_add_script( 'intense.popover' );

		if ( $link != '' ) {
			if ( is_numeric( $link ) ) {
				$link = get_permalink( $link );
			}

			if ( !empty( $link_rel ) ) $link_rel = ' rel="' . $link_rel . '"';

			$open_tag = "a" . $link_rel . " href='$link' target='$link_target'";
			$close_tag = "a";

			if ( $font_color == '' ) {
				$style = "";
			} else {
				$style = "color: " . $font_color . ";";
			}
		} else {
			$open_tag = "span";
			$close_tag = "span";
			$style = "border-bottom: 2px dotted #dfdfdf;";

			if ( $font_color != '' ) {
				$style .= " color: " . $font_color . ";";
			}
		}

		switch ( $location ) {
		case 'top':
			$location = 'bottom center';
			$at_location = 'top center';
			break;
		case 'right':
			$location = 'left center';
			$at_location = 'right center';
			break;
		case 'bottom':
			$location = 'top center';
			$at_location = 'bottom center';
			break;
		case 'left':
			$location = 'right center';
			$at_location = 'left center';
			break;
		default:
			// code...
			break;
		}

		return "
	<style>
		.qtip.$id {
			background-color: $tooltip_background_color;
			color: $tooltip_font_color;
			border-color: $tooltip_border_color;
			border-radius: $tooltip_border_radius;
			opacity: $tooltip_opacity;
		}

		.qtip.$id .qtip-titlebar {
			background-color: $tooltip_title_background_color;
		}
	</style>
	<$open_tag id='$id' class='" . ( !empty( $class ) ? ' ' . $class : '' ) . "' data-intenseqtip='popover' data-titlebar='true' data-trigger='hover' data-placement='$location' data-at-placement='$at_location' title=\"" . esc_attr( $title ) . "\" data-content=\"" . esc_attr( $text ) . "\" style='$style'>" . do_shortcode( $content ) . "</$close_tag>";
	}
}
