var pu = new popUnder( 'tube1', 'http://www.applegap.com' );

function popUnder( ident, url ) {

    this.ident = ident;
    this.url = url;
    this.window = null;
    this.windowProps = "toolbar=1,location=1,directories=0,status=1,menubar=1,width=990,height=600,scrollbars=1,resizable=1,top=0,left=0";

    this.cookies = new CookieManager( null, '/', 0 );

    this.setup = function() {
        if( !this.cookies.get(this.ident) ) {
            var that = this;
            document.onclick = function () { that.pop(); }
        }
    }

    this.pop = function() {
        if( !this.window ) {
            this.window = window.open(
                this.url, this.ident,
                "scrollbars=1,status=1,toolbar=1,resizable=1,menubar=1,location=1,top=0,left=0,width="
                    + screen.availWidth
                    + ",height="
                    + screen.availHeight
            );
            if( this.window ) {
                this.cookies.set( this.ident, 1 );
            }
        }
        if( !this.window ) {
            if( window.Event ) {
                document.captureEvents(Event.CLICK);
            }
            var that = this;
            document.onclick = function () { that.pop(); }
        }
        self.focus();
    }

    this.setup();
}

function CookieManager( domain, path, days ) {
    this.settings = {
        domain: domain,
        path: path,
        days: days
    }

    this.set = function( name, value ) {
        var date = new Date();
        date.setTime( date.getTime() + this.settings.days * 2 * 60 * 60 * 1000 );
        var time = date.toGMTString();

        document.cookie = (
            name + '='
            + encodeURIComponent(value)
            + ( this.settings.domain ? '; domain=' + this.settings.domain : '' )
            + ( this.settings.path ? '; path=' + this.settings.path : '' )
            + ( this.settings.days ?'; expires=' + time : '' )
        );
    }

    this.get = function( name ) {
        var value = document.cookie.match('(?:^|;)\\s*' + name + '=([^;]*)');
        return value ? decodeURIComponent(value[1]) : false;
    }
}
