var IdUserLog //   remember the lst id  inserted
var sitepageTitle='home';
var curMediaFile=''// keep track  if  a   media  file  is  clicked
var sess=''
var curMediaTitle=''
var curMediaFullUrl='';
var curMediaType='';
var curUrl;

function setLog() {

  
	var url = '/extranet/inc/intra_tracker.php';
    pageTitle=document.title
	fullUrl="&fullUrl="+curUrl;
	var pars = 'sess='+sess+fullUrl+'&MediaFile='+curMediaFile;
    pars+='&MediaType='+curMediaType;
    pars+='&MediaTitle='+curMediaTitle;
    //alert(url+'?'+pars)
    //$('debug').update(url+'?'+pars);

 
    //pars=URLencode(pars);
	//window.status=pageTitle+" -  In " + url
	
	var myAjax = new Ajax.Request( 
	 url, { 
		    method: 'get', 
			parameters: pars,
			onComplete: showResponse,
			onError:reportError
		   }
	);
}

function showResponse(originalRequest) 	{
	//put returned XML in the textarea
	IdUserLog = originalRequest.responseText;
}
    
function reportError(request) {
	alert('Sorry. There was an error.');
}
	
	

function updateLog() {
  var url = '/logTest.php';
  var pars = 'IdUserLog='+IdUserLog ;
  var myAjax = new Ajax.Request( 
   url, { 
	     method: 'get', 
		 parameters: pars,
		 //onComplete: showResponse,
		 onError:reportError
     	}
	);
}
	

// store the media file ext 
extArray= new Array(
    'pdf',
    'mp3',
    'flv',
    'swf',
    'doc',
    'zip'
);
 
// class  store the click on media doc
var storeMedia = Class.create();
storeMedia.prototype = {
   initialize: function(ctrl) {
        this.ctrl = ctrl;
        // assegno l'evento  al  link
	    Event.observe(this.ctrl, 'click', this.setMedia.bindAsEventListener(this), false);
    },

    //  check if is a link to a media file
    setMedia: function() {
        isMedia=0; //  flaf  to  check  if  the  media  ext  has been  found
		curUrl=this.ctrl.href
        
        curMediaTitle=this.ctrl.getAttribute('title');
        curMediaFile=get_file_name(curUrl)
		curExt=get_file_ext(curMediaFile)
		//curLang=get_file_lang(curUrl)
		for(a=0;a<extArray.length;a++){
           if(extArray.indexOf(curExt)!=-1 && isMedia==0){
		     extArray.indexOf(curExt)
			 setLog()
			 isMedia=1
		   }   
        }
    }
}


// some  function to  get  the page  data
function get_file_name(str) { 
  var slash = '/' 
  if (str.match(/\\/)) { slash = '\\'} 
  sURL = str.substring(str.lastIndexOf(slash) + 1, str.lastIndexOf('.')+5) 
  return sURL
}

function get_file_ext(str){
  sExt = str.substring(str.lastIndexOf('.') + 1, str.lastIndexOf('.')+5) 
  return sExt
}
	
// this   function init  the logTracker
function  initLog(){
     //setLog()
    // assegno la  classe a  tutti i link della  pagina 
   for(a=0;a<document.links.length;a++){
       new storeMedia($(document.links[a]));
   }
}

function URLencode(sStr) {
    return escape(sStr).
             replace(/\+/g, '%2B').
                replace(/\"/g,'%22').
                   replace(/\'/g, '%27').
                     replace(/\//g,'%2F');
  }
Event.observe(window, 'load',initLog, false)
//Event.observe(window, 'unload',updateLog, false);
 
