/**
 * @copyright Keyade 2009
 */
var ___kCk = {
	set : function( n, v, d, dmn ) {
		var exp = '';
		if ( d != undefined && d != -1 ) {
			var dt = new Date();
			dt.setTime( dt.getTime() + ( 86400000 * parseFloat( d ) ) );
			exp = '; expires=' + dt.toGMTString();
		}
		if ( dmn == undefined ) {
			var loc = ( window.top != window ) ? window.parent.location : window.location;
			dmn = loc.hostname.replace( /^www\./i, '.' );
		}
		return ( document.cookie = escape( n ) + '=' + escape( v || '' ) + exp + '; domain=' + dmn + '; path=/' );
	},
	get : function( n ) {
		var c = document.cookie.match( new RegExp( '(^|;)\\s*' + escape( n ) + '=([^;\\s]*)' ) );
		return ( c ? unescape( c[2] ) : null );
	},
	erase : function( n ) {
		var c = ___kCk.get( n ) || true;
		___kCk.set( n, '', -1 );
		return c;
	},
	accept : function() {
		if ( typeof navigator.cookieEnabled == 'boolean' ) {
			return navigator.cookieEnabled;
		}
		___kCk.set( '_test', '1' );
		return ( ___kCk.erase( '_test' ) ===  '1' );
	}
};

var ___kPQs = function() {
	var a = this;
	a.params = {};
	a.qs = '';
		
	a.get = function( key, default_ ) {
		var value = a.params[key.toLowerCase()];
		return ( value != null ) ? value : default_ ;
	};
	
	a.contains = function( key ) {
		var value = a.params[key.toLowerCase()];
		return ( value != null );
	};
	
	a.loc = ( window.top != window ) ? window.parent.location : window.location;
	a.qs = a.loc.search.substring( 1, a.loc.search.length ).replace( /\?/g, '&' );
	
	if ( a.qs.length == 0 ) return;

	a.qs = a.qs.replace(/\+/g, ' ');
	
	var args = a.qs.split( '&' ); 
	
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split( '=' );
		var name = decodeURIComponent( pair[0] );
		
		var value = ( pair.length == 2 )
			? decodeURIComponent( pair[1] )
			: '0';
		
		a.params[name.toLowerCase()] = value;
	}
};

var ___kMtc = function( ckeDomain ) {
	var a = this;
	
	a.ptClickId = '';
	a.mtCmk = '';
	a.landingId = 0;
	a.linkPartnerSub = '';
	a.ref = '';
	
	a.tckDomain = 'mt.k.keyade.com';
	
	a.loc = ( window.top != window ) ? window.parent.location : window.location;
	a.host = a.loc.hostname.replace( /^www\./ig, '' );
	a.ckeDomain = ( ckeDomain !== undefined && ckeDomain != 'undefined'  ) ? ckeDomain : '.' + a.host;
	
	a.imgPath = '/mmt/1/';
	
	a.debug = false;
	
	a.foo = function () {}
	
	a.setPtClickId = function( i ) {
		a.ptClickId = i;
	};
	
	// old name, deprecated, use setMtCmk
	a.setMtcId = function( i ) {
		a.mtCmk = i;
	}
	
	a.setMtCmk = function( i ) {
		a.mtCmk = i;
	}
	
	a.setLandingId = function( i ) {
		a.landingId = i;
	}
	
	a.setLinkPartnerSub = function( str ) {
		a.linkPartnerSub = str;
	}
	
	a.setRef = function( ref ) {
		a.ref = ref;
	}
	
	a.track = function() {
		var qs = new ___kPQs();

		if ( a.ptClickId == '' || qs.contains( 'tckka' ) ) {
			return false;
		}

		var fbCmks = [
			'000001', '010001', '020001', '030001', '040001', '050001', '050002', '060001',
			'080001', '080002', '092001', '093001', '094001', '110001', '110010', '120001',
			'120002', '130001', '140001', '160001', '170001', '190001', '195001', '200001',
			'210001', '240001', '260001', '270001', '500001', '500201', '510001', '510201',
			'520001', '520201', '530001', '540001', '550001', '560001', '561001', '580001',
			'580301', '592001', '592201', '593000', '593001', '593002', '593301', '594000',
			'594001', '594002', '594003', '594201', '612000', '612001', '612201', '613000',
			'613201', '614001', '615101', '618001', '618002', '623001', '624001', '630001',
			'640001'
		];
		
		for( cmk in fbCmks ) {
			if ( a.mtCmk == fbCmks[cmk] ) {
				return false;
			}
		}
		
		var oldPtClickId = ___kCk.get( '__kaPtClickId' );
		if ( oldPtClickId !== null && oldPtClickId == a.ptClickId ) {
			return false;
		}
	
		var clientId = 68454;
		
		var domain = a.tckDomain;
		if ( a.debug ) {
			domain = "kslug.keyade.com";
		}
		
		var url = a.loc.protocol + '//' + domain + a.imgPath;
		
		url += '?kaClkPtId=' + encodeURIComponent( a.ptClickId );
		url += '&kaPcId=' + encodeURIComponent( clientId );
		url += '&kaTckMcId=' + encodeURIComponent( a.mtCmk );
		url += '&kaLnPtSb=' + encodeURIComponent( a.linkPartnerSub );
		url += '&kaLandinId=' + encodeURIComponent( a.landingId );
		url += '&kaLanding=' + encodeURIComponent( a.loc.href );
		
		var ref = '';
		if ( a.ref == '' ) {
			ref = encodeURIComponent( document.referrer );
		}
		else {
			ref = encodeURIComponent( a.ref );
		}
		
		if ( ref.length + url.length < 2000 ) {
			url += '&kaRef=' + ref;
		}
		else {
			url += '&kaRef=';
		}
		
		var img = new Image( 3, 2 );
		img.src = url;
		img.onload = function() { a.foo() };
		
		 ___kCk.set( '__kaPtClickId', a.ptClickId, 30, a.ckeDomain );
		 
		return true;
	}
}

var _kTck = {
	getTracker : function( ckeDomain ) {
		return new ___kMtc( ckeDomain  );
	}
};

