var splashActive = true;
var wmtt = null;
var libLoaded = false;
var startRequested = false;
var staticlib = true;
var theLib = '';

function starter (){
    // debugger;
//    display_splash ();
//    if (staticlib){
//        libLoaded = true;
//    }
//    if (debugging){
//        libLoaded = true;
//        //splashActive = false;
//    // nothing
//    }
//    if (! staticlib){
//        // debugger;
//        load_lib ("../ext3.1/ext-all.js", ext_loaded);
//    }
//
//    if (startRequested){
//        real_start ();
//    }
//
//    function ext_loaded (){
//        // alert ('ext-all')
//        load_lib ("tlib.js", lib_loaded);
//    }

    function lib_loaded (){
        // alert ('lib');
        var f = document.createElement('script');
        f.setAttribute ("type","text/javascript");
        f.text = theLib;
        var ptr = document.getElementsByTagName("head");
        ptr [0].appendChild(f);
        libLoaded = true;
        // debugger;
        if (startRequested){
            real_start ();
        }
    }
}

function tuba_load (){
    if (libLoaded){
        real_start ();
    }
    else {
        startRequested = true;
        var tmp = document.getElementById("splash_0"); 
        tmp.style.display = "block";
    }
// real_start ();
}

function real_start (){
    // debugger;
    cm.writeCookie (tco, 'active', 10000) // etwa eine Woche ..
    tuba_start ();
}

function display_splash (){
    var msg, t_short, t_shortm, t_splash, isHome = gup ('home').length > 0 || gup ('journal').length > 0; 
    var nosplash = false;
    var cm = new cm_class ();
    var img = 'background-image:url(icons/bg_teaser.jpg); background-repeat:no-repeat;';
    // var tubaimg = 'background-image:url(icons/.jpg); background-repeat:no-repeat;';
    var font = 'font-family: arial, helvetica, sans-serif; font-size: 11px;';

    if (locale () == 'de'){
        if (isHome){
            t_shortm = 'einen Moment bitte - es geht gleich weiter'
        }
        else {
            t_shortm = 'einen Moment bitte - Die Tuba Seite wird geladen ... '
            t_splash = teaser_info ('de')
        }
    }
    else {
        if (isHome){
            t_shortm = 'wait some seconds - the page will appear immediately'
        }
        else {
            t_shortm = 'just a moment ... the tuba will start in a few seconds '
            t_splash = teaser_info ('de');  // später: en !!!
        }
    }
    t_short =
    '<span><div id="starter" style="' + img + font +'"><div id="LPidle" style="position:absolute; top:100px; left: 150px;">' +
    t_shortm +
    '<div style="position: relative;left:50px;top:50px"><img src="icons/wait2.gif"/></div></div></div></span>';

    splashActive = ! (isHome || nosplash);
    var tco = 'tubasplash';
    if (cm.readCookie (tco) == 'active'){
        splashActive = false;
    }

    if (splashActive){
        msg = t_splash
    }
    else {
        msg = t_short
        startRequested = true;
    }
    // debugger;
    msg += base_dom();
    document.write(msg);
    document.onmousemove = updateWMTT;
}


function load_lib (fname, next){
    var callback = next, fn = fname;
    load_File (fname, file_loaded)

    function file_loaded (txt){
        theLib += txt;
        callback ();
    }

}

function load_File (fname, next){
    var callBack = next;
    var xmlHttp = null;
    try {
        // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
        xmlHttp = new XMLHttpRequest();
    } catch(e) {
        try {
            // MS Internet Explorer (ab v6)
            xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            try {
                // MS Internet Explorer (ab v5)
                xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                xmlHttp  = null;
            }
        }
    }
    if (xmlHttp) {
        xmlHttp.open('GET', fname, true);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                var msg = xmlHttp.responseText;
                callBack(msg);
            }
        };
        xmlHttp.send(null);
    }
}

function load_lib_fox (fname, next){


    f.onload = loaded;
    f.onlayoutcomplete = function (){
        alert ('layouted')
        };
    f.attachEvent ("load", loaded);
    // f.setAttribute ("onload", loaded);
    if (typeof f != "undefined") {
        document.getElementsByTagName("head")[0].appendChild(f);
    }
    callback ();

    function statechange (txt){
        if (txt == 'loaded'){
            //    alert ('IE loaded');
            callback ();
        }
    }

    function loaded (a){
        alert ('Fox loaded');
        callback ();
    }
}


