if (!window.console) { window.console = {log: function() { }}; }
/**
 * @author United Internet AG
 * @date 25.06.2010
 * @version 0.1untested
 * @revision $Id$
 * @originator jwiemers
 * @maintainer jwiemers
 */

(function() {

        MakeMyHomepage = function(options) {

            this.cookieClicks       = null;
            this.cookieHide         = null;
            this.topperDiv          = null;
            this.show               = true;
            this.layerOpen          = false;
            this.clicks             = null;

            this.options = {
                trackingNie     : '',
                trackingNein    : '',
                trackingJa      : '',
                trackingAnzeige : '',
                brand           : 'WEB.DE',
                homepage        : 'http://suche.web.de/search/starthp/?src=se_startpage_',
                message         : 'So einfach geht\'s',
                text1           : '&Ouml;fter hier?',
                text2           : 'Dann WEB.DE &ndash; Suche als Startseite setzen.',
                text3           : 'Jetzt einrichten',
                timeout         : 1000
                }
            
                for (var i in mmhOptions) {
                    if (!mmhOptions.hasOwnProperty(i)) { continue; }
                    this.options[i] = mmhOptions[i];
                }

            return this;
        }

    MakeMyHomepage.prototype = {

        showMessage: function(){

            if(!document.getElementById('ff-message')) {

                var trackingPx3 = new Image(); trackingPx3.src = this.options.trackingJa.replace('[Counter_aus_Cookie]',this.getCookieValues('clicks'));

                var b = navigator.userAgent.toLowerCase();
                if(navigator.userAgent.match(/firefox/i)) {
                    var dialog       = document.createElement('div');
                    var body         = document.getElementsByTagName('body')[0];
                    body.insertBefore(dialog, body.firstChild);
                    
                    var message = this.options.message.replace('{homepage}', this.options.homepage+'ff');
                    message = message.replace('src="images/homebutton_idle_small.png"', 'src="//img.ui-portal.de/search/img/mmh/images/homebutton_idle_small.png"');
                    dialog.innerHTML ='<div id="ff-message" class="ff5-notification"><div id="cursor"><img src="//img.ui-portal.de/search/img/mmh/images/homebutton_focused_small.png" /></div><div class="ff5-notification-panel"><div class="ff5-notification-content">'+message+'<span id="ff5-notification-close" class="ff5-notification-close" title="Diese Nachricht schlie&szlig;en">x</span></div></div><style type="text/css">/*<![CDATA[*/.ff5-notification{position:fixed;top:50px;left:23px;font-family:Arial, Verdana, Helvetica, sans-serif;background:url(//img.ui-portal.de/search/img/mmh/images/statusleiste_ff_bg.png) repeat-x 0 0;font-size:12px;width:310px;font-weight:400;overflow:visible;z-index:99999;border:1px solid #000000;-moz-border-radius:8px;padding:4px;-moz-box-shadow:#333333 0 0 3px,inset rgba(255,255,255,0.8) 0 0 1px;}.ff5-notification-panel {border:1px solid #43494C;overflow:hidden;position:relative;margin:auto 0;-moz-border-radius:2px;-moz-box-shadow:rgba(255,255,255,0.8) 0 0 1px;background:#FAFAFA;} .ff5-notification-content {line-height:15px;padding:10px 20px 10px 20px;overflow:hidden;text-align:left;} .ff5-notification-close {position:absolute;top:13px;right:13px;width:0px;height:0px;padding:5px;background:url(//img.ui-portal.de/search/img/mmh/images/statusleiste_ff_ico_schliessen.gif) no-repeat 50% 50%;overflow:hidden;cursor:pointer;} .ff5-notification-content ul { list-style: decimal; margin: 10px 0px 0px 25px; } .ff5-notification-content ul li { margin: 5px 0 5px; }/*]]>*/</style>';

                    var h = document.getElementById('ff5-notification-close');
                    h.addEventListener('click', (function(c) { return function() { c.hideMessage('close'); return false; } })(this) ,true);

                    if (/Firefox.(\d+)/.test(navigator.userAgent) && +RegExp.$1 >= 4) {
                        var f = document.getElementById('ff-message');
                        f.style.right = '20px';
                        f.style.left  = 'auto';
                    }
                    else {
                        document.getElementById('cursor').style.display = 'none';
                    }

                    var aClass = window.setTimeout(function(){ var m = document.getElementById('ff-message'); m.className += ' animate'; }, 150);
                    this.setCookieValues('nomore', this.clicks);
                }

            }
        },

        hideMessage: function() {
            var m = document.getElementById('ff-message');
            if(m) { var x = m.parentNode.parentNode.removeChild(m.parentNode) };
        },

        close: function(cookie){
            this.slide(false);
            if(cookie == 'nomore') { var trackingPx1 = new Image(); trackingPx1.src = this.options.trackingNie.replace('[Counter_aus_Cookie]',this.getCookieValues('clicks')); }
            if(cookie == 'close')  { var trackingPx2 = new Image(); trackingPx2.src = this.options.trackingNein.replace('[Counter_aus_Cookie]',this.getCookieValues('clicks')); }
            this.setCookieValues(cookie, this.getCookieValues('clicks'));
            this.hideMessage();
        },

        slide: function(direction) {

            var showInterval = window.setInterval(function(){
                var elm = document.getElementById('topper');

                var pos = elm.style.height;
                pos     = pos.replace('px', '');

                if(direction) {
                    if(pos <= 35) {
                        pos++;
                        elm.style.height = pos+'px';
                    }
                    if(pos == 35) { window.clearInterval(showInterval); }
                }
                else {
                    if(pos >= -1) {
                        pos--;
                        elm.style.height = pos+'px';
                    }
                    if(pos == 0) { window.clearInterval(showInterval); }
                }
            } ,10);
        },

        getCookie: function(name, defaultValue) {
            var re = new RegExp('(^|;\\s*)'+name+'=([^;]+)');
            var match = document.cookie.match(re)
            if (match) {
                return match[2];
            } else {
                return defaultValue;
            }
        },

        setCookie: function(name, value, expires) {
            document.cookie = name+'='+value+' ;expires='+expires.toGMTString();
        },

        setCookieValues: function (cookie, clicks) {

            var expires, lifetime, maxlifetime;
            maxlifetime = new Date();
            time        = new Date();

            e = maxlifetime.getTime()+(730 * 24 * 60 * 60 * 1000)
            maxlifetime.setTime(e);

            if (cookie == 'nomore') { expires = e; }
            if (cookie == 'close')  { expires = time.getTime()+(2 * 24 * 60 * 60 * 1000); }
            if (cookie == '3days')  { expires = time.getTime()+(3 * 24 * 60 * 60 * 1000); }
            if (cookie == 'now')    { expires = 0; }
            //if (cookie == 'close')  { expires = time.getTime() }
            if(clicks == undefined) { clicks = 1 }

            var cookievalue = expires+'|'+clicks.toString();
            this.setCookie(this.options.brand+'.mmh', cookievalue, maxlifetime);

        },

        getCookieValues: function(get){
            var cookieValue = this.getCookie(this.options.brand+'.mmh');
            if(cookieValue != undefined) {
                var cSplit = cookieValue.split('|');
                if(get == 'hideit') { return cSplit[0]; }
                if(get == 'clicks') { return cSplit[1]; }
            }
            else {
                return 0;
            }
        },


        fill: function() {

            var content         = document.createElement('div');
            content             = this.topperDiv.appendChild(content);
            content.className   = 'topper-content topper-sprite';

            var message         = document.createElement('div');
            message             = content.appendChild(message);
            message.className   = 'message';

            var teaser          = document.createElement('div');
            teaser              = message.appendChild(teaser);
            teaser.className    = 'teaser';

            if((navigator.userAgent.match(/msie 9/i)) || (navigator.userAgent.match(/msie 8/i))) {
                teaser.onclick  = (function(c) { return function() {
                                        this.style.behavior='url(#default#homepage)';
                                        this.setHomePage(c.options.homepage+'ie');
                                        var trackingPx3 = new Image(); trackingPx3.src = c.options.trackingJa.replace('[Counter_aus_Cookie]',c.getCookieValues('clicks'));
                                        c.close('nomore');
                                        return false;
                                    } })(this);
                }
            else {
                teaser.onclick  =(function(c) { return function() { c.showMessage(); return false; } })(this);
            }

            teaser.href         = '#';
            teaser.innerHTML    = this.options.text1+' <strong>'+this.options.text2+'</strong>&nbsp;<a href="#">'+this.options.text3+'</a>';

            var closeTopper     = document.createElement('div');
            closeTopper         = message.appendChild(closeTopper);
            closeTopper.className = 'topper-close';

            var closeText       = document.createElement('a');
            closeText           = closeTopper.appendChild(closeText);
            closeText.innerHTML = 'Nicht mehr anzeigen';
            closeText.className = 'close';
            closeText.onclick   = (function(c) { return function() { c.close('nomore'); return false; } })(this);

            var closeIcon       = document.createElement('a');
            closeIcon           = closeTopper.appendChild(closeIcon);
            closeIcon.innerHTML = 'schlie&szlig;en';
            closeIcon.className = 'close-icon topper-sprite';
            closeIcon.onclick   = (function(c) { return function() { c.close('close'); return false; } })(this);

        },

        createWrapper: function(){

            this.topperDiv                  = document.createElement('div');
            this.topperDiv.id               = 'topper';
            this.topperDiv.style.height     = '0px';
            this.topperDiv.style.overflow   = 'hidden';
            this.topperDiv.style.position   = 'relative';
            var body                        = document.getElementsByTagName('body')[0];
            body.insertBefore(this.topperDiv, body.firstChild);

        },

        init: function(){
        
            var b = navigator.userAgent.toLowerCase();
            if(navigator.userAgent.match(/firefox/i) || /MSIE.(\d+)/.test(navigator.userAgent) && +RegExp.$1 >= 8) {
                
                var time    = new Date();
                var hide    = this.getCookieValues('hideit');
                this.clicks = this.getCookieValues('clicks');
                
                if(this.getCookie(this.options.brand+'.mmh') == undefined) {
                    this.setCookieValues('now', 0);
                }
                
                if(hide <= time.getTime().toString()) {
                
                    var body = document.getElementsByTagName('body')[0];
                    body.className += this.options.brand;
                    body.style.overflowY = 'scroll';
                
                    this.clicks++;
                        
                    // Bei mehr als drei Anzeigen ohne Reaktion für 3 Tage ausblenden
                    if(this.clicks >= 3) {
                        this.setCookieValues('3days', this.clicks);
                    }
                    else {
                        this.setCookieValues('now', this.clicks);
                    }
                        
                    // Tracking Pixel für Anzeige
                    var impressionPx = new Image(); impressionPx.src = this.options.impressionPx.replace('[Counter_aus_Cookie]',this.getCookieValues('clicks'));
                        
                    // Topper Rendern
                    this.createWrapper();
                    this.fill();
                    window.setTimeout((function(c) { return function() { c.slide(true); return false; } })(this), this.options.timeout);
                }
            } 

        }

    };
    if(window.mmhOptions) { makeMyHomepage = new MakeMyHomepage(); makeMyHomepage.init(); window.mmhOptions = ''; }
})();

