// The jQuery-cookie extension

jQuery.noConflict();
cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// Our own code
var MAX_VIEWS = 1;
var COOKIE_NAME = 'ddc_ads';

jQuery(document).ready(function() {
    // Get currentViews from cookie
    var currentViews = parseInt(cookie(COOKIE_NAME));
    if (currentViews === null || isNaN(currentViews))
        currentViews = 0;

    if (currentViews < MAX_VIEWS) {
        var elem = jQuery('<div>').attr('id', 'ad');
        elem.append(jQuery('<div>').addClass('close').html("<a href=\"#\"><img src=\"http://online-gesund-abnehmen.de/close-button.gif\" alt=\"X\" /></a>"));
        
        var link = jQuery('<a>').attr('href', 'http://online-gesund-abnehmen.de').css('clear', 'both');
        link.append(jQuery('<img>').attr('src', 'http://www.gewusst-abnehmen.de/wp-content/themes/theme1126/images/aufmacher_35.jpg').attr('border', 0));
        elem.append(link);


        jQuery('body').append(jQuery('<div>').attr('id', 'ad-overlay').css({ filter: "alpha(opacity=80)" }));

        jQuery('body').append(elem);
        jQuery('#ad .close a').click(function() { jQuery('#ad-overlay, #ad').hide(); return false; });

        var height = elem.height();
        var width = elem.width();
        var top = jQuery(window).height() / 2 - height / 2;
        var left = jQuery(window).width() / 2 - width / 2;

        var position = {
            width: width + "px",
            height: height + "px",
            top: top + "px",
            left: left + "px"
        };
        elem.css(position);
        elem.offset({ top: top + "px",
            left: left + "px" });

        jQuery('#ad-overlay, #ad').show();

        cookie(COOKIE_NAME, currentViews + 1, { path: '/' });
    }
});

