﻿// begin
var frameContentHeight = 0;
var ghh = {

    // Tagging parameters
    file: "http://www.for-me-online.de/Assets/Core/Common/Scripts/Unica/ntpagetag.js",
    site: "site=for-me-online.de",
    domain: ".for-me-online.de",
    pv: 1,
    program: "germany",
    platform: "external-site",
    siteType: "external-site",
    sitename: "BSI_Swiffer2",
    section: "",
    subsection: "",
    page: "homepage",

    // Overlay texts
    lang: {
        close: "Schließen",
        powered: '<img src="http://www.for-me-online.de/Assets/Local/Microsites/BSI_Swiffer2/img/logo.png" alt="For-me-online" id="logo" />',
        link: "http://www.for-me-online.de",
        info: "In Partnerschaft mit for me. Bewerten Sie, diskutieren Sie mit und profitieren Sie von Ideen und Rabatten, die Ihr tägliches Leben vereinfachen."
    },
    /* END LOCALIZATION */

    MAX_HEIGHT: 450,
    ov: null,
    byClass: {},

    /**
    * Tracking
    */
    track: function () {

        if (ghh.page == null) return;
        var site = ghh._cleanDomain();
        if (ghh.site) {

            // config
            window.NTPT_GLBLEXTRA = ghh.site;
            window.NTPT_IDCOOKIE_DOMAIN = ghh.domain;
            window.NTPT_PGEXTRA = 'program=' + ghh.program + '&platform=' + ghh.platform + '&site-type' + ghh.siteType + '&sitename=' + ghh.sitename + '&section=' + ghh.section + '&subsection=' + ghh.subsection + '&page=' + ghh.page;

            // create tracking script tag
            var sc = document.createElement("script");
            sc.type = "text/javascript";
            sc.src = ghh.file;
            document.getElementsByTagName("head")[0].appendChild(sc);
        }
        ghh.page = null;
    },

    _cleanDomain: function () {

        var d = document.domain;
        d = d.substr(0, Math.max(d.lastIndexOf('.'), 0));
        d = d.substr(d.lastIndexOf('.') + 1);
        if (d.length)
            return d = d.replace(/[.-]/g, '_').toUpperCase();
        else return null;
    },

    /**
    * Create overlay with content
    */
    open: function (url, height, width) {

        ghh.close();
        frameContentHeight = height;
        frameContentWidth = width;
        ghh.ov = ghh._build();
        document.body.appendChild(ghh.ov);

        ghh._iframeLoad(url);

        ghh.ov.style.display = "block";
        ghh.center();

        setTimeout(ghh._show, 0);
    },

    _show: function () {

        ghh.ov.className += " ghh-ov-show";

        // foot FIX IE6
        var foot = ghh.byClass["ghh-pa-foot"];
        foot.style.left = "0px";

    },

    close: function () {

        if (!ghh.ov) return;

        clearInterval(ghh.scrollTimer);

        document.body.removeChild(ghh.ov);
        ghh.ov = null;
        ghh.byClass = {};
        getPersonalization();
    },

    /**
    * Panel builder
    */
    _build: function () {

        var n = ghh._createNode;
        return n("div", "ghh-ov",
            n("div", "ghh-ov-bg"),
            n("div", "ghh-center",
                n("div", "ghh-pa",
					n("div", "ghh-pa-borderbottom"),
					n("div", "ghh-pa-borderbottomright"),
                    n("div", "ghh-pa-foot",
                        n("a", "ghh-pa-foot-link", ghh.lang.link, ghh.lang.powered),
                        n("div", "ghh-pa-foot-info", ghh.lang.info)
                        ),
                    n("div", "ghh-close",
                        n("a", "ghh-close-link", "javascript:ghh.close()",
                            n("span", "ghh-close-bg", ghh.lang.close)
                            )
                        ),
                    n("div", "ghh-pa-top", "&nbsp;"),
                    n("div", "ghh-pa-body",
                        n("div", "ghh-pa-in",
                            n("iframe", "ghh-pa-iframe", ghh._iframeInit)
                            ),
                        n("div", "clear")

			)
                    )
                )
            );
    },

    /**
    * Panel centering
    */
    center: function () {

        if (!ghh.autoCenter) {

            ghh.autoCenter = true;
            ghh._event(window, "resize", ghh._resize);
            ghh._event(window, "scroll", ghh._resize);
        }
        ghh._resize();
    },

    _resize: function () {

        if (!ghh.ov) return;
        // bg
        var bg = ghh.byClass["ghh-ov-bg"];
        bg.style.height = ghh._bodyHeight() + "px";

        // panel
        var b = document.body;
        var de = document.documentElement;
        var top = de.scrollTop || b.scrollTop;
        var height = de.clientHeight || b.clientHeight;
        var width = de.clientWidth || b.clientWidth;
        var pa = ghh.byClass["ghh-pa"];
        pa.style.width = ghh.frameWidth + "px";
        pa.style.top = top + Math.max(0, Math.round((height - ghh.frameHeight - 34 - 54) / 2)) + "px";
        pa.style.left = Math.max(0, Math.round((width - ghh.frameWidth - 6) / 2)) + "px";
    },

    _bodyHeight: function () {

        var b = document.body;
        var de = document.documentElement;

        return Math.max(
            Math.max(de.scrollHeight, de.clientHeight),
            Math.max(b.scrollHeight, b.clientHeight)
            );
    },

    /**
    * IFrame management
    */
    _iframeInit: function (node) {

        node.setAttribute("frameborder", "0");
        node.setAttribute("scrolling", "yes");
        node.setAttribute("foo", "bar")
        ghh._event(node, "load", ghh._iframeLoaded);
    },

    _iframeLoad: function (url) {

        ghh.frameHeight = 100;
        ghh.frameWidth = 540;

        var node = ghh.byClass["ghh-pa-iframe"];
        node.style.opacity = 0;
        node.style.height = ghh.frameHeight;
        node.style.width = ghh.frameWidth;
        node.src = url;
    },

    _iframeLoaded: function () {

        var node = ghh.byClass["ghh-pa-iframe"];
        if (!node) return;
        var src = node.getAttribute("src");
        if (src == null || src == "") return;

        if (frameContentHeight == undefined) {
            height = 400;

            try {
                var doc = node.contentWindow.document || node.contentDocument;
                height = Math.min(ghh.MAX_HEIGHT - 34 - 54, doc.body.clientHeight || height);
                console.log(doc.body.clientHeight);
                console.log(height);
            }
            catch (err) { }
        }
        else
            height = frameContentHeight;

        if (frameContentWidth == undefined || frameContentWidth < 540)
            width = 540;
        else
            width = frameContentWidth;

        node.style.height = height + "px";
        node.style.width = width - 10 + "px";
        node.style.opacity = 1;
        ghh.frameHeight = height;
        ghh.frameWidth = width;
        ghh.center();
    },


    /**
    * Node builder
    */
    _createNode: function (tag, className) {

        var node = document.createElement(tag);

        if (className) {

            node.className = className;
            ghh.byClass[className] = node;
        }

        var i = 2;
        if (tag == "a") node.href = arguments[i++] || "#";

        for (; i < arguments.length; i++) {

            var arg = arguments[i];
            if (typeof arg == "function") arg(node);
            else if (typeof arg == "string") node.innerHTML += arg;
            else node.appendChild(arg);
        }
        return node;
    },

    _event: function (node, ev, fn) {

        if (!ghh.registered) ghh.registered = [];
        ghh.registered.push({
            node: node,
            ev: ev,
            fn: fn
        });
        if (node.attachEvent) node.attachEvent("on" + ev, fn);
        else node.addEventListener(ev, fn, false);
    }
}

