$(document).ready(function () {
    $.ajaxSetup({ cache: false });

    // add hover class to nav items
    $('#main-nav li a, #main-sub-nav li a').parents('li').bind('mouseenter', function () { $(this).addClass('hover'); }).bind('mouseleave', function () { $(this).removeClass('hover'); });


    // make dt height equal to dd height for specified dls
    $('.default-dl dt').each(function (i, dt) {
        $(dt).height($(dt).next('dd').height());
    });

    // make dt height equal to dd height for specified dls
    $('.btn-2s').bind('mouseenter', function () {
        $(this).addClass('hover');
    }).bind('mouseleave', function () {
        $(this).removeClass('hover');
    });

    // make right div height equal left div height
    $('.careers-pd .paper-block .summary-container .right').each(function (i, rDiv) {
        $(rDiv).height($('.careers-pd .paper-block .summary-container .left').height()).css({ 'position': 'relative' });
        $('.btn-2s', rDiv).css({ 'left': (($(rDiv).innerWidth() - $('.btn-2s', rDiv).outerWidth(true)) / 2), 'top': (($(rDiv).innerHeight() - $('.btn-2s', rDiv).outerHeight(true)) / 2), 'position': 'absolute' });
    });

    // highlight inputs onfocus
    $('.form .input-boxed input, .quote-form .input-boxed select').focus(function () { $(this).parent().addClass('active'); }).blur(function () { $(this).parent().removeClass('active'); });

    // recently viewed open / close
    $('.content-wrapper .left-column .recently-viewed .btn-view').click(function () {
        $(this).hide().siblings('.btn-close').show().parents('.recently-viewed').find('ul').slideDown();
        return false;
    }).siblings('.btn-close').click(function () {
        $(this).hide().siblings('.btn-view').show().parents('.recently-viewed').find('ul').slideUp();
        return false;
    });

    $(".tab_content").hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".tab_content:first").show();

    $("ul.tabs li").click(function () {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    });

    var ajaxPopup = $('#ajax-popup').length > 0 ? $('#ajax-popup') : ($('<div/>').attr('id', 'ajax-popup').append($('<div/>').addClass('inner-wrapper').css('position', 'absolute').html("YAY"))).appendTo('body');
    $(ajaxPopup).bind('show', function () {
        $(ajaxPopup).show('fade');
        $(window).trigger('resize');
    }).bind('hide', function () {
        $(ajaxPopup).hide('fade');
    });
    $(window).bind('resize', function () { $('>.inner-wrapper', ajaxPopup).css({ 'left': ($(window).width() - $('>.inner-wrapper', ajaxPopup).width()) / 2, 'top': ($(window).height() - $('>.inner-wrapper', ajaxPopup).height()) / 2 }) }).trigger('resize');

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect		
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        //transition effect
        $(id).fadeIn(2000);

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });

    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#topnav li .sub").hide();
    $("ul#topnav li.products, ul#topnav li.portfolios").hoverIntent(config);

    //default first li sub item
    $('ul#topnav li.top a').mouseover(function () {
        var li = $(this).parent().find('.sub-nav-links li:first');
        $(li).addClass('default').siblings().removeClass('default');
        $(li).removeClass('nohover');
        $(li).addClass('hover').siblings().removeClass('hover');
        $(li).siblings().addClass('nohover');
        $('>img', li).css({ opacity: 1 });
        li.find('.show-on-hover').css({ top: -1 * $(this).position().top + 2, 
            width: 980 - 155, 'min-height': 468 }).show();
        $(li).parent().css({ 'min-height': li.find('.show-on-hover').outerHeight() + 20 });
    }).mouseout(function () {
        var li = $(this).parent().find('.sub-nav-links li:first');

        $(li).removeClass('hover');
        $('>img', li).css({ opacity: .5 });

        $('ul#topnav li .sub ul').css({ 'min-height': 468 });
    });



    $('.sub').bind('mouseover', function (e) {
        var li = $('ul#topnav li.top a.current');
        //$(".sub-nav-links li.default", li).find('.show-on-hover').show(); /* css({ top: -1 * $('.sub-nav-links li.default').position().top + 20, width: 980 - 215, 'min-height': 468 }) */
    });




    $(".show-on-hover").hide();
    $(".sub-nav-links li > img").css({ opacity: .5 });
    $('.sub-nav-links li').bind('mouseenter', function (e) {
        var li = $(this);
        $(this).addClass('default').siblings().removeClass('default');
        $(this).removeClass('nohover');
        $(li).addClass('hover').siblings().removeClass('hover');
        $(li).siblings().addClass('nohover');
        $('>img', li).css({ opacity: 1 });
        $('.show-on-hover', li).css({ top: -1 * $(this).position().top + 20, 
            width: 980 - li.outerWidth(true), 'min-height': 468 }).show();
        $(li).parent().css({ 'min-height': $('.show-on-hover', li).outerHeight() + 20 });
    }).bind('mouseleave', function (e) {
        var li = $(".sub-nav-links li.default");
        $(this).removeClass('hover');
        $('>img', this).css({ opacity: .5 });
        $('>div', this).hide();
        $('ul#topnav li .sub ul').css({ 'min-height': 468 });
        li.find('.show-on-hover').css({ top: -1 * $(this).position().top + 20,  
            width: 980 - 155, 'min-height': 468 }).show();

    }).children('.show-on-hover').hide();

    $('#slideleft .collapse-image').parents('.inner').click(function () {
        if (parseInt($(this).css('left'), 10) < 0) {
            $('#slideleft .collapse-image').removeClass('closed');
            $(this).stop(true, false).animate({ left: 0 });
        } else {
            $('#slideleft .collapse-image').addClass('closed');
            $(this).stop(true, false).animate({ left: (-1 * $(this).outerWidth()) + $('#slideleft .collapse-image').outerWidth(true) });
        }
    });

    // position drop downs
    $('#topnav > li').each(function (i, li) {
        var subnav = $('.sub', li);

        $(subnav).css({ left: -1 * $(li).position().left });
    });

    $("#faq-panels").find(".faq-title").click(function () {
        var $self = $(this);
        $(this).next().slideToggle(function () {
            $(".slide-link a", $self).text($(this).is(':visible') ? "close" : "open");
        });
        return false;
    }).next().hide();

    // open all link
    $('a.open-link', '.expand-links').click(function () {
        $('div.panel', '#faq-panels').show('slow');
        $(".slide-link a", '#faq-panels').text("close");
    });

    // close all link
    $('a.close-link', '.expand-links').click(function () {
        $('div.panel', '#faq-panels').hide('slow');
        $(".slide-link a", '#faq-panels').text("open");
    });

    $('.product-2 .top-content .tabs .tab').each(function () {
        $(this).click(function () {
            var $self = $(this);
            var $trg = $('.tab-content[tabname=' + $self.attr('tabname') + ']', $(this).parents('div').first());
            $trg.show().siblings('.tab-content').hide();
            $self.addClass('selected').siblings().removeClass('selected');
        });
    });

    $('.content-wrapper .right-column ul.tabs .tab').each(function () {
        $(this).click(function () {
            var $self = $(this);
            var $trg = $('.tab-content[tabname=' + $self.attr('tabname') + ']', $(this).parents('div').first());
            $trg.show().siblings('.tab-content').hide();
            $self.addClass('selected').siblings().removeClass('selected');
        });
    });

    $('.thumbnail a').first().click(function () {
        $('.thumb-resize').removeClass('thumb-lrg');
        $(this).addClass('on').next().removeClass('on');
        return false;
    }).next().click(function () {
        $('.thumb-resize').addClass('thumb-lrg');
        $(this).addClass('on').prev().removeClass('on');
        return false;
    });

    /*
    $('.submit-filter').click(function () {
    if ($(this).hasClass("selected")) return false;
    $('#query-results').html("<div class='loading-ajax'>Updating.. <img id='loading' alt='' src='../../content/images/ajax-loader.gif' /> Please wait.</div>")
    });
    */

    $('.sort-by').click(function () {
        $('.sort-by').removeClass("selected");
        $(this).addClass("selected");
        $('#SortBy').val($(this).val());
    });

    $('.thumb-size').click(function () {
        $('#ThumbnailSize').val($(this).val());
    });

    $('.content-wrapper .right-column .subcategory-right .filter-wrapper .filter-box .actions.open .cancel').click(function () {
        $.each($(this).parent().siblings(), function () {
            var li = $(this).children('.checkbox');
            if (li) {
                li.attr('checked', false);
            }
        });
        $(this).parent().siblings().andSelf().not(':first').slideUp().last().slideDown();
        $(this).parents('ul').addClass('open').removeClass('closed');
        $(this).closest('form').submit();
        return false;
    });

    $('.content-wrapper .right-column .subcategory-right .filter-wrapper .filter-box .actions.closed a').click(function () {
        $(this).parent().slideUp().siblings().slideDown();
        $(this).parents('ul').addClass('open').removeClass('closed');
        return false;
    });

    /*
    $('.icon-enlarge a').each(function (i, ele) {
    $(this).click(function (e) {
    $('#gallery-popup').modal({
    overlayClose: true, autoResize: true, autoPosition: true, closeClass: 'gallery-close',
    onShow: function () {
    $('#gallery-scroller').MGGalleryScroller({ thumbContainer: '.scroller', scrollerLeft: '.ctrl-prev', scrollerRight: '.ctrl-next' });
    $('#gallery-carousel').MGCarousel({ banner_nav: '#gallery-scroller .scroller', banner_nav_items: '#gallery-scroller .scroller-container li', animType: 'slide', animSpeed: 500, rotationMilli: 3000, rotationResumeMilli: 6000, rotate: false, selectedIdx: i });
    }
    });
    return false;
    }).parent().parent().parent()
    .siblings('a').click(function () { return false; });
    });
    */
    $('#gallery-scroller .scroller li').each(function () {
        $(this).attr('idx', $(this).data('idx'));
    });

    $('.icon-enlarge a, .icon-enlarge-sml a, .product-popup a, a.product-gallery-enlarge').each(function (i, ele) {
        $(this).click(function (e) {
            var imgIdx = $(this).closest('.subcategory-image').data('idx');
            if (typeof imgIdx == "undefined") {
                imgIdx = $(this).attr('data-idx');
            }
            $('#gallery-scroller .scroller').append($('#gallery-scroller .scroller li[idx=' + imgIdx + ']').prevAll().get().reverse());
            $('#product-list-popup').modal({
                overlayClose: true, autoResize: true, autoPosition: true, closeClass: 'product-gallery-close',
                onShow: function () {
                    $('#gallery-scroller').MGGalleryScroller({ thumbContainer: '.scroller', scrollerLeft: '.ctrl-prev', scrollerRight: '.ctrl-next', height: 400 });
                    $('#gallery-carousel').MGCarousel({ banner_nav: '#gallery-scroller .scroller', banner_nav_items: '#gallery-scroller .scroller-container li', animType: 'slide', animSpeed: 500, rotationMilli: 3000, rotationResumeMilli: 6000, rotate: false, selectedIdx: i });
                }
            });
            return false;
        }).parent().parent().parent()
			.siblings('a').click(function () { return false; });
    });

    $('.gallery-container .gallery .gallery-list .list a').each(function (i, ele) {
        $(this).click(function (e) {
            $('#gallery-popup').modal({
                overlayClose: true, autoResize: true, autoPosition: true, closeClass: 'gallery-close',
                onShow: function () {
                    $('#gallery-scroller').MGGalleryScroller({ thumbContainer: '.scroller', scrollerLeft: '.ctrl-prev', scrollerRight: '.ctrl-next' });
                    $('#gallery-carousel').MGCarousel({ banner_nav: '#gallery-scroller .scroller', banner_nav_items: '#gallery-scroller .scroller-container li', animType: 'slide', animSpeed: 500, rotationMilli: 3000, rotationResumeMilli: 6000, rotate: false, selectedIdx: (i + 1) });
                }
            });
            return false;
        }).parent().parent().parent()
			.siblings('a').click(function () { return false; });
    });

    $('.gallery-container .gallery .gallery-list').MGGalleryScroller({ thumbContainer: '.list', scrollerLeft: '.prev', scrollerRight: '.next' });
    $('.ind-port #gallery-scroller').MGGalleryScroller({ thumbContainer: '.scroller', scrollerLeft: '.ctrl-prev', scrollerRight: '.ctrl-next' });
    $('.ind-port #gallery-carousel').MGCarousel({ banner_nav: '#gallery-scroller .scroller', banner_nav_items: '#gallery-scroller .scroller-container li', animType: 'slide', animSpeed: 500, rotationMilli: 3000, rotationResumeMilli: 6000, rotate: false });
    $('.ind-port .gallery-scroller-container .status .hide-thumbs').unbind().bind('click', function () {
        $(this).hide().siblings().show().parent().parent().animate({ bottom: -121 });
        return false;
    });
    $('.ind-port .gallery-scroller-container .status .show-thumbs').unbind().bind('click', function () {
        $(this).hide().siblings().show().parent().parent().animate({ bottom: 0 });
        return false;
    });

    //alignProdPHs();
    $('.results-cont .results-nav .select-all').click(function () { $('.cb input[type=checkbox]', $(this).parents('.results-cont')).attr('checked', 'checked'); return false; });
    $('.results-cont .results-nav .remove-all').click(function () { $('.cb input[type=checkbox]', $(this).parents('.results-cont')).removeAttr('checked'); return false; });

    //old homepage design
    //hpInit();

    //Swap Image on Click Homepage
    $("#home ul.thumb li a").click(function (event) {
        var $this = $(this);
        var mainImage = $this.attr("data-largerImage"); //Find Image Name
        $("#main_view img").attr({ src: mainImage });
        $('#main_view #newReleaseTitle').html($this.attr('title'));
        $('#main_view a').attr('href', $this.attr('href'));
        event.preventDefault();
    });

    submitFormIfNeeded();

    //replace all broken images with fake placeholders on images with "dynamic-image" added to their class
    /*$('img.dynamic-image').bind('error', function () {
        $(this).attr('src', '/content/images/placeholders/products/thumb_large/Baresque-Logomark_520.gif').addClass('error');
    });*/
});

