// ******************************************************** //
// wlf - 3x1010@gmail.com
// ******************************************************** //

var WlfTrackingObject = {
	// the tracking url
	trackingServletUrl : "http://lab.bitmama.it/tracking",
	// the tracking swf url
	trackingSwfUrl : "http://lab.bitmama.it/static/tracking/WlfTracking.swf",
	// the cookies Name
	trackingCookieName : "WlfTracking",
	// the cookies Duration
	trackingCookieDuration : 20,
	
	// get the tracking local cookie
	getLocalTrackingCookie : function () {
			 var sNome = this.trackingCookieName;
			 var asCookies = document.cookie.split("; ");
			 for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
			 {
			   var asCookie = asCookies[iCnt].split("=");
			   if (sNome == asCookie[0]) 
			     return (unescape(asCookie[1]));
			 }
  			 return null;
		},
	// set the tracking local cookie
	setLocalTrackingCookie : function ( sValore ) {
			 var sNome   = this.trackingCookieName;
			 var iGiorni = this.trackingCookieDuration;
			 //
  			 var dtOggi = new Date();
  		     var dtExpires = new Date();
  			 dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000);
  			 document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
		},
	// put permanent store swf in DOM
	putSwf : function ()  {
				var obj = "";
				obj += "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='1' height='1' id='WlfTrackingSwf' align='middle'>";	
				obj += "<param name='movie' value='"+this.trackingSwfUrl+"' />";
				obj += "<param name='allowScriptAccess' value='always' />";
    			obj += "<param name='wmode' value='transparent' />";	
   	 			obj += "<embed src='"+this.trackingSwfUrl+"' type='application/x-shockwave-flash' allowScriptAccess='always' width='1' height='1' wmode='transparent'></embed>";
				obj += "</object>";
 				//
 				document.write( obj );	 
		},
	// Ajax Request
	createXMLHttpRequest : function () {
			   try { return new XMLHttpRequest(); } catch(e) {}
			   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
			   return null;
		},
	// Swf CallBack
	swfPingCallBack : function ( id ) {
			this.setLocalTrackingCookie( id );
		},
	// ping
	ping : function ( bReset ) {
				if (bReset)
					this.setLocalTrackingCookie("");
				var localValue = this.getLocalTrackingCookie();
				if (localValue == null || localValue == "")
				{
					// flash ping --> callback
					this.putSwf();
				}
				else
				{
					// Ajax Ping
					var xhReq = this.createXMLHttpRequest();
  					xhReq.open("GET", this.trackingServletUrl+"?ID="+localValue+"&URL="+escape(window.location.href.toString())+"&RND="+(new Date()).getTime(), true);
  					// xhReq.onreadystatechange = function () {alert("OK");};
 					xhReq.send(null);
  				}
 		}
}

// OK
// alert(WlfTrackingObject.getLocalTrackingCookie());
WlfTrackingObject.ping(false);