function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

function locale (){
    if ( navigator ) {
        if ( navigator.language ) {
            return navigator.language;
        }
        else if ( navigator.browserLanguage ) {
            return navigator.browserLanguage;
        }
        else if ( navigator.systemLanguage ) {
            return navigator.systemLanguage;
        }
        else if ( navigator.userLanguage ) {
            return navigator.userLanguage;
        }
    }
}

function updateWMTT(e) {
    if (wmtt != null && wmtt.style.display == 'block') {

        x = ((e && e.pageX) ? e.pageX : window.event.x) + wmtt.offsetParent.scrollLeft - wmtt.offsetParent.offsetLeft;
        y = ((e && e.pageY) ? e.pageY : window.event.y) + wmtt.offsetParent.scrollTop - wmtt.offsetParent.offsetTop;
        wmtt.style.left = (x + 20) + "px"; wmtt.style.top = (y + 20) + "px";
    }
}
function showWMTT(id) {
    wmtt = document.getElementById(id); wmtt.style.display = "block";
}

function hideWMTT() {
    wmtt.style.display = "none";
}

function teaser_info (lg) {
    var result, ztxt = '';
    var more_info = 'Klicken - dann gibts mehr Info';
    var start_info = 'Starten Sie das Tubaportal. <br/><strong>Mit Klick gehts los.</strong>';
    var start_tuba = 'Interessiert? <br/>Ein Klick auf mich<br/>- und es geht los!'
    var active_info = 'machen Sie aktiv mit!<br/>(mehr Info beim Mausklick)';
    lst = [
    {
        width:300,
        height:290,
        left: 340,
        top: 450,
        cls:"h2hide",
        de: 'das Tubaportal wird geladen - bitte haben Sie etwas Geduld.',
        de_tip: 'wie lange das dauert h&auml;ngt von der Schnelligkeit Ihres Internetzuganges ab'
    },

    {
        width:300,
        height:90,
        top: 183,
        left: 551,
        cls: "h2in",
        de: 'Stellen Sie Ihre eigene Lokalzeitung zusammen - nach Ihren Interessen und f&uuml;r jeden Platz auf dem Globus',
        de_tip: active_info
    },

    {
        width:250,
        height:50,
        left: 525,
        top: 270,
        cls: "h2in",
        de: 'Entwerfen Sie Ihre eigene <br/>kostenlose Homepage',
        de_tip: active_info
    },

    {
        width:240,
        height:90,
        left: 500,
        top: 330,
        cls: "h2in",
        de: 'Werden Sie Redakteur Ihrer eigenen Zeitung - und verdienen Sie damit Geld!',
        de_tip: active_info
    },

    {
        width:150,
        height:115,
        left:7,
        top:350,
        cls:"h2out",
        de: 'ist etwas passiert und Sie waren dabei ? Schreiben Sie einen Artikel !',
        de_tip: active_info
    },

    {
        width:300,
        height:90,
        left:7,
        top:47,
        cls: "h2bout",
        action: 'real_start ()',
        de: 'die Tuba ist das umfassende lokale Informationsportal, das jedem etwas bietet - auch Ihnen!',
        de_tip: start_info
    },

    {
        width:200,
        height:80,
        left: 7,
        top: 140,
        cls:"h2out",
        de: 'Suchen Sie einen Babysitter, eine Waschmaschine oder einen Partner beim Skat ? ',
        de_tip: more_info
    },

    {
        width:160,
        height:68,
        left: 7,
        top: 230,
        cls:"h2out",
        de: 'Planen Sie ein Konzert oder eine andere Veranstaltung ? ',
        de_tip: more_info
    },

    {
        width:208,
        height:290,
        left: 740,
        top: 250,
        cls:"h2in",
        action: 'tuba_load ()',
        de: '<img src="icons/tubatransa.gif" />',
        de_tip: start_tuba
    }
    ]

    for (var i = 0; i <lst.length; i++){
        var txt, e = lst [i];
        txt = (lg == 'de' ? e.de : txt);
        var tooltxt = (lg == 'de' ? e.de_tip : txt);
        var q =
        '<div id="spltip_' + i + '" class="tooltip">' + tooltxt + '</div>' +
        '<div id="splash_' + i + '"  class="' + e.cls + '"' +
        'style="position:absolute; width:' + e.width + 'px; height:' + e.height + 'px; top:' + e.top + 'px; left:' + e.left +
        'px; "onmouseover="showWMTT(\'spltip_' + i + '\')" onmouseout="hideWMTT()"'  + (e.action ? ' onclick="' + e.action + '";' : '') +
        '>' + txt + '</div>';
        ztxt += q;
    }

    result =
    '<div id ="splashScreen" style="display:block; width:860px; height:700px; background-position: 0px 0px; background-image:url(icons/kback.jpg); background-repeat:no-repeat;background-color:white;">' +
    ztxt + '</div>';
    return result;
}