function alignProdPHs() {
    var maxHs = [];
    var phs = ['.product-gallery-title', '.product-gallery-title-links', '.product-gallery-text', '.product-category-links', '.product-gallery-links'];
    var rows = 0;
    $('.product-placeholder').each(function (i, item) {
        $(item).attr('rowIdx', rows);
        if ($(item).hasClass('last'))
            rows++;
    });
    for (var rowCount = 0; rowCount < rows + 1; rowCount++) {
        maxHs[rowCount] = new Array();
        for (var phCount = 0; phCount < phs.length; phCount++) {
            $(phs[phCount], '[rowIdx=' + rowCount + ']').each(function (i, item) {
                maxHs[rowCount][phs[phCount]] = maxHs[rowCount][phs[phCount]] ? Math.max(maxHs[rowCount][phs[phCount]], $(item).height()) : $(item).height();
            });
        }
    }
    for (var rowCount = 0; rowCount < rows + 1; rowCount++) {
        for (var phCount = 0; phCount < phs.length; phCount++) {
            $(phs[phCount], '[rowIdx=' + rowCount + ']').each(function (i, item) {
                $(item).height(maxHs[rowCount][phs[phCount]]);
            });
        }
    }
}

function megaHoverOver() {
    $(this).find(".sub").stop().fadeTo('fast', 1).show();
}

