function setCookie(value) {
    document.cookie = "th_ad_cookie=" + escape(value);
}

function getCookie() {
    var dc = document.cookie;
    var prefix = "th_ad_cookie=";
    var begin = dc.indexOf("; " + prefix);

    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie() {
    if (getCookie()) {
        document.cookie = "th_ad_cookie=; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function getRandomUrl () {
    var subscript = Math.floor (Math.random () * 100) % adUrls.length;
    return adUrls[subscript];
}

function OpenAdWindow () {
    var ad = getRandomUrl ();
    adsWindow = window.open (ad, "ad", "toolbar=no,height=200,width=300,scrollbars=no,resize=no");
    if (adsWindow) adsWindow.moveTo ((screen.width - 300) / 2, (screen.height - 200) / 2);
}

function OpenWindow () {
    helmetWindow = window.open ("helmet.html", "work", "toolbar=no,height=200,width=450,scrollbars=no,resize=no");
    if (helmetWindow) helmetWindow.moveTo ((screen.width - 450) / 2, (screen.height - 200) / 2);
}

function uuidGen() {
    var guid = "";
    for (var i = 1; i <= 32; ++i) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20)) {
            guid += "-";
        }
    }
    return guid;
}

function OpenQWindow (mov) {
    var w = 620
    var h = 400
    var opts  = "resizable=yes,directories=no,left=10,top=10,toolbar=no,location=no";
    opts += ",scrbollbars=no,width=" + w + ",height=" + h;

    qtwin = window.open ('qtvr/choose4542.php?qt=' + mov, "qt", opts);
    qtwin.moveTo ((screen.width - w) / 2, (screen.height - h) / 2)
}

function OpenPWindow (url,height,width) {
    infoWindow = window.open (url,
            Math.floor (Math.random () * 100000),
            "toolbar=no,height=" + (height + 28)
            + ",width=" + (width + 20)
            + ",scrollbars=no,resize=no");
    infoWindow.moveTo ((screen.width - width) / 2, (screen.height - height) / 2);
    infoWindow.focus;
}

function Init() {
    var cook  = getCookie();

    if ((sPage == "index.php" ||
         sPage == "")          &&
        (cook  == null)) {
        uuid = uuidGen();
        setCookie(uuid);
        OpenAdWindow();
    } else {
        uuid = cook;
    }

    AttachToLinks();
    event_add_listener (document, "click", ClickTrack);
}

function ClickTrack(e) {
    var x = e.clientX + document.body.scrollLeft;
    var y = e.clientY + document.body.scrollTop;
    var w = screen.width;
    var h = screen.height;
    var d = screen.colorDepth ? screen.colorDepth : screen.pixelDepth;
    var l = (e.currentTarget.nodeName == "A" ? e.currentTarget.href : "");
    
    var url = "http://www.thrillseekers.cc/_track.php?sess=" + uuid
            + "&page=" + escape(sPage)
            + "&l=" + escape(l)
            + "&x=" + x
            + "&y=" + y
            + "&w=" + w
            + "&h=" + h
            + "&d=" + d
            + "&ua=" + escape(navigator.userAgent)
            + "&r=" + Math.random() * 500;

    try { (new Image).src = url;
    } catch (e) {}

    e.stopPropagation();
}

function AttachToLinks() {
    var links = document.getElementsByTagName("a");
    for (var i=0; i<links.length; ++i) {
        event_add_listener (links[i], "click", ClickTrack);
    }
}



/*
 * (c) 2004-2005 copyright by Jeremy Vinding
 * all rights reserved
 */
function event_W3CDOM (target) {
    this.currentTarget = target;
    this.target = (window.event.target ? window.event.target : (window.event.srcElement ? window.event.srcElement : target));
    this.preventDefault = function () { window.event.returnValue = false };
    this.stopPropagation = function () { window.event.cancelBubble = true };
    this.keyCode = window.event.keyCode;
    this.charCode = window.event.charCode;
    this.altKey = window.event.altKey;
    this.ctrlKey = window.event.ctrlKey;
    this.layerX = window.event.offsetX;
    this.layerY = window.event.offsetY;
    this.clientX = window.event.clientX;
    this.clientY = window.event.clientY;
    return this;
}

function event_add_listener (object, event, callback)
{
    if (object != null) {
        if (object.addEventListener) {  // w3c DOM
            if (event == 'keypress' && navigator.userAgent.indexOf ('Safari') >= 0){
                // workaround some wierdness in safari
                object.onkeypress = callback;
            } else {
                object.addEventListener (event, callback, false);
            }
        } else if (object.attachEvent) { // IE
            object.attachEvent ('on' + event, function () { callback (new event_W3CDOM (object)) } );
        } else { // IE5/mac for example
            var oldHandler = object['on'+event];
            object['on'+event] = function () { if (typeof oldHandler == 'function') { oldHandler(); } callback (new event_W3CDOM (object)); };
        }
    }
}