function cm_class () {
    // cookie manipulation
    /** creates / modifies a cookie
     * @param n the name
     * @param w the value
     * @param e expiration time in minutes
     */
    this.writeCookie = function(n,w,e)
    {
        var a = new Date();
        a = new Date(a.getTime()+e * 60000);
        document.cookie = n+'='+w+'; expires='+a.toGMTString()+';';
    // alert ("cookie written: " + a);
    };


    /** deletes all cookies */
    this.cleanCookies = function(){
        var c=document.cookie.split(";");
        for(var i=0;i<c.length;i++){
            var e=c[i].indexOf("=");
            var n=e>-1?c[i].substr(0,e):c[i];
            document.cookie=n+"=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
        }
    }

    /** reads cookie
     * @param name - the name
     * @return the content of the cookie
     */
    this.readCookie = function (name) {
        // debugger;
        var cookieValue = "";
        var search = name + "=";
        if(document.cookie.length > 0)
        {
            offset = document.cookie.indexOf(search);
            if (offset != -1)
            {
                offset += search.length;
                end = document.cookie.indexOf(";", offset);
                if (end == -1) end = document.cookie.length;
                cookieValue = unescape(document.cookie.substring(offset, end))
            }
        }
        return cookieValue;
    }


    /** removes a cookie
     * @param n - the name
     */
    this.deleteCookie = function(n)
    {
        document.cookie = n+'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    };

    /** are cookies allowed ?
     * @return yes or no
     */
    this.checkCookiePermitted = function(){
        var result;
        var txt = new Date().toString().substring (0, 10).trim();
        self.writeCookie('testCookie', txt, 5);
        var newVal = self.readCookie('testCookie');
        result = (newVal == txt);
        // alert (txt + " - " + self.readCookie('testCookie'));
        self.deleteCookie('testCookie');
        return result;
    };
}

    
function base_dom (){
    var lib_modules = [
    "http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php",
    "../ext3.1/adapter/ext/ext-base.js", "../ext3.1/ext-all.js"];
    var debug_modules = [
    "text.js", "struc.js", "extlang.js", "firstactions.js", "firstlayout.js", "Katlist.js", "catnames.js",
    "winpool.js", "pulldown.js", "miscobj.js", "jsonlib.js", "categorydialog.js", "catlistdialog.js",
    "communicator.js", "infoDisplay.js", "googlemap.js", "carousel.js", "user.js", "menuitems.js",
    "topmenu.js", "menu.js", "setup.js", "io.js", "newarticle.js", "profile.js", "agent.js", "runtime.js",
    "factory.js", "infopage.js", "link.js", "locator.js", "editmenuitems.js", "menusubitems.js", "editmenu.js",
    "locationmenu.js", "profilemenu.js", "glue.js", "fader.js", "article.js", "homepage.js", "newsline.js",
    "selectlist.js", "comment.js", "message.js", "friends.js", "wizard.js", "plaininfo.js", "newspaper.js",
    "basics.js", "layouter.js", "extbase.js", "startup.js", "globals.js"
    ];
    var production_modules = [

    ]

    // var scripts = get_scrpt (lib_modules);
    // scripts += get_scrpt (debug_modules);

    var result = '\
        <div id="LPloading" style="display:none"> \
            <!-- loading indicator -->\
        </div>\
        <!-- container encapsulates the whole page -->\
        <div id="container"> <!-- encapsulates the whole page -->\
        <div id="LPteaser" style="display: block"></div>\
        <div id="LPnewspage" style="display: none"></div>\
        <div id="LPeditor" style="display: none"></div>\
        </div> <!-- end container -->'

    // result += scripts;
    return result;
    
    function get_scrpt (lst){
        var result = '';
        for (var i = 0; i < lst.length; i++){
            result += '<script type="text/javascript" src="' + lst [i] + '"></script>\n';
        }
        return result;
    }


}



 
