({
	name:"Cookie",
	set:function(n,v,c){var d,e="";if(c){d=new Date();d.setTime(d.getTime()+c*24*60*60*1000);e="; expires="+d.toGMTString()}document.cookie=escape(n)+"="+v+e+"; path=/"},
	get:function(n){var e,b,p,c=document.cookie;p=n+"=";b=c.indexOf(";"+" "+p);if(b==-1){b=c.indexOf(p);if(b!=0)return ""}else b+=2;e=c.indexOf(";",b);if(e==-1)e=c.length;return unescape(c.substring(b+p.length,e))},
	unset:function(n){return this.set(n,"")},
	init:function(){window[this.name]=this}
}).init();

function _onunload() {
	Cookie.unset("svd_cat_sent");
};
window.attachEvent ? window.attachEvent("onbeforeunload", _onunload) : window.addEventListener("unload", _onunload, false);

//	wrapping object
var adaptMA = {};

//	Members
adaptMA.__getCookie = function (_cid)
{ 
	if (document.cookie.length > 0)
	{ 
		var begin = document.cookie.indexOf(_cid+"=");
		if (begin != -1)
		{ 
			begin += _cid.length+1;
			var end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end));
		}
	}
	return "no_cookie";
};

adaptMA.__extractCategories = function ()
{
	var result = "";
	var url = document.location.href;
	var questionMarkIndex = url.indexOf("?");
 	if(questionMarkIndex > 0){
 		url = url.substring(0, questionMarkIndex);
 	}
   	var urlTokens = (url.replace(/\\/g,"/").replace(/((f|ht)tp(s?):\/\/)/i,"")).split("/");
  	if(urlTokens.length>1 && urlTokens[1] && urlTokens[1].indexOf(".")==-1)
		result+="svd_"+urlTokens[1];
	if(urlTokens.length>2 && urlTokens[2] && urlTokens[2].indexOf(".")==-1)
		result+=",svd_"+urlTokens[2];
	return result;
};

// 	Generates Guid - Global Unique Identifier
adaptMA.__newGuid = function ()
{
    var g = "";
    for(var i = 0; i < 32; i++)
    	g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "");
    return g;
};

adaptMA.__sendRequest = function (url)
{
	var bearer = new Image();
	bearer.src = url;
	setTimeout( function() {
			if (!bearer.complete) {
				bearer = null;
				return;
			}
		}
		, 5000);
};

adaptMA.start = function ()
{
	var cookieEnabled = (navigator.cookieEnabled) ? true : false;
	if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
		document.cookie = "testcookie";
		cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
	}
	if (!cookieEnabled) return;
	
	//	config values
    var _aid = 'svd';

	//	server-load control
	var _peload = 100;
	var _maloadprob=Math.floor(Math.random()*101);
	if (_maloadprob > _peload) return;

	if (Cookie.get("svd_cat_sent")) {		
		return;
	}else{
		Cookie.set("svd_cat_sent", "is_set", 1);
	}
	//	metadata retrieval
	var meta = adaptMA.__extractCategories();
	if (!meta) return;
	
	var url  = 'http://svd.adtlgc.com/markAction?category=' + meta;
	adaptMA.__sendRequest(url);
	
};

//	This is entry-point
adaptMA.start();
