var Compass = function() {
	this.h = 'http://ark-s.jp/compass/';
	this.reg = /(www\.){0,1}ark\-s\.jp\/compass\//;
	this.nh = this.h + 'luna/';
	this.pfx = 'link';
	this.nq = this.pfx + 'ref';
	this.np = this.pfx + 'pide';
	this.npd = this.pfx + 'pd';
	this.tg = 'taggid';
	this.ti = 'i';
	this.ns = this.nh + 'cores.php';
	this.nl = this.nh + 'corel.php';
	this.nt = this.nh + 'coret.php';
	this.tcl = this.nh + 'corec.php';
	this.tl = this.nh + 'coretl.php';
	this.key = 'cpskey';
	this.event_id = 0;

	this.f = false;
	if (document.referrer.search(this.reg) == -1) {
		this.f = true;
	}

	this.CookieWrite = function(kword, kdata, kday) {
		if(!navigator.cookieEnabled){
			return;
		}
		sday = new Date();
		sday.setTime(sday.getTime() + (kday * 1000 * 60 * 60 * 24));
		s2day = sday.toGMTString();
		document.cookie = kword + "=" + escape(kdata) + ";expires=" + s2day;
	}

	this.CookieRead = function(kword){
		if(typeof(kword) == "undefined") {
			return "";
		}
		kword = kword + "=";
		kdata = "";
		scookie = document.cookie + ";";
		start = scookie.indexOf(kword); 
		if (start != -1){
			end = scookie.indexOf(";", start);
			kdata = unescape(scookie.substring(start + kword.length, end))
		}
		return kdata;
	}
	
	this.branch = function (id) {
		if (!this.f) return;
		var cid = this.CookieRead(this.key + this.event_id);
		if (!cid) {
			cid = 0;
		}
		var u = this.ns + '?cid=' + cid + '&pid=' + id + '&' + this.nq + '=';
		var body = "<script type='text/javascript' src='" + u + encodeURIComponent(document.referrer) + "'></script>";
		document.write(body);
	}
	
	this.land = function(id) {
		if (!this.f) return;
		var cid = this.CookieRead(this.key + this.event_id);
		if (!cid) {
			cid = 0;
		}
		var to = this.nl + '?cid=' + cid + '&' + this.nq + '=' + encodeURIComponent(document.referrer) + '&' + this.npd + '=0&' + this.np + '=' + id;
		var body = '<script type="text/javascript" src="' + to + '"></script>';
		document.write(body);
		
		body = "<script type='text/javascript' src='";
		body += this.nh + 'scook.php?pid=' + id + "&eid=" + this.event_id;
		body += "'></script>";
		document.write(body);
	}
	
	this.conv = function(id) {
		if (!this.f) return;
		var cid = this.CookieRead(this.key + this.event_id);
		if (!cid) {
			cid = 0;
		}
		var to = this.nl + '?cid=' + cid + '&' + this.nq + '=' + encodeURIComponent(document.referrer) + '&' + this.npd + '=1&' + this.np + '=' + id;
		var body = '<script type="text/javascript" src="' + to + '"></script>';
		document.write(body);
	}
	
	this.cpstag = function(id) {
		var to = this.nt + '?' + this.tg + '=' + id + '&' + this.nq + '=' + encodeURIComponent(document.referrer);
		var body = '<script type="text/javascript" src="' + to + '"></script>';
		document.write(body);
	}
	
	this.tconv = function(id) {
		var to = this.tl + '?' + this.ti + '=' + id + '&' + this.nq + '=' + encodeURIComponent(document.referrer);
		var body = '<script type="text/javascript" src="' + to + '"></script>';
		document.write(body);
	}
	
	this.clt = function(id, flg) {
		var to = this.tcl + '?' + this.ti +'=' + id + '&' + this.nq + '=' + encodeURIComponent(document.referrer);
		if (flg) {
			window.open(to);
		} else {
			document.location = to;
		}
	}
}

var cps = new Compass();
function branch(pid, eid) {
	cps.event_id = eid;
	cps.branch(pid);
}
function land(pid, eid) {
	if (eid && !cps.event_id) {
		cps.event_id = eid;
	}
	cps.land(pid);
}
function conv(pid, eid) {
	if (eid && !cps.event_id) {
		cps.event_id = eid;
	}
	cps.conv(pid);
}
function cpstag(id) {
	cps.cpstag(id);
}
function tconv(id) {
	cps.tconv(id);
}
function clt(id) {
	cps.clt(id);
}
