/**
 * Common functions used by all chemgapedia pages.
 * Usually this file is imported before all other
 * javascript files
 *
 * $HeadURL: http://trac.gruen.fiz-chemie.de/svn/vs/vscms/trunk/src/vsengine/static/javascript/chemgapedia_common.js $
 * $Revision: 11135 $
 *
 * Copyright 2006 FIZ CHEMIE Berlin
 */

/** @namespace */
var Common = {};
/**
 * Global variables
 */
/** @field */
var vsiCurrentSubMenu = null;
/** @field */
var vsimenuName = null;

/**
 * @field
 * storage for all molecules of one page
 * javascript for filling this array is embedded into
 * the HTML body in the context of the object tag.
 */
var molecules = new Array();
/**
 * @field
 * storage for all applets of one page
 */
var applets = new Array();
var models = new Array();

if (typeof logger == 'undefined') {
    var logger = log4javascript.getLogger("chemgapedia_common");
    var layout = new log4javascript.PatternLayout("[%-5p] %m");
    var appender = new log4javascript.PopUpAppender(layout);
    log4javascript.setEnabled(false);
    appender.setThreshold(log4javascript.Level.DEBUG);
    logger.addAppender(appender);
} else {
    // simulate a new error in order to retieve filename and linenumber
    try {
        throw new Error("logger already installed.");
    } catch(e) {
        logger.trace(e.message + " " + e.fileName + ", " + e.lineNumber);
    }
}

/**
 * @function
 * Set the window load listener.
 *
 * @deprecated - use $(function($) {...} instead
 */
Common.addLoadEventListener = function/*void*/(/*Function*/listener) {
    // Which method is used depends a lot on the mime type.
    // In Safari window.onload does not work for mime type
    // application/xhtml+xml or text/xml
    // window.addEventListener does not work for older IE
    if (window.addEventListener) {
        window.addEventListener("load",listener,null);
    } else {
        window.onload = listener;
    }
}

/**
 * @function
 * Replace MathML with images.
 *
 * Call this in onLoad handler.
 *
 * Works with Firefox, IE, Firefox,Opera - needs lot of testing and cleanup.
 *
 * Works with Konqueror 3.4, if called directly and mime type is text/xml.
 * however the onload handler is called by Konqueror only with mime
 * type text/html (or application/xhtml+xml). In this case the mathml is
 * not available in the DOM.
 *
 * Works with Safari, if mime type is application/xhtml+xml.
 */
Common.replaceMathML = function/*void*/() {
    var nodeList = document.getElementsByTagName("math");
    if (nodeList.length == 0) {
    // Workaround for Opera 9
        nodeList = document.getElementsByTagName("m:math");
    }
    // nodeList is live, so copy it and work on the copy.
    var mathElements = new Array();
    for (var i = 0; i< nodeList.length;i++) {
        mathElements[i] = nodeList[i];
    }

    var inlineElements = new Array();
    var blockElements = new Array();
    for (var i = 0; i< mathElements.length;i++) {
        var math = mathElements[i];
        var altimg = math.getAttribute("altimg");
        if (! altimg)
            continue;
        var img = DOMUtil.createElement("img");
        //img.setAttribute("src",altimg);
        img.setAttribute("alt","Formula");

        var pair = new Object();
        pair.img = img;
        pair.math = math;
        pair.src = altimg;
        if (math.getAttribute("display") == "block") {
            img.className = "mathml-block";
            blockElements[blockElements.length] = pair;
        } else {
            img.className = "mathml-inline";
            inlineElements[inlineElements.length] = pair;
        }
    }

    // Replace inline elements first ...
    for (var i=0; i<inlineElements.length; i++) {
        var pair = inlineElements[i];
        // Replace math tag as soon and only if the image was successfully loaded.
        Common.replaceMathElement(pair);
    }

    // ... and blocktype elements later. The display flickering is
    // somewhat reduced compared to doing it in one rush.
    for (var i=0; i<blockElements.length; i++) {
        var pair = blockElements[i];
        Common.replaceMathElement(pair);
    }
}

