/**
 * Created by IOAN on 11/26/2015.
 */
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 openDirections(loc_id) {
    BootstrapDialog.show({
        title: 'Get Map Directions',
        cssClass: 'large-map',
        message: function(dialog){
            var $frm = $('<iframe id="process_frm" name="process_frm"></iframe>');
            $frm.attr('src', full_uri + '/amodules/adverts_map/map_directions.php?location_id=' + loc_id);
            $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');
            resizeLargeMap();
        },
        onhide:function(dialog) {
            $('.scrollup').removeClass('hidden');
        }
    });

}

$(window).on('resize', function(e){
    resizeLargeMap();
});


function openBigMap(loc_id) {
    BootstrapDialog.show({
        title: big_map_title,
        cssClass: 'large-map',
        message: function(dialog){
            var $frm = $('<iframe id="process_frm" name="process_frm"></iframe>');
            $frm.attr('src', full_uri + '/amodules/adverts_map/map_enlarge.php?location_id=' + loc_id);
            $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');
            resizeLargeMap();
        },
        onhide:function(dialog) {
            $('.scrollup').removeClass('hidden');
        }
    });

}



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 makeMarker(options, info_content){
    var pushPin = new google.maps.Marker({map:map_gmap});
    pushPin.setOptions(options);
    markerArray.push(pushPin);
    google.maps.event.addListener(pushPin, 'click', function(){
        var infowindow = new google.maps.InfoWindow({
            content: info_content
        });

        infowindows.push(infowindow);
        infowindow.open(map_gmap, pushPin);
    });


    return pushPin;
}

function showMarker(i){
    google.maps.event.trigger(markerArray[i], 'click');
};

$(document).ready(function(){

    var click_link = click_count_path;
    $('.click-link').each(function(){
        var sub_content = '<a href="' + click_link + '" target="_blank">' + $(this).html() + '</a>';
        $(this).replaceWith(sub_content);
    });

    if(category_advert && have_map) {
        appendBootstrap();
    }

    $('a.mailto').each(function(){
        var email = $(this).attr('rel');
        email = email.replace('[ at ]', '@');
        $(this).attr('href', 'mailto:' + email).html('<i class="fa fa-envelope fa-fw"></i> ' + email);
    });

    $('.email-str').each(function(){
        var email = $(this).html();
        email = email.replace('[ at ]', '@');
        $(this).html(email);
    });

    if(category_advert || have_map) {
        $('.map_directions_link').on('click', function(e){
            e.preventDefault();
            var loc_id = $(this).data('rel');
            openDirections(loc_id);
            return false;
        });

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




    /*$('a.click-count').each(function(i){
        var field = "cc";
        var href = $(this).attr('href');
        var cc_type = -1;


        if(href.indexOf('?' + field + '=') != -1) {
            cc_type = 1;
        }
        else if(href.indexOf('&' + field + '=') != -1) {
            cc_type = 2;
        }
        else if(href.indexOf('?') != -1) {
            cc_type = 0;
            $(this).attr('href', href + '&cc=1');
        }
        else {
            cc_type = -1;
            $(this).attr('href', href + '?cc=1');
        }
    });*/




});