/* Copyright 2008 United Internet AG */
var SMV = new function() {
    this.pType       = 'XP';
    this.iDomain   = '';
    this.site        = 'undef';
    this.section     = 'undef/undef';
    this.urlPrefix   = '';
    this.sectionList = {};
    this.tax_id = '1';

    this._urlpath = function() {
        var hosturl = window.location.host;
        return hosturl;
    }

    this.pixeltypeList = {
        'CP':'CP'
    ,   'SP':'SP'
    ,   'AP':'AP'
    ,   'XP':'XP'
    };

    this.box = {
        'smv':      ''
    ,   '84296':    '84296'
    ,   '40023':    '40023'
    };

    this._setSite = function(site) {
        if (typeof(this.box[site]) != 'undefined') {
            this.site = site;
            this.iDomain = this.box[site];
        }
    }

    this._validateSection = function(section) {
        var section = '/' + String(section).toLowerCase() + '/';
        var pos = section.indexOf('//');
        while (pos > -1) {
            section = section.substr(0, pos) + section.substr(pos+1);
            pos = section.indexOf('//');
        }
        return section.substr(1, section.length-2);
    }

    this._setType = function(type) {
        if (typeof(this.pixeltypeList[type]) != 'undefined') {
            this.pType = type;
        } else {
            this.pType = "XP";
        }
    }

    this.checkId = function(cclass) {
        if (isNaN(cclass)){
            this._setSection(cclass);
        } else {
            this.tax_id=cclass;
        }
    }

    this._setSection = function(section) {
        this.section = this._validateSection(section);
    }

    this.setSectionIdMap = function(sectionList) {
        this.sectionList = sectionList;
    }

    this._getIdCode = function() {
        var thisSection = '/' + this.section + '/';
        var thisLength = thisSection.length;

        for (var section in this.sectionList) {
            if (thisLength >= section.length && thisSection.substr(0, section.length) == section) {
                this.tax_id = this.sectionList[section];
                break;
            }
        }
        return this.tax_id;
    }

    this._getNgUserId = function() {
        var uim_ngid = '';
        if (document.cookie && document.cookie.indexOf('NGUserID=')>=0) {
            var uim_c = document.cookie + ';';
            var uim_cs_pos = uim_c.indexOf('NGUserID=') + 'NGUserID='.length;
            var uim_ce_pos = uim_c.indexOf(';', uim_cs_pos);
            uim_ngid = uim_c.substring(uim_cs_pos, uim_ce_pos);
        }
        return uim_ngid;
    }

    this._createSemParams = function(params) {
        var qs = '';
        for (var i in params) {
            if (params[i]) {
                qs += '%26'+i+'%3D'+params[i];
            }
        }
        return (qs ? qs.substr(3) : '');
    }

    this._getPixelboxUrl = function(options) {

        var visitcounter =0;

        var vid = Utils._getCookie("SmvVisit",2);
        if(vid == "none")
            vid = Utils._getTimestamp();

        visitcounter = Utils._getCookie("SmvVisit",1);
        if(visitcounter == "none")
            visitcounter = 1 ;
        else
            visitcounter = Number(visitcounter) + 1;

        var params = {
            'sc' :this.section //tempsection
        ,   'crg':options['crg']
        ,   'jsv':Statistic._getJsVersion()
        ,   'scr':Statistic._getScreenResolution()
        ,   'flv':Statistic._getFlashVersion()
        ,   'smv':this._getCertificate(vid)
        ,   'res':Utils._getCookie("SmvVisit",0)
        ,   'vct':visitcounter
        ,   'vid':vid
        };

        Utils._setCookie("SmvVisit", this.section+","+visitcounter+","+vid);

        var query = "?d=" + Utils._getRandom() + '&r=' + escape(document.referrer);
        var uim_szmurl = '//pixelbox.uimserv.net/cgi-bin/'+this.site+'/'+this.pType+'/';
        return uim_szmurl + this._getIdCode() + ";" + this._createSemParams(params) + query;
    }

    this._loadPixel = function(url) {
        url = this.urlPrefix+url;
        if (Image && url) {
            (new Image()).src = url;
        }
    }

    this._getCertificate = function(val) {
        var z = val.toString().split('');
        var cert = 0;
        for(var i = 0; i < z.length; i++){
            cert += Number(z[i]);
        }
        return cert;
    }
    
    this.smvInit = function(options) {
        if(options['site']) {
            this._setSite(options['site']);
        } else {
            this._setSite(PUT.nr);
            }
        if(options['pageidentifier']) {
            this._setSection(options['pageidentifier']);
        }
        if(options['contentclass']) {
            this.checkId(options['contentclass']);
        } else {
            this.setSectionIdMap(PUT.setSectionIdMap);
            }
        this._setType('CP');
        this._loadPixel(this._getPixelboxUrl(options));
        
    }
}


var Utils = new function() {
    this._trim = function(str){
        str = str.replace(/\s+$/,"");
        return (str.replace(/^\s+/,""));
    }

    this._getRandom = function() {
        return Math.round(Math.random()*100000);
    }

    this._getTimestamp = function() {
        var d = new Date();
        return d.getTime();
    }

    this._setCookie = function(name, value){
        var expired = new Date();
        expired = new Date(expired.getTime() + 30 * 1000 * 60);
        document.cookie = name + "=" + value + "; expires="+expired.toGMTString()+";";
    }

    this._getCookie = function(cookiename,index){
        if (document.cookie) {
            var cookies = document.cookie.split(";");
            for(var i=0;i<cookies.length;i++){
                var cookietext = cookies[i].split("=");
                if(this._trim(cookietext[0]) == cookiename){
                    return cookietext[1].split(",")[index];
                }
            }
        }
        return("none");
    }
}


var Statistic = new function() {
    this._getJsVersion = function() {
        var vers = "1.0";
        if (typeof frames.focus == "function")      { vers = "1.1"; }
        if (typeof window.forward == "function")    { vers = "1.2"; }
        if (typeof isFinite == "function")          { vers = "1.3"; }
        if (typeof arguments == "object")           { vers = "1.4"; }
        if (typeof Number.toFixed == "function")    { vers = "1.5"; }
        return vers;
    }

    this._getScreenResolution = function() {
        if (window.screen) {
            return window.screen.width + "x" + window.screen.height;
        }
    }

    this._getFlashVersion = function() {
        if (navigator.platform == "Win32" && navigator.userAgent.indexOf("MSIE") > -1) {
            window.flashVersion = 0;
            window.execScript('\
                on error resume next\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))): If temp=true Then flashVersion = 2\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))): If temp=true Then flashVersion = 3\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))): If temp=true Then flashVersion = 4\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))): If temp=true Then flashVersion = 5\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))): If temp=true Then flashVersion = 6\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))): If temp=true Then flashVersion = 7\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))): If temp=true Then flashVersion = 8\n\
                temp = false: temp = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))): If temp=true Then flashVersion = 9\n\
            ', 'VBScript');
        } 
        else if (navigator.plugins) {
            if (navigator.plugins['Shockwave Flash 2.0'] || navigator.plugins['Shockwave Flash']) {
                var isVersion2 = navigator.plugins['Shockwave Flash 2.0'] ? ' 2.0' : '';
                var flashDescription = navigator.plugins['Shockwave Flash' + isVersion2].description;
                window.flashVersion = parseInt(flashDescription.substring(16));
            }
        }
        if (window.flashVersion) {
            return window.flashVersion;
            } else {
                return 0;
            }
    }
}