function megaHoverOut() {
    $(this).find(".sub").stop().fadeTo('fast', 0, function () {
        $(this).hide();
    });
}

function hpInit() {
    var cols = 9, rows = 0;
    $('.latest-release-image').each(function (i, div) {
        var row = Math.floor(i / cols);
        col = i - row * cols;
        div = $(div);
        div.data('posX', col).data('posY', row);
        hpLatestAddBehaviour(div);
        div.css({ left: div.position().left, top: div.position().top });
        rows = row + 1;
    });
    $('.latest-releases-container .latest-releases-content .floaters').css({ height: 110 * rows });
    $('.latest-release-image').each(function (i, div) {
        div = $(div);
        div.css({ position: 'absolute' }).data('origPosition', div.position());
        $('.large', div).hide();
    });
    $('.latest-release-image').first().next().next().click();
}

function updateAccountTypeFields() {
    var otherVal = $('#OtherId')[0].value;
    var selectedVal = $('#AccountType option:selected')[0].value;
    if (selectedVal == otherVal) {
        $('#other-type').show();
        $('#extrafields').hide();
    }
    else {
        var values = $('.hidden-acc-types').map(function () { return $(this).val(); }).get();
        if ($.inArray(selectedVal, values) > -1) {
            // It's in here...
            $('#extrafields').show();
        }
        else {
            $('#extrafields').hide();
        }
        $('#other-type').hide();
    }
}

