/*-----------------------------------------------------------------------
 * 汎用ライブラリ
 *
 * DEPENDENCIES
 *
 * @version $Revision: 1.2 $
 *-----------------------------------------------------------------------
 */

//クッキー操作用オブジェクト
function NVCookie(name){
    this.name = name ? name : "";
	this.value = this.read();
	this.expires = null;
	this.path = null;
	this.domain = null;
	this.secure = null;
}
NVCookie.prototype = {
	write : function(){
	    var c =  this.name + "=" + escape(this.value) +
			(this.expires ? "; expires=" + this.expires : "") +
			(this.path ? "; path=" + this.path : "") +
			(this.domain ? "; domain=" + this.domain : "") +
			(this.secure ? "; secure" : "");
		document.cookie = c;
	},
	read : function(){
		if(!document.cookie){
			return null;
		}
		var cookies = document.cookie.split(";");
		var regex = new RegExp("\\s*" + this.name + "\\s*=\\s*(\\S+)");
		for(var i = 0; i < cookies.length; i++){
			if(cookies[i].match(regex)){
				return unescape(RegExp.$1);
			}
		}
		return null;
	},
	remove : function(){
		this.expires = this.dayToExpires(-1);
		this.write();
	},
	dayToExpires : function(exprDay){
	    var expires = new Date();
		expires.setTime( (new Date()).getTime() + 3600000 * 24 * exprDay ); 
		return expires.toGMTString();
	}
}

//テンプレートマージ用のContextオブジェクト
function NVContext(){
	this.map = new Object();
}
NVContext.prototype = {
	put : function(key,val){
		this.map[key] = val;
	},
	getAll : function(){
		return this.map;
	}
}

//Contextと文字列をマージする関数
function NVTemplate(){
}
NVTemplate.prototype = {
	evaluate : function(template, context){
		var ret = template;
		var map = context.getAll();
		for(var key in map){
			ret = ret.replace((new RegExp("\\$\\{" + key + "\\}", "g")), map[key]);
		}
		return ret;
	}
}

//パラメータ操作用
//NVRequest.getParameter("パラメータ名")でパラメータが取れる
function _NVRequest(){
	this.parameterMap = this.init();
}
_NVRequest.prototype = {
	queryString : function(){
		return window.location.search.substr(1);
	},
	init : function(){
	    var params = this.queryString().split("&");
		var paramMap = new Object();
	    for(var i = 0; i < params.length; i++) {
	        var keyVal = params[i].split("=");
			paramMap[keyVal[0]] = keyVal[1];
	    }
		return paramMap;
	},
	getParameter : function(nm){
		return this.parameterMap[nm];
	}
}
var NVRequest = new _NVRequest();