/**
 * Replaces the param "bkms" within a URL string with the current server time value in case the user used the back button twice from a search results page.
 * The bkms value is used to track if a user has used the back button by using the server time in millseconds.
 * @USAGE
 * Function can be called upon the following node types:FORM,INPUT,A(HREF)
 * Here is a sample of the correct useage:
 *  var fixBKmsEls = new Array('fyc_to_url', 'fyc-browse-by-body-link');
 *  new fixBKms(fixBKmsEls);
 * @PARAMS
 * els (required) - an array of the elements that need to be updated with the most current BKms value
 */

/**
 * Validate Prototype Version
 */
if((typeof Prototype=='undefined') ||
    (typeof Element == 'undefined') ||
    (typeof Element.Methods=='undefined') ||
    parseFloat(Prototype.Version.split(".")[0] + "." +
               Prototype.Version.split(".")[1]) < 1.6){
    throw("fixBKms requires the Prototype JavaScript framework >= 1.6.0")
}

// BKms Fix
var fixBKms = Class.create({
    BKmsVal:null,
    BKmsNum:null,
    cacheBuster:null,
    els:null,
    initialize : function(els) {
        this.cacheBuster = Math.round((Math.random()*9000)+1);
        this.els = $A(els);
        this.getBKms();           
    },
    getBKms: function() {
        new Ajax.Request('/ajax/backButtonFix.jsp?Log=0&rand=' + this.cacheBuster, {
          method: 'get',
          onSuccess : this.onSuccess.bind(this)          
        });
    },
    onSuccess: function(transport) {
        this.BKmsVal = transport.responseText;
        var tmpBKms  = transport.responseText.split('=');
        this.BKmsNum = tmpBKms[1];
        this.setBKms();
    },
    setBKms: function() {
        var elsLen = this.els.length;
        for(var i=0; i < elsLen; i++){
            if($(this.els[i])){
                if($(this.els[i]).nodeName == 'A'){
                    this.fixHref(this.els[i]);
                } else {
                    this.fixForm(this.els[i]);
                }
            }
        }
    },
    updateStr: function(str) {
        if(str.indexOf('?',0) == -1){
            return str + '?bkms=' + this.BKmsNum;
        }
        var strLen = str.length;
        var strStart = str.indexOf('bkms=',0);
        var strSub1 = str.substring(0,strStart);
        var strEnd = str.indexOf('&',strStart);
        var strSub2 = null;
        if (strEnd != -1) {
            strSub2 = str.substring(strEnd,strLen);
        } else {
            strSub2 = '';
        }
        var srtFinal = strSub1 + 'bkms=' + this.BKmsNum + strSub2;
        return srtFinal.replace(/\s+/g, '');
    },    
    fixForm: function(el) {
        var attrType = null;
        var elStr = null;
        if($(el).nodeName == 'FORM'){
            attrType = 'action';
            elStr = $(el).readAttribute('action');
            $(el).writeAttribute({'action':this.updateStr(elStr)});
        }else if($(el).nodeName == 'INPUT'){
            attrType = 'value';
            elStr = $(el).readAttribute('value');
            $(el).writeAttribute({'value':this.updateStr(elStr)});
        }else if($(el).nodeName == 'AREA'){
            attrType = 'href';
            elStr = $(el).readAttribute('href');
            $(el).writeAttribute({'href':this.updateStr(elStr)});
        } else {
            return;
        }
    },
    fixHref: function (el) {
        var elHref = $(el).readAttribute('href');
        $(el).writeAttribute({'href':this.updateStr(elHref)});
    }
});