function updateProjectName() {
    var value = $('#project-selector')[0].value;
    if (value == '-1') {
        // show box.
        $('#new-project-name').show();
    }
    else {
        $('#new-project-name').hide();
    }

    var extra = $('#multi-select-projects');
    if (extra) {
        if (value == '-2') {
            extra.show();
        }
        else {
            extra.hide();
        }
    }
}

function submitFormIfNeeded() {
    var found = false;
    $('.subcategory-right ul').each(function () {
        $('input[type=checkbox]', this).each(function () {
            if ($(this).is(':checked')) {
                $(this).parent().siblings().andSelf().not(':last').slideDown();
                $(this).parents('ul').addClass('open').removeClass('closed');
                found = true;
                return false;
            }
        });
    });
    var sort = $('#SortBy');
    if (sort.length > 0 && sort.val() != 'Date added') {
        $('.sort-by').removeClass("selected");
        var id = '.sort-by[value="' + sort.val() + '"]';
        $(id).addClass("selected");
        found = true;
    }
    if (found) {
        $('#ParentId').closest('form').submit();
    }
}

function hpLatestAddBehaviour(ele) {
    var cols = 9, rows = Math.ceil($('.latest-release-image').size() / 9), minWH = 110, maxWH = 323;
    ele.bind('click', function (e) {
        e.stopImmediatePropagation();
        var totalEle = $('.latest-releases-container .latest-releases-content .floaters .latest-release-image').size();
        if ($(ele).width() < maxWH) {
            var activeIdx = $(ele, ele.parent()).index();
            var idxOffset = 0;

            var $unavailSlots = $(ele);
            $unavailSlots = $unavailSlots.add($(ele).parent().children(':eq(' + (activeIdx + 0) + ')').nextAll(':lt(' + (2) + ')'))
						.add($(ele).parent().children(':eq(' + (activeIdx + 8) + ')').nextAll(':lt(' + (3) + ')'))
						.add($(ele).parent().children(':eq(' + (activeIdx + 17) + ')').nextAll(':lt(' + (3) + ')'));

            var osX = 0, osY = 0;
            var row = Math.floor(activeIdx / cols);
            var col = Math.floor(activeIdx - row * cols);

            if (col > cols - 3) { osX = -3 + (cols - col); }
            if (row > rows - 3) { osY = -3 + (rows - row); }
            if (osY + row < 0) { osY++; }

            var clearSlots = new Array();
            if ($(ele).width() < maxWH) {
                for (var i = 0; i < 3; i++) {
                    for (var j = 0; j < 3; j++) {
                        clearSlots.push(activeIdx + (i * cols) + j + osX + (cols * osY));
                    }
                }
            } else {
                for (var i = 0; i < 1; i++) {
                    for (var j = 0; j < 1; j++) {
                        clearSlots.push(activeIdx + (i * cols) + j + osX + (cols * osY));
                    }
                }
            }

            var divs = $.makeArray($(ele).parent().children());
            var divCounter = 0;
            var xPos = 0;
            var yPos = 0;
            for (var r = 0; r < rows + 1; r++) {
                for (var c = 0; c < cols; c++) {
                    var div = divs[divCounter];

                    xPos = c * (minWH + 1);
                    yPos = r * (minWH + 1);

                    if (div == ele[0]) {
                        divCounter++;
                        div = divs[divCounter];
                    }

                    if ($.inArray((c + (cols * r)), clearSlots) >= 0) {
                        continue;
                    }
                    if (div != ele[0]) {
                        $(div).animate({ 'z-index': 1, left: xPos, top: yPos, width: minWH, height: minWH }, function () {
                            $('.large', $(this)).hide();
                            $('.small', $(this)).show();
                        });
                    }
                    if (totalEle + 7 == c + (cols * r)) { $('.latest-releases-container .latest-releases-content .floaters').animate({ height: yPos + $('.small', div).outerHeight(true) }); }
                    divCounter++;
                }
            }
            $('.large', ele).show();
            $('.small', ele).hide();
            xPos = $(ele).data('origPosition').left + (osX * (minWH));
            yPos = $(ele).data('origPosition').top + (osY * (minWH));
            $(ele).animate({ 'z-index': 2, left: xPos, top: yPos, width: maxWH, height: maxWH });
        } else {
            var $last = $('.latest-releases-container .latest-releases-content .floaters .latest-release-image').last();
            $('.latest-releases-container .latest-releases-content .floaters').animate({ height: $last.data('origPosition').top + minWH });
            $(ele).parent().children().each(function (i, div) {
                $(div).animate({ width: minWH, height: minWH, left: $(div).data('origPosition').left, top: $(div).data('origPosition').top }, function () {
                    $('.large', $(this)).hide();
                    $('.small', $(this)).show();
                });
            });
        }

    });

}

