// --  Plus Quellen Tracker v2.1
// ## Controller ## ------------------------------------------
function PQT(){
	this.version	= "v2.1 (2010-07-29)";

	// Ausgangsvariablen
	this.keks		= new Keks();
	this.session	= new Session(this.keks.get("srefid"));
	this.counter	= new Counter(this.keks);
	
	// Session-Typ: session, postclick, postview
	this.stype		= "s";	
	
	// Wievielte PI in der Session
	this.picount	= 0;
	
	// Nedstat-Variablen
	this.nedstat	= "&amp;counter="+this.counter.pi;
	this.kanal		= "notset";
	this.partner	= "notset";
	this.kampagne	= "plus";
	this.link		= "notset";	// ID des Partners
	this.fee		= 0;
	
	/* Funktion: QuellenTyp ermitteln */
	this.chkSrcType	= function(){
		if(this.refid.isset){
			return "refid";
		}else{
			if(this.referrer.isset){
				if(this.suma.isset){
					return "suma";
				}else{
					return "inbound";
				}
			}else{
				if(this.postclick.isset){
					this.stype	= "pc";
					return "postclick";
				}else{
					// Postview ggfs. hier implementieren
					return "direkt";
				}
			}
		}
	}
	
	// -- LOGIK --
	/* Funktion: Kanal-Logik */
	this.setRefID	= function(refid){
		this.session.setRefid(refid);
		
		// Kanal-Splittung
		rp	= refid.split("_");
		
		switch(rp[0]){
			case "NL":
				this.kanal		= "Newsletter";
				this.partner	= rp[1];
				break;
			case "PSM":
				this.kanal		= "Preisportale";
				this.partner	= rp[1];
				break;
			case "A":
				this.kanal		= "Affiliate";
				switch(rp[1]){
					case "z":
						this.partner	= "Zanox";
						break;
					case "a":
						this.partner	= "Affilinet";
						break;
					case "td":
						this.partner	= "Tradedoubler";
						break;
					default:
						this.partner	= rp[1];
				}
				
				// NonStopConsulting-Tracking
				nsctrid	= getParam("nsctrid",document.URL,"&");
				if(nsctrid!=false){
					this.keks.set("nsctrid",nsctrid,60*24*30);
				}
				break;
			case "SEM":
				this.kanal		= "SEM";
				this.partner	= rp[1];
				break;
			case "K":
				this.kanal		= "Bannerkampagnen";
				this.partner	= rp[1];
				break;
			case "PN":
				this.kanal		= "PlusNetzwerk";
				this.partner	= rp[1];
				break;
			case "SOC":
				this.kanal		= "Social";
				this.partner	= rp[1];
				break;
			case "R":
				this.kanal		= "Rest";
				this.partner	= rp[1];
				break;
			case "SEO":
				this.kanal		= "SEO";
				this.partner	= rp[1];
				break;
			case "I":	// Postclick-Inbound
				this.kanal		= "Inbound";
				this.partner	= rp[1];				
				break;
			default:
				this.kanal		= "Unbekannt";
				this.partner	= refid;
		}
		
	}
	
	this.setInbound	= function(){
		if(this.referrer.domain=="plus"){
			// Resumer
			this.kanal		= "Inbound";
			this.partner	= this.referrer.domain;
		}else{
			// Inbound
			this.kanal		= "Inbound";
			this.partner	= this.referrer.domain;		
		}
		this.session.setRefid("I_"+this.partner);
	}
	
	this.setSuma	= function(){
		this.kanal		= "SEO";
		this.partner	= this.referrer.domain;
		this.session.setRefid("SEO_"+this.partner);
	}
	
	this.setDirekt	= function(){
		this.kanal		= "Direct";
		this.partner	= "";
		this.keks.set("srefid","Direct",0);
	}
	
		
	// Nedstat-Parameter setzen
	this.setKanal	= function(v){
		this.nedstat	= this.nedstat+"&amp;ns_mchannel="+v;
		this.keks.set("kanal",v,0);
	}
	
	this.setPartner	= function(v){
		this.nedstat	= this.nedstat+"&amp;ns_source="+v;
		this.keks.set("partner",v,0);
	}
	
	this.setLink	= function(v){
		this.nedstat	= this.nedstat+"&amp;ns_linkname="+v;
		this.keks.set("link",v,0);
	}
	
	this.setKampagne	= function(v){
		this.nedstat	= this.nedstat+"&amp;ns_campaign="+v;	
		this.keks.set("kamp",v,0);
	}
	
	this.setFee	= function(v){
		this.nedstat	= this.nedstat+"&amp;ns_fee="+v;
	}
	
	this.setDebug	= function(){
		this.nedstat	= this.nedstat+"&amp;db=2_05";	
	}
	
	this.setSType	= function(){
		this.nedstat	= this.nedstat+"&amp;stype="+this.stype;	
	}
	
	// ## KONTROLL-LOGIK
	if(this.session.isset){
	// Aktive Session vorhanden		
		this.setKanal(this.keks.get("kanal"));		
		this.setPartner(this.keks.get("partner"));
		this.setLink(this.keks.get("link"));
		this.setKampagne(this.keks.get("kamp"));
		this.setFee("0");
	}else{
	// Keine Aktive Session
	
		this.refid		= new RefID();
		this.referrer	= new Referer();
		this.suma		= new Suma(this.referrer.domain);
		this.postclick	= new Postclick(this.keks.get("pcrefid"));
		// this.postview	= new Postview(this.keks.chk("pvrefid"));
		
		// -------------------------	
		// -- Analyse: Quellentyp --	
		switch(this.chkSrcType()){
			case "refid":
				this.setRefID(this.refid.wert);
				break;
			case "suma":
				this.setSuma();
				break;
			case "inbound":
				this.setInbound();
				break;
			case "postclick":
				this.setRefID(this.postclick.wert);
				break;
			case "postview":
				break;
			case "direkt":
				this.setDirekt();
				break;
			default:				
				out(this.chkSrcType());
		}
		
		// ## -- Nedstat ------------------------		
		this.setKanal(this.kanal);		
		this.setPartner(this.partner);
		this.setLink(this.link);
		this.setKampagne(this.kampagne);
		this.setFee(this.fee);
		this.setDebug();
		this.setSType();
	} 
	// -- ENDE: KONTROLL-LOGIK -----------------------------
}
// -----------------------------------------------------------------------------
/* KLASSE :: SESSION */
function Session(wert){

	this.isset	= false;
	this.wert	= wert;
	
	// ?? Session vorhanden ??
	if(this.wert!=false){
		this.isset	= true;
	}
	
	/* Funktion: setzt Session- und Postview-RefID */
	this.setRefid	= function(refid){
		keks	= new Keks();
		// Session-RefID
		keks.set("srefid",refid,0);	
		if(refid!="Direkt"){
			// Postview-RefID
			keks.set("pcrefid",refid,60*24*30);
		}
	}
}

