//***********************************************************************************************************
//***********************************************************************************************************
// (c) 2010 M-Soft.War. Internet Developpement & GAFMEDIASTUDIO
//  Programer : Laurent Moulinier
//  Designer  : Gabriel Faucon
//***********************************************************************************************************
//  Name         : call_recorder.js 
//  Version      : V 1.0
//  Release      : 04-06-2010
//  Modification : 31-08-2010
//***********************************************************************************************************
//
//***********************************************************************************************************
var gv_Rec_Filename = "recorder.php";
var gv_Rec_Path     = "/visits_tracker/";
//var gv_Rec_Path     = "";
var gv_IsIe         = ( navigator.appName == 'Microsoft Internet Explorer' ) ? true : false;
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_GetInfoData(){
	var lv_infoData      = "";
	var lv_url           = "";
	var lv_pathArray     = window.location.href.split( '//' );
	var lv_pathRootArray = lv_pathArray[1].split( '/' );
	for(var i = 1; i < lv_pathRootArray.length; i++) if (lv_pathRootArray[i] != "" ) lv_url = lv_url + lv_pathRootArray[i] + "/";
	if (lv_url == "" ) lv_url = "/";
	lv_infoData += "rs="  + screen.width + "x" + screen.height;
	lv_infoData += "&rf=" + escape( document.referrer );
	lv_infoData += "&ur=" + lv_url;
	lv_infoData += "&tl=" + document.title;
	return(lv_infoData);
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_None(){
	return;
}
//------------------------------------------------------------------------
// Call recorder.php
//------------------------------------------------------------------------
function f_DoRecord( _infoData ){
	var lv_request_url = gv_Rec_Path + gv_Rec_Filename + "?rd=" + Math.random() + "&" + _infoData;
	var lv_request_image = new Image(1,1);
	lv_request_image.onload = f_None;
	lv_request_image.src = lv_request_url;
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_GetEventElement( _Event ){
	var lv_event = _Event ? _Event : (window.event ? window.event : "");
	var lv_href_element = gv_IsIe  ? window.event.srcElement : _Event.currentTarget;
	return (lv_href_element);
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_EventCallback( _Event ){
	var lv_href_element = f_GetEventElement( _Event );
	while( lv_href_element.nodeName != "A" ){
		if( typeof lv_href_element.parentNode == 'undefined' ) return;
		lv_href_element = lv_href_element.parentNode;
	}
	var lv_infoData = "ln=" + lv_href_element.href;
	f_DoRecord( lv_infoData );
}
//------------------------------------------------------------------------
function f_LinkMouseDown( _Event ){
	var lv_href_element = f_GetEventElement( _Event );
	lv_href_element.down = true;
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_LinkMouseOut( _Event ){
	var lv_href_element = f_GetEventElement( _Event );
	lv_href_element.down = false;
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_LinkMouseUp( _Event ){
	var lv_href_element = f_GetEventElement( _Event );
	if ( lv_href_element.down == true ){
		f_EventCallback(_Event);
	}
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_AddEvent(_Element, _Event, _Function){
	if ( gv_IsIe == true ){
		_Element.attachEvent("on" + _Event, _Function);
	} else if ( _Element.addEventListener ){
		_Element.addEventListener(_Event, _Function, false);
	}
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
function f_Init(){
	if ( typeof document.location.host != 'undefined' ){
		var lv_HostName = document.location.host;
	} else {
		var lv_HostName = document.location.toString().replace(/^[^\/]*\/+([^\/]*)(\/.*)?/,'$1');
	}
	//--------------------------------------------------------------------------
	// External Link Hook
	//--------------------------------------------------------------------------
	var lv_Links = document.getElementsByTagName("a");
	for (var i = 0; i < lv_Links.length; i++){
		var lv_href_value = lv_Links[i].href;
		if( lv_href_value.match( eval('/^(http(s)?:\\/\\/)?' + lv_HostName + '/')) ) continue;
		f_AddEvent( lv_Links[i], 'mousedown', f_LinkMouseDown);
		f_AddEvent( lv_Links[i], 'mouseout',  f_LinkMouseOut);
		f_AddEvent( lv_Links[i], 'mouseup',   f_LinkMouseUp);
	}
	//--------------------------------------------------------------------------
	// Record Entry
	//--------------------------------------------------------------------------
	var lv_InfoData = f_GetInfoData();
	f_DoRecord( lv_InfoData );
}
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
var f_OldOnLoad = window.onload;
if (typeof f_OldOnLoad != 'function') {
	window.onload = f_Init;
} else {
	window.onload = function() {
		f_Init();
		f_OldOnLoad();
	}
}
