<?php

/**
 * Description of GoogleMapWidget
 *
 * @author Mohammed Osman  <osmancode@gmail.com>
 */
class GoogleMapWidget extends ShortCodeWidget {

    protected $_options = array(
        array(
            'name' => 'model_id',
            'label' => 'Map ID',
            'value' => 0,
        ),
    );
    public $modelClass = "GoogleMap";
    public $shortCode = 'googlemap';

    public function run() {
        /* @var $cs CClientScript */
        /* @var $model GoogleMap */



        if (isset($this->_options['model_id'])) {
            $model = CActiveRecord::model($this->modelClass)->findByPk($this->_options['model_id']);
			$mapKey=Option::model()->findByAttributes(array('key' => 'MAP_KEY'));
			$mapKey=$mapKey!=null?$mapKey->value:"";


            if ($model !== NULL) {
                $cs = Yii::app()->clientScript;
                $cs->registerScriptFile('https://maps.googleapis.com/maps/api/js?sensor=false&key='.$mapKey);

                $options = $model->getOptions();
                $postfix = "{$this->getId()}_{$model->id}";

                echo "<div id='mapid-{$postfix}'></div>";


                $markersJs = '';
                foreach ($model->markers as $marker) {
                    $markerOptions = $marker->getOptions("map{$postfix}");

                    $markersJs .= "var Marker{$postfix}_{$marker->id} = new google.maps.Marker({$markerOptions});\r\n";
                    $markersJs.= "
                        var MarkerInfoWindow{$postfix}_{$marker->id} = new google.maps.InfoWindow({content: '{$marker->title}'});
                        google.maps.event.addListener(Marker{$postfix}_{$marker->id}, 'click', function() {
                            MarkerInfoWindow{$postfix}_{$marker->id}.open(map{$postfix}, Marker{$postfix}_{$marker->id});
                        });";
                }
                $fitMarkersIntoView = '';
                if ($model->fit_markers_into_view && count($model->markers)) {
                    $fitMarkersIntoView .= "/*fit markers to zoom*/\r\n";
                    $fitMarkersIntoView .= "var bound = new google.maps.LatLngBounds();\r\n";
                    foreach ($model->markers as $marker) {
                        $position = $marker->getPosition();
                        $fitMarkersIntoView .= "bound.extend($position);\r\n";
                    }
                    $fitMarkersIntoView.="map{$postfix}.fitBounds(bound);\r\n";
                    $fitMarkersIntoView .= "/*!fit markers to zoom*/\r\n";
                }


                $js = " var map{$postfix};
                        function LoadMap{$postfix}() {
                            var gmapdiv = document.createElement('div');
                            gmapdiv.id = 'gmapid-{$postfix}';
                            gmapdiv.style.width = '{$model->width}';
                            gmapdiv.style.height = '{$model->height}';
                            document.getElementById('mapid-{$postfix}').appendChild(gmapdiv);
                            map{$postfix} = new google.maps.Map(document.getElementById('gmapid-{$postfix}'), {$options});

                            {$markersJs}

                            {$fitMarkersIntoView}
                        }

        google.maps.event.addDomListener(window, 'load', LoadMap{$postfix});";

                $x = $cs->registerScript("mapid{$postfix}", $js);
            }
        } else {
            echo '<!--PARAM NOT SET-->';
        }
    }

}
