DynamicAddressChanger.prototype.instance = null;
function DynamicAddressChanger() {
	this.restrictLastHash=0;
    this.currentAddressParsed;//{var:value,var:value...}
    this.parseCurrentAddress();
    this.addListener();
    this.callNum=0;
    this.callOut=0;
}

DynamicAddressChanger.prototype.hashChangeHandler;

DynamicAddressChanger.prototype.addListener=function(){
	var self=this;
	$(window).bind('hashchange', this.hashChangeHandler=function(){
		self.callNum++;
		if(self.callNum<self.callOut){
			self.restrictLastHash-=self.callOut-self.callNum;
			self.callOut=self.callNum;
		}
		
		if(self.restrictLastHash==0){
			self.parseCurrentAddress();
			$(window).trigger('historyBack');
		}else{
			self.restrictLastHash-=1;
		}
	});
}

DynamicAddressChanger.prototype.removeListener=function(){
	$(window).unbind('hashchange',this.hashChangeHandler);
}

DynamicAddressChanger.prototype.parseCurrentAddress = function () {
    var address = window.location.hash;
    //try to parse according to: #/var1/value1/var2/value2...
    var varValuePairs = address.split('/');//maybe too simple
    /*if (varValuePairs.length % 2 == 1) {
        this.currentAddressParsed = {};
        for (var currentVarIndex = 1; currentVarIndex < varValuePairs.length; currentVarIndex+=2) {//first element is '#'
            this.currentAddressParsed[varValuePairs[currentVarIndex]]=varValuePairs[currentVarIndex+1];
        }
    } else {
        this.currentAddressParsed = {};
    }*/
    this.currentAddressParsed=[];
    for (var currentVarIndex = 1; currentVarIndex < varValuePairs.length; currentVarIndex++) {//first element is '#'
        this.currentAddressParsed[currentVarIndex-1]=varValuePairs[currentVarIndex];
    }
};

DynamicAddressChanger.prototype.changeAddress = function (newAddress) {//syntax as currentAddress
    this.currentAddressParsed = newAddress;
    this.refreshAddress();
};

DynamicAddressChanger.prototype.refreshAddress = function () {
    var newAddressString = '#';
    for (var currentVar in this.currentAddressParsed) {
        newAddressString += '/' + this.currentAddressParsed[currentVar];
    }
    this.callOut++;
    this.restrictLastHash+=1;
    if(window.location.hash != newAddressString)
    	window.location.hash = newAddressString;
};

DynamicAddressChanger.prototype.getValue=function(index){
	if(this.currentAddressParsed[index]){
		return this.currentAddressParsed[index];
	}
	return null;
};

DynamicAddressChanger.prototype.setValue=function(index,value){
	this.currentAddressParsed[index]=value;
	//this.refreshAddress();
};

/*DynamicAddressChanger.prototype.getValue=function(varName){
    if(this.currentAddressParsed[varName]){
        return this.currentAddressParsed[varName];
    }
    return null;
};*/
/*
DynamicAddressChanger.prototype.setValue = function (varName, value) {
    this.currentAddressParsed[varName] = value;
    this.refreshAddress();
};*/

DynamicAddressChanger.prototype.clear=function(){
	this.currentAddressParsed=[];
	//this.refreshAddress();
};

DynamicAddressChanger.prototype.getInstance = function () {
    if (DynamicAddressChanger.prototype.instance == null)
        DynamicAddressChanger.prototype.instance = new DynamicAddressChanger();
    return DynamicAddressChanger.prototype.instance;
};
