﻿$(document).ready(function () {
    var log = function (m, mm) {
        try { console.log(m, mm); }
        catch (e) { }
    }
    $('.tooltip').hover(function () {
        this.toolTip = this.title;
        $(this).append('<div class="tooltipContainer"><p>' + this.title + '</p></div>');
        this.title = '';
        $('.tooltipContainer').fadeIn(200).css('top', $(this).height());
    },
        function () {
            $('.tooltipContainer').fadeOut(200).remove();
            this.title = this.toolTip;
        }
    );

    $('.comparison table tbody tr').hover(
        function () { $(this).addClass('hover'); },
        function () { $(this).removeClass('hover'); }
    );

    $('form input[type=text], form textarea').bind({
        focus: function () {
            $(this).parent().addClass('active');
        },
        blur: function () {
            $(this).parent().removeClass('active');
        }
    });

    var cities = [];
    $.each($('.ddCities option'), function (i, v) {
        cities.push(v.value);
    });

    $('.txtcombobox').focus(function () {
        var me = $(this);
        var c = $('<div class="combobox"></div>');
        var h = $('<ul></ul>');
        var d = [];
        me.parent().find('select option').each(function (i, v) {
            d.push(v.value);
        });

        $.each(d, function (i, v) {
            var a = $('<a href="#"></a>').text(v).click(function (e) {
                var me = $(this);
                var cb = me.closest('.combobox')
                cb.prev().val(me.text());
                e.preventDefault();
            })
            var l = $('<li></li>').append(a);
            h.append(l);
        });

        c.append(h);
        var p = me.position();
        c.css({ top: p.top + 25, left: p.left });
        me.after(c);

        setTimeout(function () {
            $('body').click(function (e) {
                setTimeout(function () {
                    $('.combobox').fadeOut(function () { $(this).remove() });
                    $('body').unbind('click');
                }, 200)
            });
        }, 500);
    });

    var url = function () {
        var s = window.location.search.slice(1).split('&');
        this.p = window.location.pathname;
        this.query = {};

        for (var i = 0, l = s.length; i < l; i++) {
            var q = s[i].split('=');
            if (q != "") {
                this.query[q[0]] = q[1];
            }
        }
        this.set = function (k, v) {
            this.query[k] = v;
        }
        this.get = function () {
            var u = this.p + '?';
            for (var o in this.query) {
                u += o + '=' + this.query[o] + '&';
            }
            return u.slice(0, u.length - 1);
        }
    }

    var ManageResults = function () {
        var bindelements = function () {
            $('#ddcities').change(function () {
                var u = new url();
                u.set('city', $(this).val());
                window.location = u.get();
            });
            $('#comtype').change(function () {
                var u = new url();
                u.set('comtype', $(this).val());
                window.location = u.get();
            });
            $('#rating').change(function () {
                var u = new url();
                u.set('rating', $(this).val());
                window.location = u.get();
            });
            $('.refinements :checkbox').change(function () {
                var u = new url();
                var me = $(this);
                var o = '';
                $.each($('.refinements :checkbox:checked'), function (i, v) {
                    o += v.id + ',';
                });
                o = o.slice(0, o.length - 1);
                u.set('has', o);
                log(u.get());
                window.location = u.get();
            });
        }
        var fillselects = function () {
            var q = window.location.search.substr(1).split('&');
            if (q == "") return;
            for (var i = 0, l = q.length; i < l; i++) {
                var kv = q[i].split('=');
                $('#' + kv[0]).val(kv[1]);
            }
        }
        var checkcbs = function () {
            var u = new url();
            var s = u.query.has.split(',');
            var r = $('.refinements');
            for (var i = 0, l = s.length; i < l; i++) {
                r.find('#' + s[i]).attr('checked', 'true');
            }
        }
        this.init = function () {
            bindelements();
            fillselects();
            checkcbs();
        }
    }
    if ($('#comtype').length) {
        var results = new ManageResults().init();
    }

    var MapCreator = function (address) {
        var Map = null;

        function LoadMap(fullAddress) {

            Map = new VEMap('hiddenMap');
            Map.LoadMap();
            //Find(what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback);
            Map.Find(null, fullAddress, null, null, null, null, null, null, false, null, function () { loadVisibleMap(fullAddress) });
        }

        function loadVisibleMap(fullAddress) {
            containerId = 'visibleMap';
            var sc = $('#mapsize').text().split(','), h = sc[0], w = sc[1];
            var code = '<iframe id="map" scrolling="no" width="' + w + '" height="' + h + '" frameborder="0" src="http://www.bing.com/maps/embed/?v=6.3&amp;cp=coords&amp;lvl=14&amp;sty=r&amp;where1=addy&pp=coords&emid=22860556-3bb8-281d-c962-da649b055d48"></iframe><div id="LME_maplinks" style="line-height:20px;"><a id="LME_largerMap" href="http://www.bing.com/maps/?v=6.3&lvl=4&sty=r&where1=addy" target="_blank" style="margin:0 7px">View Larger Map</a><a id="LME_directions" href="http://www.bing.com/maps/?v=6.3&sty=r&where1=addy&rtp=~adr.addy" target="_blank" style="margin:0 7px">Driving Directions</a><a id="LME_birdsEye" href="http://www.bing.com/maps/?v=6.3&lvl=1&sty=b&where1=addy" target="_blank" style="margin:0 7px">View Bird\'s Eye</a>'
            var coords = "" + Map.GetCenter();
            coords = coords.replace(',', '~').replace(' ', '');
            code = code.replace(new RegExp('coords', 'g'), coords).replace(new RegExp('addy', 'g'), fullAddress);
            $('#' + containerId).html(code);
        }
        var defaultAddress = $('#address').text();
        var address = $(document).getUrlParam('address');
        if (address == null) {
            address = defaultAddress;
        }
        LoadMap(address);
    }

    var address = $('#address');
    if (address.text() != '') {
        var map = new MapCreator(address.text());
    }

    if ($('.datepicker').length > 0) {
        $('.datepicker:not(.year) input').datepicker();
        $('.datepicker.year input').datepicker({ changeYear: true, dateFormat: 'yy' });
    }
    $('.section.collapsible h3').click(function () {
        var m = $(this), p = m.parent(), n = m.next();
        if (p.hasClass('collapsed')) {
            p.removeClass('collapsed');
            n.slideDown();
        }
        else {
            p.addClass('collapsed');
            n.slideUp();
        }
    });
    $('a.print').click(function () {
        window.print();
        return false;
    });

    //print preview
    if ($('#print-content').length > 0) {
        var crtcols = function () {
            var c = $('#print-content'), li = c.find('li'), l = li.length, d = l>4 ? l/2+1 : l/2, col1 = $('<ul></ul'), col2 = $('<ul></ul');
            $.each(li, function (i, v) {
                log(i, d);
                if (i < d) col1.append($(this));
                else { col2.append($(this)); }
            });
            c.empty().append(col1).append(col2).fadeIn();
        }
        crtcols();
    }
});
