﻿//This function renders the output
function renderCSLWidget_SearchHistory(theWidgetId, theMaxHistory, theMaxTitleLength, theHitsText, theToolTip, theHitsSingleText) {
    CSLWidget_SearchHistory_Count = $.cookie('CSLWidget_SearchHistory_Count' + theWidgetId);
    //Get stored search history from cookie 
    if ((CSLWidget_SearchHistory_Count != null) && (CSLWidget_SearchHistory_Count > 0)) {
        //Add div and title
        $('#CSLWidget_SearchHistory').append('<div id="CSLWidget_SearchHistory_Container" class="CS_buyTheBook box"><div>');
        $('#CSLWidget_SearchHistory').append('<h2>Sökhistorik</h2>');

        $('#CSLWidget_SearchHistory').append('<ul id="CSLWidget_SearchHistory_LI"></ul>');
        var title = '';
        var shortTitle = '';
        // Added 010120
        var postText = theHitsText;
        
        for (var i = 1; i <= theMaxHistory; i++) {
            if (($.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + i) != null) && ($.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + i) != '')) {
                title = $.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + i);
                shortTitle = title;
                if (shortTitle.length > theMaxTitleLength) {
                    shortTitle = title.substr(0, theMaxTitleLength - 3) + '...';
                }
                
                // Added 010120
                postText = theHitsText;
                if ($.cookie('CSLWidget_SearchHistory_Hitcount' + theWidgetId + i) == 1) {
                    postText = theHitsSingleText;
                }
                
                $('#CSLWidget_SearchHistory_LI').append(
                        '<li id="' + title + '_li" style=""><a href="' + $.cookie('CSLWidget_SearchHistory_Url' + theWidgetId + i) + '" class="" title="' + theToolTip + '">'
                        + shortTitle + ' (' + $.cookie('CSLWidget_SearchHistory_Hitcount' + theWidgetId + i) + postText + ')'
                        + '</a></li>');

            }
        }
    }
}

function saveCSLWidget_SearchHistory(theWidgetId, Title, URL, Hitcount, theMaxHistory) {
    //Search already saved?
    for (var i = 1; i < theMaxHistory; i++) {
        if ($.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + i) == Title) {
            return true;
        }
    }
    CSLWidget_SearchHistory_Count = CSLWidget_SearchHistory_Count + 1;
    if (CSLWidget_SearchHistory_Count > theMaxHistory) {
        CSLWidget_SearchHistory_Count = theMaxHistory;
        //Shift history backwards
        for (var i = 1; i < theMaxHistory; i++) {
            $.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + i,
                            $.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + (i + 1)));
            $.cookie('CSLWidget_SearchHistory_Url' + theWidgetId + i,
                            $.cookie('CSLWidget_SearchHistory_Url' + theWidgetId + (i + 1)));
            $.cookie('CSLWidget_SearchHistory_Hitcount' + theWidgetId + i,
                            $.cookie('CSLWidget_SearchHistory_Hitcount' + theWidgetId + (i + 1)));
        }

    }
    //Set history cookie
    $.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + CSLWidget_SearchHistory_Count, Title);
    $.cookie('CSLWidget_SearchHistory_Url' + theWidgetId + CSLWidget_SearchHistory_Count, URL);
    $.cookie('CSLWidget_SearchHistory_Hitcount' + theWidgetId + CSLWidget_SearchHistory_Count, Hitcount);

    $.cookie('CSLWidget_SearchHistory_Count' + theWidgetId, CSLWidget_SearchHistory_Count);
}

//Adds query to list
function initCSLWidget_SearchHistory(theWidgetId, theMaxHistory, theSeparator) {
    CSLWidget_SearchHistory_Count = parseInt($.cookie('CSLWidget_SearchHistory_Count' + theWidgetId));

    //Parse querystrings
    var query = getQueryString('query');
    var title = getQueryString('title');
    var tag = getQueryString('tag');
    var author = getQueryString('author');
    var group = getQueryString('group');
    var serie = getQueryString('serie');
    var genre = getQueryString('genre');
    var classification = getQueryString('classification');
    var shelflocation = getQueryString('shelflocation');
    var isbn = getQueryString('isbn');
    var publishyearmin = getQueryString('publishyearmin');
    var publishyearmax = getQueryString('publishyearmax');
    var language = getQueryString('language');
    var media = getQueryString('media');
    var library = getQueryString('library');
    var department = getQueryString('department');
    var sort = getQueryString('sort');
    //Added 010120
    var contributor = getQueryString('contributor');

    //Build title
    var historytitle = '';
    if (query != null) historytitle = historytitle + query + theSeparator;
    if (title != null) historytitle = historytitle + title + theSeparator;
    if (tag != null) historytitle = historytitle + tag + theSeparator;
    if (author != null) historytitle = historytitle + author + theSeparator;
    if (group != null) historytitle = historytitle + group + theSeparator;
    if (serie != null) historytitle = historytitle + serie + theSeparator;
    if (genre != null) historytitle = historytitle + genre + theSeparator;
    if (classification != null) historytitle = historytitle + classification + theSeparator;
    if (shelflocation != null) historytitle = historytitle + shelflocation + theSeparator;
    if (isbn != null) historytitle = historytitle + isbn + theSeparator;
    if (publishyearmin != null) historytitle = historytitle + publishyearmin + theSeparator;
    if (publishyearmax != null) historytitle = historytitle + publishyearmax + theSeparator;
    if (language != null) historytitle = historytitle + language + theSeparator;
    if (media != null) historytitle = historytitle + media + theSeparator;
    if (library != null) historytitle = historytitle + library + theSeparator;
    if (department != null) historytitle = historytitle + department + theSeparator;
    //Added 010120
    if (contributor != null) historytitle = historytitle + contributor + theSeparator;
    //Removed 010120
    //if (sort != null) historytitle = historytitle + sort + theSeparator;

    if (historytitle != '') {
        //Remove trailing /
        var iLen = String(historytitle).length;
        historytitle = unescape(String(historytitle).substring(0, iLen - 1));
        var hits = $('#hitcount').html();
        saveCSLWidget_SearchHistory(theWidgetId, historytitle, window.location, hits, theMaxHistory);
    }
}
//This function returns a querystring
function getQueryString(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return decodeURIComponent(ft[1].replace('+',' '));
        }
    }
};

function prepareSearchHistoryCookies(theWidgetId, theMaxHistory) {
    //Make sure all cookies exists
    if (!$.cookie('CSLWidget_SearchHistory_Count' + theWidgetId)) {
        $.cookie('CSLWidget_SearchHistory_Count' + theWidgetId, '0');
    }
    //Make room for 5 items in history
    for (var i = 1; i <= theMaxHistory; i++) {
        if (!$.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + i)) {
            $.cookie('CSLWidget_SearchHistory_Title' + theWidgetId + i, '');
        }
        if (!$.cookie('CSLWidget_SearchHistory_Url' + theWidgetId + i)) {
            $.cookie('CSLWidget_SearchHistory_Url' + theWidgetId + i, '');
        }
        if (!$.cookie('CSLWidget_SearchHistory_Hitcount' + theWidgetId + i)) {
            $.cookie('CSLWidget_SearchHistory_Hitcount' + theWidgetId + i, '');
        }
    }
}