function initPopup() {
    $('#ajaxLoadOverlay').show();
}

function openPopup() {
    if ($.trim($('#ajax-popup #popup-content').html()) != "") {
        $('#ajax-popup').trigger('show');
    }
    return false;
}

function closePopup() {
    $('#ajax-popup').trigger('hide');
    return false;
}

function errorPopup(response) {
    var message = response;

    if (response.responseText) {
        message = response.responseText;    
    }

    $('#ajax-popup #popup-content').html('<h2>Error</h2><div class="content clearfix">' + message + ' <p><button class="button" onclick="closePopup();">OK</button></p></div>').trigger('show');

}

$(document).ready(function () {
    $('a.confirm').bind('click', function (event) {
        ajaxConfirm("Confirmation", "Are you sure", "Yes", "No", $(this).onclick);
        event.preventDefault();
    });
});

function ajaxConfirm(title, message, okMessage, cancelMessage, okEvent) {
    var buttons = '<button id="okBtn" class="button" type="button">' + okMessage + '</button>' + '<button id="cancelBtn" class="button" type="button">' + cancelMessage + '</button>';
    var object = $('<h2>' + title + '</h2><div class="content clearfix"><p>' + message + '</p>' + buttons + '</div>');
    $('#ajax-popup #popup-content').empty().prepend(object);

    $('#okBtn').bind('click', okEvent);

    $('#cancelBtn').bind('click', function () {
        closePopup();
    });

    openPopup();
}

