
/*
 * Copyright 2005-2008 IDF Technologies, LLC
 * by Konstantin Goudkov
 */

/*
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
* Change the opening body tag to:
* <body onLoad="startTk()">
* To display the current position on the page, type this in the address bar:
* old: javascript:alert(Math.floor((document.body.clientHeight+document.body.scrollTop) / document.body.scrollHeight*100));
* new: javascript:alert(Math.floor((document.documentElement.clientHeight+document.documentElement.scrollTop)/document.documentElement.scrollHeight*100));
*/

/*
 * example:
<html>
<head>
<script language="JavaScript" src="prototype-1.6.0.2.js"></script>
<script language="JavaScript" src="st4.js"></script>
</head>
<body onLoad="startTk()">
*/

// Either uncomment it here, or bring outside into the html page before sourcing this file.
//var ST_FILE_NAME="/st4goudkov.txt";
var ST_EXTERNAL_VISITOR_ID=6227649;

// Set to true if you want hits every second regardless of scroll changes. With false, only hits on scroll change.
var STGOUDKOV_OPT_HIT_SAME_S=false;

var STGOUDKOV_HIT_COUNTER=0;
var STGOUDKOV_VISITOR_ID=-1;
var STGOUDKOV_OLD_S=-1;

function startTk(){
	STGOUDKOV_VISITOR_ID=establishVisitorID();
	tkCycle();
	setInterval('tkCycle()',1000);
}

function establishVisitorID(){
	var v=-1;
	if(ST_EXTERNAL_VISITOR_ID==-1){
		var sIndex=document.cookie.indexOf("stgoudkov=");
		if(sIndex!=-1){
			var eIndex=document.cookie.indexOf(";",sIndex);
			if(eIndex==-1){
				eIndex=document.cookie.length;
			}
			v=document.cookie.substring(sIndex+10,eIndex);
		}else{
			v=""+Math.random()+"-"+new Date().getTime();
			document.cookie="stgoudkov="+v+"; expires="+new Date(new Date().getTime()+31536000000).toGMTString()+"; path=/";
		}
	}else{
		v=ST_EXTERNAL_VISITOR_ID;
	}
	return v;
}

function getScreenSize(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}

function getScrollY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfY;
}

function getDocumentHeight(){
	var h=0;
	if(document.documentElement&&document.documentElement.scrollHeight){
		h=document.documentElement.scrollHeight;
	}else if(document.body&&document.body.offetHeight){
		h=document.body.offsetHeight;
	}
	return h;
}


function tkCycle(){
	var screenSize=getScreenSize();
	var scrollY=getScrollY();
	var docHeight=getDocumentHeight();
	var s=Math.floor(((screenSize+scrollY)/docHeight)*100);
	if(STGOUDKOV_OPT_HIT_SAME_S || STGOUDKOV_OLD_S!=s){
		callTracker(STGOUDKOV_VISITOR_ID,s,STGOUDKOV_HIT_COUNTER);
	}
	STGOUDKOV_OLD_S=s;
	STGOUDKOV_HIT_COUNTER++;
}

function callTracker(v,s,t){
	//var hitImg=new Image();
	//hitImg.src=ST_FILE_NAME+"?"+"v="+v+"&s="+s+"&t="+t;
	new Ajax.Request(ST_FILE_NAME, {
		method:'get',
		parameters: {v: v, s: s, t:t}
	});
}
