/*  
 * scrollAnalytics JavaScript, version 1.0.0
 *  
 * 対応ブラウザ: Internet Explorer 5以降、Firefox、Safari、Opera
 * 
 * @copyright toshiyuki.tanaka(tanaka@ritsmate.jp)
 */

/* 解析プログラムの絶対パス
 * ※変更不要
 */
var APP_PATH = 'http://keiba-manbaken.com/scrollAnalytics/logger/index.php';

/* 解析コード 
 * ※解析コードは解析単位となるページごとに一意の数字を振り分ててください。
 */
var SITECODE = '1';

/* アクセス日時 */
var nowDate         = new Date();
var myYear          = nowDate.getFullYear();
var myMonth         = nowDate.getMonth() + 1;
var myDate          = nowDate.getDate();
var myHours         = nowDate.getHours();
var myMinutes       = nowDate.getMinutes();
var mySeconds       = nowDate.getSeconds();
var ACCESS_DATETIME = myYear+'/'+myMonth+'/'+myDate+' '+myHours+':'+myMinutes+':'+mySeconds;

/* スクロール位置を格納する変数 */
var pos = '';

/* 測定点を格納する変数 */
var sap1 = '';
var sap2 = '';
var sap3 = '';
var sap4 = '';
var sap5 = '';

/* 二重送信防止用フラグ */
var sap0_flg = 0;
var sap1_flg = 0;
var sap2_flg = 0;
var sap3_flg = 0;
var sap4_flg = 0;
var sap5_flg = 0;

window.onscroll = function scrl() 
{	
	// 現在のスクロール位置を取得
	var pos = _getScrollPosition();
	
	// 測定点を格納
	var sap1 = document.getElementById('sap1');
	var sap2 = document.getElementById('sap2');
	var sap3 = document.getElementById('sap3');
	var sap4 = document.getElementById('sap4');
	var sap5 = document.getElementById('sap5');

	// 座標情報をPHPに送信
	if (sap5 !== null && sap5_flg == 0) {
		if (pos >= sap5.offsetTop) {
			_sendAnalytics('5');
			sap5_flg = 1;
		}
	}
	if (sap4 !== null && sap4_flg == 0) {
		if (pos >= sap4.offsetTop) {
			_sendAnalytics('4');
			sap4_flg = 1;
		}
	}
	if (sap3 !== null && sap3_flg == 0) {
		if (pos >= sap3.offsetTop) {
			_sendAnalytics('3');
			sap3_flg = 1;
		}
	}
	if (sap2 !== null && sap2_flg == 0) {
		if (pos >= sap2.offsetTop) {
			_sendAnalytics('2');
			sap2_flg = 1;
		}
	}
	if (sap1 !== null && sap1_flg == 0) {
		if (pos >= sap1.offsetTop) {
			_sendAnalytics('1');
			sap1_flg = 1;
		}
	}
	if (sap0_flg == 0) {
		_sendAnalytics('0');
		sap0_flg = 1;
	}
};

/* 現在のスクロール位置を取得するメソッド */
function _getScrollPosition() {
	return (document.documentElement.scrollTop || document.body.scrollTop);
}

/* 解析情報をPHPに送信するメソッド */
function _sendAnalytics(sap) {
	// リクエストオブジェクト
	var aj = new Ajax.Request(APP_PATH, {
		method : "post",
		parameters : {
			sitecode: SITECODE,
			access_datetime: ACCESS_DATETIME,
			sap: sap
		},
		onComplete : function(request) {
		}
	});
}