//autocomplete

$(document).ready(function () {
    $(".autocompleteSearch").each(function (index, element) {
        var $el = $(element);

        if (typeof $el.attr("data-selectFunc") != "undefined") {
            selectFunc = $el.attr("data-selectFunc");
        }

        var searchFor = "both";
        if (typeof $el.attr("data-searchFor") != "undefined") {
            searchFor = $el.attr("data-searchFor");
        }

        $el.data('searchFor', searchFor);

        $el.autocomplete({
            source: function (request, response) {
                var $this = $(this);
                $.ajax({
                    url: "/search/autocomplete",
                    type: "POST",
                    dataType: "json",
                    data: {
                        searchText: request.term,
                        maxResults: 12,
                        searchFor: $el.data('searchFor')
                    },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.Name,
                                url: item.Url,
                                value: item.Name,
                                id: item.Id
                            }
                        }));
                    }
                });
            },
            autoFill: false,
            highlight: true,
            minLength: 3,
            select: function (event, ui) {
                //javascript is annoying sometimes.
                try {
                    window[selectFunc](event, ui);
                } catch (error) {
                    if (ui.item) {
                        window.location = ui.item.url;
                    }
                }
            }
        });
    });
});

function equalHeight(group) {
    var tallest = 0;
    group.each(function () {
        var thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