function RefID(){
	this.isset	= false;
	
	// URL-Parameter "RefID" auslesen
	this.wert	= getParam("RefID",document.URL,"&");
	
	
	// ?? RefID vorhanden ??
	if(this.wert!=false){
		this.isset	= true;
	}
}

// KLASSE :: REFERRER
function Referer(){
	this.isset	= false;
	this.host;	// Host ohne Pfad (www.google.de)
	this.pfad;	// Pfad ohne Host & Parameter (/search)
	this.param	// URL-Parameter des Referrer (q=plus&num=50)
	
	this.top;	// Top-Level de
	this.domain;	// Domain	 google
	this.sub;	// SubDomain www
	
	// Referrer vorhanden
	if(document.referrer!=""){
		this.isset	= true;
		// Host extrahieren
		tmp	= document.referrer.split("/");
		this.host	= tmp[2];		
		
		tmp	= this.host.split(".");
		switch(tmp.length){
			case 1:
				this.domain	= tmp[0];
				break;
			case 2:
				this.domain	= tmp[0];
				this.top	= tmp[1];
				break;
			case 3:
				this.sub	= tmp[0];
				this.domain	= tmp[1];
				this.top	= tmp[2];
				break;
			default:
		}
		
		// Pfad & Parameter extrahieren
		tmp	= document.referrer.split(tmp[tmp.length-1]+"/");
		
		tmp	= tmp[1].split("\?");	
		this.pfad	= tmp[0];
		if(tmp.length>1){
			this.param	= tmp[1];
		}
	}
}

