﻿/*var facetContainerId = "searchFacets";
var showClass = "show";
var hideClass = "remove";
var nonLinkHoverClass = "hover";
var currentClass = "current";
var showAllLinkClass = "showAll";
var showTopLinkClass = "showTop";*/

/*$("#holdingsShowHide").click(
    function(){
        alert("here");
        $(".holdingsDetail").hide("slow");
    }
);*/


$(document).ready(function () {
    
    narrowYourResults();

    InnerSearchTabs();

    JournalsNavigationList();

    HoldingsShowHide();
});

function HoldingsShowHide(){
    $('.holdingsDetail').each(
        function(){
            $(this).hide();
    });
    
    $('.holdingsHeader').each(
        function(){
            $(this).append(' (<a href=\"\" class=\"holdingsShowHide\">Show</a>)');
    });

    $('.holdingsShowHide').click(
        function() {
            //adding a callback, I need the table to display as a table and not a block when shown.
            $(this).parent().siblings('.holdingsDetail').toggle("slow");
            if ($(this).text() == 'Show')
                $(this).text('Hide');
            else
                $(this).text('Show');
            return false;
        });
}

function JournalsNavigationList() {
    $("#journalsNavigationList li a").click(function () {

        var urlText = $(this).text();
        var url = $(this).attr("href");
        url = url.replace("/journals-results.aspx", "/searchresultsrecords.aspx");

        // HACK: Because the SearchResults.xslt paging code was assuming the current page
        // was 'searchresultsrecords.aspx' when it was actually 'journals-results.aspx' and it was
        // causing the paging functionality to break. Yeah, Ajax is cool and all that, but you kinda
        // have to think these things through properly first :-)
        JournalsNavigationListAjax(url + '&replaceAjaxUrl=journals-results.aspx', formatHeaderText(urlText));
        return false;
    });
}


function formatHeaderText(urlText) {
    var text = $("#resultMessage .emphasise").first().text();
    var indexofSlash = text.indexOf('/');

    if (indexofSlash > 1)
        text = text.substring(0, indexofSlash);

    text += " / " + urlText;

    return text;
}

function JournalsNavigationListAjax(url, headerText) {
    var $resultsOutput = $('#resultsOutput');
    $.ajax({
        url: url,
        cache: false,
        beforeSend: function () {
            $resultsOutput.empty()
            $resultsOutput.html("<div class='tac' style='padding:10px'><img src='/images/ajaxLoader.gif' alt='loading... ' /></div>");
        },
        success: function (html) {
            $resultsOutput.hide();
            $resultsOutput.empty();

            var $html = $(html);

            var $returnedHeader = $html.find('#resultsHeaderMessage h3');
            $returnedHeader.find('.emphasise').text(headerText);
            $('h3#resultMessage').html($returnedHeader.html());

            $resultsOutput.append($html.html());
            $resultsOutput.fadeIn("fast");
            HoldingsShowHide();
            bindsfxlightbox();
        },
        error: function (html) {
            $resultsOutput.hide();
            $resultsOutput.empty();
            var timedOutMsg = '<br /><p><strong>Time out, please try again.</strong></p>';
            $resultsOutput.append(timedOutMsg);
        }
    });
}

function bindsfxlightbox() {
    $("a[href^='http'][href*='http://shelcat.org/sfxlcl3/menu?']")
        .addClass('sfxPopup')
        .each(function () {
            var anchor = $(this);
            anchor.attr('onclick', '');
            anchor.attr('target', '');
            anchor.removeClass('newWindow');

            anchor.attr('title', '');
            anchor.children('img').attr('alt', '');

            anchor.attr('href', '/SfxTransform.aspx?sfxUrl=' + encodeURIComponent(anchor.attr('href') + "&sfx.response_type=multi_obj_xml") + '&ajax=true&width=600&height=300');
            anchor.prettyPhoto({ deeplinking: false, keyboard_shortcuts: false, social_tools: '', theme: 'light_rounded', opacity: 0.60, changepicturecallback: function () {

                var height = $('.pp_inline').height();
                $('.pp_content').height(height + 50);

                if ($('#redirectUrl').length > 0) {
                    var redirectHref = $('#redirectUrl').attr('href');
                    if (redirectHref.length > 0) {
                        setTimeout("window.open('" + redirectHref + "', '_blank')", 3000);
                    }
                }
            }
            });
        });
}

function InnerSearchTabs() {
    $("#tabInnerSearch li a").click(function() {        
        var url = $(this).attr("href");
        var formType;        
        if (url.indexOf("adv") >= 0) { formType = "adv" } else { formType = "basic" }
        url = url.replace("/search-results.aspx", "/SearchFormsAjax.aspx");
        InnerSearchAjax(url, formType);
        return false;
    });
}


function InnerSearchAjax(url, formType) {    
    $.ajax({
        url: url,
        cache: false,
        beforeSend: function() {
                $("#innerSearch").empty();
                $('#innerSearch').html("<div class='tac' style='padding:10px'><img src='/images/ajaxLoader.gif' alt='loading... ' /></div>");
        },
        success: function(html) {            
            $("#innerSearch").hide();
            $("#innerSearch").empty();            
            $("#innerSearch").append($(html).find("#innerSearchForm"));
            $("#innerSearchMain").attr("class",formType);
            $("#innerSearch").fadeIn("fast");
        }
    });
}

function ViewMore(spacetId){        
    $("#" + spacetId + " li").each(
        function(){                        
            if($(this).children("a").hasClass("showAll"))                
                $(this).css("display", "none");
            else if($(this).children("a").hasClass("showTop"))
                $(this).css("display", "block");
            else                
                $(this).show();                
        }
    );    
    return false;
}

function showTop(spacetId, showTopLimit){
    $("#" + spacetId + " > li:gt(" + (showTopLimit - 1) + ")").each(
        function(){                        
            if($(this).children("a").hasClass("showAll"))
                $(this).css("display", "block");
            else
                $(this).hide();
        }
    );   
    return false;
}

/*  Narrow your results section has 2 levels of results lifting to be displayed as popups  */
function narrowYourResults() {
    $('.facetL1').hide().addClass("floatingBox");
    $('.facetL2').hide().addClass("floatingBox");
    $('.narrowYourResults span.popup').wrapInner("<a href='#' class='all'></a>");
    $('.floatingBox')
        .prepend("<img src='/images/close_popup.png' alt='close' class='floatingBoxClose' />")
            .click(function () {
                $('.floatingBox .floatingBox').hide();
                $('.floatingBox').hide();

            });
            $('.narrowYourResults span.popup a').click(function () {                
                $('.facetL1').hide();
                $('.facetL2').hide();
                $(this).parents("li").children("div").show();
                return false;
            });
}



