<?php

class Intense_Map extends Intense_Shortcode {

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

        global $intense_visions_options;

        $this->title = __( 'Map', 'intense' );
        $this->description = __( 'The map shortcode allows you to include a Google map on your page. There are a number of options to choose from when creating your map. There are color options, marker options and different layers that can be added to your map.', 'intense' );
        $this->category = __( 'Media', 'intense' );
        //$this->show_preview = true;
        $this->is_container = true;
    }

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

        //google maps api and styled marker
        intense_add_script( 'googlemap' );
        intense_add_script( 'styledmarker' );

        $output = '';
        $random = ( !empty ( $id ) ) ? $id : rand();
        $GLOBALS['map_number'] = $random;
        $mapID = ( !empty ( $id ) ) ? $id : 'map_' . $random;
        $mapFunction = 'Map' . $random;
        $shadow_style = "margin-bottom: 0;";
        $GLOBALS['fit_bounds'] = $fit_bounds;
        $stylers = '';
        $styler_options = '';

        if ( isset( $hue ) || isset( $saturation ) || isset( $lightness ) || isset( $gamma ) || $lightness_invert != 0 ) {
            if ( isset( $hue ) ) {
                $styler_options .= "          { 'hue': '" . $hue . "' },";
            }

            if ( isset( $saturation ) ) {
                $styler_options .= "          { 'saturation': '" . $saturation . "' },";
            }

            if ( isset( $lightness ) ) {
                $styler_options .= "          { 'lightness': '" . $lightness . "' },";
            }

            if ( isset( $gamma ) ) {
                $styler_options .= "          { 'gamma': '" . $gamma . "' },";
            }

            if ( $lightness_invert != 0 ) {
                $styler_options .= "          { 'invert_lightness': true },";
            }

            $styler_options = substr($styler_options, 0, -1);
        }

        $output = '';

        if ( $shadow ) {
            $output .= "<div><div style='$shadow_style'>";
        }

        if ( is_numeric( $width ) ) {
            $width .= 'px';
        }

        if ( is_numeric( $height ) ) {
            $height .= 'px';
        }

        $output .= "<div id='" . $mapID . "' class='mapcanvas" . ( !empty( $class ) ? ' ' . $class : '' ) . "' style='width: " . $width . "; height: " . $height . "; direction: ltr;'></div>";

        if ( $shadow ) {
            $output .= "</div><img src='" . INTENSE_PLUGIN_URL . "/assets/img/shadow{$shadow}.png' class='intense shadow' style='" . ( isset( $width ) ? 'width: ' . $width . '; height: auto;' : '' ) . "vertical-align: top; border:0px; border-radius: 0px; box-shadow: 0 0 0;' alt='' /></div>";
        }

        $output .= "<script type='text/javascript'>";

        if ( !empty( $styler_options ) ) {
            $output .= "      var styles" . $random . " = [";
            $output .= "        {";
            $output .= "          stylers: [";
            $output .= $styler_options;
            $output .= "          ]";
            $output .= "        }";
            $output .= "      ];";
        }

        $output .= "  function " . $mapFunction . "() {";
        $output .= "    latitude" . $random . " = '';";
        $output .= "    longitude" . $random . " = '';";
        $output .= "    var mapOptions" . $random . " = {";
        $output .= "      zoom: " . $zoom . ",";
        $output .= "      scrollwheel: " . ( $scroll_wheel ? 'true' : 'false' ) . ",";
        $output .= "      draggable: " . ( $is_draggable ? 'true' : 'false' ) . ",";
        $output .= "      scaleControl: " . ( $scale_control ? 'true' : 'false' ) . ",";
        $output .= "      streetViewControl: " . ( $street_view ? 'true' : 'false' ) . ",";
        $output .= "      overviewMapControl: " . ( $overview_map ? 'true' : 'false' ) . ",";
        $output .= "      panControl: " . ( $pan_control ? 'true' : 'false' ) . ",";
        $output .= "      mapTypeControl: " . ( $map_type_control ? 'true' : 'false' ) . ",";
        $output .= "      mapTypeControlOptions: {";
        $output .= "        style: google.maps.MapTypeControlStyle." . strtoupper( $map_type_style );
        $output .= "      },";
        $output .= "      zoomControl: " . ( $zoom_control ? "true" : 'false' ) . ",";
        $output .= "      zoomControlOptions: {";
        $output .= "        style: google.maps.ZoomControlStyle." . strtoupper( $zoom_control_style );
        $output .= "      },";
        $output .= "      mapTypeId: google.maps.MapTypeId." . strtoupper( $type ) . ",";
        $output .= "    };";
        $output .= "    var map" . $random . " = new google.maps.Map(document.getElementById('" . $mapID . "'), mapOptions" . $random . ");";

        if ( !empty( $styler_options ) ) {
            $output .= "map" . $random . ".setOptions({styles: styles" . $random . "});";
        }

        $output .= "    var bounds" . $random . " = new google.maps.LatLngBounds();";

        if ( !empty( $latitude ) && !empty( $longitude ) ) {
            $output .= '    map' . $random . '.setCenter(new google.maps.LatLng(' . $latitude . ', ' . $longitude . '));';
        }

        if ( !empty( $address ) ) {
            $output .= 'var geocoder' . $random . ' = new google.maps.Geocoder();';
            $output .= 'geocoder' . $random . '.geocode({ "address": "' . $address . '" }, function (results, status) {';
            $output .= '  if (status == google.maps.GeocoderStatus.OK) {';
            $output .= '    map' . $random . '.setCenter(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));';
            $output .= '  }';
            $output .= '});';
        }

        $output .= do_shortcode( $content );

        if ( $traffic_layer != 0 ) {
            $output .= "    var trafficLayer" . $random . " = new google.maps.TrafficLayer();";
            $output .= "    trafficLayer" . $random . ".setMap(map" . $random . ");";
        }

        if ( $transit_layer != 0 ) {
            $output .= "    var transitLayer" . $random . " = new google.maps.TransitLayer();";
            $output .= "    transitLayer" . $random . ".setMap(map" . $random . ");";
        }

        if ( $bicycle_layer != 0 ) {
            $output .= "    var bicycleLayer" . $random . " = new google.maps.BicyclingLayer();";
            $output .= "    bicycleLayer" . $random . ".setMap(map" . $random . ");";
        }

        $output .= "  }";

        $output .= "  function toggleBounce(markerId) {";
        $output .= "    var tmarker = eval('marker' + markerId);";
        $output .= "    if (tmarker.getAnimation() != null) {";
        $output .= "      tmarker.setAnimation(null);";
        $output .= "    } else {";
        $output .= "      tmarker.setAnimation(google.maps.Animation.BOUNCE);";
        $output .= "    }";
        $output .= "  }";
        $output .= "</script>";

        $output .= "<script>  jQuery(function($){";
        $output .= " $(document).ready(function() {";
        $output .= "    google.maps.event.addDomListener(window, 'load', " . $mapFunction . ");";
        $output .= " });";
        $output .= "}); </script>";

        return $output;
    }    
}
