﻿/*
 * 下記スクリプトをbodyタグの終了直前に配置する。
 *
 * var LogClass = new Log();
 * LogClass.send(LogClass.getParam());
 *
 *
 * Log 1.0 -> created
 * Log 1.1 -> Method company has added.
 * Log 1.2 -> Method product has added.
 * Log 1.3 -> Method catalog has added.
 * Copyright (c) 2008 e-space Inc. All Rights Reserved.
 * created by Yuya -> 2008.03.18
 */

//コンストラクタ
function Log(){
	this.version = parseInt(navigator.appVersion);
	this.os = String(navigator.platform);
	this.url = String(location);
	this.host = String(location.host);
	var start = (Number(this.url.indexOf(this.host)) + Number(this.host.length) + 1);
	var end = Number(this.url.length);
	this.path = this.url.substring(start,end);
	this.pathAry = this.path.split('/');
	//this.LogUrl = 'https://' + this.host + '/log/';
	this.LogUrl = '/log/';
	//Ajax通信のオブジェクトを生成
	var AjaxHTTP = [
		function(){ return new XMLHttpRequest(); },
		function(){ return new ActiveXObject("Msxml2.XMLHTTP"); },
		function(){ return new ActiveXObject("Microsoft.XMLHTTP"); }
	];
	for(var i=0;i<AjaxHTTP.length;i++){
		try{
			var factory = AjaxHTTP[i];
			var request = factory();
			if(request != null){
				this.AjaxHTTP = request;
				break;
			}
		}catch(e){
			continue;
		}
	}
	if(!this.AjaxHTTP){
		this.AjaxHTTP = null;
	}
}

//プロトタイプの作成
Log.prototype = {
//Log.fn = Log.prototype = {
	//フレームワークのコントローラの取得、なければ空文字
	controller : function(){
		var controller = this.pathAry[0];
		if(controller && controller.indexOf('.htm') < 0){
			return controller;
		}
		return '';
	},
	//フレームワークのアクションの取得、なければ空文字
	action : function(){
		var action = this.pathAry[1];
		if(action && action.indexOf('.htm') < 0){
			return action;
		}
		return '';
	},
	//静的コンテンツを指定した場合のファイル名を返す
	filename : function(){
		var last = (this.pathAry.length - 1);
		var elem = this.pathAry[last];
		if(elem.indexOf('.htm') >= 0){
			return elem;
		}
	return '';
	},
	//企業ページを閲覧している場合、企業IDを取得する
	company : function(){
		var count = 0;
		var flag = false;
		while(this.pathAry[count]){
			if(this.pathAry[count] == 'id'){
				flag = true;
			}else if(flag){
				//数値であれば
				if(!isNaN(this.pathAry[count])){
					return this.pathAry[count];
				}
			}
			count++;
		}
	return '';
	},
	//商品詳細ページを閲覧している場合、商品IDを返す
	product : function(){
		var count = 0;
		var flag = false;
		while(this.pathAry[count]){
			if(this.pathAry[count] == 'detail'){
				flag = true;
			}else if(flag){
				//数値であれば
				if(!isNaN(this.pathAry[count])){
					return this.pathAry[count];
				}
			}
			count++;
		}
	return '';
	},
	//カタログ詳細ページを閲覧している場合、カタログIDを返す
	catalog : function(){
		var count = 0;
		var flag = false;
		while(this.pathAry[count]){
			if(this.pathAry[count] == 'detail'){
				flag = true;
			}else if(flag){
				//数値であれば
				if(!isNaN(this.pathAry[count])){
					return this.pathAry[count];
				}
			}
			count++;
		}
	return '';
	},
	//CSR詳細ページを閲覧している場合、CSRIDを返す
	csr : function(){
		var count = 0;
		var flag = false;
		while(this.pathAry[count]){
			if(this.pathAry[count] == 'detail'){
				flag = true;
			}else if(flag){
				//数値であれば
				if(!isNaN(this.pathAry[count])){
					return this.pathAry[count];
				}
			}
			count++;
		}
	return '';
	},
	//エコブック閲覧中のエコブックIDを返す
	ecobookId : function(){
		var count = 0;
		var flag = false;
		while(this.pathAry[count]){
			if(this.pathAry[count] == 'id'){
				flag = true;
			}else if(flag){
				//数値であれば
				if(!isNaN(this.pathAry[count])){
					return this.pathAry[count];
				}
			}
			count++;
		}
	return '';
	},
	//エコブック閲覧中の指定企業IDを取得する
	companyId : function(){
		var count = 0;
		var flag = false;
		while(this.pathAry[count]){
			if(this.pathAry[count] == 'companyId'){
				flag = true;
			}else if(flag){
				//数値であれば
				if(!isNaN(this.pathAry[count])){
					return this.pathAry[count];
				}
			}
			count++;
		}
	return '';
	},
	
	//設定情報をすべて取得、オブジェクト(連想配列)で返す
	getParam : function(){
		var param = {};
		if(this.url){
			param.url = this.url;
		}
		if(this.controller()){
			param.controller = this.controller();
		}
		if(this.action()){
			param.action = this.action();
		}
		if(this.filename()){
			param.filename = this.filename();
		}
		switch(param.controller){
			case 'company':
				param.company_id = this.company();
				switch(param.action){
					case 'product':
						param.product_id = this.product();
						break;
					case 'catalog':
						param.catalog_id = this.catalog();
						break;
					case 'csr':
						param.csr_id = this.csr();
						break;
				}
				break;
			case 'ecobook':
				param.company_id = this.companyId();
				switch(param.action){
					case 'catalog':
						param.ecatalog_id = this.ecobookId();
						break;
					case 'csr':
						param.ereport_id = this.ecobookId();
						break;
				}			
				break;
		}
		return param;
	},
	//ログ情報をサーバへ送信
	send : function(param){
		if(!this.AjaxHTTP || !this.LogUrl){
			return;
		}
		var url = this.LogUrl + '?';
		var value = '';
		for(i in param){
			value = encodeURI(param[i]);
			url += i + '=' + value + '&';
		}
		var max = (url.length - 1);
		url = url.substring(0,max);
		var request = this.AjaxHTTP;
		request.open("GET",url,true);
		request.setRequestHeader("User-Agent","XMLHttpRequest");
		request.send(null);
	}
}

//プロトタイプが読み込まれている場合のみ
if(Event.hasOwnProperty('observe')){
	//企業ページのクリックログ取得
	Event.observe(window,'load',function(){
	var elems = $$('.officialLink');
	var count = elems.length;
	var i = 0;
	var domElem;
		while(i < count){
			domElem = elems[i];
			Event.observe(domElem, 'click',function(){
				var logClass = new Log();
				var companyId = logClass.company();
				var param = { 
					type:'hp',
					companyId:companyId
					};
				logClass.send(param);
			});
			i++;
		}
	});
}
