/**
 * Cookie()コンストラクタ
 * 指定したcookie情報を設定する
 * @param cname cookie名
 */
function Cookie(cname) {
	//cookieの名前を記憶する
	this.$name = cname ;
	
	//このドキュメントに付随するcookieをすべて読み込む
	var cookieObj = document.cookie ;
	//cookieが存在しない場合は処理を終了
	if ( cookieObj == "" ) return ;
	
	var cookies = cookieObj.split(';') ;
	var cookie = null ;
	for ( var i = 0 ; i < cookies.length ; i++ ) {
		//引数で指定されたcookie名の場合
		if (cookies[i].substring(0, cname.length+1) == (cname + "=")) {
			cookie = cookies[i] ;
			break ;
		}
	}
	//指定したcookieが存在しない場合は処理を終了
	if ( cookie == null ) return ;
	
	//cookieに格納されている値を取得する
	var cval = cookie.substring(cname.length+1) ;
	var wk = cval.split('&') ;
	for ( var i = 0 ; i < wk.length ; i++ ) {
		wk[i] = wk[i].split(':') ;
	}
	for ( var i = 0 ; i < wk.length ; i++ ) {
		//デコードした値を格納する
		this[wk[i][0]] = decodeURIComponent(wk[i][1]);
	}
}

/**
 * cookieの発行処理
 * @param cduration cookieの有効期限（0:Cookie削除、null:セッションクッキー（ブラウザ終了時削除））
 * @param cpath cookieのパス
 * @param cdomain cookieのドメイン
 * @param csecure cookieのsecure属性 （true:secureを設定する、false:secureを設定しない）
 */
Cookie.prototype.publish = function ( cduration , cpath , cdomain , csecure ) {
	var cval = "" ;
	for ( var prop in this ) {
		//$で始まるプロパティ、メソッドを除く
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
			continue ;
		if (cval != "") cval += '&' ;
		cval += prop + ':' + encodeURIComponent(this[prop]) ;
	}
	
	//cookieに保存する文字列を編集する
	var cookie = this.$name + '=' + cval ;
	if ( cduration || cduration == 0 ) {
		var expi = new Date();
		expi.setTime(expi.getTime() + cduration*24*60*60*1000);
		
		cookie += "; expires=" + expi.toGMTString() ;
		//cookie += "; max-age=" + (cduration*24*60*60) ;
	}
	if ( cpath ) cookie += "; path=" + cpath ;
	if ( cdomain ) cookie += "; domain=" + cdomain ;
	if ( csecure ) cookie += "; secure" ;

	document.cookie = cookie ;
}

/**
 * cookieの削除処理
 * @param cpath cookieのパス
 * @param cdomain cookieのドメイン
 * @param csecure cookieのsecure属性 （true:secureを設定する、false:secureを設定しない）
 */
Cookie.prototype.remove = function( cpath , cdomain , csecure ) {
	//取得しているcookie情報を削除する
	for(var prop in this) {
		if (prop.charAt(0) != '$' && typeof this[prop] != 'function') 
			delete this[prop] ;
	}
	//有効期限0でcookieを発行する
	this.publish( 0, cpath , cdomain , csecure ) ;
}

/**
 * cookieの有効判定
 * 有効な場合はtrue、無効な場合はfalseを返す
 * @return （true:有効、false:無効）
 */
Cookie.enabled = function() {
	//使用中のブラウザが、navigator.cookieEnabledが使用できる場合には、
	//navigator.cookieEnabledにて判定を行う
	if ( navigator.cookieEnabled != undefined ) return navigator.cookieEnabled ;
	
	//すでに判定済みであれば、判定結果をキャッシュから返す
	if ( Cookie.enabled.cache != undefined ) return Cookie.enabled.cache ;
	
	//とりあえずcookieを発行してみる
	document.cookie = "checkcookie=test; max-age=10000" ;
	var cookies = document.cookie ;
	if ( cookies.indexOf("checkcookie=test") == -1 ) {
		//発行できなかったので、結果をキャッシュに保存してfalseを返す
		return Cookie.enabled.cache = false ;
	} else {
		//発行したcookieを削除し、
		document.cookie = "checkcookie=test; max-age=0" ;
		//結果をキャッシュに保存してtrueを返す
		return Cookie.enabled.cache = true ;
	}
}
