/**
 * 操作の記録モジュール
 */
var Logging = {
	baseUrl : "./log.php",
	name : "log",

	initialize : function(){
		
	},
	
	sendLog : function(params){
		$.ajax({
			type: "GET",
			url: this.baseUrl,
			data : params,
			success: function(){}	//js側では何もしない
		});
	},
	
	getBaseParams: function(){
		return {
			"user_id" : this.user_id,
			"query" : this.query,
			"num" : this.num,
			"time" : this.getDateTime()				
		};
	},
	
	getDateTime : function(){
		var now = new Date();
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var day = now.getDate();
		var hour = now.getHours();
		var minute = now.getMinutes();
		var sec = now.getSeconds();
		var format = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + sec;
		return format;
	},
	
	//検索が実行される度に記録
	search : function(query,num){
		this.user_id = user_id;
		this.query = query;
		this.num = num;
		var params = this.getBaseParams();
		params.type = "search";
		this.sendLog(params);
	},
	
	//再ランキングの度に記録
	rerank : function(operation,attribute,term,base){
		var params = this.getBaseParams();
		params.type = operation;
		params.attribute = attribute;
		params.term = encodeURIComponent(term);
		switch (attribute) {
			case "title":
			case "summary":
			case "smallurl":
				params.orank = base.id.replace("item","");
				var current_rank = $E.getCurrentRank(base.id);
				params.crank = $E.getCurrentRank(base.id);
				var item = $R.items[current_rank - 1];
				//params['title'] = encodeURIComponent(item.title);
				//params['summary'] = encodeURIComponent(item.summary);
				params.url = encodeURIComponent(item.url);
				break;
			case "tag":
				params.tagsize = base.getAttribute('size').replace("%","");
				params.tagrank = base.getAttribute('rank');
				break;
		}
		this.sendLog(params);
	},
    
    //ページにアクセスした時に記録
    access : function(){
        var params = {
            "user_id" : user_id,
            "time" : this.getDateTime()
        };
        if(typeof referer != "undefined" && referer){
            params.referer =  encodeURIComponent(referer);
        }
        params.type = "access";
        this.sendLog(params);
    },
	
	//URLを開く
	click : function(base){
		var params = this.getBaseParams();
		params.type = "click";
		params.orank = base.id.replace("item","");
		var current_rank = $E.getCurrentRank(base.id);
		params.crank = $E.getCurrentRank(base.id);
		var item = $R.items[current_rank - 1];
		//params['title'] = encodeURIComponent(item.title);
		//params['summary'] = encodeURIComponent(item.summary);
		params.url = encodeURIComponent(item.url);		
		this.sendLog(params);		
	}

};

var $L = Logging;