/**
 * @function
 * Opera 9 does not fire onload events for img, if the page is reloaded.
 * So use this function instead of the closure.
 */
Common.replaceMathElement = function/*void*/(/*Object*/pair) {
    var parent = pair.math.parentNode;
    parent.replaceChild(pair.img,pair.math);
    pair.img.src = pair.src;
}

/**
 * @function
 * Link target resolver; foreign targets will open in a new browser
 * window.
 */
Common.setLinkTarget = function() /*void*/{

    // collect all relevant link elements
    var pagelinks = DOMUtil.getElementsWithClassName("a", "link_page");
    var vlulinks = DOMUtil.getElementsWithClassName("a", "link_vlu");
    var links = pagelinks.concat(vlulinks);

    if (links && links.length > 0) {
        logger.trace("Common.setLinkTarget: " + links.length);

        var current_location = document.location.pathname;
        var vlu_regexp = Properties.vlu_expr;
        var ext_regexp = Properties.ext_expr;
        var result = vlu_regexp.exec(current_location);
        var current_vlu = (vlu_regexp.exec(current_location) != null) ? vlu_regexp.exec(current_location)[1] : null;
        var supplement = ext_regexp.exec(current_location);
        var is_supplement = (ext_regexp.exec(current_location) != null);

        for (var i=0; i<links.length; i++) {
            if (is_supplement) {
                links[i].target = "_blank";
            } else {
                var current_href = links[i].href;
                var current_target = links[i].target;
                var target_vlu = (vlu_regexp.exec(current_href) != null) ? vlu_regexp.exec(current_href)[1] : null;
                var is_foreign = (target_vlu != current_vlu) ? true : false;

                if (is_foreign) {
                    links[i].target = "_blank";
                }
            }
        }
    }
}

Common.activateModels = function() /*void*/{
    if (models && models.length > 0) {
        for (var i=0; i < models.length; i++) {
            var model = models[i];
            model.bindHandler(i);
        }
    }
 }

/**
 * @function
 * insert molecules depending on browser support
 */
Common.activateMolecules = function() /*void*/{
    var delay = 0;
    if (molecules && molecules.length > 0) {
//        debugger;
        logger.debug("Common.activateMolecules: [" + molecules.length + "]");
        for (var i=0; i<molecules.length;i++) {
            var molecule = molecules[i];
            if (molecule.scope == 'reaction3d') {
                molecule.writeHTML();
            } else {
//                Common.activateTimed(molecule, delay);
                molecule.writeHTML();
//                molecule.bindHandler(i);
            }
            delay += 2000;
        }
    }
};

Common.activateTimed = function(molecule, delay) {
    window.setTimeout(function() {
        var _i = i;
        var _m = molecule;
        var _id = molecule.id;
        _m.markactive();
        _m.writeHTML();
    }, delay)
}
/**
 * @function
 * activate a single molecule
 */
Common.activateMolecule = function(/*String*/id) /*boolean*/{
    var _id = id;
    for (var i=0; i<molecules.length;i++) {
        var molecule = molecules[i];
        if (_id == molecule.id) {
            $("#overlay-" + _id).remove();
            $("#" + _id).css({
                "margin-bottom": "0px"
            });
            $("#img-" + _id).replaceWith(molecule.replacement);

            logger.debug("activate applet");
            $("#load" + _id).fadeOut(1000, function() {
                molecule.writeHTML();
            });
            return true;
            break;
        }
    }
    return false;
}

