﻿/* SEARCH RESULTS specific scripts */
/* site specific css for work done for advanced search in july 2011 */
// ===== 08-10-11 - Updated to [christiesgreatestates-20110809] =====//

(function ($) { // closure and $ portability

    $(document).ready(function () {
        /*
        $('#results-sort-by').multiselect({
        multiple: false,
        header: false,
        selectedList: 1,
        height: "auto"
        });
        */

        $('.amount-select').multiselect({
            classes: "amount-select",
            header: false,
            height: "auto",
            selectedList: 1,
            multiple: false
        });

        $('.unit-select').multiselect({
            classes: "unit-select",
            header: false,
            height: "auto",
            multiple: false
        });


        // NEW - This method has been made more generic, to allow usage on the results list view

        function setupCarousel(el) {

            var carousel = el.find(".carousel-items");
            var bullets = el.find(".bullets li");
            var items = el.find("li.item");

            carousel
        .carousel({
            selectorItem: "li.item",
            previous: el.find("button.previous"),
            next: el.find("button.next"),
            transition: "crossfade",
            transitionOptions: { duration: 750 }
        })
        .bind("carousel.beforechange", function (event, data) {
            bullets.removeClass("current");
            bullets.eq(items.index(data.newItem)).addClass("current");
        });


            bullets.click(function () {
                carousel.carousel("to", { index: bullets.index($(this)) });
                return false;
            });

        }


        //*******************************/
        /* featured properties carousel */
        /********************************/

        var fp = $('#featured-properties');

        if (fp.length) {
            setupCarousel(fp);
        }

        // setup featured property image cropping

        //fp.find(".img-crop img").resizeto({ width: 316, height: 232 });


        var btmr = $('#bt-modify-results');
        var mr = $('#modify-results');
        var mrw = $('#wrap-modify-results');


        var sr = $('#search-results');

        // calculate the correct place for the btmrTop button (based on the classes in the search results)

        var btmrTop = 3;

        if (sr.hasClass("with-summary-feature")) {
            // BOTH the summary and featured properties are present
            btmrTop = 287;
        } else if (sr.hasClass("with-summary")) {
            // JUST the summary is present
            btmrTop = 20;
        } else if (sr.hasClass("with-feature")) {
            // JUST the feature is present
            btmrTop = 264;
        }



        // Setup elements to allow map switching

        var resultsNonMap = $('#results-sort-by-inputPair,.results-list,.results-grid');
        var resultsMap = $('.results-map');

        var mapToggle = $('.map-toggle');

        var mapAnimateOptions = { duration: 500, easing: "easeOutQuart" };


        function afterExpandMap() {
            // callback to do something with the real google map after expand (maybe zoom in?)
            resultsMap.data("animating", false);  /* MAP SCROLL NEW! */
        }

        function afterReduceMap() {
            // callback to do something with the real google map after reduce (maybe zoom out?)
            resultsMap.data("animating", false);   /* MAP SCROLL NEW! */
        }


        function expandMap() {
            $('.results-list-wrap,.results-grid-wrap').addClass("results-wrap-bleed");

            resultsMap.data("animating", true);

            resultsMap.addClass("results-map-expanded");

            $('#filter-tab-grid').removeClass("current");
            $('#filter-tab-list').removeClass("current");
            $('#filter-tab-map').addClass("current last-current");

            mapToggle.css("top", 16).animate({ width: 944 }, $.extend({}, mapAnimateOptions, { complete: function () { mapToggle.addClass("map-toggle-shown").find("h6").stop(true, true).fadeIn($.fd("normal")); } }));       /* MAP SCROLL UPDATED! */
            mapToggle.find(".action").html("reduce map");
            resultsNonMap.hide();
            resultsMap.stop(true).css("top", 0).animate({ width: 974 }, mapAnimateOptions);
            resultsMap.find(".gmap").stop(true).animate({ height: 504 }, $.extend({}, mapAnimateOptions, { complete: afterExpandMap }));

        }


        function reduceMap() {

            resultsMap.data("animating", true); /* MAP SCROLL NEW! */

            $('.results-list-wrap,.results-grid-wrap').removeClass("results-wrap-bleed");

            if ($('.results-grid').length) {
                $('#filter-tab-grid').addClass("current");
            } else {
                $('#filter-tab-list').addClass("current");
            }

            $('#filter-tab-map').removeClass("current last-current");

            mapToggle.stop(true).animate({ width: 222 }, $.extend({}, mapAnimateOptions, { complete: function () { mapToggle.removeClass("map-toggle-shown"); } })).find("h6").stop(true, true).hide();
            mapToggle.find(".action").html("expand map");
            resultsMap.removeClass("results-map-expanded");
            resultsMap.stop(true).animate({ width: 234 }, $.extend({}, mapAnimateOptions, { complete: function () { resultsNonMap.fadeIn($.fd("normal")); } })); // addClass("results-map-mini");
            resultsMap.find(".gmap").stop(true).animate({ height: 218 }, $.extend({}, mapAnimateOptions, { complete: afterReduceMap }));
        }

        //call this when we need to hightlight the search result list item manually
        function setHighlightSearchResultListItem(propertyId) {
            $('ul.results li.result-hover').removeClass("result-hover");
            var listItem = 'ul.results #item_' + propertyId;
            $(listItem).addClass("result-hover");
        }

        /* BEGIN MAP SCROLL NEW! */

        var resultsWrap = $('.results-wrap');

        WireUpMapScroll();

        /* END MAP SCROLL NEW! */

        //$(window).scroll();


        // BEGIN DEMO CODE - REMOVE IN PRODUCTION */
        /* disabled demo map */
        /*
        $("#gmap-hotspot-mock").hover(
        function () {
        if (resultsMap.hasClass("results-map-expanded")) {
        $('#gmap-mock-balloon').show();
        }

        },
        function () {
        $('#gmap-mock-balloon').hide();
        }
        );
        */

        // END DEMO CODE */

        /*
        mapToggle.click(function () {
        if (mapToggle.hasClass("map-toggle-shown")) {
        reduceMap();
        } else {
        expandMap();
        }
        });
        */

        /*
        $('#filter-tab-map a').click(function () {

        if ($('.results-list').length) {
        // if we're viewing the list already, dynamically expand the map
        expandMap();
        return false;
        }

        // ... otherwise fall through to open the list page with map initially visible


        });*/

        /*
        $('#filter-tab-list a').click(function () {

        if ($('.results-list').length) {
        // if we're viewing the list already, dynamically reduce the map (if need be)
        if (mapToggle.hasClass("map-toggle-shown")) {
        reduceMap();
        }

        return false;
        }

        // ... otherwise fall through to open the list page 


        });*/


        /* -- Setup Dialog Sample -- */

        // setup all dialog ".close" elements to hide the dialog

        $('.dialog .close').click(function () {
            $(this).closest(".dialog").hide().unaffix().unmodalize();

            return false;
        });

        $('#see-all-options').click(function () {

            $('#dialog-clarify').modalize().affix({ w: "n+40" }).fadeIn($.fd("fast"));


        });




        function hideModifyResults(duration) {
            $("div#search-results").css("overflow", "hidden");
            if (sr.hasClass("with-summary-feature")) {
                // BOTH the summary and featured properties are present
                btmrTop = 287;
            } else if (sr.hasClass("with-summary")) {
                // JUST the summary is present
                btmrTop = 40;
            } else if (sr.hasClass("with-feature")) {
                // JUST the feature is present
                btmrTop = 264;
            }
            else
                btmrTop = 3;

            btmr.removeClass("shown");
            mrw.removeClass("shown");
            mr.animate({ left: -324 }, { duration: duration, easing: "easeOutQuart" });
            btmr.animate({ width: 178, right: -170, top: btmrTop }, { duration: duration, easing: "easeOutQuart" });
        }

        function showModifyResults(duration) {
            btmr.addClass("shown");
            mrw.addClass("shown");
            mr.animate({ left: 0 }, { duration: duration, easing: "easeOutQuart" });
            btmr.animate({ width: 41, right: -34, top: 3 }, { duration: duration, easing: "easeOutQuart" });
        }

        var drawerTimerID;
        btmr.click(function () {
            if (btmr.hasClass("shown")) {
                //$("#search-results").removeClass("overflowDrawer").addClass("overflowDrawer");
                if (SearchResultsJS.HasFilterChanged) {
                    mr.find(".bt-update").click();
                }
                else {
                    //$("div#search-results").css("overflow", "hidden");
                    hideModifyResults(350);
                }
            } 
            else {
                showModifyResults(500);
                clearTimeout(drawerTimerID);
                //setTimeout(function () { $("#search-results").removeClass("overflowDrawer"); }, 300);
                //Timer needed so the drawer is fully shown only after it has almost fully slid out
                drawerTimerID = setTimeout(function () { $("div#search-results").css("overflow", "visible"); }, 300);
            }
            return false;
        });

        mr.find("a.back").click(function () {
            hideModifyResults();
            //return false; //because it should just reload the page
        });

        // make other links open the modify panel
        $('a.modify-results').click(function () {
            showModifyResults();
            return false;
        });

        mr.find(".bt-close").click(function () {
            hideModifyResults();
        });

        var updateTimerId = 0;
        mr.find(".bt-update").click(function () {
            // here, we'd probably fire off an ajax request, or submit the form
            if (SearchResultsJS.HasFilterChanged) {
                //$("div#search-results").css("overflow", "hidden");
                hideModifyResults();
                clearTimeout(updateTimerId);
                updateTimerId = setTimeout(function () { $('#frmSearchFilter').submit(); }, 250);
            }
            return false;
        });

        //$('ul.results-grid .img-crop img').resizeto({ width: 316, height: 232 });

        // setup modify results toggle panels

        mr.find(".section").each(function () {

            var $el = $(this);

            $el.find(".title").reveal({

                triggerEvent: "click",
                el: $el.find(".content"),
                classShown: "toggle-shown",
                show: "slideDown",
                showOptions: { duration: $.fd(150) },
                hide: "slideUp",
                hideOptions: { duration: $.fd(150) }

            });


        });


        /*
        // setup results rollovers
        $('ul.results li.result').revealHover({
        showDelay: 800, 
        affix: { classGlueFromPrefix: "pr-glue-", classGlueToPrefix: "pr-glue-", to: "e", from: "w" },
        show: "fadeIn",
        showOptions: { duration: $.fd("fast") },
        hide: "fadeOut",
        hideOptions: { duration: $.fd("fast") }
        });/* make this a reasonable delay, to minimise unwanted rollover reveals */




        /* TESTING CODE BELOW: ==> moved to the search-result-drawer-ascx page

        // The code below allows us to preview the various states of the location selection in the modify results drawer
        // This will need to be modified and/or fleshed out in production!

        var loc_cs = $('#location-city-selected');
        var loc_ss = $('#location-state-selected');
        var loc_sel = $('#location-selector');

        mr.find("#find-a-location-panel .bt-add").click(function () {
        // show the "Paris, France" city location selection
        loc_cs.show();
        loc_sel.hide();

        return false;
        });

        mr.find("#browse-all-locations-panel .bt-add").click(function () {
        // show the "NY 10001" state location selection
        loc_ss.show();
        loc_sel.hide();

        return false;
        });

        loc_cs.find('.bt-remove').click(function () {
        // "remove" the location, by showing the location selection 
        loc_cs.hide();
        loc_sel.show();
        return false;
        });

        loc_ss.find('.bt-remove').click(function () {
        // "remove" the location, by showing the location selection 
        loc_ss.hide();
        loc_sel.show();
        return false;
        });
        */


        // Setup Results List View
        /*
        $('ul.results-list').find(".carousel").each(function () {
        $(this).find(".img-crop img").resizeto({ width: 246, height: 178 });
        setupCarousel($(this));
        });

        // linkify the list result panels. 
        // "linkify" is a plug-in that, in this case, finds the first link in the container
        // and makes a click on the entire container go to that href. Note that clicks on any buttons or links
        // inside the container will still act as normal

        $('ul.results-list').find("li.result").linkify();
        */

        //*************************/
        /* search results tooltip */
        /**************************/
        srtt = $('#search-results-tooltip');
        mus = $('#map-update-search');

        if (srtt.length) {
            $('button').click(function () {
                if ($(this).hasClass('bt-yes') && !(mus.attr('checked'))) {
                    mus.click();
                };
            });
            $('.hide-map-tooltip').click(function () {
                srtt.hide();
            });
        }

        // class for any modals that should show on page load
        $('.dialog-show').modalize().affix({ w: "n+40" }).fadeIn($.fd("fast"));

        // debugging
        // showModifyResults(0);




    });



})(jQuery);

function WireUpMapScroll()
{
    var resultsWrap = $('.results-wrap');
    var resultsMap = $('.results-map');
    var mapToggle = $('.map-toggle');

    if (resultsMap.length) {

        $(window).bind("scroll", function () {

            var moveMap = function () {

                if (!resultsMap.data("animating") && !resultsMap.hasClass("results-map-expanded")) {

                    var wst = $(window).scrollTop();
                    var mapTop = resultsWrap.offset().top;
                    var topPos = Math.max(wst - mapTop, 0);

                    resultsMap.stop(true).animate({ "top": topPos }, { duration: 300, easing: "easeOutQuart" });
                    mapToggle.stop(true).animate({ "top": 16 + topPos }, { duration: 300, easing: "easeOutQuart" });
                }

            };

            if (window.moveMapTimeout) {
                clearTimeout(window.moveMapTimeout);
            }

            window.moveMapTimeout = setTimeout(moveMap, 200);

        });
    }
}
