var rum = {};

// Begin customization

// the URL of the log service
rum.urlPrefix = 'http://www.nutzungsmonitor.com/zhaw-log/';
// the name of the cookie containing the user session ID
rum.sessionCookieName = 'fe_typo_user';

// End customization


rum.RUM = function(urlPrefix, sessionCookieName, 
                   sessionParam, restrictionParam, pageParam, typeParam) {
    this.sep = '::';
    this.urlPrefix = urlPrefix;
    this.sessionCookieName = sessionCookieName;
    this.sessionParam = sessionParam;
    this.restrictionParam = restrictionParam;
    this.pageParam = pageParam;
    this.typeParam = typeParam; 
    this.docIdParam = null;
    this.queryParam = 'queryString';
    this.queryIdParam = null;
    
    this.location = document.location;
    this.search = document.search;
    this.parameters = this.getParameters(this.location.toString());

    // sessionId
    this.sessionId = this.getSessionId(this.sessionCookieName, this.sessionParam);

    // queryString
    this.queryString = this.parameters.get(this.queryParam, '');

    // queryId
    this.queryId = this.parameters.get(this.queryIdParam, this.str2hex(this.queryString));

    // queryRestriction
    this.queryRestriction = this.parameters.get(this.restrictionParam, '-');

    // searchPage
    this.searchPage = this.parameters.get(this.pageParam, 1);

    // queryType
    this.queryType = this.parameters.get(this.typeParam, 'simple_search');
};


rum.RUM.prototype.getSessionId = function(cookieName, param) {
    var sessionId = null;
    if (cookieName && cookieName != '') {
        sessionId = this.getCookie(document, cookieName);
    }
    else if (!sessionId && param && param != '') {
        sessionId = this.parameters.get(param, null);
    }
    else {
        sessionId = '';
    }

    if (sessionId == null) {
        // if cookies are disabled, the session ID is put into the URL
        var href = window.location.href;
        if (href != null) {
            var i = href.indexOf('id=');
            if (i > 0) sessionId = href.substr(i + 3);
        }
    }

    return sessionId;
}


// dummy function
rum.voidf = function() {}

// Returns an object mapping parameters contained in the URL-encoded string.
// The object has a method get(s,def), which returns the value of the
// name s or def if the name is empty or not bound.
rum.RUM.prototype.getParameters = function(str) {
    var p = {};
    p.get = function(s, def) {
        if (!s) return def;
        var res = this[s];
        if (!res) return def;
        return res;
    }

    if (!str) return p;

    var i = str.indexOf('?');
    if (i >= 0) str = str.substring(i+1);
    var split = str.split('&');
    for (var i = 0; i < split.length; i++) {
        var s = split[i].split('=');
        if (s.length == 2) p[s[0]] = s[1];
    }

    return p;
};


// Converts the string argument by replacing each character with a 2-character hex-code.
rum.RUM.prototype.str2hex = function(str) {
    var a, b, d;
    var hex = '';
    for (var i = 0; i < str.length; i++) {
      d = str.charCodeAt(i);
      a = d % 16;
      b = (d - a)/16;
      hex += '0123456789abcdef'.charAt(b) + '0123456789abcdef'.charAt(a);
    }
    return hex;
};


// Returns the cookie with the given name in the document.
// If there is no cookie associated with that name, null is returned.
rum.RUM.prototype.getCookie = function(document, name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }

    return null;
};


// Creates a log entry for the results page
// with the specified number of ranks, query string and query restriction.
// Must be executed on loading the results page.
rum.RUM.prototype.pageLog = function(nrOfRanks, query, restriction) {
    query = query.replace(/\+/g, ' '); 
    this.queryString = unescape(query) + '======';
    this.queryId = this.str2hex(this.queryString);

    if (restriction != '') {
        this.queryType = 'advanced_search';
        this.queryRestriction = restriction;
    }

    // <sessionId>::<queryId>::<queryString>::<queryRestriction>::<searchPage>::<queryType>::<nrOfRanks>::<emptyField>::<cookieValues>    
    var pageparam = [this.sessionId,
                     this.queryId,
                     this.queryString,
                     this.queryRestriction,
                     this.searchPage,
                     this.queryType,
                     nrOfRanks,
                     '-','-'].join(this.sep);

    this.log(this.urlPrefix+'pagelog.php?v='+escape(pageparam).replace(/\+/g, '%2b'));
    
    return true;
}

// Creates a log entry for the selection of a result with the specified rank and
// doc ID (which is usually the URL of the result document).
// Must be called on clicking a result link.
rum.RUM.prototype.documentLog = function(rank, docId) {
    this.queryId = this.str2hex(this.queryString);

    // documentId
    var documentId = this.parameters.get(this.docIdParam, docId);
    
    var i = documentId.indexOf(';jsession'); // if cookies disabled
    if (i > 0) documentId = documentId.substr(0, i);
    
    // collectionId
    var collectionId = this.parameters.get(this.collectionIdParam, '-');

    // rank
    var rankNr = this.parameters.get(this.rankParam, rank);
    
    // <sessionId>::<queryId>::<documentId>::<rank>::<collectionId>::<mimeType>::<docLength>::<renderType>
    var docparam = [this.sessionId,
                    this.queryId,
                    documentId,
                    rankNr,
                    '-','-','-','-'].join(this.sep);
    
    this.log(this.urlPrefix+'documentlog.php?v='+escape(docparam));

    return true;
}


rum.RUM.prototype.log = function(s) {
    if (document.images) {
        var im = new Image(1,1);
        im.src = s;
        im.onload = function() { rum.voidf();}
    }
};


rum.global = new rum.RUM(rum.urlPrefix, rum.sessionCookieName, null, null,
                         'tx_zhaweurospider_pi1%5Bpointer%5D',
                         'tx_zhaweurospider_pi1%5Bsearch_mode%5D');

function PageLog(nrOfRanks, query, restriction) {
    return rum.global.pageLog(nrOfRanks, query, restriction);
}

function DocumentLog(rank, docId) {
    return rum.global.documentLog(rank, docId);
}

