if (window.statistikCalled == undefined) {
    window.statistikCalled = false;
}
if (typeof statisticHost == 'undefined') { statisticHost = 'http://statistik.porsche.co.at'; }

function markExternals(temptable, data) {
    window.oldOnLoad = (window.onload) ? window.onload : new Function;
    window.onload = function() {
        var links = document.getElementsByTagName('a');
        for(var i=0;i<links.length;i++) {
            var link = links[i];
            var href = link.href;
            var exitRel = '';
			var noStat = false;

            // Sonderlinks
            if (href.substr(0, 11) != 'javascript:') {
                rels = link.rel.split(' ');
                for (x=0; x<rels.length; x++) {
                    if (rels[x].substr(0, 4) == 'stat') {
                        href += href.indexOf('?') == -1 ? '?' : '&';
                        href += 'rel=' + rels[x];
                        if (document.all) {
                            link.href = ' ' + href;
                        } else {
                            link.href = href;
                        }
                    } else if (rels[x].substr(0, 4) == 'exit') {
                        exitRel = rels[x].substr(4);
                    } else if (rels[x] == 'noStat') {
						var noStat = true;
					}
                }
            }
			
			if (noStat) continue;

            // Externe Links oder Dateien
            if (href.indexOf('?') > 0) { href = href.substr(0, href.indexOf('?')); }
            if ((
                    href.substr(0, statisticHost.length) != statisticHost &&
                    href.substr(0, 11).toLowerCase() != 'javascript:' &&
                    href.substr(location.protocol.length+2, location.hostname.length) != location.hostname &&
                    href != ''
                ) ||
                href.substr(href.length-4) == ".pdf" ||
                href.substr(href.length-4) == ".zip" ||
                href.substr(href.length-4) == ".doc" ||
                href.substr(href.length-4) == ".xls" ||
                exitRel != ''
                //links.href.substr(links.href.length-4) == ".jpg"
                //links.href.substr(links.href.length-4) == ".gif"
                //links.href.substr(links.href.length-4) == ".png"
            ) {
                var statdata = {};
                for (var d in data) {
                    statdata[d] = data[d];
                }

                statdata.D_Referer 	= location.pathname;
                if (link.href.substr(0, 7) == 'mailto:') {
                    statdata.D_Seite = link.href;
                    statdata.D_Typ  = 'mail';
                } else if (link.href.substr(location.protocol.length+2, location.hostname.length) != location.hostname) {
                    statdata.D_Seite = link.href;
                    statdata.D_Typ 	= 'extern';
                } else {
                    statdata.D_Seite = link.href.replace(location.protocol + '/' + '/' + location.host, '');
                    statdata.D_Typ 	= 'file';
                }
                if (exitRel != '') {
                    statdata.D_Linktext = exitRel;
                    statdata.D_Typ = 'exit';
                }

                link.data = data;
                link.statdata = statdata;
                link.stattable = temptable;

                if (link.href.substr(0, 6) != 'mailto') {
					var h = createExternalLink(link.href, link.stattable, link.statdata);
					if (h != undefined) {
						if (document.all) {
							link.href = ' ' + h;
						} else {
							link.href = h;
						}
					}
				}
                link.onStatClick = (link.onclick != undefined) ? link.onclick : function(){return true; };
                link.onclick = function() {
					var ret = this.onStatClick();
                    if ((ret != undefined && !ret && this.onStatClick.toString().indexOf('this.href') == -1) ||
					   this.href.substr(0, 6) == 'mailto')
					{
                        sendCounter(this.stattable, this.statdata);
                    }
                    return ret;
                }
            }
        }
        window.oldOnLoad();
    }
}

function createExternalLink(href, temptable, data)
{
	var stat = '&temptable=' + temptable;
	for (var d in data) {
		stat += '&' + d + '=' + encodeURIComponent(data[d]);
	}
	return statisticHost + '/redirect.php?location=' + encodeURIComponent(href) + stat;
}


function count(temptable, data, domain) {
	if (domain) { statisticHost = domain; }
    var post = getPost(data);
    sendCounter(temptable, post);

    //debugger;
    if (!window.statistikCalled) {
        markExternals(temptable, post);

        match = location.search.match(/rel=(.+)/);
        if (match) {
            pos = match[1].indexOf('&');
            if (pos > 0) {
                match[1] = match[1].substr(0, pos);
            }
            rels = match[1].split('|');
            for (x=0; x<rels.length; x++) {
                parts = rels[x].match(/^stat(.+)_(.*)$/);
                if (parts != null) {
                    var extraData = post;
                    extraData.D_Typ = parts[1];
                    extraData.D_Target = parts[2];
                    try {
                        extraData.D_Target = decodeURI(extraData.D_Target);
                    } catch(ex) {
                    }
                    extraData.D_Target = unescape(extraData.D_Target);
                    sendCounter(temptable + '_extra', extraData);
                }
            }
        }
    }
    window.statistikCalled = true;
}

