/**
 * Created by IOAN on 11/24/2015.
 */
var map_gmap = null;
function init_gmap(lat, lng, zoom) {

    myLatlng = new google.maps.LatLng(lat * 1, lng * 1);
    var myOptions = {
        zoom: zoom * 1 - 1,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    };
    map_gmap = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);


    // Creating a marker and positioning it on the map
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map_gmap
    });

    markerArray.push(marker);

    google.maps.event.trigger(map_gmap, 'resize');
    map_gmap.setZoom( map_gmap.getZoom() );
    //AutoCenter();
}

function AutoCenter() {
    //  Create a new viewpoint bound
    var bounds = new google.maps.LatLngBounds();
    //  Go through each...
    $.each(markerArray, function (index, marker) {
        bounds.extend(marker.position);
    });
    //  Fit these bounds to the map
    map_gmap.fitBounds(bounds);
}



function resizeMap(){
    google.maps.event.trigger(map_gmap, "resize");
    map_gmap.setCenter(myLatlng);
}

function init_gmap_null() {
    return true;
}


function appendBootstrap() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "https://maps.google.com/maps/api/js?key=AIzaSyDxpjfVxlLazmPt1gkiw_wIy55ofWAUJvY&callback=init_gmap_null&language=en&region=CY";
    //document.body.appendChild(script);
    $('head').append(script);
}

function resizeLargeMap() {
    var h = $('.large-map .modal-header').height() + 31;
    var f = $('.large-map .modal-footer').height() + 31;
    var b = $('.large-map .modal-body').height();
    var wh = $(window).height();
    var bb = wh - (f + h);

    var $topnav = $('#main-container > .topnav');
    var tn = $topnav.height();
    var tn_visible = $topnav && $topnav.is(':visible');
    var pad = 30;
    if(tn_visible) {
        wh = wh - tn;
        bb = wh - (f + h);
        $('.modal.large-map').css('top', tn + 'px');
        pad = 0;
    }
    else {
        $('.modal.large-map').css('top', '0px');
        pad = 30;
    }


    $('.large-map .bootstrap-dialog-message').css('height', bb-pad + 'px');
    $('.large-map .bootstrap-dialog-body').css('height', bb-pad + 'px');
    $('.large-map .modal-body').css('height', bb + 'px');
    $('.large-map  .modal-dialog').css('height', wh + 'px');
}

function openBigMap() {
    BootstrapDialog.show({
        title: (typeof big_map_title !== undefined) ? big_map_title : 'Map',
        cssClass: 'large-map',
        message: function(dialog){
            var $frm = $('<iframe id="process_frm" name="process_frm"></iframe>');
            $frm.attr('src', window.full_uri + 'amodules/main_article/map_enlarge.php?map_data=' + map_data);
            $frm.attr('width', '100%');
            $frm.attr('height', '100%');
            $frm.css('border', '0px solid #000000');
            $frm.css('overflow-y', 'hidden');
            return $frm;
        },
        buttons: [
            {
                label: 'Close',
                cssClass: 'btn btn-danger btn-sm',
                action: function(dlg) {
                    dlg.close();
                }
            }
        ],
        onshown: function(dialog) {
            $('.scrollup').addClass('hidden');

            /*var $dlg = $('.bootstrap-dialog.large-map');
            $dlg.find('.modal-body').height($(window).height() - 130);
            $dlg.find('.bootstrap-dialog-body').height($(window).height() - 145);
            $dlg.find('.bootstrap-dialog-message').height($(window).height() - 150);*/

            resizeLargeMap();
        },
        onhide:function(dialog) {
            $('.scrollup').removeClass('hidden');
        }
    });

}

$(document).ready(function(){
    if(typeof have_article_photos !== 'undefined' && have_article_photos) {
        //alert('Have article photos');

        Galleria.ready(function() {
            $('.article-gallery').removeClass('hidden');
        });

        Galleria.loadTheme(full_uri + 'assets/plugins/galleria/themes/cyprusnet/galleria.cyprusnet.js?_q=' + new Date().getTime());
        Galleria.run('#galleria', {
            height: 360,
            width: 'auto',
            lightbox: true,
            imageCrop: true,
            responsive: true,
            showInfo: true,
            showImagenav: true,
            carousel: true
        });
    }


    if(typeof have_article_map !== 'undefined' && have_article_map) {
        appendBootstrap();

        $('.map_enlarge_link').on('click', function (e) {
            e.preventDefault();
            var loc_id = $(this).attr('rel');
            openBigMap();
            return false;
        });
    }

});