Common.scriptCheckMolecule = function(/*String*/id, /*String*/script) /*void*/{
    var _div_id = id;
    var _applet_id = "applet-" + _div_id;

    for (var i=0; i<molecules.length;i++) {
        var molecule = molecules[i];
        if (_div_id == molecule.id) {
            var _applet = $("#" + _applet_id);
            var _script = $("#script" + _div_id);
            if (_applet.length == 1) {
                var check;

                if ( _applet[0].isActive()) {
                    check = _applet[0].scriptCheck(script);
                } else {
                    logger.error("applet not active");
                }
                if (check != "") {
                    check = check.substring(0, check.length - 2);
                    check = check.replace(/</g,"&lt;");
                    check = check.replace(/>/g,"&gt;");
                    check = check.replace(/\n/g,"<br />");
                    var msg = '<div id="scripterror" style="text-align: left; font-family: monospace;">' + check + '</div>';
                    $.blockUI(
                        {
                            fadeIn: 250,
                            fadeOut: 250,
                            message: msg,
                            css: {
                                border: 'ridge 3px #FFCC00',
                                padding: '10px',
                                width: '480px',
                                top: '20px',
                                left: '20px'
                            }
                        }
                    );
                    $('html').click(function() {
                        $.unblockUI();
                    });

                } else {
                    Control.execute(id, script);
                }
                return false;
            } else {
                $("#overlay-" + _div_id).remove();
                $("#" + _div_id).css({
                    "margin-bottom": "0px"
                });
                $("#img-" + _div_id).replaceWith(molecule.replacement);

                logger.debug("activate applet");
                $("#load" + _div_id).remove();
                molecule.writeHTML();
                Common.scriptCheckMolecule(_div_id);
            }
            break;
        }
    }
    return false;
}
/**
 * @function
 * insert applets depending on browser support
 */
Common.activateApplets = function() /*void*/{
    if (applets && applets.length > 0) {
        for (var i=0; i<applets.length; i++) {
            var applet = applets[i];
            applet.writeHTML();
        }
    }
}

/**
 * @function
 * Remove additional mspace for Mozilla
 */
Common.mspaceFilter = function() /*void*/{
    if (browser.id.MOZ) {
        var mspaces = DOMUtil.getElementsWithClassName("mspace", "unit", "http://www.w3.org/1998/Math/MathML");
        for (var i=0; i<mspaces.length; i++) {
            var currentNode = mspaces[i];
            var currentParent = currentNode.parentNode;
            currentParent.removeChild(currentNode);
        }
    }
}

/**
 * @function
 * Navigation properties that need to be set by Javascript
 * since CSS width and height properties fail for absolute
 * positioned page contents.
 * Register this function for load and resize events on window.
 * Only needed for full windows with navigation.
 */
Common.navAdjust = function() /*void*/{

    var mainMenu = document.getElementById('vsimenu');
    if (! mainMenu) {
        logger.trace("Page has no navigation menu");
        return;
    } else {
        logger.trace("Page with navigation menu");
    }

    title = document.getElementById('vsi-main-title') ? document.getElementById('vsi-main-title') : undefined;
    nav  = document.getElementById('vsinavigation');
    menu = document.getElementById('vsimenu');

    if (title) {
        title.style.width = (Common.getInsideWindowWidth() - 250)+'px';
    }
    var hasLinkBlock = Boolean(document.getElementById('google-linkblock'));
    var iconHeight = hasLinkBlock ? 300 : 190;
    if (nav) {
        nav.style.height = Math.max(170,(Common.getInsideWindowHeight() - iconHeight))+'px';
    }

    // set menu width. mozilla bug in scrollbar
    if (menu) {
        menu.style.width = (Common.getInsideWindowWidth() - 220)+'px';
    }
}
/**
 * @function
 * swap images linked by an imagemap
 * inserted in control.xsl, object.xsl
 */
Common.changeObj = function(/*String*/image, /*String*/replacement) /*void*/{
    var obj = document.getElementById(image);
    obj.src = Properties.media + replacement;
}


/**
 * helper function for handleMenu
 */
