var adblock_enabled = true;

var wstats = {
  ws_version:1,
	ws_siteid:"",
	ws_unique:0,
	ws_session:0,
	ws_sequence:0,
	ws_info:{},
	
	_pagerefresh:0,

	_beholder:"http://test.indigo.it/wss/server/",
	
  _config:{
    scripturl:""
  },
  
	//http://www.quirksmode.org/js/cookies.html	
	setCookie : function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	getCookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	delCookie : function(name) {
		createCookie(name,"",-1);
	},
	
	
		//http://diveintohtml5.org/detect.html	
  _checkTagCanvas : function() {
		return !!document.createElement('canvas').getContext;
	},
	_checkTagVideo : function() {
		return !!document.createElement('video').canPlayType;
	},
	_detectVideoFormats : function() {
	  var lst = [];
		var v = document.createElement("video");
		if (v) {
			if  (v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"')) {
				lst.push('mp4');
			}
			if  (v.canPlayType('video/ogg; codecs="theora, vorbis"')) {
				lst.push('ogg');
			}
			if  (v.canPlayType('video/webm; codecs="vp8, vorbis"')) {
				lst.push('webm');
			}
		}
		return lst;
	},
	_checkGeolocation : function() {
    return !!navigator.geolocation;
  },

	_detectAdblock : function() {
	  var img = new Image();
		img.src = wstats._config['scripturl']+'js/adsense.js';
		img.onerror = function (e) { wstats.ws_info.adblock = "1" };
		img.onload = function (e) { wstats.ws_info.adblock = "0" };
	},
	
	_init : function() {

		this.ws_siteid = "";		
    this.ws_sequence = 0;
		
		this.ws_info = {};
		
		var lastpage = this.getCookie('____wsp');
		
		if (lastpage == location.href) {
		  this._pagerefresh = 1;
		}
		
		this.ws_unique = this.getCookie('____wsu');
    if ((this.ws_unique == null) || (isNaN(this.ws_unique))) {
			var dt = new Date();
			var rd = Math.floor(Math.random()*65535);
			this.ws_unique = String(dt.getTime() - 1276121812633) + Number(rd).toString(16);
		} else {
		  this.ws_info.ret = "1";
		}
		this.setCookie('____wsu',this.ws_unique,30);

		this.ws_session = this.getCookie('____wse');
		this.ws_sequence = this.getCookie('____wsq');
		if ((this.ws_sequence == null) || (isNaN(this.ws_sequence))) {
		  this.ws_sequence = 0;
		} else {		
			var ref = document.referrer;
			var domain = location.href;
			var pos = domain.lastIndexOf('/');
			if (pos) {
				domain = domain.substr(0,pos+1);
			}			
			if ((this._pagerefresh == 0) && (ref != domain)) {
			  this.ws_sequence = 0;
			}
		}		
		this.setCookie('____wsq',this.ws_sequence);
	
	  if ((this.ws_sequence == 0) || (this.ws_session == null)) {
			var dt = new Date();
			this.ws_session = String(dt.getTime() - 1276121812633);
		}
		this.setCookie('____wse',this.ws_session);
		
		this.setCookie('____wsp',location.href);
	
		if (FlashDetect) {
			if (FlashDetect.installed) {
        this.ws_info.flash = FlashDetect.raw;
				this.ws_info.flashver = FlashDetect.major;
			} else {
        this.ws_info.flash = "";
				this.ws_info.flashver = "0";
			}
		}
		
		this.ws_info.screen = [screen.width, screen.height];
		this.ws_info.window = [screen.availWidth,screen.availHeight];

		//iPad orientation
		if (window.orientation) {
		  this.ws_info.orientation = window.orientation;
		}
		
		this.ws_info.appcodename = navigator.appCodeName
		this.ws_info.appname = navigator.appName;
    this.ws_info.appversion = navigator.appVersion;
		this.ws_info.userlanguage = navigator.userLanguage;
		this.ws_info.platform = navigator.platform;
		
		this.ws_info.cookies = (navigator.cookieEnabled == true) ? "1" : "0";
		this.ws_info.java = (navigator.javaEnabled() == true) ? "1" : "0";
		
		this.ws_info.canvas = (this._checkTagCanvas() == true) ? "1" : "0";
		this.ws_info.video = (this._checkTagVideo() == true) ? "1" : "0";
		if (this.ws_info.video == "1") {
		  this.ws_info.videoformats = this._detectVideoFormats().toString();
		}
		this.ws_info.geo = (this._checkGeolocation() == true) ? "1" : "0";
		
		this._detectAdblock();
		
		this.ws_info.console = (typeof window.console != 'undefined');
    this.ws_info.firebug = this.ws_info.console && (typeof window.console.firebug != 'undefined');
		
	},

	
	_asyncTrack : function(disp,url) {
	  var track = new Image();
		
		var link = this._beholder+disp+"?"+
		  "v="+this.ws_version+
			"&s="+escape(this.ws_siteid)+
		  "&u="+this.ws_unique+
			"&e="+this.ws_session+
			"&q="+this.ws_sequence+
			"&w="+escape(url);
			
		if (document.referrer != '') {
		  link+="&r="+escape(document.referrer);
		}
			
		for(id in this.ws_info) {
		  if (this.ws_info[id]) {
		    link+="&"+id+"="+escape(this.ws_info[id].toString());
			}
		}
		
		track.src = link;
	},
	
	
	trackPage : function(url) {
		this.ws_sequence++;
		this.setCookie('____wsq',this.ws_sequence);
	
	  setTimeout("wstats._asyncTrack('p.php','"+url+"')",100);
	},
	
	enable : function(siteid,opts) {  
	  this.ws_siteid = siteid;
    if (opts != 'undefined') {
      for(var a in opts) this._config[a] = opts[a];
    }
		if (this._pagerefresh == 0) {      
		  this.trackPage(location.href);
		}
	}
		
};

wstats._init();