// Google Suche / Google Produktsuche / Yahoo / Bing / MSN / ASK
function Suma(domain){
	this.isset	= false;
	this.name;
	
	switch(domain){
		case "google":
		case "yahoo":
		case "ask":
		case "bing":
			this.isset	= true;
			this.name	= domain;
			break;
		default:
	}
}

/* Prüft auf Postclick */
function Postclick(wert){
	this.isset	= false;
	this.wert	= wert;
	
	// ?? Session vorhanden ??
	if(this.wert!=false){
		this.isset	= true;
	}
}

/* Prüft auf Postview */
function Postview(){
	this.isset	= false;
	this.wert	= wert;
	
	// ?? Session vorhanden ??
	if(this.wert!=false){
		this.isset	= true;
	}
}


// ## HELPER ## ------------------------------------------------
/* Keks */
function Keks(){
	// ?? Cookie mit Name "name" vorhanden ??	
	this.chk	= function(name){
		if(this.get(name)){
			return true;
		}else{
			return false;
		}
	}

	// Extrahiert Cookie-Variable >> false wenn nicht vorhanden
	this.get		= function(name){
		return getParam(name,document.cookie,";");
	}
	
	/* Beliebigen Cookie setzen */
	this.set	= function(name,wert,min){
		var d		= new Domain(document.URL);
		
		var basedomain	= d.domain+"."+d.top;
	
		var date	= new Date();
		var valid	= date.getTime()+1000*60*min;
		date.setTime(valid)
		if(min==0){
			document.cookie	= name+"="+wert+";path=/;domain=."+basedomain;
		}else{
			document.cookie	= name+"="+wert+";path=/;domain=."+basedomain+";expires="+date.toGMTString();
		}
	}
}

/* PI-Counter: n-te PI in Visit (Keks-basierend)  */
function Counter(keks){
	this.pi	= 0;
	
	if(keks.get("pqt_counter")!=false){
		this.pi	= Number(keks.get("pqt_counter"))+1;
	}else{
		this.pi	= 1;
	}
	
	keks.set("pqt_counter",this.pi,0);
}

/* URLparam */
function getParam(name,base,trenner){
	tmp	= base.split(name+"=");	
	if(tmp.length>1){
		param	= tmp[1].split(trenner);	
		return param[0];
	}else{
		return false;
	}
}

/* Zerlegt und analysiert beliebige Domain */
function Domain(url){
	this.host;	// Host ohne Pfad (www.google.de)
	this.pfad;	// Pfad ohne Host & Parameter (/search)
	this.param	// URL-Parameter des Referrer (q=plus&num=50)
	
	this.top;	// Top-Level de
	this.domain;	// Domain	 google
	this.sub;	// SubDomain www
	
	// Host extrahieren
	tmp	= url.split("/");
	this.host	= tmp[2];		
	
	// Host Bestandteile (Toplevel, Domain, SubDomain)
	tmp	= this.host.split(".");
	switch(tmp.length){
		case 1:
			this.domain	= tmp[0];
			break;
		case 2:
			this.domain	= tmp[0];
			this.top	= tmp[1];
			break;
		case 3:
			this.sub	= tmp[0];
			this.domain	= tmp[1];
			this.top	= tmp[2];
			break;
		default:
	}
		
	// Pfad & Parameter extrahieren
	tmp	= url.split(tmp[tmp.length-1]+"/");
	
	// Trennt am ?
	tmp	= tmp[1].split("\?");	
	this.pfad	= tmp[0];
	
	// Wenn nicht Leer
	if(tmp.length>1){
		this.param	= tmp[1];
	}
}

/* Debug Informationen */
function out(msg){
	// document.getElementById("out").innerHTML	= msg;	
}
