var promioTracker = {
    params: {'lid':'','r':''},
    url: 'https://mailer-service.de/confirm.php?',
    init: function() {

        this.load('lid');
        this.load('r');

        this.saveParams();

        this.trackSuccessPage();
    },
    //check for lid parame  ter - if set per request setup the cookie, if not read from cookie if set
    load: function(name) {
        if (document.location.search.lastIndexOf(name+'=')!=-1) {
            var s = document.location.search;
            var value = s.substring(s.lastIndexOf(name+'=')+(name).length+1);
            if (value.indexOf('&')!=-1) {
               value = value.substring(0, value.indexOf('&'));
            }

            this.params[name] = value;
        } else if(document.cookie != '' && document.cookie.indexOf('pt'+name+'=') != -1) {
            var valPos = document.cookie.indexOf('pt'+name+'=');
            var value = document.cookie.substring(valPos+('pt'+name+'=').length);
            if (value.indexOf(';')!=-1) {
                value = value.substring(0, value.indexOf(';'));
            }

            this.params[name] = value;
        }
        this.log(name + ': ' + this.params[name]);
    },
    saveParams: function() {
        var now = new Date();
        now.setTime(now.getTime() + (4*60*60*1000));

        if (this.params['lid']) {
            document.cookie = "ptlid="+this.params['lid']+";"+now.toGMTString()+';path=/';
        }
        if (this.params['r']){
            document.cookie = "ptr="+this.params['r']+";"+now.toGMTString()+';path=/';
        }
    },
    trackSuccessPage:function() {
        if (this.params['r'] && this.params['lid'] && jQuery('div.success_page').length > 0) {
            var code = '<img src="'+this.url+'&lid='+this.params['lid']+'&r='+this.params['r']+'" width="1" height="1" />';
            jQuery('body').append(code);
            this.log(code);
        }
    },
    log: function(msg) {
        //console.log(msg);
    },
    deleteParams: function()
    {
        try {
            document.cookie = 'ptlid=;Expires: Thu, 02-Jan-70 00:00:01 GMT';
            document.cookie = 'ptr=;Expires: Thu, 02-Jan-70 00:00:01 GMT';
        } catch(e) {this.log(e);}
    }
}

jQuery(document).ready(function() {
    promioTracker.init();
});

