﻿$(document).ready(function () {
    // Apply select replacement
    //$('.searchResultsBar select').selectbox(); 

    // Sort display results text if select boxes replaced
    if ($('.searchResultsBar select').is(':hidden')) {
        $('.searchResultsBar .displayingResults').css('margin-left', '5px');
        $('.ie7 .searchResultsBar .pagination, .ie6 .searchResultsBar .pagination').css('margin-left', '0');
        $('.ie6 .searchResultsBar .displayingResults').css('margin-right', '0');
        $('.ie6 .searchResultsBar').css({ height: '24px' });
    }

    // Fix searchResultsBar replaced dropdowns
    /*$('.searchResultsBar').each(function () {
    $(this).find('label:eq(0)').addClass('sortBy');
    $(this).find('label:eq(1)').addClass('resultsAmount');
    });*/

    // Fix z-index bug
    // Is this page a search results page?
    var isResourcesPage = document.getElementById('searchResults');
    if (isResourcesPage) {
        //console.log('This is a resources (search) page and displaying a gridview, so we apply z-index fix to this content only');
        $(function () {
            var zIndexNumber = 600; //900
            $('#content div').each(function () {
                $(this).css('zIndex', zIndexNumber);
                zIndexNumber -= 1; //5
            });
        });
    }

    // Display correct image for ViewType in IE6
    if ($.browser.msie && parseFloat($.browser.version) < 7) {
        if ($('.gridViewBtn').is('.active')) {
            $('.gridViewBtn').addClass('gridViewActive');
        } else if ($('.listViewBtn').is('.active')) {
            $('.listViewBtn').addClass('listViewActive');
        }
    }

    // Hover panel on pcr results
    //    $('.pcrResultsCell').each(function () {
    //        var distance = 10;
    //        var time = 250;
    //        var hideDelay = 100;

    //        var hideDelayTimer = null;

    //        var results = $('#pcrResultsGrid .pcrResultsCell');

    //        var beingShown = false;
    //        var shown = false;
    //        var trigger = $('a', this);
    //        var info = $('.previewPane', this).css('opacity', 0);

    //        $([trigger.get(0), info.get(0)]).mouseover(function () {
    //            if (results.length == 4 && $(this).parent().is(':last-child')) {
    //                $(this).parent().addClass('right');
    //            }
    //            if (hideDelayTimer) clearTimeout(hideDelayTimer);
    //            if (beingShown || shown) {
    //                // don't trigger the animation again
    //                return;
    //            } else {
    //                // reset position of info box
    //                beingShown = true;

    //                info.css({
    //                    top: 30,
    //                    display: 'block'
    //                }).animate({
    //                    top: '-=' + distance + 'px',
    //                    opacity: 1
    //                }, time, 'swing', function () {
    //                    beingShown = false;
    //                    shown = true;
    //                });
    //            }

    //            return false;
    //        }).mouseout(function () {
    //            if (hideDelayTimer) clearTimeout(hideDelayTimer);
    //            hideDelayTimer = setTimeout(function () {
    //                hideDelayTimer = null;
    //                info.animate({
    //                    top: '-=' + distance + 'px',
    //                    opacity: 0
    //                }, time, 'swing', function () {
    //                    shown = false;
    //                    info.css('display', 'none');
    //                    if ($(this).parent().hasClass('right')) {
    //                        $(this).parent().removeClass('right');
    //                    }
    //                });


    //            }, hideDelay);

    //            return false;
    //        });

    //    });

    // Hover panel on main search results
    //    $('.gridCell').each(function () {
    //        var distance = 10;
    //        var time = 250;
    //        var hideDelay = 100;
    //        var hideDelayTimer = null;
    //        var results = $(this).parent().children('.gridCell');
    //        var beingShown = false;
    //        var shown = false;
    //        var trigger = $('.gridCellLink .magGlass', this);
    //        var info = $('.previewPane', this);
    //        info.css('opacity', 0);

    //        $([trigger.get(0), info.get(0)]).mouseover(function () {
    //            if (results.length == 4 && $(this).closest('.gridCell').is(':last-child')) {
    //                $(this).closest('.gridCell').addClass('right');
    //            }
    //            if (hideDelayTimer) clearTimeout(hideDelayTimer);
    //            if (beingShown || shown) {
    //                // don't trigger the animation again
    //                return;
    //            } else {
    //                // reset position of info box
    //                beingShown = true;

    //                info.css({
    //                    top: 30,
    //                    display: 'block'
    //                }).animate({
    //                    top: '-=' + distance + 'px',
    //                    opacity: 1
    //                }, time, 'swing', function () {
    //                    beingShown = false;
    //                    shown = true;
    //                });
    //            }

    //            return false;
    //        }).mouseout(function () {
    //            if (hideDelayTimer) clearTimeout(hideDelayTimer);
    //            hideDelayTimer = setTimeout(function () {
    //                hideDelayTimer = null;
    //                info.animate({
    //                    top: '-=' + distance + 'px',
    //                    opacity: 0
    //                }, time, 'swing', function () {
    //                    shown = false;
    //                    info.css('display', 'none');
    //                    if ($(this).parent().hasClass('right')) {
    //                        $(this).parent().removeClass('right');
    //                    }
    //                });


    //            }, hideDelay);

    //            return false;
    //        });
    //    });

    previewPanes('.pcrResultsCell', '.gridCellLink .magGlass', '.previewPane');
    previewPanes('.gridCell', '.gridCellLink .magGlass', '.previewPane');

    $('#searchResultsGrid a.downloadBtn').bind('click', function (e) {
        var self = $(this),
            resourceTitle = self.siblings('.description').children('a').text();
        _gaq.push(['_trackEvent', 'Resources', 'SearchDownload', resourceTitle]);
        //console.log('Push event for SearchDownload of ' + resourceTitle);
    });

});

