var isOnHashchangeSupport = ('onhashchange' in window);

var ajaxHistory;
var ajaxHistoryObject = function(){

    var self = this;
    this.hashCounter = 0;
    this.innerHTML = {};
    this.url = '/wsp/wj7/frontend.cgi?func=ajaxHistory.getCookie';
    this.div = $('main');
    this.cookie = new Cookie();
    this.cookieStateName = 'wj7_history_state'
    this.log = [];
    this.currentHash = document.location.hash;

    this.setHash = function(){
        self.currentHash = 'wj7' + (++self.hashCounter);
        self.innerHTML[self.currentHash] = escape(self.div.innerHTML.replace(/\n/g,''));
        document.location.hash = self.currentHash;
        self.saveState();
    };
    this.saveState = function(){
       /*
       // (2011-08-24 HHA): This causes older IE browsers (pre 9) to go very slow because the cookie parameter is filled
       // with html source code (larger for every click). In agreement with PL, this function was therefore disabled.
       if(self.cookie && !self.currentHash.blank()){
           var v = Object.toJSON(self.innerHTML);
           self.cookie.set(self.cookieStateName, self.currentHash);
           new Ajax.Request(self.url, {
               method: 'post',
               parameters: { cookie: v, debug: 'off', skipResponders: '1' }
           });
       }
       */
    };
    this.storeState = function(key){
       var docHash = document.location.hash;
       if(self.cookie && self.cookie.get(self.cookieStateName)){
          var defaultTitle = document.title;
          ajaxing = new Ajax.Request(self.url, {
              parameters: { debug: 'off', skipResponders: '1' },
              method: 'get',
              onCreate: function(){
                  $(document.body).setStyle('cursor: progress');
              },
              onLoading: function(){
                  document.title = freetext.retrieving_search_result || "retrieving_search_result";
              },
              onSuccess: function(r){

                 $(document.body).setStyle('cursor: default');
                 document.title = defaultTitle;

                 var data = r.responseText.evalJSON();
                 var h = $H(self.innerHTML).update(data); 
                 self.innerHTML = h.toObject();
                 self.hashCounter = h.keys().length - 1; 
 
                 if(!docHash.blank()){
                    var hashName = docHash.replace('#','');
                    self.handler(hashName);
                 } 

                 if(isOnHashchangeSupport){
                    self._addLocationHash();
                 }
              }
          });

       }

    };
    this._addLocationHash = function(){
         try {
         for(var h in self.innerHTML){
            if(h != self.currentHash){
               document.location.hash = h;
            }
         }
         if(self.currentHash.blank()){
            self.setHash();
         }
         }catch(e){ alert(e.description); }
    };
    this.handler = function(h){
        var key = h || 'wj7';
        if(typeof(self.innerHTML[key]) != 'undefined'){
          self.div.update( unescape(self.innerHTML[key]) );
        }
    };
   
    this.init = function(){ 
       self.innerHTML['wj7'] = escape(self.div.innerHTML.replace(/\n/g,'')); 
       self.storeState();
       return self;
    };
    return this.init();
};
Ajax.Responders.register({
    onComplete: function(a){
        if(!a.parameters.skipResponders){
            self.hashCounter++;
            ajaxHistory.setHash();
        }
    }
});
if( false == ('onhashchange' in window)){
      dhtmlHistory.create({
        toJSON: function (o) {
            return Object.toJSON(o);
        },
        fromJSON: function (s) {
            return s.evalJSON();
        }
      });
}
 
document.observe('dom:loaded', function(){
try {
    ajaxHistory = new ajaxHistoryObject();
if (!isOnHashchangeSupport) {
        dhtmlHistory.initialize();
        dhtmlHistory.addListener(ajaxHistory.handler);
    } else {
        window.onhashchange = function () {
            ajaxHistory.handler(document.location.hash.replace('#',''));
        }
    }
}catch(e){
  alert(e);
}
});