setTimeout(ghh.track, 10);

// BSI Cross-Domain handler
// DO NOT MODIFY

function getPersonalization() {
    var url = "http://www.for-me-online.de/brandsiteintegration.ashx?methodname=";

    $.getJSON(url + "getpersonalization&jsoncallback=?", function (data) {
        Name = data.Name;

        if (Name != "") {
            $("#welcome").html("Hallo " + Name);
            $("#logged").css("display", "inline-block");
            $("#notLogged").css("display", "none");

        }
        else {
            $("#welcome").html("");
            $("#login").html("Log-In / Registrieren");
            $("#logged").css("display", "none");
            $("#notLogged").css("display", "inline-block");
        }
    })
}
$(document).ready(function () {
    var url = "http://www.for-me-online.de/brandsiteintegration.ashx?methodname=";
    $("#logout").click(function () {

        var logoutUrl = url + "logout&jsoncallback=?";

        $.getJSON(logoutUrl, function (data) {
            var result = data.Result;

            if (result == "OK") {
                $("#welcome").html("");
                $("#login").html("Log-In / Registrieren");
                $("#logged").css("display", "none");
                $("#notLogged").css("display", "inline-block");
            }
        })

        return false;
    });


    getPersonalization();

    if (parseInt($.browser.version, 10) == 530 && $.browser.safari) {
        $("#logout").hide();
        $("#about").css("margin-left", "0px");
    }
});

// eof
