if (typeof RTS != 'object')
    RTS = {};

if (typeof szmvars == 'undefined')
	var szmvars; // Do not delete, required for REMP !
var valeur = 0; // Do not
RTS.stats = {
    options: {
        propriety: {
            domain: '',
            subdomain: '',
            page: '',
            folder: ''
        },
		//loaded: false,
        isSecure: document.location.protocol === 'https:',
        initialized: false,
        waitTime: 20
    },
    load: function() {
        this.remp.load();
        this.wysistat.load();
    },
    init: function(o, s) {
        if (this.options.initialized) return;
		
		if (typeof o == 'string') {
			if (typeof s != 'string')
				s = '';
			o = {
				domain: o,
				page: s
			}
		}

        for (var p in o)
            if (typeof this.options.propriety[p] !== 'undefined')
                this.options.propriety[p] = o[p];

        if (!this.options.propriety.domain) {
            RTS.stats.log('[RTS.stats] "domain" propriety is required! No stats!');
            return;
        }
        this.remp.init();
        this.wysistat.init();
        this.options.initialized = true;
    },
    wysistat: {
        options: {
            file: 'http://www.wysistat.com/statistique.js',
            video: 'http://www.wysistat.com/statistique.js?video=1'
        },
        init: function() {
			this.load();
            this.send();
        },
        load: function() {
            var script = document.getElementsByTagName('script');
			var found = false;
			for (var i = 0 ; i < script.length ; i++) {
				if (~script[i].src.indexOf('www.wysistat.com/statistique.js') && !~script[i].src.indexOf('?video=1'))
					found = true;
			}
			if (!found) {
				if (RTS.stats.options.isSecure)
					for (var p in this.options.propriety)
						this.options.propriety[p] = this.options.propriety[p].replace('http:','https:');
				if(typeof wysistat === "undefined") {
					document.write('<script src="' + this.options.file + '"></script>');
				}
			}
        },
        send: function() {
			var me = this;
            var o = RTS.stats.options,
                s = o.propriety.domain;

            if (o.propriety.subdomain || o.propriety.page) {
                s += ';';
                if (o.propriety.subdomain)
                    s += o.propriety.subdomain;
                if (o.propriety.subdomain && o.propriety.page)
                    s += '/';
                if (o.propriety.page)
                    s += o.propriety.page;
            }
            
            if(typeof wysistat != 'undefined') {
                wysistat('tsr','','','',o.propriety.folder,'','',s)
				me.getVideo();
				RTS.stats.log('[RTS.stats.wysistat] "' + s + '"');
			} else {
				setTimeout(function() {
					me.send();
				}, 56);
			}
        },
        getVideo: function() {
			if (!document.getElementById('wysi_js'))
				document.write('<script id="wysi_js"></script>');
			if (!document.getElementById('wysi_video_js'))
				document.write('<script id="wysi_video_js"></script>');
			document.write('<script src="' + this.options.video + '"></script>');
        }
    },
    remp: {
        options: {
            img: '<img src="http://tsrch.wemfbox.ch/cgi-bin/ivw/CP/###proprieties###?r=' + escape(document.referrer) + '&amp;d=' + (Math.random()*100000) + '" width="1" height="1" alt="">',
            file: 'http://tsrch.wemfbox.ch/2004/01/survey.js' // not used yet... because there are f***ing document.write inside
        },
        init: function() {
            var o = RTS.stats.options,
                s = o.propriety.domain;

			if (document.getElementById('stats-remp'))
				return;

            if (o.propriety.subdomain)
                s += '/' + o.propriety.subdomain;
            if (o.propriety.page)
                s += '/' + o.propriety.page;

            this.options.img = this.options.img.replace('###proprieties###', s);
            document.write(this.options.img);

            szmvars = 'tsrch//CP//' + s;
            RTS.stats.log('[RTS.stats.remp] "' + s + '"');
        }
    },
    log: function(s) {
		if (typeof console == 'object' && typeof console.info == 'function')
			console.info(s);
    }
};

if (!~document.location.href.indexOf('.tsr.ch'))
	var statistiques = RTS.stats.init;
