/***** 文字列解析用ライブラリ paramsControl.js *****
 ***** 文字解析用関数 paramsControl ****************
 * .str					:文字列
 * .query				:StringSearch オブジェクト
 * .pattern				:RegExp 正規表現パターンオブジェクト
 * .getScriptUrl		:scriptタグの末端からパラメータの値を取得する。
 *						 外部jsファイルが読み込まれた最初に実行すると外部jsファイルに付加されたパラメータが取得出来る。
 * .item(key)			:内部から"key"に関連づけられたvalueを取得する。
 * .length				:配列数を返す。
 * .getQuery(pattern)	:
 * .toString()
 ***************************************************/
var paramsControl = function(_str){
	if(_str){this.str=_str;}
};
paramsControl.prototype ={
	str			:null,
	query		:null,
	pattern		:null,
	setPatternString:function(_pattern)
	{
		this.pattern=new RegExp(_pattern,"g");
	},
	getScriptUrl:function()
	{	
		//現時点の表現されているscriptタグを配列で取得
		var scripts;
		if (document.getElementsByTagName){
			scripts = document.getElementsByTagName("script");
		}else if (document.scripts){
			scripts = document.scripts;
		}else if (document.all && document.all.tags){
			scripts = document.all.tags("script");
		}
		if (!scripts){
			throw this.error.getScriptUrl();
		}else{
			//読みこみ中に実行されるので、最後のscriptが自分自身を表す
			var script = scripts[scripts.length -1];
			//自分自身のsrc属性(ここに引数も含まれる)
			var _str=script.src+"";
			this.str=_str.match(/\?(.*)$/)[1];
			//set pattern
			this.setPatternString(this.config.pattern.url);
			return this.str;
		}
	},
	item		:function(key)
	{
		if(this.query){
			return this.query.item(key);
		}else{
			throw this.error.item();
		}
	},
	length		:function()
	{
		return this.query?this.query.length()	:false;
	},
	getQuery	:function(_pattern)
	{
		if(_pattern){
			setPatternString(_str);
		}
		if(this.str){
			this.query = new StringSearch(this.str,this.pattern);
			return (this.query.sAry?this.query.Ary:null);
		}else{
			throw this.error.getQuery();
		}
	},
	toString	:function()
	{
		var center,closure,klt,kgt,vlt,vgt;
		ce=	this.config.strings.center	;
		cl=	this.config.strings.closure	;
		klt=this.config.strings.klt		;
		kgt=this.config.strings.kgt		;
		vlt=this.config.strings.vlt		;
		vgt=this.config.strings.vgt		;
		if(this.query){
			var str=null;
			for(_str in this.query.sAry)
			{
				str=(str?str:"") + cl+klt + _str + kgt+ce+vlt+this.item(_str)+vgt;
			}
			return str;
		}else{
			return "NoData";
		}
	},
	config:{
		strings:{
			center:		":",
			closure:	",",
			klt:		"&lsquo;",
			kgt:		"&rsquo;",
			vlt:		"&ldquo;",
			vgt:		"&rdquo"
		},
		pattern:{
			url:"([^\?\&]+)=([^\?\&]+)"
		},
		toString:function(){return "will be used to script the value of the fixed value is maintained.";}
	},
	error:{
		getScriptUrl:	function(name){return new Error(name+"Error:Not Strings");},
		getQuery:		function(name){return new Error(name+"Error:Not PatternStrings");},
		item:			function(name){return new Error(name+"Error:Not Doing '.getQuery'");},
		toString:function(){return "Management of the script errors.";}
	}
};

/***** 文字列からkeyとvalueを抽出するライブラリ*****
 * .sSource				:抽出元の値
 * .sAry				:抽出後の連想配列
 * .pattern				:RegExp 正規表現パターンオブジェクト
 * .getScriptUrl		:scriptタグの末端からパラメータの値を取得する。
 *						 外部jsファイルが読み込まれた最初に実行すると外部jsファイルに付加されたパラメータが取得出来る。
 * .item(key)			:内部から"key"に関連づけられたvalueを取得する。
 * .length				:配列数を返す。
 * .getQuery(pattern)	:
 * .toString()
 ***************************************************/
var StringSearch=function(_Source,p)
{
	this.serach(_Source,p);
};

StringSearch.prototype = {
	sAry		:null,
	sSource		:null,
	sPattern	:null,
	/* 値 */
	valueOf		:function () {
		return this.sSource;
	},
	serach		:function(_Source,p){
		this.sSource =_Source;
		this.sPattern = (p?p:this.sPattern);
		this.reSerach();
	},
	reSerach	:function()
	{
		var str =this.sSource;
		var p=this.sPattern;
		this.sAry = new Array();
		if(p)
		{
			var point=true,key,value;
			while(point!==0)
			{
				point=p.exec(str);if(!point){break;}
				key		=point[1];value	=point[2];
				this.sAry[key]=value;
			}
		}else{
			throw new Error("Pattern is Nothing !!");
		}
	},
	/* 値を取り出すメソッド */
	item		:function (sVarname) {
		if(this.Ary===null){this.error.item();}
		return (this.sAry[sVarname]?this.sAry[sVarname]:'');
	},
	/* 要素数 */
	length		:function () {
		return this.sAry.length;
	},
	error:{
		item :function(){throw new Error("Don't Play Serch");}
	},
	/* toString */
	toString	:function () {
		return this.length() + '';
	}
};

/*****Cookie操作用関数群*****/
var CookieToolbox={
	/**
	 * Get Cookie
	 * @param name KeyName
	 * @return cookie's string
	 */
	getCookie	:function( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	},
	/**
	 * Setting Cookie
	 * @param name KeyName
	 * @param value
	 * @param expires
	 * @param domain
	 * @param secure
	 */
	setCookie	:function( name, value, expires, path, domain, secure ) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
			( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
			( ( path ) ? ';path=' + path : '' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			( ( secure ) ? ';secure' : '' );
	},
	/**
	 * Delete Cookie
	 * @param name
	 * @param path
	 * @param domain
	 */
	deleteCookie	:function( name, path, domain ) {
		if ( getCookie( name ) ) document.cookie = name + '=' +
				( ( path ) ? ';path=' + path : '') +
				( ( domain ) ? ';domain=' + domain : '' ) +
				';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	},
	/**
	 * Cookie can use?
	 * @return true/false;
	 */
	mood	:function(){
		return navigator.cookieEnabled;
	}
};
