/**
 * ページのリクエストURL情報を保持するクラス
 */
function Query(search) {
    this.queries = [];
    this.setSearch(search);
}
Query.prototype = {
    setSearch: function(search) {
        if (search) {
            var idx = search.indexOf('?');
            if (idx >= 0) {
                search = search.substring(idx + 1);
            }
            var qStr = search.split('&');
            for (var i = 0; i < qStr.length; i++) {
                var pair = qStr[i].split('=');
                this.queries.push({key:pair[0], value:pair[1]});
            }
        }
    },
    idx: function(key) {
    	for (var i = 0; i < this.queries.length; i++) {
    		if (this.queries[i].key == key) {
    			return i;
    		}
    	}
    	return -1;
    },
    set: function(key, value) {
    	var pair = {
    		key: key, 
    		value:encodeURIComponent(value)
    	};
    	var idx = this.idx(key);
    	if (idx >= 0) {
    		this.queries[idx] = pair;
    	} else {
    		this.queries.push(pair);
    	}
        return this;
    },
    get: function(key) {
    	var idx = this.idx(key);
    	if (idx >= 0) {
    		return decodeURIComponent(this.queries[idx].value);
    	} else {
    		return null;
    	}
    },
    remove: function(key) {
    	var idx = this.idx(key);
    	if (idx >= 0) {
    		this.queries.splice(idx, 1);
    	}
    },
    /**
     * GET URLを生成する。
     * @param string pathName location.pathnameのような、ファイル名までのURL
     */
    serialize: function(pathName) {
        pathName = pathName || "";
        var str = [];
        for (var i = 0; i< this.queries.length; i++) {
        	var pair = this.queries[i];
            str.push(pair.key + "=" + pair.value);
        }
        return pathName + "?" + str.join("&");
    }
}
