// -*-java-*-
//
// global.js
//
// Globales Javascript für alle Seiten.
// 
// Basiseinstellungen, globale Variablen und Hilfsfunktionen für alle Skripts
// von www.sternenfall.de
//
// Copyright (C) 2005-2006.
// 
////////
// Author: Andreas Spindler
// $Writestamp: 2006-05-30 11:59:20 SpindleA$
//

//////////////////////////////////////////////////////////////////////////////
// Konfiguration.
//

var g = new Object;             // Globale Konfiguration
g['debug'] = false;

// Detect Browser type.
var is_NS = navigator.appName == "Netscape";
var is_Opera = navigator.appName == "Opera";
var is_IE = navigator.appName.indexOf("Microsoft") != -1;

if (is_NS && parseInt(navigator.appVersion)<=3) {
    is_NS = false;
}

// Browser settings.
var have_cookies = (navigator.cookieEnabled == true);
var have_java = (navigator.javaEnabled() == true);


//////////////////////////////////////////////////////////////////////////////
// Hilfsfunktionen.
//
function zufall( min, max ) {
    return (min - 1) + Math.ceil( Math.random() * max );
}

function window_size()
{
    // Innere Fensterhöhe und -breite in Pixeln bestimmen.
    //
    // Siehe http://www.quirksmode.org/viewport/compatibility.html#link2
    //
    var x,y;
    if (self.innerHeight) {         // all except Explorer
        x = self.innerWidth;
        y = self.innerHeight;
    } else if (document.documentElement && 
               document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        x = document.documentElement.clientWidth;
        y = document.documentElement.clientHeight;
    } else if (document.body) {     // other Explorers
        x = document.body.clientWidth;
        y = document.body.clientHeight;
    }
    var R = new Array(2);
    R[0] = x; R[1] = y;
    return R;
}

function scrolling_offset()
{
    // Wie weit die Seite gescrollt wurde.
    //
    // Siehe http://www.quirksmode.org/viewport/compatibility.html#link2
    //
    var x,y;
    if (self.pageYOffset) { // all except Explorer
        x = self.pageXOffset;
        y = self.pageYOffset;
    } else if (document.documentElement && 
               document.documentElement.scrollTop) { // Explorer 6 Strict Mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    } else if (document.body) { // all other Explorers
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }
    var R = new Array(2);
    R[0] = x; R[1] = y;
    return R;
}


function println(s) {
    document.write( s + "<br>" );
}

function print_setup()
{
    if( is_IE ) { println( "Internet Explorer" ); }
    else if( is_NS ) { println( "Netscape" ); }
    else if( is_Opera ) { println( "Opera" ); }
    else { println( "Unknown Browser!" ); }

    println( "Window width = " + window_size()[0] );
    println( "Window height = " +  window_size()[1] );
    if( g_mouse_pos[0] != null ) {
        println( "Mouse [x] " + g_mouse_pos[0] );
        println( "Mouse [y] " + g_mouse_pos[1] );
    }
}

//////////////////////////////////////////////////////////////////////////////
// Globale Ereignisverwaltung.
//

////////
// Hauptthread.
//
var g_kernintervall = 1000;
var g_kernthread = setInterval( "kern()", g_kernintervall );

function kernen() {
    if( g['sternenschnee'] )
        sternenschnee_beginnen();
}

function kern() {

}

function kernlos() {
    if( g_kernthread != null ) {
        clearInterval( g_kernthread );
        g_kernthread = null;
    }
}

////////
// Alle Skriptfehler verwerfen.
//
function drop_error() { return true; }
if (!g['debug']) { window.onerror = drop_error; }

////////
// Mausposition innerhalb des Fensters verfolgen.
//
var g_mouse_pos = new Array(2);

if (is_NS) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = on_mouse_move;

function on_mouse_move(e)
{
    var x,y;
    if (is_IE) {
        x = event.clientX + document.body.scrollLeft;
        y = event.clientY + document.body.scrollTop;
    } else if (is_NS) {
        x = e.pageX;
        y = e.pageY;
    } else {
        x = 0;
        y = 0;
    }
    
    // catch possible negative values in NS4
    if (x < 0) x = 0;
    if (y < 0) y = 0;

    // Die Breite/Höhe des gescrollten Bereichs hinzuzählen!
    var so = scrolling_offset();
    g_mouse_pos[0] = x + so[0];
    g_mouse_pos[1] = y + so[1];

    return true;
}

