'use strict';

function AddressResults(args) {
    /*jshint validthis: true */
    var options = {
        position: {
            //top: '100px',
            //left: '100px',
            //bottom: '100px',
            //right: '100px'
        },
        center: true,
        id: 0
    };
    $.extend(options, args);
    this.options = options;
}

AddressResults.prototype.init = function(basicSearch) {
    var self = this; // assign Class context so it doesnt conflict with jQuery 'this'

    var template = 'template/AddressResults.html';

    $.ajax({
        url: template,
        type: 'GET',
        dataType: 'html',
        cache: false
    }).done(function(data) {

        var $panel = $(data);
        self.panel = $panel;
        $panel.hide().appendTo('#map');

        //Close Panel
        $panel.find('.panel-close').click(function() {
            self.remove();
        });

        //Click The Maximize/Minimize Button
        $panel.find('.panel-min-max-btn').click(function() {
            $('.panel-collapse', $panel).collapse('toggle');
        });

        $panel.on('hide.bs.collapse', function() {
            $panel.data('lastWidth', $panel.outerWidth());
            $panel.data('lastHeight', $panel.outerHeight());
            $panel.animate({
                width: '200px',
                height: '30px'
            }, {
                duration: 300,
                easing: "easeInOutQuad"
            });

        });

        $panel.on('hidden.bs.collapse', function() {
            var headingIcon = $panel.find('.panel-min-max-btn');
            headingIcon.toggleClass('glyphicon-minus');
            headingIcon.toggleClass('glyphicon-plus');

            $panel.resizable('destroy');
        });

        $panel.on('show.bs.collapse', '.panel-collapse', function() {
            var panelTop = $panel.position().top;
            var lastPanelHeight = $panel.data('lastHeight');
            var marginTop;

            if ((lastPanelHeight + panelTop) > $('#map').height()) {
                marginTop = -$panel.data('lastHeight') + 40;
            } else {
                marginTop = 0;
            }

            //alert(marginTop)

            if (typeof $panel.data('lastWidth') === 'undefined') {
                $panel.animate({
                    width: '300px'
                }, {
                    duration: 300,
                    easing: "easeInOutQuad"
                });
            } else {
                $panel.animate({
                    width: $panel.data('lastWidth') + 'px',
                    height: $panel.data('lastHeight') + 'px',
                    marginTop: marginTop + 'px'
                }, {
                    duration: 300,
                    easing: "easeInOutQuad",
                    complete: function() {
                        $panel.css("height", "auto"); // set panel height to auto so the scroll bar will appear if needed
                        $panel.css('margin-top', '0px');
                        $panel.css('top', $panel.position().top + marginTop);
                    }

                });
            }
        });

        // when the window is resized, position the panel so it doesn't go outside the map frame
        $(window).resize(function() {
            $('.panel').each(function() {
                if (($(this).position().left + $(this).width()) >= $('#map').outerWidth()) {
                    $(this).css('left', $('#map').outerWidth() - $(this).width() - 2);
                }

                if (($(this).position().top + $(this).height()) >= $('#map').outerHeight()) {
                    $(this).css('top', $('#map').outerHeight() - $(this).height() - 2);
                }
            });
        });

        $panel.on('shown.bs.collapse', function() {
            var headingIcon = $panel.find('.panel-min-max-btn');
            headingIcon.toggleClass('glyphicon-minus');
            headingIcon.toggleClass('glyphicon-plus');
            $panel.resizable();
        });



        self.setMaxPanelHeight($panel);
        self.showWindow($panel);


        $panel.resizable();

        $panel.draggable({
            containment: "#map",
            handle: ".panel-heading",
            helper: false,
            start: function() {
                $(this).css("opacity", "0.7")
            },
            stop: function() {
                $(this).css("opacity", "1")
            }
        });

        basicSearch();
    });

    return this;
};

AddressResults.prototype.showWindow = function() {
    // append to DOM (hidden) so we can get height/width of floating window
    // and center the window before showing it
    //var popupHeightCenter = (($("#map").height() - this.panel.height()) / 2) + 'px';
    var popupHeightCenter = '5px';
    var popupWidthCenter = (($("#map").width() - this.panel.width()) / 2) + 'px';

    this.panel.css({
        position: 'absolute',
        top: popupHeightCenter,
        left: popupWidthCenter
    });

    this.panel.fadeIn(300);
};

AddressResults.prototype.setMaxPanelHeight = function($panel) {

    $panel.css({
        height: "300px",
        width: "325px"
    });

    $panel.find('.panel-body').css('max-height', '300px');
    $panel.find('.panel-body').css('height', '300px');
    $panel.on('resize', function() {
        $panel.find('.panel-body').css('max-height', '300px');
        $panel.find('.panel-body').css('height', '300px');
    });

    $panel.css({
        minWidth: "325px",
        maxWidth: "325px",
        maxHeight: "250px"
    });
    $(window).on('resize', function() {
        $panel.css({
            maxHeight: $('#map').outerHeight() - 20
        });
    });
};


AddressResults.prototype.remove = function() {
    this.panel.fadeOut(200, function() {
        $(this).find(".edx-search-result-text").empty();
        $(this).find("#basic-result-edx-body").empty();
        $("#searchForm input").val('')
    });
};