/**
ident: 10000004145
adds agof stuff, frabo tag
mclient tag

Author: clemens brÃ¼ckmann
cbrueckmann@euro.real.com
*/
$(document).ready(function(){
var sec = document.location.protocol.indexOf('https') >-1;
app.agof_IVW = {
	getAgofCode: function () {
		var agof;
/** according to IVW checkerman this must be registered as homepage*/
		if (pageEnv.page.match(/^(video)(\/)?$/)){
			return 'homepage';
		}
var _code = self.pageEnv.page.match(/^(rp_update|rp14|rp12|video\/livenews|music|games|spot|video|upsell\/superpass|realplayer|inc\/artistinfo|upsells\/twe|free-offers\/rpsp|impressum)/);		
var code = _code && _code[0];
		switch (code) {
			case 'music':
			case 'inc/artistinfo':
				agof = 'Musik';
				if (self.pageEnv.page == 'music/what-is-realmusic') {
					agof = 'wirm';
				}
				break;
			case 'video':
				
				agof = 'Video';
				break;
			case 'games':
				agof = 'Spiele';
				break;
			case 'spot':
				agof = 'Suche';
				break;
			case 'rp_update':
			case 'rp12':
			case 'rp14':
			case 'realplayer':
			case 'upsells/twe':
			case 'free-offers/rpsp':	
				agof = 'RealPlayer';
				break;	
			case 'video/livenews':
			case 'upsell/superpass':
				agof = 'Live News';
				break;
			case 'impressum':
				agof = 'impressum';
				break;
			default:
				agof = self.pageEnv.page.substr(-12,12).replace('/','_');
		}
		
		return agof;
	},
	prot: (sec) ? 'https' : 'http',
	mclient: (sec) ? 'mclientssl' : 'mclient',
	szm_cookiename: 'szm_mc',
	szmvars:'',
	page: '',
	offerid: 'realcom',
	type: 'CP',
	version: '1208',
	code: self.pageEnv.page.substr(-12,12).replace('/','_'),
	comment: app.detect.isRp ? 'realplayer' : 'browser',
	addIVWimgTag: function (){
		var src = "http://" + app.agof_IVW.offerid +".ivwbox.de/cgi-bin/ivw/" +app.agof_IVW.type+"/" +app.agof_IVW.code+";"+ app.agof_IVW.comment+"?r=" +escape(document.referrer) + "&d=" +(Math.random()*100000);		
		var IVWimg = $('<img width="1" src="' + src+ '" alt="szmtag" />');
		$('body').append(IVWimg);
	},
	addIVWsurvey:false,
	/** fires only on login*/
	trackLoginIVW: {
		clearBeforeLogin: false, //NYI
		check: function () {
			var cTemp = this.getTempCookie();
			if (!cTemp) {return;}
			var cSzm = this.szm_getcookie();
			if (cSzm && cSzm == cTemp) {return;}
			var src = this.prot + "://"+this.mclient+".ivwbox.de/cgi-bin/ivw/"+app.agof_IVW.type+"/"+app.agof_IVW.version+"/"+app.agof_IVW.offerid+"/" + cTemp;
			this.szm_getmclient(src, false);
			this.szm_setcookie(cTemp);
			this.clearTempCookie();
		},
		clearTempCookie: function (){
			$.cookie.set('szm_temp', self, this.offerid, { path:'/', domain:'.real.com'});
		},
		getTempCookie: function (){
			return $.cookie('szm_temp');
		},
		szm_setcookie: function (code) {
			$.cookie(app.agof_IVW.szm_cookiename,self, code,{ path:'/', domain:'.real.com'});
		},
		szm_clear: function () {
			szm_setcookie(app.agof_IVW.offerid);
		},
		szm_getcookie: function () {
			return $.cookie(app.agof_IVW.szm_cookiename);
		},
		szm_getmclient: function (src, callBack) {
			var IVWimg = $('<img width="1" height="1" src="' + src+ '" alt="szmmctag" />');
			$('body').append(IVWimg);
			if (callBack ) {self.setTimeout( function () { callBack(); }, 10 );}
		}
	}
};

/** needs to be global?? */
var szmvars;

(function (){
	app.agof_IVW.code = app.agof_IVW.getAgofCode();
	szmvars = app.agof_IVW.offerid+"//"+app.agof_IVW.type+"//"+app.agof_IVW.code;

	console.log('app.agof_IVW.code',app.agof_IVW.code);

	app.agof_IVW.addIVWimgTag();
/** FRABO (fragebogen, survey)- Tag */
	
	if ( app.agof_IVW.addIVWsurvey ) {
		$.script.add("http://"+app.agof_IVW.offerid+".ivwbox.de/2004/01/survey.js");
	}
	if (app.auth){
		app.auth.addCallback(function(){
			app.agof_IVW.trackLoginIVW.check();
		});
	}
})();

});