function hideSubMenu() /*void*/{
    return; // currently not working :-/
    var msg = '';
    var EventObj       = typeof(obj.id) != 'undefined' ? obj.id.indexOf('vsi') : -1;
    var EventObjParent = (typeof(obj.parentNode) != 'undefined') ? ((typeof(obj.parentNode.id) != 'undefined') ? obj.parentNode.id.indexOf('vsi') : -1) : -1;
    if (EventObj == -1 && EventObjParent == -1) {
        if (vsiCurrentSubMenu != null) {
            document.getElementById(vsiCurrentSubMenu).style.display = "none";
            vsiCurrentSubMenu = null;
        }
        return;
    }

    if (vsiCurrentSubMenu != null) {
        msg +='hideSubMenu invoced\nvsiCurrentSubMenu: '+vsiCurrentSubMenu+'\nvsimenuName: '+vsimenuName;
        if( vsimenuName != null && vsiCurrentSubMenu == vsimenuName ) {
            msg+='\nleave var vsiCurrentSubMenu AS IS:'+vsiCurrentSubMenu+'\nvsimenuName: '+vsimenuName;
            //      return;
        }
        else {
            document.getElementById(vsiCurrentSubMenu).style.display = "none";
            vsiCurrentSubMenu = null;
            msg+='\nhide vsiCurrentSubMenu\nvsimenuName: '+vsimenuName;
            //      return;
        }
    }
    else {
        msg+='\nhideSubMenu invoced\ndo nothing';
    }
}

/**
 * event handler function for the top menu
 */
function handleEVENT(/*Event*/e) /*void*/{
    if (!e) {
        var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    }
    logger.trace("Common.handleEvent: " + typeof(e) + " - " + e);
    if (e.target) {
        obj = e.target;
    } else if (e.srcElement) {
        obj = e.srcElement;
    }
    var rightclick;
    if (e.which) {
        rightclick = (e.which == 3);
    } else if (e.button) {
        rightclick = (e.button == 2);
    }

    if (rightclick) {
        return;
    }
    hideSubMenu();
    //    workaround for Mozilla 1.0.2
    if (obj.nodeType == '3') obj = obj.parentNode;
    if (obj.className == 'vsi-show-views') handleViews(obj,e);

    if (obj.parentNode == null) {
        return;
    }
    if (typeof(obj.parentNode.id) != 'undefined') {
        if (obj.id == 'vsi-admin') {
            hideSubMenu();
            toggleCSS('vsi-admin-link');
        } else if (obj.parentNode.id == 'vsifile') {
            handleMenu(obj);
        } else if (obj.parentNode.id == 'vsidirectory') {
            handleMenu(obj);
        } else if (obj.parentNode.id == 'vsisearch') {
            handleMenu(obj);
        } else if (obj.parentNode.id == 'vsitools') {
            handleMenu(obj);
        } else if (obj.parentNode.id == 'vsisearchsubmenu') {

        }
    }
}

/**
 * submenu interaction for the authoring tools
 */