function mark(temptable, data) {
    if (!window.statistikCalled) {
        markExternals(temptable, getPost(data));
    }
    window.statistikCalled = true;
}

function sendCounter(temptable, post)
{
    get = '';
    for (var d in post) {
        get += '&' + d + '=' + encodeURIComponent(post[d]);
    }
    var i = new Image(1,1);
    i.src = statisticHost + '/count.php?temptable=' + temptable + get + '&' + Math.floor((Math.random()*1000000));
}

function getPost(data) {
    var post = new Object();
    for (var d in data) {
        post[d] = data[d];
    }

    data = getData();
    for (var d in data) {
        if (!post[d]) {
            post[d] = data[d];
        }
    }
    return post;
}

function getData(href)
{
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    windowWidth = String(Math.floor(windowWidth/100)*100) + '-' + String(Math.ceil(windowWidth/100)*100);
    windowHeight = String(Math.floor(windowHeight/100)*100) + '-' + String(Math.ceil(windowHeight/100)*100);
    screenWidth = screen.width;
    var widths = new Array(160, 220, 240, 320, 480, 640, 720, 800, 1024, 1152, 1280, 1366, 1400, 1440, 1600, 1680, 1920, 2048, 2560, 3200, 3840, 5120, 6400, 7680);
    for (var x=0; x<widths.length; x++) {
        if (screenWidth < widths[x] && screenWidth > widths[x-1] && widths[x] != screenWidth) {
            screenWidth = widths[x-1] + '-' + widths[x];
        }
    }
    screenHeight = screen.height;
    var heights = new Array(120, 160, 176, 180, 200, 240, 320, 480, 600, 768, 800, 864, 900, 1024, 1050, 1080, 1200, 1440, 1536, 1600, 2048, 2400, 4096, 4800);
    for (var x=0; x<heights.length; x++) {
        if (screenHeight < heights[x] && screenHeight > heights[x-1] && heights[x] != screenHeight) {
            screenHeight = heights[x-1] + '-' + heights[x];
        }
    }

    return {
        'D_Seite': location.pathname + location.search,
        'G_Session': getCookie('PHPSESSID'),
        'D_Referer': document.referrer.replace(location.protocol + '/' + '/' + location.host, ''),
        'D_Useragent': navigator.userAgent,
        'D_WindowWidth': windowWidth,
        'D_WindowHeight': windowHeight,
        'D_ScreenWidth': screenWidth,
        'D_ScreenHeight': screenHeight,
        'D_FlashVersion': getSwfVer(),
        'D_Js': 'yes'
    }
}

function loadXMLHttpRequest() {
    var pageRequest = false;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            pageRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                pageRequest = false;
            }
        }
    @end
    @*/

    if (!pageRequest && typeof XMLHttpRequest != 'undefined') {
        pageRequest = new XMLHttpRequest()
    }
    return pageRequest;
}

// JavaScript helper required to detect Flash Player PlugIn version information
/* some parts by quirksmode.org and www.javascriptkit.com/javatutors/conditionalcompile3.shtml */
function getSwfVer(i){

    /*@cc_on
    @if (@_jscript_version >= 5)
        // IE flash detection.
        for(var i=17; i>0; i--){
                try{
                        var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
                        if(flash) {
                            return i;
                        }
                }
                catch(e){
                }
        }
    @end
    @*/

    // NS/Opera version >= 3 check for Flash plugin in plugin array
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
            descArray = flashDescription.split(" ");
            tempArrayMajor = descArray[2].split(".");
            return tempArrayMajor[0];
        } else {
            flashVer = -1;
        }
    }
    // MSN/WebTV 2.6 supports Flash 4
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
    // WebTV 2.5 supports Flash 3
    else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
    // older WebTV supports Flash 2
    else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
    // Can't detect in all other cases
    else {

        flashVer = -1;
    }
    return flashVer;
}

function getCookie(Name) {
  var search = Name + "="
  var CookieString = document.cookie
  var result = null
  if (CookieString.length > 0) {
    offset = CookieString.indexOf(search)
    if (offset != -1) {
      offset += search.length
      end = CookieString.indexOf(";", offset)
      if (end == -1)
        end = CookieString.length
      result = unescape(CookieString.substring(offset, end))

      }
    }
   return result
}
