﻿// mcm.at standard scripts

var bolDOM = (document.getElementById ? true : false);
var bolIE4 = ((document.all && !bolDOM) ? true : false);
var bolIE5 = ((document.all && bolDOM) ? true : false);
var bolNS4 = (document.layers ? true : false);
var bolOpera = (navigator.userAgent.toLowerCase().indexOf("opera") > -1) ? true : false;
var bolSafari = (navigator.userAgent.toLowerCase().indexOf("safari") > -1) ? true : false;
var bolIE = ((document.all && !bolOpera) ? true : false);

var itvNavOutDelay;

function addLoadListener(fn) {
    if (typeof window.addEventListener != "undefined") {
        window.addEventListener("load", fn, false);
    } else if (typeof document.addEventListener != "undefined") {
        /* for OPERA */
        document.addEventListener("load", fn, false);
    } else if (typeof window.attachEvent != "undefined") {
        /* for MSIE */
        window.attachEvent("onload", fn);
    } else {
        var fnOld = window.onload;
        if (typeof window.onload != "function") {
            window.onload = fn;
        } else {
            window.onload = function() {
                fnOld();
                fn();
            };
        }
    }
}

function attachEventListener(target, eventType, functionRef, capture) {
    if (typeof target.addEventListener != "undefined") {
        target.addEventListener(eventType, functionRef, capture);
    } else if (typeof target.attachEvent != "undefined") {
        target.attachEvent("on" + eventType, functionRef);
    } else {
        eventType = "on" + eventType;
        if (typeof target[eventType] == "function") {
            var oldListener = target[eventType];
            target[eventType] = function() {
                oldListener();
                return functionRef();
            };
        } else {
            target[eventType] = functionRef;
        }
    }
}

function getScrollingPosition() {
    var position = [0, 0];
    if (typeof window.pageYOffset != 'undefined') {
        position = [window.pageXOffset, window.pageYOffset];
    } else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
        position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
    } else if (typeof document.body.scrollTop != 'undefined') {
        position = [document.body.scrollLeft, document.body.scrollTop];
    }
    return position;
}

function getCursorPosition(eventObj) {
    if (typeof eventObj == "undefined") {
        eventObj = window.event;
    }
    var scrollingPosition = getScrollingPosition();
    var cursorPosition = [0, 0];
    if (typeof eventObj.pageX != "undefined" && typeof eventObj.x != "undefined") {
        cursorPosition[0] = eventObj.pageX;
        cursorPosition[1] = eventObj.pageY;
    } else {
        cursorPosition[0] = eventObj.clientX + scrollingPosition[0];
        cursorPosition[1] = eventObj.clientY + scrollingPosition[1];
    }
    return cursorPosition;
}

function getViewportSize() {
    var size = [0, 0];
    if (typeof window.innerWidth != 'undefined') {
        size = [window.innerWidth, window.innerHeight];
    } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
    } else {
        size = [document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight];
    }
    return size;
}

function getEventTarget(event) {
    /* returns the element from which the event originated */
    var targetElement = null;
    if (typeof event.target != "undefined") {
        targetElement = event.target;
    } else {
        targetElement = event.srcElement;
    }
    while (targetElement.nodeType == 3 && targetElement.parentNode != null) {
        targetElement = targetElement.parentNode;
    }
    return targetElement;
}

/*
To get all a elements in the document with a info-links class.
getElementsByClassName(document, "a", "info-links");
To get all div elements within the element named container, with a col class.
getElementsByClassName(document.getElementById("container"), "div", "col"); 
To get all elements within in the document with a click-me class.
getElementsByClassName(document, "*", "click-me"); 
*/

