(function($) {
	var hm=function(){
		var evHashChange=function(newLocation, historyData){
			//console.log(newLocation, historyData)
			//console.log($.HistoryManager.isFirstLoad())
			var locationUtils=new jslt.element.Location()
			var params=locationUtils.keys3(newLocation)
			for(var i=0;i<$.HistoryManager._modules.length;i++){
				$.HistoryManager._modules[i].loadState(params)
			}
		}
		return{
			create:function(options){
				var settings = $.extend({
				debugMode: false
				},options||{});
				window.dhtmlHistory.create({
				debugMode:settings.debugMode
		        ,toJSON: function(o) {
		                return o;
		        }
		        , fromJSON: function(s) {
		                return s;
		        }
			});
			},
			init:function(){
			this._modules=[]
			dhtmlHistory.initialize();
        	dhtmlHistory.addListener(evHashChange);
		}
			//register module that must implement: saveState() & loadState()
			,register:function(ref){
				if(!this._modules){
					return
				}
				this._modules.push(ref)
			}
			,save:function(ref){
				if(!this._modules){
					return
				}
			var obj=ref.saveState()
			var types=[]
			if(obj.params!=undefined && obj.params.cms_id!=undefined ){
				types.push("/cms_id/"+obj.params.cms_id)
			}
			types.push("/_get")
			if(obj.params!=undefined){
				var ar=[]
				for(var p in obj.params){
					ar.push(p+'/'+obj.params[p])
				}
				types.push(ar.join('/'))
			}
			tracker.instance.trackContentView(types.join('/'))
			types.push("hmgrp/"+obj.hmgrp)
			window.dhtmlHistory.add(types.join('/'))
		}
		,isFirstLoad:function(ref){
				return window.dhtmlHistory?window.dhtmlHistory.isFirstLoad():false
			}
			/**
			 * @return boolean true if hash contains data for load state
			 */
			,loadFromURL:function(){
				if(location.hash!=''){
					evHashChange(location.hash.substr(1))
					return true
				}
				return false
			}
		}
	};
	$.HistoryManager=new hm()
})(jQuery);
/*
,saveState:function(){
		var p={}
		return {hmgrp:'G',params:p}
	}
	,loadState:function(par){
		if(par.hmgrp=='G'){
			if(par.p!=undefined){
			}
		}
	}
$.HistoryManager.register(this)
$.HistoryManager.save(this) 
 * 
 */