function handleMenu(/*Object*/obj, e) /*void*/{
    var vsiSubmenu  = 'submenu';
    vsimenuName = obj.parentNode.id + vsiSubmenu;
    logger.trace("Common.handleMenu: " + vsimenuName + " - " + typeof(obj));
    var vsimenuObj  = document.getElementById(vsimenuName);
    var objTop      = ((DOMUtil.getElementStyle(obj.parentNode,'top','top'))!='auto') ? (DOMUtil.getElementStyle(obj.parentNode,'top','top')) : (getElementPosition(obj.parentNode).top +'px');
    var objLeft     = ((DOMUtil.getElementStyle(obj.parentNode,'left','left'))!='auto') ? (DOMUtil.getElementStyle(obj.parentNode,'left','left')) : (getElementPosition(obj.parentNode).left +'px');
    var objWidth    = ((DOMUtil.getElementStyle(obj,'width','width'))!='auto') ? (DOMUtil.getElementStyle(obj,'width','width')) : (document.getElementById(obj.id).style.width +'px');
    var MenuWidth   = vsimenuObj.style.width == '' ? DOMUtil.getElementStyle(vsimenuObj,'width','width') : vsimenuObj.style.width;
    var elementStyle = vsimenuObj.style.display == '' ? DOMUtil.getElementStyle(vsimenuObj,'display','display') : vsimenuObj.style.display;
    // schneller Hack fuer den lieben IE
    //  objLeft = document.all ? parseInt(objLeft) - 200 : parseInt(objLeft);
    var cWidth = Common.getInsideWindowWidth() - 200;

    var gLeft = parseInt(objLeft) + parseInt(MenuWidth);

    var mDiff = (parseInt(MenuWidth) - parseInt(objWidth));
    var mLeft = (parseInt(objLeft) - parseInt(mDiff));
    //  alert('objLeft: '+objLeft+'\nobjWidth: '+objWidth+'\nmDiff: '+mDiff+'\nmLeft: '+mLeft+'\ncWidth: '+cWidth+'\nMenuWidth: '+MenuWidth+'\nCommon.getInsideWindowWidth: '+Common.getInsideWindowWidth());return;
    objTop = parseInt(objTop)+ 26 +'px';
    vsimenuObj.style.top  = objTop;
    if (cWidth < gLeft) {
        vsimenuObj.style.left = document.all ? mLeft - 200 +'px' : mLeft +'px';
        vsimenuObj.style.right = '';
    } else {
        vsimenuObj.style.left = objLeft;
    }

    //  menu.style.width = (Common.getInsideWindowWidth() - 220)+'px';
    objParent   = obj.parentNode;
    objParentId = obj.parentNode.id;
    //  alert('vsiCurrentSubMenu: '+vsiCurrentSubMenu+'\nvsimenuName: '+vsimenuName);
    if ( vsiCurrentSubMenu != null ) {
        if ( vsiCurrentSubMenu != vsimenuName ) {
            //alert('hideSubMenu from handleMenu');
            hideSubMenu();
            vsimenuObj.style.display = "block";
            vsiCurrentSubMenu = vsimenuName;
        }
        if ( vsiCurrentSubMenu == vsimenuName ) {
            if ( elementStyle == "none") {
                // alert('1'+vsiCurrentSubMenu + vsimenuName);
                vsimenuObj.style.display = "block";
                vsiCurrentSubMenu = vsimenuName;
            } else {
                //alert('2'+vsiCurrentSubMenu + vsimenuName);
                vsimenuObj.style.display = "none";
                vsiCurrentSubMenu = null;
            }
        }
    } else {
        vsimenuObj.style.display = "block";
        vsiCurrentSubMenu = vsimenuName;
    }
}

/**
 * @function
 * Return the available content width space in browser window
 */
