var CategoryLog = {
	LOG_A_URL : '/js/common/category_log_a.html',
	LOG_B_URL : '/js/common/category_log_b.html',
	STATUS_DO : 1,
	STATUS_SAME : 0,
	STATUS_EXIT : -1,
	STATUS_A : 2,
	STATUS_B : 3,
	status : this.STATUS_DO,
	logAList : new Array( '/product/','/info/','/service/','/charge/','/support/','/corporate/','/life/' ),
	logBList : new Array( '/service/communication/','/service/information/','/service/entertainment/','/service/convenience/','/service/customize/','/service/world/','/service/safety/','/service/data/','/service/func_tool/','/service/imode/','/service/iappli/',
'/charge/introduction/','/charge/purchase/','/charge/counseling/','/charge/bill_plan/','/charge/discount/','/charge/service/','/charge/online/','/charge/bill_schedule/','/support/utilization/software_update/','/support/utilization/application/','/support/trouble/manual/download/','/support/utilization/product/','/support/shop/','/support/inquiry/','/support/benefit/','/product/','/service/','/charge/','/support/','/corporate/','/life/','/info/' ),
	
	ajax	: null,
	ajaxFlag : false,
	referrerResult : false,
	hrefResult : false,
	
	init : function(){
		if( document.referrer == "" || document.referrer == undefined ){
			return;
		}
		if( this.logA() == this.STATUS_SAME ) this.logB();
	},
	
	logA : function(){
		var status = this.check( this.logAList );
		if( status == this.STATUS_SAME || status == this.STATUS_EXIT ){
			return( status );
		}
		this.referrerResult = this.convA( this.referrerResult );
		this.hrefResult = this.convA( this.hrefResult );
		this.status = this.STATUS_A;
		this.doRequest( this.LOG_A_URL );
		return( this.STATUS_DO );
	},
	
	logB : function(){
		if( this.check( this.logBList ) != this.STATUS_DO ){
			return;
		}
		this.referrerResult = this.convB( this.referrerResult );
		this.hrefResult = this.convB( this.hrefResult );
		this.status = this.STATUS_B;
		this.doRequest( this.LOG_B_URL );
	},
	
	convA : function( word ){
		return( word.replace(/\//g, ""));
	},
	
	convB : function( word ){
		word = word.replace(/^\//, "");
		word = word.replace(/\/$/, "");
		word = word.replace(/\//g, "__");
		return( word );
	},
	
	check : function( list ){
		this.referrerResult = this.matchWord( document.referrer, list );
		if( this.referrerResult == undefined ) return( this.STATUS_EXIT );

		this.hrefResult = this.matchWord( location.href, list );
		if( this.hrefResult == undefined ) return( this.STATUS_EXIT );
		
		if( this.referrerResult == this.hrefResult ) return( this.STATUS_SAME );
		return( this.STATUS_DO );
	},
	
	matchWord : function( url, list ){
		var path = url.replace(/^http:\/\/[\w\.-]+\//, "/");
		var result = list.find(
			function( word ){
				return(( path.indexOf( word ) == 0 ));
			}
		);
		return( result );
	},
	
	doRequest : function( url ){
		var param = this.referrerResult + "=" + this.hrefResult;
		var req = url + "?" + param;
		this.ajax = new Ajax.Request(
			req, 
			{
				method: 	'post', 
				parameters: param,
				onSuccess: 	this.onSuccess.bind( this )
			}
		);
	},
	
	onSuccess : function(){
		if( this.status == this.STATUS_A ) this.logB();
	}
}

Event.observe( window, "load", CategoryLog.init.bindAsEventListener( CategoryLog ));