function getElementsByClassName(oElm, strTagName, strClassName) {
    /*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
    */
    var arrElements = (strTagName == "*" && document.all) ? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for (var i = 0; i < arrElements.length; i++) {
        oElement = arrElements[i];
        if (oRegExp.test(oElement.className)) {
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function setIEOverClass(startNode, strTagName) {
    if (startNode) {
        for (var i = 0; i < startNode.childNodes.length; i++) {
            node = startNode.childNodes[i];
            if (node.nodeName.toLowerCase() == strTagName) {
                node.onmouseover = function() { this.className += " over"; }
                node.onmouseout = function() { this.className = this.className.replace(" over", ""); }
            }
            if (node.hasChildNodes()) setIEOverClass(node, strTagName);
        }
    }
    return;
}

function initWincodeInput() {
    var elContainer = document.getElementById("c_header_wincode");
    var iptField;
    if (elContainer) {
        for (var i = 0; i < elContainer.childNodes.length; i++) {
            if (elContainer.childNodes[i].nodeName.toLowerCase() == "input") {
                iptField = elContainer.childNodes[i];
            }
        }
        if (!iptField) { return; }
        iptField.value = "";
        iptField.onfocus = function() {
            iptField.style.backgroundPosition = "left bottom";
        };
    }
}

function startSamplePlayer(strSampleUrl) {
    // build container if not existing already
    if (!document.getElementById("samplePlayerContainer")) {
        var elContainer = document.createElement("div");
        elContainer.id = "samplePlayerContainer";
        elContainer.style.position = "absolute";
        elContainer.style.top = 0;
        elContainer.style.left = 0;
        elContainer.style.visibility = "hidden";
        var elBody = document.getElementsByTagName("body")[0];
        elBody.appendChild(elContainer);
    }
    var strFileExt = strSampleUrl.substr(strSampleUrl.length - 3);
    umpi_buildPlugin((strFileExt == "wma") ? "wmf" : "swf", 1, 1, strSampleUrl, 'samplePlayerContainer');
    return true;
}

function stopSamplePlayer() {
    if (document.getElementById("samplePlayerContainer")) {
        var elContainer = document.getElementById("samplePlayerContainer");
        document.getElementsByTagName("body")[0].removeChild(elContainer);
    }
    return true;
}

function showElement(elmName) {
    var obj = document.getElementById(elmName);
    if (obj) {
        if (obj.style.display == "none") {
            obj.style.display = "block";
        } else {
            if (obj.style.visibility == "hidden") {
                obj.style.visibility = "visible";
            } else {
                obj.style.display = "block";
                obj.style.visibility = "visible";
            }
        }
    }
}

function hideElement(elmName, strHideType) {
    var obj = document.getElementById(elmName);
    if (obj) {
        if (strHideType == "hidden") {
            obj.style.visibility = "hidden";
        } else {
            obj.style.display = "none";
        }
    }
    // exception for shaped teaser on homepage
    if (elmName == "shaped_teaser") {
        document.getElementById("counter_home").style.display = "block";
    }
}

function checkPlugin(strPluginName) {
    // check for IE
    if (!navigator.plugins || navigator.plugins.length == 0) return null;
    // check other browsers
    for (var i = 0; i < navigator.plugins.length; i++) {
        if (navigator.plugins[i].name.toLowerCase().indexOf(strPluginName.toLowerCase()) > -1) {
            return true;
        }
    }
    return false;
}

function navOut() {
    window.clearInterval(itvNavOutDelay);
    document.getElementById("safari_flashblock").style.display = "none";
}

function initNav() {
    // workaround for missing :hover pseudoclass support in MSIE 6.0 and lower
    if (bolIE) {
        navRoot = document.getElementById("navMain");
        setIEOverClass(navRoot, "li");
    }
    if (bolSafari && false) {
        navRoot = document.getElementById("navMain");
        if (navRoot) {
            var nodes = navRoot.getElementsByTagName("a");
            alert(nodes.length);
            for (i = 0; i < nodes.length; i++) {
                var node = nodes[i];
                node.onmouseover = function(e) {
                    window.clearInterval(itvNavOutDelay);
                    document.getElementById("safari_flashblock").style.display = "block";
                    itvNavOutDelay = window.setInterval("navOut()", 1000);
                }
            }
        }
    }
}

function toggleDisplay(strElm, strType) {
    switch (strType) {
        case 'display':
            document.getElementById(strElm).style.display = (document.getElementById(strElm).style.display == 'none') ? 'block' : 'none';
            break;
        case 'visibility':
            document.getElementById(strElm).style.visibility = (document.getElementById(strElm).style.visibility == 'hidden') ? 'visible' : 'hidden';
            break;
        default:
            break;
    }
}

function openInFrame(target, url) {
    // alert(target + " -> " + url);
    target.location.href = url;
}

function openInBlank(url) {
    window.open(url, "window", "");
}

function openInOpener(url) {
    window.opener.location.href = url;
}

function openCentered(strUrl, strName, strParams) {
    // opens popup window centered on screen
    var arrParams = strParams.split(",");
    var strCenteredParams = "";
    var intLeft, intTop, intWidth, intHeight;

    for (var i = 0; i < arrParams.length; i++) {
        if (arrParams[i].indexOf("width") > -1) {
            intWidth = arrParams[i].substr(6);
            intLeft = parseInt((screen.availWidth - intWidth) / 2);
        } else if (arrParams[i].indexOf("height") > -1) {
            intHeight = arrParams[i].substr(7);
            intTop = parseInt((screen.availHeight - intHeight) / 2);
        } else if (arrParams[i].indexOf("left") > -1) {
        } else if (arrParams[i].indexOf("top") > -1) {
        } else {
            strCenteredParams += arrParams[i] + ",";
        }
    }
    strCenteredParams += "width=" + intWidth + ",height=" + intHeight + ",left=" + intLeft + ",top=" + intTop;
    window.open(strUrl, strName, strCenteredParams);
}

function getUrlFromFlash(url, target) {
    var objFrame;
    var urlFull = url; //"https://" + window.location.hostname + url;

    if (!url) return;
    if (!target || target == "") target = "_self";

    var elBody = document.getElementsByTagName("body")[0];
    var newForm = document.createElement("form");
    newForm.id = "formFlashLink";
    newForm.method = "get";
    newForm.action = urlFull;
    newForm.target = target;
    elBody.appendChild(newForm);

    // alert(newForm.action);

    var f = document.getElementById("formFlashLink");
    f.submit();
}

/* TRACKING
------------------------------------------------ */
function trackPage() {
    var strPath = window.location.pathname;
    // check if page is opened from newsletter
    if (/\/newsletter\/[0-9]{8}\//.test(strPath)) {
        // track the newsletter url for newsletter stats
        var strNlPath = strPath.match(/\/newsletter\/[0-9]{8}\//, "") + strPath.replace(/\/newsletter\/[0-9]{8}\//, "").replace(/\//g, "_");
        pageTracker._trackPageview(strNlPath);
        //urchinTracker(strNlPath);
        // track the real url for overall site stats
        var strContentPath = strPath.replace(/\/newsletter\/[0-9]{8}/, "");
        pageTracker._trackPageview(strContentPath);
        //urchinTracker(strContentPath);
    } else {
        pageTracker._trackPageview();
        //urchinTracker();	
    }
}

function trackExternalLink(url) {
    // Google Analytics
    var urchinUrl = url.replace(/https:\/\//, "");
    pageTracker._trackPageview("/outgoing/" + urchinUrl); //urchinUrl.replace(/\./g, "_"));	
    //urchinTracker("/outgoing/" + urchinUrl); //urchinUrl.replace(/\./g, "_"));	
    //
    obj = document.getElementById("trackImg");
    if (obj) {
        obj.src = "/tracking/index.aspx?e=" + encodeURIComponent(url) + "&em=" + encodeURIComponent("https://" + window.location.hostname + window.location.pathname) + "&rnd=" + Math.random();
    }
}
/* --------------------------------------------- */

function countDown() {

    var jahr = 2009, monat = 11, tag = 25, stunde = 12, minute = 00, sekunde = 00;
    var zielDatum = new Date(jahr, monat - 1, tag, stunde, minute, sekunde);

    startDatum = new Date(); // Aktuelles Datum

    // Countdown berechnen und anzeigen, bis Ziel-Datum erreicht ist
    if (startDatum < zielDatum) {

        var jahre = 0, monate = 0, tage = 0, stunden = 0, minuten = 0, sekunden = 0;

        // Jahre
        while (startDatum < zielDatum) {
            jahre++;
            startDatum.setFullYear(startDatum.getFullYear() + 1);
        }
        startDatum.setFullYear(startDatum.getFullYear() - 1);
        jahre--;

        // Monate
        while (startDatum < zielDatum) {
            monate++;
            startDatum.setMonth(startDatum.getMonth() + 1);
        }
        startDatum.setMonth(startDatum.getMonth() - 1);
        monate--;

        // Tage
        while (startDatum.getTime() + (24 * 60 * 60 * 1000) < zielDatum) {
            tage++;
            startDatum.setTime(startDatum.getTime() + (24 * 60 * 60 * 1000));
        }

        // Stunden
        stunden = Math.floor((zielDatum - startDatum) / (60 * 60 * 1000));
        startDatum.setTime(startDatum.getTime() + stunden * 60 * 60 * 1000);

        // Minuten
        minuten = Math.floor((zielDatum - startDatum) / (60 * 1000));
        startDatum.setTime(startDatum.getTime() + minuten * 60 * 1000);

        // Sekunden
        sekunden = Math.floor((zielDatum - startDatum) / 1000);

        if (document.getElementById("wildcard-d")) {
            document.getElementById("wildcard-d").innerHTML = tage;
            document.getElementById("wildcard-h").innerHTML = stunden;
            document.getElementById("wildcard-m").innerHTML = minuten;
            document.getElementById("wildcard-s").innerHTML = sekunden;
        }

        setTimeout('countDown()', 200);
    }
    // Anderenfalls alles auf Null setzen
    else {
           document.getElementById("wildcard-d").innerHTML = "0";
           document.getElementById("wildcard-m").innerHTML = "0";
           document.getElementById("wildcard-s").innerHTML = "0";
    }
}

//addLoadListener(countDown);
// addLoadListener(initWincodeInput);
addLoadListener(initNav);