$(window).load(function () {

    /*setHeight('#searchResultsGrid .gridCellLink');
    setHeight('#searchResultsGrid .gridCell .description');
    setHeight('#searchResultsGrid .gridCell .gridFileInfo');*/
    $('#searchResultsGrid .row').each(function () {
        var cellLink = $(this).find('.gridCellLink');
        var desc = $(this).find('.gridCell .description');
        var info = $(this).find('.gridCell .gridFileInfo');
        setHeight(cellLink);
        setHeight(desc);
        setHeight(info);
    });

    setHeight('.pcrResultsCell a:first-child');
    setHeight('.pcrResultsCell .description');
    //$('.pcrResultsCell a:first-child').css({ display: 'block', overflow: 'hidden' });
    $('.pcrResultsCell > a').css({ display: 'block', overflow: 'hidden' });


    // Force Row to correctly flowed height
    /*if ($.browser.msie && parseFloat($.browser.version) < 8) {
    if ($('body').hasClass('ie7')) {
        var newHeight = 0;
        $('#searchResultsGrid .row').each(function () {
            if ($(this).height() > newHeight) {
                newHeight = $(this).height() + 10;
            }
        });
        $('#searchResultsGrid .row').css({ height: newHeight });
    }*/
    if ($('body').hasClass('ie7')) {
        $('#searchResultsGrid .row').each(function () {
            var self = $(this);
            self.css({ height: self.height() + 10 });
        });
    }

    function setHeight(elem) {
        var maxHeight = 0;
        elem = $(elem);

        elem.each(function () {
            if ($(this).height() > maxHeight) {
                maxHeight = $(this).height();
            }
        });

        elem.css({ height: maxHeight });
    }

});

function previewPanes(elem, triggerElem, infoElem) {
    $(elem).each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 100;
        var hideDelayTimer = null;
        var results = $(this).parent().children(elem);
        var beingShown = false;
        var shown = false;
        var trigger = $(triggerElem, this);
        var info = $(infoElem, this);
        var showTriggerDelay = 250;
        var showTriggerTimer = null;
        info.css('opacity', 0);

        $([trigger.get(0), info.get(0)]).mouseenter(function () {
            if (results.length == 4 && $(this).closest(elem).is(':last-child')) {
                $(this).closest(elem).addClass('right');
            }
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (showTriggerTimer) clearTimeout(showTriggerTimer);
            if (beingShown || shown) {
                // don't trigger the animation again
                return;
            } else {
                showTriggerTimer = setTimeout(function () {
                    showTriggerTimer = null;

                    // reset position of info box
                    beingShown = true;
                    $(this).closest(elem).parent().css({ height: ($(this).closest(elem).parent().css('height') + 30) });
                    info.css({
                        top: 30,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function () {
                        beingShown = false;
                        shown = true;
                        // Add class to link to keep magnifying glass visible whilst in preview
                        trigger.closest('.gridCellLink').addClass('hovered');
                    });
                }, showTriggerDelay);
            }

            return false;
        }).mouseleave(function () {
            if (showTriggerTimer) clearTimeout(showTriggerTimer);
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                trigger.closest('.gridCellLink').removeClass('hovered');
                info.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                    if ($(this).parent().hasClass('right')) {
                        $(this).parent().removeClass('right');
                    }
                });
            }, hideDelay);
            return false;
        });
    });
}
