var last_location_hash = location.hash;

/* Checks wether location.hash has changed. If yes, it calls a callback function. */
function check_location_hash(cb) {
    if (location.hash != last_location_hash) {
        last_location_hash = location.hash;
        cb();
    }
}

/* Calls a function each time the location hash changes. */
function monitor_location_hash(cb, interval) {
    window.setInterval('check_location_hash(' + cb + ')', interval);
}

function hash_to_array(hash) {
    if(hash == '') {
        return [];
    }
    return hash.split('#')[1].split(',')
}

function array_to_hash(array) {
    return array.join(',');
}

/* Checks wether the location.hash contains the given substring
   within a list of values separated by the given separator*/
function in_location_hash(str) {
    return hash_to_array(location.hash).contains(str);
}

function add_to_location_hash(str) {
    array = hash_to_array(location.hash);
    if(!array.contains(str)) {
        array.push(str);
    }

    location.hash = array_to_hash(array);
}

function remove_from_location_hash(str) {
    array = hash_to_array(location.hash);
    array.erase(str);

    location.hash = array_to_hash(array);
}