Common.getInsideWindowWidth = function() /*Integer*/{
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (((document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false)) {
            // measure the html element's clientWidth
        return document.body.parentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    }
    return 0;
}

/**
 * @function
 * Return the available content height space in browser window
 */
Common.getInsideWindowHeight = function() /*Integer*/{
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (((document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false)) {
        // measure the html element's clientHeight
        return document.body.parentElement.clientHeight
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
    return 0;
}

/**
 * @function
 *
 */
Common.addEventSimple = function(obj,evt,fn) {
    if (obj.addEventListener)
        obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent)
        obj.attachEvent('on'+evt,fn);
}

/**
 * @function
 *
 */
Common.removeEventSimple = function(obj,evt,fn) {
    if (obj.removeEventListener)
        obj.removeEventListener(evt,fn,false);
    else if (obj.detachEvent)
        obj.detachEvent('on'+evt,fn);
}

/**
 * @function
 *
 */
Common.getEventTarget = function(e) {
    var target;
    var event;

    if (!e) {
        event = window.event;
    } else {
        event = e;
    }

    if (event.target) {
        target = event.target;
    } else if (event.srcElement) {
        target = event.srcElement;
    } else {
        alert("Error in event.target determination.");
    }
    // if target is a text node return its parent node as target
    if (target.nodeType == 3) {
        target = target.parentNode;
    }

    return target;
}
/**
 * @namespace PDF related stuff
 */
Common.PDF = {};
/**
 * store PDF error status information
 * @field
 * @type boolean
 */
Common.PDF.errors = "";
/**
 * stored status messages from a PDF
 * @field
 */
Common.PDF.status_messages = "";
/**
 * PDF-Status einer Seite oder einer Lerneinheit.
 * Das zugehoerige PDF zu einer Seite oder einer Lerneinheit wird
 * per Ajax geladen. Sind keine Fehler vorhanden, wechselt die Farbe
 * des Status-Icons nach Blau (#0000FF), sonst nach Rot (#FF0000)
 *
 * @function PDF-Status einer Seite oder einer Lerneinheit.
 */
Common.PDF.getStatus = function() {
    var targets = DOMUtil.getElementsWithClassName("span", "pdf-status");
    if (targets.length == 0) return; // keine Statusanzeige auf Seiten, die dafuer nicht vorgesehen sind
    var target = targets[0];
    logger.trace("productid: " + target.innerHTML);
    logger.trace("objectid: " + target.title);
    var status_url = "/vsengine/pdf/status"
        + target.title + ".html";
    var status;
    var status_img;
    var xmlHttp = DOMUtil.createXMLHttpRequest();;
    if (xmlHttp) {
        xmlHttp.open('GET', status_url, true);
        xmlHttp.onreadystatechange = function() {
            switch (xmlHttp.readyState) {
            case 2:
                logger.trace("readyState 2");
                status_img = DOMUtil.createElement("img");
                status_img.src = "/vsengine/images/loading.gif";
                status_img.setAttribute("class", "pdf-status");
                status_img.alt = "loading";
                target.parentNode.appendChild(status_img);
                break;
            case 4:
                logger.trace("readyState 4");
                if (xmlHttp.status != 200) {
                    target.style.color = "red";
                    status_img.src = "/vsengine/images/pdf-error.gif";
                    status_img.setAttribute("title", "PDF has errors");
                } else {
                    Common.PDF.status_messages = xmlHttp.responseXML.documentElement;
                    Common.PDF.errors = (Common.PDF.status_messages.attributes[1].value == 'true')
                    logger.trace("status complete " + Common.PDF.errors + " [" + typeof(Common.PDF.status_messages) + "]");
                    if (Common.PDF.errors) {
                        target.style.color = "red";
                        status_img.src = "/vsengine/images/pdf-error.gif";
                        status_img.setAttribute("title", "PDF has errors");
                    } else {
                        target.style.color = "blue";
                        status_img.src = "/vsengine/images/pdf-ok.gif";
                        status_img.setAttribute("title", "No errors");
                    }
                }
                break;
            default:
                break;
            }
        };
        xmlHttp.send(null);
    }
}

/**
 * ResourceBundles for Plugins
 */
Common.Plugins = {};
Common.Plugins.Flash = {
        "msg": { "en": "Adobe Flash Player required!",
                 "de": "Adobe Flash Player erforderlich!"},
        "download": "http://www.adobe.com/products/flashplayer/"
};
Common.Plugins.Java = {
        "msg": { "en": "Java Plugin required!",
                 "de": "Java Plugin erforderlich!"},
        "download": "http://www.java.com/de/download/"
};

Common.LimeSurvey = function() {
    /**
     * show survey popup and create/check survey-cookie
     */
    if (document.URL.indexOf("chemgapedia") != -1) {
        $(function($) {
            logger.debug("popup block start");

            //Insert Survey-Links in vsimenu
            var vsimenu = document.getElementById("vsimenu");
            var span = document.createElement("span");
            var a = document.createElement("a");
            var img = document.createElement("img");
            var umfrage = document.createTextNode(" Umfrage");
            span.style.cssText = "position: relative; top: 12px; padding: 6px 12px 0px 12px; border-left: solid 1px #FFCE00;";
            img.setAttribute("src", "/vsengine/images/chemgaroo/survey-cross.gif");
            a.setAttribute("href", "/limesurvey/index.php?sid=98736?=de");
            a.style.cssText = "font-size: 1.1em; color:rgb(0,139,208);";
            a.setAttribute("target", "_blank");
            a.appendChild(img);
            a.appendChild(umfrage);
            span.appendChild(a);
            vsimenu.appendChild(span);

            if (navigator.cookieEnabled == true) {
                logger.trace("cookie and url test passed");
                var a = document.cookie;
                if (a.indexOf("survey=false") != -1) {
                    logger.trace("Umfrage Popup wird nicht eingeblendet");
                } else {
                    var msg = '<h1 style="margin: 0 0 10px 0; padding: 4px 10px;">ChemgaPedia-Umfrage</h1>';
                    msg += '<div style="padding: 10px;"><p>Liebe ChemgaPedia-Nutzerin,<br />';
                    msg += 'lieber ChemgaPedia-Nutzer,</p><br />';
                    msg += '<p>das FIZ CHEMIE Berlin ist st&#228;ndig bem&#252;ht, seine Produkte und' +
                            ' Angebote zu verbessern und an die Bed&#252;rfnisse seiner Kunden und' +
                            ' Nutzer anzupassen.';
                    msg += ' Dies bedarf nat&#252;rlich Ihrer R&#252;ckmeldung. Deshalb w&#252;rden wir' +
                            ' uns freuen, wenn Sie die folgenden Fragen zur ChemgaPedia und der' +
                            ' Nutzung dieses Angebotes beantworten.</p>';
                    msg += '<p>Die Umfrage umfasst maximal 16 Fragen (Bearbeitungsdauer ca. f&#252;nf Minuten).</p>';
                    msg += '<p>Ist dies Ihr erster Besuch, f&#252;hren Sie die Umfrage bitte zu einem sp&#228;teren' +
                            ' Zeitpunkt durch. Links zur Umfrage befinden sich am oberen' +
                            ' Seitenrand der ChemgaPedia.</p>';
                    msg += '<p>Vielen Dank f&#252;r Ihr Interesse an der ChemgaPedia,</p>';
                    msg += '<p>Ihre ChemgaCrew</p>';
                    msg += '<br />';
                    msg += '<div><input type="button" id="yes" value="An der Umfrage teilnehmen" />' +
                            '<input type="button" id="no" value="ChemgaPedia nutzen" /></div></div>';

                    //document.cookie = 'survey=false; expires=Thu, 30 Dec 2009 23:59:00 UTC; path=/vsengine; domain=thodin.darkzone.fiz-chemie.de';
                    document.cookie = 'survey=false; expires=Thu, 30 Dec 2009 23:59:00 UTC; path=/vsengine; domain=chemgapedia.de';
                    logger.trace("Umfrage wird eingeblendet");

                    $(document).ready(function() {

                        $.blockUI({fadeIn: 1000, fadeOut: 1000, message: msg, css: {border: 'ridge 3px #FFCC00', width: '480px', top: '1%', left: '1%'} });
                    });

                    var yes = 'http://www.chemgacourse.de/ecourses/counter/yes.php';
                    var no = 'http://www.chemgacourse.de/ecourses/counter/no.php';

                    var legal = document.getElementById("legal");
                    var img = document.createElement("img");
                    img.setAttribute("width", "0px");
                    img.setAttribute("height", "0px");
                    img.style.visible = 'none';

                    $('#yes').click(function() {
                        // update the block message
                        $.unblockUI();
                        img.setAttribute("src", yes);
                        legal.appendChild(img);
                        window.location.href = '/limesurvey/index.php?sid=98736?=de';
                    });

                    $('#no').click(function() {
                        img.setAttribute("src", no);
                        legal.appendChild(img);
                        $.unblockUI();
                    });
                }
            } else {
                logger.trace("cookie and url test missed");
            }
        });
    }
};