// набор аргументов с параметрами ( массив объектов )
var DTVArgs = new Array();

var DTVLocation = function () {
	//var pp = window.location.hash.match(/([a-zA-Z]+)\/([a-zA-Z0-9]+)/ig);
	var pairs = DTVLocation.getHash().split('&');

	for(j=0;j<pairs.length;j++) {
		var vals = pairs[j].split('=');
		if(vals.length > 1) {
			DTVArgs.push( {
				param: vals[0],
				value: vals[1]
			} );
		}
	}
}

DTVLocation.getHash = function() {
	return window.location.hash.replace('#','');
}
DTVLocation.setHash = function (arr) {
	var hash = new Array();
	for(ind in arr) {
		hash.push( arr[ind].param + "=" + arr[ind].value );
	}
	if( hash.length > 0 ) window.location.hash = hash.join('&') ;
	else DTVLocation.cleanAll();
}

DTVLocation.changeHash = function (param, value) {
	var found = false;
	for(ind in DTVArgs) {
		if(DTVArgs[ind].param == param)
		{
			DTVArgs[ind].value = value;
			found = true;
			break;
		}
	}

	if(!found) DTVArgs.push({ param: param, value: value });

	DTVLocation.setHash(DTVArgs);
}
DTVLocation.setArg = DTVLocation.changeHash;

DTVLocation.removeParam = function (param) {
	for(ind in DTVArgs) {
		if(DTVArgs[ind].param == param)
		{
			DTVArgs.pop(ind);
			break;
		}
	}
	DTVLocation.setHash(DTVArgs);
}
DTVLocation.removeArg = DTVLocation.removeParam;

DTVLocation.getValue = function(param_name,def_value){
	for(ind in DTVArgs) {
		if(DTVArgs[ind].param == param_name) {
            if(DTVArgs[ind].value) {
                return DTVArgs[ind].value;
            } else {
                return def_value;
            }
			break;
		}
	}
	return null;
}
DTVLocation.getArg = DTVLocation.getValue;

DTVLocation.clearHash = function(param_name){
	window.location.hash = '';
}
DTVLocation.cleanAll = DTVLocation.clearHash;

$(document).ready(function() {
	//запуск конструктора
	DTVLocation();
});